Using a potentiometer with Arduino to turn on LEDs

how to control leds with potentiometer arduino image

In this tutorial I will be explaining how to use a potentiometer with an Arduino. We will be turning it to turn on some LEDs, which will be turning on according to how much resistance is on the potentiometer. The parts in this project are included in our absolute starter kit and electronic component kit. To follow this tutorial, you will already have to be familiar with LEDs which there is already a tutorial for here.

The potentiometer with arduino

A potentiometer is an electric component that can change its resistance. We can use this in our advantage because we can read off the voltage on the potentiometer. The middle pin on a potentiometer is meant to read off the voltage, so we will connect it to an analog pin.

Part Amount
Potentiometer1
LED6
Wires11
Arduino1
Resistor(220 ohm)6
Part list

Wiring everything up

The first thing we will do is connecting everything. We will be using six LEDs which will all need 220 ohm resistors, thus we will need to connect them to six digital pins. As said, the potentiometer will need to be connected to Ground as well as five volts which can be on either side. Just follow the schematic on the bottom and you’ll be sure to succeed.

Potentiometer

Programming the Arduino

Now that you have hooked everything up, we can start programming. We will first have to set all of the pins for the LEDS and define a value to read the potentiometer from.

// Value to read the potentiometer
 int pot_val = 0;
 void setup() {
   //setting the LED pins for output
   pinMode(8, OUTPUT);
   pinMode(9, OUTPUT);
   pinMode(10, OUTPUT);
   pinMode(11, OUTPUT);
   pinMode(12, OUTPUT);
   pinMode(13, OUTPUT);
 }

With the easy part done, we have to read off and use the potentiometer and LEDs. To read off the potentiometer, use the analogRead command, this command will return a number between 0 and 1024. In the second part, we check if the voltage is above a certain threshold. If it is, we turn the LED on, if it isn’t, we turn it off. Repeat this for every LED and upload it.

void loop() {
   pot_val = analogRead(0);// Read off the potentiometer
   if (pot_val>0){// If the value of the potentiometer is higher then 0
     digitalWrite(8, HIGH);// Set pin 8 HIGH or turn on LED
     }
   else{
     digitalWrite(8, LOW);// If the former was not true, set pin low
     }
     if (pot_val>170){
     digitalWrite(9, HIGH);
     }
   else{
     digitalWrite(9, LOW);
     }
     if (pot_val>341){
     digitalWrite(10, HIGH);
     }
   else{
     digitalWrite(10, LOW);
     }
     if (pot_val>511){
     digitalWrite(11, HIGH);
     }
   else{
     digitalWrite(11, LOW);
     }
     if (pot_val>682){
     digitalWrite(12, HIGH);
     }
   else{
     digitalWrite(12, LOW);
     }
     if (pot_val>852){
     digitalWrite(13, HIGH);
     }
   else{
     digitalWrite(13, LOW);
     }
 }

After uploading the script, you should now be able to turn the LEDs on. Potentiometers are vital in electronics and are specifically used in testing.

Leave a Reply

[]