Skip to Content

Potentiometer

This project demonstrates how to read an analog input from a Potentiometer and use it to control the brightness of an external LED using a microcontroller.

A potentiometer has a shaft that can be turned with a knob or a screw. Turning the shaft changes the amount of internal resistance, which changes the voltage output at the center pin. The varying voltage is read by the analog input pin on the Arduino board. Arduino boards have a circuit inside called an analog-to-digital converter (ADC) that reads a voltage that varies from 0V to 5V and proportionately converts it to a number between 0 and 1023.

Components

ComponentPurpose
Arduino NanoThe microcontroller
3 pin potentiomterThe potentiometer
2 pin LEDThe LED for which the brightness will be controlled

Circuit Diagram

nano_potentiometer_LED_fritzing

Connections

3 Pin potentiometer

Arduino Nano PinPotentiometer Pin
A0Output
5v5V
GNDGND

2 pin LED

For an LED the longer pin is the anode (+ve) and is connected to the signal pin while the shorter pin is the cathode (-ve) and is connected to ground.

Always add a resistor in series with the LED to reduce the current flowing through the LED, especially if the LED will be on continously. This is to prevent early burnout of the LED. Select the resistance value depending on the LED brightness you need.

Arduino Nano PinLED Pin
D3 (PWM)Anode (longer leg)
GNDCathode (shorter leg)

Code

/* Project: Physical Computing Circuit: Potentiometer-LED Boards Supported: UNO R3/R4, Nano, ESP32 Function: Reads a varying voltage from a potentiometer and controls the brightness of an LED. Author: STEMVentor Educonsulting This code is copyrighted. Please do not reuse or share for any purpose other than for learning with subscribed STEMVentor programs. This code is for educational purposes only and is not for production use. */ // LIBRARIES // PIN DEFINTIONS #LED_PIN 3 // Must be a PWM pin #POTENTIOMETER_PIN A0 // CONSTANT DEFINITIONS // GLOBAL VARIABLES uint8_t potentiometer_value; uint8_t brightness; // INITIALIZE OBJECTS // LOCAL FUNCTIONS void ledSetup() { // Set the LED pin mode to output. pinMode(LED_PIN, OUTPUT); // digitalWrite() can only send two values (HIGH or LOW) to a pin. digitalWrite(LED_PIN, LOW); } void potentiometerSetup() { // initialize the Potentiometer pin as an input. pinMode(POTENTIOMETER_PIN, INPUT); } void readPotentiometer() { // Read the value of the Potentiometer state. potentiometer_value = analogRead(POTENTIOMETER_PIN); Serial.print("Potentiometer value: "); Serial.println(potentiometer_value); // For Arduino Nano // analogRead returns a number between 0 and 1024 while analogWrite takes a number between 0-255 // Use the map function to do the proportional conversion. brightness = map(potentiometer_value, 0, 1024, 0, 255); // For ESP32 // analogRead returns a number between 0 and 4098 // brightness = map(potentiometerValue, 0, 4098, 0, 255); Serial.print("Brightness value: "); Serial.println(brightness); } void analogWriteLed(int value) { // analogWrite() can send values from 0 to 255 to a pin (must be a PWM pin) // which will send a varying voltage to the attached component. // Note that the analogWrite() function has nothing to do with the analog pins or the analogRead() function. // Those are for analog input only. analogWrite(LED_PIN, value); } // THE setup FUNCTION RUNS ONCE WHEN YOU PRESS RESET OR POWER THE BOARD. void setup() { // Start the serial communication with baud rate suitable for your components. Serial.begin(9600); ledSetup(); potentiometerSetup(); Serial.println("The board is ready!"); } // THE loop FUNCTION RUNS OVER AND OVER AGAIN FOREVER UNTIL THE BOARD IS POWERED OFF. void loop() { readPotentiometer(); analogWriteLed(brightness); delay(1000); }