Nov 062011
 

Using a Potentiometer with Arduino: 5 LED chaser:

In this activity we connect a potentiometer with the Arduino, and use it to control the speed of 5 flashing LEDs.

arduino-5led-pot-fr

To build this activity you’ll need:

  • Potentiometer
  • 330 or 470 Ohm resistor
  • 5 LEDs
  • jumper wires
  • breadboard
  • Arduino

 

arduino-5-leds-potentiometer01

1) Connect 5 led’s to the breadboard so that the positive lead of each LED can be easily connected to pins 8,9,10,11 and 12 of the Arduino. The negative leads should be connected to a 330 Ohm (minimum) resistor which leads back to ground.

arduino-5-leds-potentiometer02

2) The potentiometer will have a positive, negative, and wiper connection. The wiper should go to pin 0 of the analog inputs while positive and negative go to +5 and Gnd repectively.

Arduino potentiometer & 5 LED chaser code:

 

int sensorValue = 0;    //make a variable where you can store incoming
                        //analog values

void setup(){
  pinMode(12, OUTPUT);  //tell arduino what you'll be using these pins
  pinMode(11, OUTPUT);  // for (output).
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);

  Serial.begin(9600);  //initialize serial
}

void loop(){      //we put the code we want executed in a loop 

Serial.print("sensor = " );  //sends what's in quotes via serial
Serial.println(sensorValue); //sends our variable (sensorValue)
                             //via serial   

  digitalWrite(12,HIGH);       // lights the led
  sensorValue = analogRead(0); // reads pin 0
  delay(sensorValue + 25);     // sensorValue used for delay
  digitalWrite(12,LOW);        //turns off the led
  delay(15);                   //delay before moving to next output pin
                               //the + 25 keeps delay from reaching zero
           //code below is for remaining 4 LEDs
  digitalWrite(11,HIGH);
  sensorValue = analogRead(0);
  delay(sensorValue + 25);
  digitalWrite(11,LOW);
delay(15);

  digitalWrite(10,HIGH);
  sensorValue = analogRead(0);
  delay(sensorValue + 25);
  digitalWrite(10,LOW);
delay(15);

  digitalWrite(9,HIGH);
  sensorValue = analogRead(0);
  delay(sensorValue + 25);
  digitalWrite(9,LOW);
delay(15);

  digitalWrite(8, HIGH);
  sensorValue = analogRead(0);
  delay(sensorValue + 25);
  digitalWrite(8, LOW);
delay(15);

  digitalWrite(9,HIGH);
  sensorValue = analogRead(0);
  delay(sensorValue + 25);
  digitalWrite(9,LOW);
delay(15);

  digitalWrite(10,HIGH);
  sensorValue = analogRead(0);
  delay(sensorValue + 25);
  digitalWrite(10,LOW);
delay(15);

  digitalWrite(11,HIGH);
  sensorValue = analogRead(0);
  delay(sensorValue + 25);
  digitalWrite(11,LOW);
delay(15);

}

How it works:

As we turn the potentiometer knob we vary the voltage reaching analog pin 0. This voltage (0-5volts) is converted to a value between 0 and 1023. This numerical value is used to change the delay between blinks in the code above, controlling the speed at which the light “moves” from LED to LED. Since we wanted a minimum delay of 25 (any faster didn’t look right) we simply added 25 to the delay value in the code (sensorValue +25).