ARDUINO
TRAFFIC LIGHTS
INTRODUCTION
Arduino is a micro-controller which can be programmed to do certain operations like lighting led, controlling servo, etc. We’ll use Arduino UNO or any other clone to create Traffic lights. A Traffic light usually consists of three lights red, yellow and green. They switch on and off within a certain time interval. The time interval between the lights is similar to the real traffic light system.
MATERIALS
REQUIRED
- Arduino UNO or any other clone and USB cable
- 5mm LED (Red, Yellow and Green)
- 20 K ohm resistor (3)
- Breadboard
- Jumpers
PROCEDURE
- Connect digital pin 8 of Arduino to longer leg of led via a resistor and connect the shorter leg of ‘Red’ LED to ground pin on Arduino.
- Similarly connect digital pin 7 to ‘Yellow’ LED and digital pin 4 to ‘Green’ LED via a resistor and back to another ground pin on Arduino.
- The circuit is complete, upload the code and you can verify the output.
- The USB cable serves both as a medium to upload code as well as 5V power source.
CODE
The code for Traffic
lights is as follows. Copy and paste it on Arduino IDE.
void setup() {
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(4, OUTPUT);
}
// the loop function
runs over and over again forever
void loop() {
digitalWrite(8, HIGH); // turn the red led on
delay(5000); // wait for some time
digitalWrite(8, LOW); // turn the red led off
delay(100); // wait for some time
digitalWrite(7, HIGH); // turn the yellow led on
delay(1000); // wait for some time
digitalWrite(7, LOW); // turn the yellow led off
delay(100); // wait for some time
digitalWrite(4, HIGH); // turn the green led on
delay(5000); // wait for some time
digitalWrite(4, LOW); // turn the green led off
delay(200); // wait for some time
}
No comments:
Post a Comment