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:
- If the analog value of the potentiometer is greater than a threshold, Arduino Nano turns the LED on
- If the analog value of the potentiometer is lower than a threshold, Arduino Nano turns the LED off
- If the output voltage of the potentiometer is greater than a threshold, Arduino Nano turns the LED on
- If the output voltage of the potentiometer is lower than a threshold, Arduino Nano turns the LED off
Hardware Preparation
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
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.
- 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;
}