To control Arduino’s on board LED is pretty simple. The first example you will program is called Blink. To blink an LED means to repeatedly switch between the two possible states by turning it on and off.In the same way that Processing programs always need to have a
setup()
and a draw()
function, Arduino programs need to have a setup()
and a loop()
ones:
setup()
: This part of the program runs only once at the beginning. This is where you tell the board what pins you are going to use, and if you are going to use them as inputs or outputs.loop()
: This part will run forever (or until you disconnect the power source). The commands inloop()
are executed in order, one after the next. When reaching the last line, it will start from the beginning again.
Example 2.1
In this example, you will make the on-board LED blink.
Materials
- 1 Arduino Uno board
Instructions
- Open the Arduino IDE and write the following code:
- Connect the Arduino board to your computer using the USB cable.
- Compile and upload the code to the board using this icon in the IDE. [upload icon]
Result
The on-board LED should now blink with a 2 second interval, being turned on for 1 second and then turned off for 1 second.
New commands
pinMode(pinNumber, OUTPUT)
: configures the digital pinpinNumber
to behave as an output.digitalWrite(pinNumber, HIGH/LOW)
: writesHIGH
orLOW
to the digital pinpinNumber
.delay( time )
: pauses the program for the amount of time, in milliseconds, specified.
Repetition:
- Every line ends with a semicolon
;
. - Blocks of code are contained within curly brackets
{ }
. - Functions start with a definition like
void
. - Functions have parameters contained between brackets
( )
. The amount of parameters depend on the function.
How it works
- In
setup()
, pin 13, the digital pin to which the on-board LED is connected, is configured as an output. - In
loop()
, the on-board LED is turned on by writingHIGH
to pin 13. This means that you send 5V to digital pin 13. - The program pauses for 1000 milliseconds, or 1 second.
- The on-board LED is turned off by writing
LOW
to pin 13. This means that you send 0V to digital pin 13. - The program pauses for another 1000 milliseconds.
loop()
continues to loop.
Add Your Own LEDs
You can add your own LEDs to the circuit. Any electronic circuit you connect needs to be connected to power and ground for the electricity to flow through and power your components. On the Arduino board, power is provided through the 5V pin or one of the digital pins (when set to HIGH
). The ground pin is marked as GND.
When working with an LED you need to remember that they have polarity. LEDs are constructed to show this. The long pin, named anode, is positive. When wiring it in a circuit, this pin should be connected to power, in this case a digital pin because you want to be able to control it. The short pin, or cathode, is negative and should be connected to GND.
LEDs will not break if you connect them in the opposite way, but they will not work. LEDs cannot carry a lot of current through them. In order to protect them from burning, you need to put a resistor in serial with them. The value of that resistor varies, in the CTC examples a 220 Ohm resistor is always used with an LED. Read more about resistors and Ohm’s law here.
To make it easier to make circuits you will use the Education shield. When mounted on your Arduino the shield extends the boards capabilities as well as it brings a few extra features. To make a circuit with an LED you will need to use the breadboard on top of the shield. Read more about the breadboard here, and about the Education shield here.
Example 2.2
In this example, you will connect an LED to your Arduino and make it blink.
Materials
- 1 Arduino Uno board
- 1 Education Shield
- 1 LED
- 1 220 ohm resistor
- 2 jumper wires
Instructions
- Attach the shield onto the top of the Arduino board.
- Connect the LED across the breadboard gap.
- Connect a 220 ohm resistor to digital pin 5 using a jumper wire. Connect the resistor to the long leg of the LED.
- Connect the short leg of the LED to GND using a jumper wire.
- Upload the following code:
Result
As in the previous example, the LED should blink with a 2 second interval.
How it works
- In
setup()
, pin 5, the digital pin to which the LED is connected, is configured as an output. - In
loop()
, the LED is turned on by writingHIGH
to pin 5. - The program pauses for 1000 milliseconds.
- The LED is turned off by writing
LOW
to pin 5. - The program pauses for another 1000 milliseconds.
loop()
continues to loop.
Example 2.3
In this example, you will modify example 2.2 slightly. You will keep the same circuit but use a variable to hold the digital pin number where the LED is connected, instead of just writing the number. Check the variable section in the previous Block, if you need help remembering what a variable is.
Instructions
- Upload the following code:
Result
This example should do exactly the same as the previous one.
How it works
- The variable
ledPin
is declared to hold the value 5, the number of the digital pin you are using. - In
setup()
, pin 5 is configured as an output, using the variableledPin
. - In
loop()
, the LED is turned on by writingHIGH
to pin 5, using the variableledPin
. - The program pauses for 1000 milliseconds.
- The LED is turned off by writing
LOW
to pin 5. - The program pauses for another 1000 milliseconds.
loop()
continues to loop.
Learn by doing
- Try and see what happens when you change the times inside the delay function. Try making the delays smaller, to see what will happen. Test different ways to make the light blink faster or slower.
- Use the light to simulate a heartbeat. Check your heartbeat by measuring your pulse. You will notice that you always get two pulses, something like tick tock … pause … tick tock … pause … Try to simulate that with an LED. (HINT: you can copy and paste code)
- Finally, test to see what happens when the delay becomes really small. Try and notice if you can still see the light blink when you change to
delay(10)
.
VU-meter
It is possible to connect and control more than one LED with your Arduino. When you are using several LEDs in a line, this is called a VU-meter in the CTC program. The EducationShield library contains a class called vuMeter with several functions you can use to easily control several LEDs. Read more about the VU-meter here.