Arduino Nano - Potentiometer fade LED

This tutorial instructs you how to use Arduino Nano to adjust the brightness of the LED based on the output of the potentiometer.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Potentiometer
1×Potentiometer Kit
1×LED
1×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of LED and Potentiometer

If you are unfamiliar with LED and potentiometer (including pinout, operation, and programming), the following tutorials can help:

Wiring Diagram

The wiring diagram between Arduino Nano and Rotary Potentiometer LED

This image is created using Fritzing. Click to enlarge image

How To Program

  • Gets the value from analog pin A0, which is a range of 0 to 1023.
int analog_value = analogRead(A0);
  • Adjusts the brightness to a value between 0 and 255.
int brightness = map(analog_value, 0, 1023, 0, 255);
  • Sets the LED connected to pin 3 to the brightness.
analogWrite(LED_PIN, brightness);

Arduino Nano Code

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-potentiometer-fade-led */ int LED_PIN = 3; // The PWM pin the LED is attached to // The setup function runs once on reset or power-up void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); // declare LED pin to be an output: pinMode(LED_PIN, OUTPUT); } // The loop function repeats indefinitely. void loop() { // reads the input on analog pin A0 (value between 0 and 1023) int analog_value = analogRead(A0); // scales it to brightness (value between 0 and 255) int brightness = map(analog_value, 0, 1023, 0, 255); // sets the brightness LED that connects to pin 3 analogWrite(LED_PIN, brightness); // print out the value Serial.print("Analog: "); Serial.print(analog_value); Serial.print(", Brightness: "); Serial.println(brightness); delay(100); }

Detailed Instructions

  • Copy the code above and open it with the Arduino IDE.
  • Click the Upload button on the Arduino IDE to compile and upload the code to the Arduino Nano.
  • Open the Serial Monitor.
  • Turn the potentiometer.
  • Check out the LED.
  • Check out the result on the Serial Monitor.
COM6
Send
Analog: 6, Brightness: 1 Analog: 34, Brightness: 8 Analog: 89, Brightness: 22 Analog: 149, Brightness: 37 Analog: 214, Brightness: 53 Analog: 297, Brightness: 74 Analog: 365, Brightness: 90 Analog: 431, Brightness: 107 Analog: 510, Brightness: 127 Analog: 589, Brightness: 146 Analog: 695, Brightness: 173 Analog: 790, Brightness: 196 Analog: 970, Brightness: 241 Analog: 996, Brightness: 248 Analog: 1018, Brightness: 253 Analog: 1023, Brightness: 255
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!