Arduino Potentiometer LED: A beginners project on controlling and fading an LED using an potentiometer as an input control (by using the ADC to read it)


Introduction

In this Arduino potentiometer LED beginners tutorial. We'll build a simple dimmable light circuit and learn some basic Arduino programming along the way. You will learn how to use a potentiometer to control the brightness of an LED connected to an Arduino.

How to control the brightness of an LED using an Arduino Uno and potentiometer

A potentiometer, commonly referred to as a "pot", is a simple 3-terminal resistor component that allows you to adjust or select a varying level of resistance.

It works by having a movable wiper contact that slides along a resistive track connecting the outer terminals. As the wiper slides, it taps into different points along this track to give a range of resistances between the minimum and maximum. So if you have a 10k Ohm pot, and set the wiper to the middle position, you have 5k Ohms resistance from the middle to one end, and 5k Ohms from the middle to the other end.

The most common usage of the potentiometer is to divide voltage. When the wiper is in the middle and you apply 5V to one end and 0V to the other you will get a voltage of 2.5V at the middle wiper. If you changed to 1k and 9k - moving the shaft so that the wiper moves nearer to one end you would get 0.5V output at the wiper. You can vary the voltage to any value between 0V and 5V.

Physically, a potentiometer looks like a dial or knob you can turn. Turning the shaft moves the wiper and changes the resistance. They typically come in common values like 10k Ohms, which is what we'll be using here.

TIP: Potentiometers come in two flavors log and linear. The log types are used in audio circuits as our ears react logarithmically so for this project you need a linear pot.

By reading the potentiometer value with the Arduino, we can map it to PWM (Pulse Width Modulation) to fade the LED from off to full brightness.

Required Components

  • Arduino Uno.
  • 10k ohm potentiometer.
  • LED (any color).
  • 220-330 ohm resistor (choose one value) - you can use 1k.
  • Jumper wires.

Arduino potentiometer LED: Circuit Diagram

The layout diagram shows how to place components on the breadboard.

Ground (0V) and supply voltage (5V) are connected from pins in the lower pin header of the Arduino Uno, while analogue input pin A1 connects to the middle wiper of the pot.

The LED is connected from pin 5 via the 1k resistor. Note that you have to place the LED the right way round -the flat side of the LED (cathode - or negative side) connects to ground (GND or 0V).

Arduino potentiometer led circuit layout
Diagram using fritzing
Arduino potentiometer led schematic

Diagram using fritzing

Arduino potentiometer LED: Using Arduino analogRead

The Arduino function analogRead is a function that sets up your Analog capable pin (choose from A0 to A5) as an analog input. Note that these pins also have digital capability, so you can use them as digital-only Input or Output i.e. using function digitalWrite and digitalRead. However, in a single project you need to choose whether the pin will be used as input, output or analog input - here you need analog input.

The analogRead function does all the hard work in the background and starts the internal ADC getting the reading and waits for that reading to complete ~100us.

Arduino potentiometer LED: Example Sketch

You can copy and paste the code below into the Arduino IDE (in a new sketch) replacing everything that is in the new sketch window (See "Uploading the code" below).

int potPin = A1;
int ledPin = 5;

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  int potValue = analogRead(potPin);
  analogWrite(ledPin, potValue/4);
}

Code Explanation

int potPin = A0;
int ledPin = 5;
int potValue;

  • Defines the pin that the potentiometer is connected to as analog pin A0
  • Defines the pin the LED is connected to as digital pin 5
  • Declares a variable to store the value read from the potentiometer

In Function setup:

pinMode(ledPin, OUTPUT);
  • The setup function runs once at the start. It sets the LED pin as an output.

In the loop function:

int potValue = analogRead(potPin);
analogWrite(ledPin, potValue/4);
  • Reads the voltage value from the potentiometer pin (0-1023 range)
  • Sends that value (divided by 4 to scale range) to the LED pin using PWM
  • PWM fades the LED by switching it on and off very fast

The loop function repeats continuously to read the pot and update the LED By reading the potentiometer position as an analog value, scaling it, and using PWM to fade the LED, the circuit smoothly adjusts the LED brightness as the pot is turned.

PWM Divide by four

You may have noticed that the value from the analogue pin is divided by 4. This is because the internal ADC has a resolution of 1024 bits and the resolution of the software generated PWM signal has a resolution of 256 bits - a difference of a factor of 4.

Arduino light sensor: Uploading the Code

There are a few steps to uploading the code using the Arduino IDE:

  • Connect the Arduino Uno to the PC with a USB cable.
  • Select the Arduino Uno hardware.
  • Open a new sketch.
  • Paste the code above into the new page (overwrite everything).
  • Press the upload button (right arrow at top).

You can find a more detailed tutorial on the Arduino IDE page.

Testing the Circuit

Using your Arduino potentiometer LED circuit:

  1. Turn the potentiometer shaft
  2. The LED should fade up and down
  3. You now have a simple adjustable light!

Further Ideas

You could combine this ability to read a potentiometer with a previous beginners project: "Arduino Light Sensor". You would use the analogue input to set a level of the light sensor that would turn on a LED effectively making a night light that turns on when it gets dark.

Arduino potentiometer LED: Conclusion

In this project, you successfully built a simple adjustable light circuit using an Arduino, potentiometer, LED, and some basic components. You now have a working understanding of how to:

  • Interface an analog potentiometer and read its variable resistance values.
  • Program Arduino code to control an output based on an input reading.
  • Utilize PWM to gradually fade an LED rather than just on/off states.
  • Construct basic circuits by properly wiring inputs, outputs.

While small, this demo illuminated the fundamentals of microcontroller-based projects. By manually adjusting brightness with the potentiometer knob, you can interact directly with code in real-time.


Written by John Main who has a degree in Electronic Engineering.

Note: Parts of this page were written using claude-instant as a research assistant.




Comments

Have your say about what you just read! Leave me a comment in the box below.

Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.



Privacy Policy | Contact | About Me

Site Map | Terms of Use