Now that you have had a chance to wire up a circuit for yourself, as well as code a blinking light program, let us break down exactly what is happening. First we will look at a few circuits that do not require a program to run, and then we will introduce coding back in to the picture.
For this assignment you will need:
• Multiple long and short wires (at least one long red and one long black)
• Two LEDs
• One Button
• Two 220-ohm resistors (red – red – brown – gold)
Example 1
Start by wiring up the following:
• Connect your red power wire from 5V to the + bus
• Connect your black ground wire from GND to the - bus
Next connect the following:
• A wire from your + bus to a horizontal row. (I went from the + bus to row 7)
• A 220-ohm resistor from your charged row to a new row further down the breadboard. (I went from row 7 to 11)
• An LED from your charged row to a new row further down the breadboard. Remember that LEDs will only work in one direction. You musthave electricity flow from the longer anode (+) to the shorter cathode (-). (I went from row 11 to 13)
• Lastly you must have your electricity flow back to the - bus to complete the circuit. (I went from row 13 back to the – bus)
What do you think will happen when you connect the circuit to power?
When you try it the LED turns on! Take a moment to follow the path of electricity from the 5V port through your circuit back to the GND port. It is extremely important that you know how electricity flows through the breadboard. You may want to collaborate with a neighbour before you move on to the next example.
The LED came on because you completed a circuit and electricity flowed through the LED.
Example 2
Let’s make the circuit a little more complicated. Keep the red wire going from the 5V port to the + bus and black wire going from the GND port to the – bus.
Next connect the following:
• A wire from your + bus to a horizontal row. (I went from the + bus to row 7)
• A 220-ohm resistor from your charged row to a new row further down the breadboard. (I went from row 7 to 11)
• An LED from your charged row to a new row further down the breadboard. Remember that LEDs will only work in one direction. You musthave electricity flow from the longer anode (+) to the shorter cathode (-). (I went from row 11 to 13)
• A wire from your charged row to a new row further down the breadboard. (I went from row 13 to 17)
• An LED from your charged row to a new row further down the breadboard. (I went from row 17 to 19)
• Lastly you must have your electricity flow back to the - bus to complete the circuit. (I went from row 19 back to the – bus)
Now what do you think will happen when we connect the circuit to power?
If you guessed that both LEDs would turn on you are correct!
You again have created a circuit that is drawing power from the 5V port and is completed at the GND port. When the electricity passes through the LED it turns on. When the Arduino is plugged in, electricity will always be flowing out of the 5V port, so the LED will remain on the entire time.
We will need to make a change if we want the LED to be able to blink. We have to be able to control the flow of electricity. Since the 5V port always has electricity flowing out of it, we have to change ports. Any of the digital ports will work well.
Example 3
Let’s move our red wire from the 5V port to the digital port 2 (D02 on the Robo Reds). There is nothing special about port 2 so feel free to pick any port you like, just make any necessary changes as we continue.
Now we can program the Arduino to turn the electricity flowing from port 2 on and off whenever we choose. To do this though we will need to code. In the Arduino program type in the following code:
Let us go through the code line by line.
void setup is a function that runs one time. This is where all of our calibration and port declaration occurs.
A port can have one of two functions. It can be an OUTPUT and provide electricity, or it can be an INPUT and take in electricity as a value.
For this example we want pin 2 to output electricity so our LEDs can turn on.
We do this by calling upon the pinMode function. This function takes two parameters. It requires a pin number (2) and it requires you to choose between OUTPUT or INPUT. In this case we pick OUTPUT.
Now our Arduino knows that we want pin 2 to be a source of power for our LEDs.
void loop is a function that runs forever. Once it finishes its commands it starts back over at the beginning.
A digital pin can either digitalRead or it can digitalWrite.
If a pin is reading, it is doing so as an INPUT pin and it is acquiring information, much like you do when you read.
If a pin is writing, it is doing so as an OUTPUT and it is giving out information, similar to what happens when you write.
In a circuit information is electricity.
The digitalWrite command has two parameters as well. It requires a pin number as well as a HIGH or LOW command. If a pin is HIGH electricity will be “turned on”. If a pin is LOW electricity will be “turned off”.
The command above is telling to turn the electricity in pin 2 on!
The delay command slows the microprocessor down. It takes milliseconds as an input. This is telling the processor to wait one full second, or 1000 milliseconds.
So when we look at everything inside void loop:
We will turn the electricity on, wait one second, turn the electricity off, wait one second.
Now we have hit the end of the code so it goes back to the beginning and turns the electricity on, waits one second, turns the electricity off, waits one second. It has hit the end of the code so it goes back to the beginning and turns the electricity on, waits one second, turns the electricity off, waits one second. It has hit the end of the code so it goes back to the beginning and turns the electricity on, waits one second, turns the electricity off, waits one second. It has hit the end of the code so it goes back to the beginning and turns the electricity on, waits one second, turns the electricity off, waits one second. It has hit the end of the code so it goes back to the beginning and turns the electricity on, waits one second, turns the electricity off, waits one second. It has hit the end of the code so it goes back to the beginning and turns the electricity on, waits one second, turns the electricity off, waits one second. It has hit the end of the code so it…
Our lights are blinking!
However, they are blinking together. If you want them to blink separately you have to change the circuit and use two ports.
Example 4
Let us set up the circuit so that the two LEDs can blink independent of each other. For this exercise all I will provide you with is the circuit. The code is up to you. This is also the first extension in Project 1!
• Attach a red wire from port 3 to the first circuit. (I went from port 3 to row 7)
• Attach a second red wire from port 2 to the second circuit. (I went from port 2 to row 17)
• Attach a black wire from GND to the – bus.
Next we will wire two separate circuits.
• A 220-ohm resistor from your charged row to a new row further down the breadboard. (I went from row 7 to 11)
• An LED from your charged row to a new row further down the breadboard. Remember that LEDs will only work in one direction. You must have electricity flow from the longer anode (+) to the shorter cathode (-). (I went from row 11 to 13)
• A wire to have your electricity flow back to the - bus to complete the circuit. (I went from row 13 back to the – bus)
• A 220-ohm resistor from your charged row to a new row further down the breadboard. (I went from row 17 to 20)
• An LED from your charged row to a new row further down the breadboard. (I went from row 20 to 22)
• A wire to have your electricity flow back to the - bus to complete the circuit. (I went from row 22 back to the – bus)
See if you can complete both Extension 1 and Extension 2 from the Project 1. Good luck!