arduino projects for beginners
Dual LED Chaser Breadboard Layout and Circuit Diagram
Build the circuit as shown in the breadboard layout and circuit diagram below. The circuit consists of one LED and one series resistor repeated 12 times. These LEDs and series resistors are connected between Arduino digital pins 2 to 13 and GND. For a similar project with more details on building the breadboard circuit, see the Arduino Knight Rider tutorial.
The resistors shown in the breadboard layout are 470 ohm resistors, but resistors with values between 220 ohms and 470 ohms will be fine.
Dual LED Chaser Arduino Sketch Code
After building the circuit as shown above, copy and paste the following Arduino sketch code to a new window in the Arduino IDE. Load the sketch to the Arduino and see the dual LED chaser light up.
The speed at which the LEDs move can be changed by changing the value in the following line of code.
// change speed of display here
#define SPEED_MS 100
Increasing the value of SPEED_MS will decrease the speed of the LEDs because the millisecond delay that an LED is left on is increased. Decreasing the value of SPEED_MS will increase the speed of the LEDs.
//---------------------------------------------------------------------
// Program: dual_chaser
//
// Description: 12 LED dual chaser. One led moves from left to right
// while the second LED moves from right to left.
// LEDs and series resistors connected to pins 2 to 13.
//
// Date: 8 April 2016 Author: W.A. Smith
// http://startingelectronics.org
//---------------------------------------------------------------------
// change speed of display here
#define SPEED_MS 100
void setup() {
// set up pins 2 to 13 as outputs
for (int i = 2; i <= 13; i++) {
pinMode(i, OUTPUT);
}
}
uint16_t chase2 = 13; // keeps track of second LED movement
void loop() {
// move first LED from left to right and second from right to left
for (int i = 2; i < 13; i++) {
allLEDsOff();
digitalWrite(i, HIGH); // chaser 1
digitalWrite(chase2, HIGH); // chaser 2
chase2--;
// stop LEDs from appearing to stand still in the middle
if (chase2 != 7) {
delay(SPEED_MS);
}
}
// move first LED from right to left and second LED from left to right
for (int i = 13; i > 2; i--) {
allLEDsOff();
digitalWrite(i, HIGH); // chaser 1
digitalWrite(chase2, HIGH); // chaser 2
chase2++;
// stop LEDs from appearing to stand still in the middle
if (chase2 != 8) {
delay(SPEED_MS);
}
}
}
// function to switch all LEDs off
void allLEDsOff(void)
{
for (int i = 2; i <= 13; i++) {
digitalWrite(i, LOW);
}
}