Create a Police Light Effect with Arduino
In this project, we will create a police light effect using Arduino and 10 LEDs. The setup includes 5 blue LEDs and 5 green LEDs, which will blink alternately in a “police-style” pattern. This is a fun and educational project for beginners to learn about basic Arduino programming and LED control.
Components Needed
- Arduino Board (e.g., Arduino Uno)
- 10 LEDs (5 blue and 5 green)
- 10 Resistors (220 ohms)
- Breadboard
- Connecting Wires
- USB Cable (for programming the Arduino)
- Power Source (optional for standalone operation)
Circuit Diagram
- Connect the blue LEDs to pins 2, 3, 4, 5, and 6 on the Arduino.
- Connect the green LEDs to pins 7, 8, 9, 10, and 11 on the Arduino.
- Attach a 220-ohm resistor in series with each LED to prevent excessive current.
- Connect the cathodes (negative terminals) of all LEDs to the Arduino’s GND pin.
- Ensure all connections are secure and neat on the breadboard.
Code
The following code will alternate the blinking of the blue and green LEDs in a police light pattern:
#define NUM_LEDS 5
// Blue LEDs connected to pins 2 to 6
int blueLeds[NUM_LEDS] = {2, 3, 4, 5, 6};
// Green LEDs connected to pins 7 to 11
int greenLeds[NUM_LEDS] = {7, 8, 9, 10, 11};
void setup() {
// Set all LED pins as outputs
for (int i = 0; i < NUM_LEDS; i++) {
pinMode(blueLeds[i], OUTPUT);
pinMode(greenLeds[i], OUTPUT);
}
}
void loop() {
// Blink blue LEDs in police pattern
for (int i = 0; i < NUM_LEDS; i++) {
digitalWrite(blueLeds[i], HIGH); // Turn on
delay(100); // Wait 100 ms
digitalWrite(blueLeds[i], LOW); // Turn off
delay(100); // Wait 100 ms
}
delay(1000); // Wait 1 second before switching
// Blink green LEDs in police pattern
for (int i = 0; i < NUM_LEDS; i++) {
digitalWrite(greenLeds[i], HIGH); // Turn on
delay(100); // Wait 100 ms
digitalWrite(greenLeds[i], LOW); // Turn off
delay(100); // Wait 100 ms
}
delay(1000); // Wait 1 second before repeating
}
Download the code |
How It Works
- The LEDs are divided into two groups: blue and green.
- Each group of LEDs blinks in a sequential pattern, mimicking a police light effect.
- After one group completes its sequence, the other group starts.
- The delay(100) function creates the blinking effect, and the delay(1000) ensures a pause before switching to the next group.
Steps to Upload the Code
- Install the Arduino IDE on your computer.
- Connect your Arduino board to the computer using a USB cable.
- Open the Arduino IDE and paste the above code.
- Select the correct board and COM port under the “Tools” menu.
- Click the Upload button to transfer the code to the Arduino.
Testing the Circuit
- Power the Arduino using the USB cable or an external power source.
- Observe the blue LEDs blinking in sequence, followed by the green LEDs.
- The pattern will repeat indefinitely.
Troubleshooting
- LEDs not lighting up: Check the wiring and ensure all connections are secure.
- Incorrect blinking pattern: Verify that the LEDs are connected to the correct pins as specified in the code.
- No response from Arduino: Ensure the correct COM port and board type are selected in the Arduino IDE.
Conclusion
This project demonstrates how to create a police light effect with Arduino using 10 LEDs. It’s a simple yet effective way to learn about Arduino programming and LED control. Feel free to modify the timing or add more LEDs to customize the effect further. Happy tinkering!