5LED Chaser-potentiometer using arduino

Today we will make a 5 LED Running circuit can adjust speed by potentiometer. Which LED Chaser circuit like this We’ve created long time ago. But today we try to use arduino. We are writing technical code “LOOP FOR” very interesting.

Parts you will needs
First of all, we need to have parts below:
• Arduino UNO R3
• Potentiometer 100K
• Five LEDs
• Five 470 ohms resistors
• Jumper wires
• Breadboard

Secondly, see the schematic diagram as Figure 1

5led running using potentimoeter_schem
Figure 1 Schematic diagram

Then, assemble parts on a breadboard and cable to usb port.

5led running using potentimoeter
Figure 2 Circuit connection

5-LED-running-using-potentimoeter
Figure 3 5LED Chaser-Potentiometer using Arduino

We use LED1-LED5 output display at D9-D13 pins. And A0 pin is used to read analog voltage from potentiometer.

Code programming

  1. int LED1 = 9;
  2. int LED2 = 10;
  3. int LED3 = 11;
  4. int LED4 = 12;
  5. int LED5 = 13;
  6. int delay1;
  7. void setup() {
  8.   pinMode(LED1, OUTPUT);
  9.   pinMode(LED2, OUTPUT);
  10.   pinMode(LED3, OUTPUT);
  11.   pinMode(LED4, OUTPUT);
  12.   pinMode(LED5, OUTPUT);
  13.   pinMode(A0, INPUT);
  14.   pinMode(9, OUTPUT);
  15. }
  16.  
  17. void loop() {
  18.  
  19.   delay1 = analogRead(A0);
  20.   //  analogWrite(9, val / 4);
  21.   digitalWrite(LED1, HIGH);
  22.   digitalWrite(LED2, LOW);
  23.   digitalWrite(LED3, LOW);
  24.   digitalWrite(LED4, LOW);
  25.   digitalWrite(LED5, LOW);
  26.   delay(delay1);
  27.   digitalWrite(LED1, LOW);
  28.   digitalWrite(LED2, HIGH);
  29.   digitalWrite(LED3, LOW);
  30.   digitalWrite(LED4, LOW);
  31.   digitalWrite(LED5, LOW);
  32.   delay(delay1);
  33.   digitalWrite(LED1, LOW);
  34.   digitalWrite(LED2, LOW);
  35.   digitalWrite(LED3, HIGH);
  36.   digitalWrite(LED4, LOW);
  37.   digitalWrite(LED5, LOW);
  38.   delay(delay1);
  39.   digitalWrite(LED1, LOW);
  40.   digitalWrite(LED2, LOW);
  41.   digitalWrite(LED3, LOW);
  42.   digitalWrite(LED4, HIGH);
  43.   digitalWrite(LED5, LOW);
  44.   delay(delay1);
  45.   digitalWrite(LED1, LOW);
  46.   digitalWrite(LED2, LOW);
  47.   digitalWrite(LED3, LOW);
  48.   digitalWrite(LED4, LOW);
  49.   digitalWrite(LED5, HIGH);
  50.   delay(delay1);
  51. }

Code programming

We use this circuit http://www.eleccircuit.com/3-led-running-int-code-by-arduino/ to set 5 LEDs section. each section we define a LED LOW-HIGH as schedule.
1. Set int LED1-LED5 as pin output 9-13
2. Set “pinMode(9-13)” as output
3. Use “pinMode A0 as INPUT that read a voltage from potentiometer.

But we see that it have too many code.
LOOP FOR

We use loop FOR for shorter code. Below!

  1. int delay1;  // to define the variable
  2. void setup()
  3. {
  4.   for (int i = 9; i <= 13; i++) 
  5.   {
  6.     pinMode(i, OUTPUT);  //to define the variable as output
  7.   }
  8. }
  9.  
  10. void loop()
  11. {
  12.   delay1 = analogRead(A0); //to read voltage at (A0) pin 
  13.  
  14.   {
  15.     for (int i = 9; i <= 13; i++)
  16.     {
  17.       digitalWrite(i, HIGH);  //to set “i” to “High”
  18.       delay(delay1);   //to delay code as “delay1” or control speed by potentiometer
  19.       digitalWrite(i, LOW); //to set “i” to “High”
  20.     }
  21.   }
  22. }

void_setup_5led_running
Figure 4 Code use loop FOR function
YELLOW: The variable “i” start is “9”
BULE: Define variable “i” max to “13”
GREEN : Define step up variable “i”

It makes variable “i” is incremented by step. (9 to 13)

We will see that our code short and easy.
We test to rotate the potentiometer to adjust speed of LEDs. As Video below.

           Eleccircuit highly recommends EasyEDA for PCB design and order

Low Cost & Fast PCB Prototypes - EasyEDA

Only $8.21 for 10 pcs, 2-Layer 100x100mm PCBs, 2-3 days delivery

 

Add a Comment

Your email address will not be published. Required fields are marked *