Basic Arduino Traffic Light Utilizing Interrupts
by 795648 in Circuits > Arduino
530 Views, 1 Favorites, 0 Comments
Basic Arduino Traffic Light Utilizing Interrupts
Hello hope you're having a great day, today after this tutorial you would be able to make a Basic Arduino Traffic Light Utilizing Interrupts whether physical or on tinker CAD you would be able to make this circuit but for the sake of this tutorial I will be making it in tinker CAD and I will also provide you with links on where to get the certain supplies I will mention in a moment along with the price so that you can also make it physically if you want to make it after the tinker CAD, without further ado here are the supplies you need in order to make the circuit
Supplies
- Arduino Uno ($23.00)
- Bread Board ($10.55)
- Push Button ($10.99)
- 6 LED'S (2 Red, 2 Yellow and 2 Green) ($7.78)
- 6 Resistors (220 ohms) ($5.99)
- At least 10 wires ($23.74)
Bring Out Your Materials
First of all you don't want to be confused in a circuit like this, you should be organized so before anything else the first thing you should do is get all of the necessary materials and lay them out like that which makes things neat and tidy, of course as we're making the circuit we'll have to add the wires after we lay the materials out like this
First LED
So basically what we're wiring first is the LED and before anything else we must connect the power and the ground to the breadboard so the power must go in the positive section and the ground must go to the negative section. Now that that's taken care of you have to place the LED at the very top just before the top negative and positive sections (basically the second place on the breadboard) and when that is done you should place one of the 220 ohm resistors at the bottom where the terminal 1 is on the negative section and terminal 2 is under the cathode of the LED but it is on a different section of the breadboard so connecting a wire helps, as for the anode of the LED connecting pin 8 from the Arduino to the bottom of the LED works
Connecting Even More LEDs
Next we're basically just repeating step 2 where now we just added 2 more LEDs which are yellow and green (like a traffic light) which means 2 more resistors and 2 more wires, just connect the pins from the Arduino to the bottom of the anodes of the LEDs so right now if you have the first one as 8 you can make the yellow one at 9 and so on and so fourth. A good tip would be to colour code your wires depending on which LED colour it's associated to so for example both my terminal 2 wire and my pin 8 wire should be red because those are both associated with the red LED this is a good tip because it will probably be very confusing what wire is for what so I'm trying to keep this as simple as possible
Second Group of Traffic Light LEDs
We're basically repeating the previous step (step 3) where this time we have to make a whole other group of traffic light LEDs so there isn't much to talk about here although this is the reason why I said to colour code because had you not colour code you'd be very confused of which wire is going where
Connecting the Button
Alright here is the step where you have to connect the button, I connected it in the middle of the breadboard so you should probably too because you need 3/4 terminals on it and if you put it on a normal spot on the breadboard there will be no room to attach anything at the top or bottom terminal, so that's that. Connect a resistor under terminal 1a just like the resistors under the cathodes of the the LEDs. Connect a wire that's starting under terminal 2a and is connecting it to the power rail (positive section) and finally the most crucial part of the button: connect pin 2 from the Arduino to the top of terminal 1b and this is crucial because pin 2 is the sole thing that makes the interrupting on the Arduino so if it wasn't there it would beat the purpose of this circuit and thus not work properly and that's it you're done the assembling part now onto the coding
The Code
Ok here is the tricky bit which is the code first we have to give the pins that we connected to the anodes of the LEDs a definition for example red's definition is int L1Red = 8; and we use these definitions in something called pin mode where we select a pin number from the definitions and then choose if we want to input or output it. Basically in the normal void loop it alternates green lights between the first light and the second one and the yellow light and time delays give off a transition between those two green lights. The button interrupts and this is why you needed that one wire that connected pin 2 to on top of terminal 1b and basically what it does in the code is if the first group of lights has it's light to red and the second group has it's light on at green they basically switch spots where the first group of lights switch to green instead of red and the second group of lights switch to red instead of green just like with two real life traffic lights. And there you have it you have built a Basic Arduino Traffic Light Utilizing Interrupts and I will also post the code so that it doesn't get confusing and you might get what I was saying about the code