Starting Electronics needs your help! Please make a donation to help cover our hosting and other costs. Click the donate button to send a donation of any amount.
Created on: 4 April 2016
An easy Arduino breadboard project for beginners that flashes four LEDs in various patterns. The patterns displayed on the LEDs can be changed by modifying the Arduino sketch. The delay time between each moving light display pattern can also be changed in the sketch.
Four LEDs with four series resistors and an Arduino board such as an Arduino Uno are required to build this breadboard project.
The video below shows the finished four LED moving light display project.
Can't see the video? View on YouTube →
Four LEDs and four series resistors are required for this project. 470 ohm resistors were used in this project, but resistors with values between 220 and 470 ohms will be fine. 3mm red LEDs were used when building this project, but the LEDs can be 3mm or 5mm and any colour will do.
Use a breadboard and wire links to build the circuit as shown in the breadboard layout and circuit diagram below. The Arduino sketch for this project can be found in the next section below.
After building the project, copy the following sketch and paste it to a new window in the Arduino IDE. Power the Arduino from a USB port on the PC and load the sketch to the Arduino. The sketch should run and display various patterns on the four LEDs.
//--------------------------------------------------------------------- // Program: moving_light_display // // Description: Flashes four LEDs connected to Arduino pins 2 to 5 // in various patterns // // Date: 4 April 2016 Author: W.A. Smith // http://startingelectronics.org //--------------------------------------------------------------------- // change speed of pattern change in milliseconds here #define SPEED_MS 300 // change LED patterns here unsigned char led_pattern[] = { 0x01, 0x02, 0x04, 0x08, 0x04, 0x02, 0x01, 0x00, 0x06, 0x09, 0x06, 0x09, 0x06, 0x09, 0x06, 0x09, 0x05, 0x0a, 0x05, 0x0a, 0x05, 0x0a, 0x05, 0x0a }; void setup() { // set pin 2 to 5 as outputs for (int i = 2; i <= 5; i++) { pinMode(i, OUTPUT); } } void loop() { DisplayPattern(led_pattern, sizeof(led_pattern)); delay(SPEED_MS); } void DisplayPattern(unsigned char *pattern, int num_patterns) { static int pattern_num = 0; // keeps count of patterns unsigned char mask = 1; // for testing each bit in pattern // do for LEDs on pin 2 to pin 5 for (int i = 2; i <= 5; i++) { // check if bit in pattern is set or not and switch LED accordingly if (pattern[pattern_num] & mask) { digitalWrite(i, HIGH); } else { digitalWrite(i, LOW); } mask <<= 1; // adjust mask for checking next bit in pattern } pattern_num++; // move to next pattern for next function call // keep pattern within limits of pattern array if (pattern_num >= num_patterns) { pattern_num = 0; } }
The update speed of the patterns displayed on the LEDs can be changed by changing the number in the following line of code at the top of the sketch.
// change speed of pattern change in milliseconds here #define SPEED_MS 300
It is initially set up as 300, which means that there is a 300ms or 0.3 second delay between different patterns displayed. Decreasing the delay will make the patterns update faster. Increasing the delay will make the patterns update slower.
To change the LED patterns displayed, the hexadecimal numbers in the following part of the code must be changed. New patterns can be added to the code and the number of patterns can be changed here. The code will automatically compensate for an increase or decrease in the number of patterns to display.
// change LED patterns here unsigned char led_pattern[] = { 0x01, 0x02, 0x04, 0x08, 0x04, 0x02, 0x01, 0x00, 0x06, 0x09, 0x06, 0x09, 0x06, 0x09, 0x06, 0x09, 0x05, 0x0a, 0x05, 0x0a, 0x05, 0x0a, 0x05, 0x0a };
Four LEDs can have 16 possible combinations of on and off LEDs. Use the table below to select the hexadecimal number to insert or add to the above code to get the pattern that you want.
As an Amazon Associate I earn from qualifying purchases: