Arduino Nano - Potentiometer LED

In a previous tutorial, We have learned how to adjust the brightness of LED based on the potentiometer's output value.

This tutorial instructs you how to use Arduino Nano and potentiometer to control LED. In detail:

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Potentiometer
1×LED
1×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter 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. We appreciate your support.

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 Potentiometer LED

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code - Analog Threshold

/* * 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-led */ const int POTENTIOMETER_PIN = A0; // The Arduino Nano pin connected to Potentiometer pin const int LED_PIN = 3; // The Arduino Nano pin connected to LED's pin const int ANALOG_THRESHOLD = 500; void setup() { pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode } void loop() { int analog_value = analogRead(POTENTIOMETER_PIN); // read the input on analog pin if(analog_value > ANALOG_THRESHOLD) digitalWrite(LED_PIN, HIGH); // turn on LED else digitalWrite(LED_PIN, LOW); // turn off LED }

Detailed Instructions

  • Connect your Arduino Nano to your computer using a USB cable.
  • Launch the Arduino IDE, choose the correct board and port.
  • Open the code in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to compile and upload the code to the Arduino Nano.
Arduino IDE Upload Code
  • Turn the potentiometer
  • Check out the alteration of the LED's condition

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

Arduino Nano Code - Voltage Threshold

A potentiometer's analog value is transformed into a voltage value. This voltage value is then compared to a voltage threshold, which will cause the LED to be activated.

/* * 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-led */ const int POTENTIOMETER_PIN = A0; // The Arduino Nano pin connected to Potentiometer pin const int LED_PIN = 3; // The Arduino Nano pin connected to LED's pin const float VOLTAGE_THRESHOLD = 2.5; // Voltages void setup() { pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode } void loop() { int analog_value = analogRead(POTENTIOMETER_PIN); // read the input on analog pin float voltage = floatMap(analog_value, 0, 1023, 0, 5); // Rescale to potentiometer's voltage if(voltage > VOLTAGE_THRESHOLD) digitalWrite(LED_PIN, HIGH); // turn on LED else digitalWrite(LED_PIN, LOW); // turn off LED } float floatMap(float x, float in_min, float in_max, float out_min, float out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; }

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!