The very first thing every electronics course does is teach you to blink a light, and this is no different. We’re going to jump in straight to making it do something, then explain what we just did before moving on.
Open the Arduino IDE. Your teacher willl tell you where to find it Press
Ctrl +
a few times to make the writing bigger Delete the code that is already there
The program you’ve just opened lets us write code, and then pushes it over to the Arduino board where it will live forever, doing whatever the code says, until we overwrite it again. When we code, we need to make especially sure we get capitals, punctuation and brackets exactly right.
Copy the following code into the Arduino IDE, making sure to get it exactly the same. Copy paste is an option, but before long you’ll be told to write your own code that isn’t on this site, so it’s probably worth typing at least some of this code yourself for practice.
1
2
3
4
5
6
7
8
9
10
11
12
const int LED = 13;
void setup() {
pinMode(LED, OUTPUT);
}
void loop() {
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED, LOW);
delay(1000);
}
We can check our code to see if it can be understood by the Arduino by compiling it. This will get it ready for the Arduino UNO even if the computer isn’t connected to it yet. We do this by clicking the Tick
icon in the top left corner. First you will get a box asking if you want to save, which you absolutely should. Then down the bottom it will say “Compiling Sketch” and then hopefully Done Compiling. If it goes orange, it means something went wrong. Make sure your code matches exactly.
Compile your code by clicking the
Tick
button. When it asks where to save, make sure to put it in your home folder, and give it the name “Blink” so that we can tell it apart from all our future projects. If you don’t save your work where you can find it, you’ll have to rewrite code again later on.
At this point, we have successfully written our code, but unless you’ve done this before, we have no idea what it does. We need to move our code onto the Arduino board so that it can actually use it. When we first plug in an Arduino board, it can sometimes take a little while for the IDE to see it. We need to make sure it does before we can proceed. We check by looking at the connected ports in the IDE.
Go to the
View
menu at the top of the IDE, look forPort
and see what options are there.
These are the ports the computer can already talk to. They are NOT the ones we want. Ever.
Now plug in the Arduino using a USB cable to a port on the computer. Let the board do whatever it’s doing (It will flash a few times) and when it settles down there should be at least one light on it. After a few seconds, go back to
View
andPort
and find out which port has shown up. This is your Arduino port.
Using different plugs on the computer will change this number. If you ever see Problem uploading to board in the orange error bar, it’s because this port number is wrong.
With the IDE now knowing where to find the Arduino, we can finally send our code to it. Find and push the Upload
button, it is the arrow next to the Compile Tick
. Down the bottom it will say “Uploading” and then hopefully “Done Uploading”. While this happens, some more lights on the Arduino will flash to show it is talking to the computer and getting new code. Once it says “Done uploading” our code is now on the Arduino and running. Hopefully you can see something happening!
We’ve made a blinky light! The speed of the blinks is controlled by how much delay
the code has between turning off and on. Notice that the code has two delay
lines, and both have a number. Big numbers = big delay = slow. It’s measured in milliseconds which means 1000 = 1 second, so delay(1000)
means “delay one second”
Change both of the
delay
lines from1000
to100
and see what happens. Try to make a light that stays on for two seconds then turns off for 250 milliseconds (Long on, short off)
There’s some punctuation you probably haven’t used before in this code. Semicolons, curly brackets, round brackets, angle brackets, and double quotes. Get used to these and their names, because we need them a lot in the future.
We should take a closer look at the 3 sections of our code before we finish up
Section of code | What it does | ||
---|---|---|---|
|
This tells the Arduino to keep track of the number 13 as the name |
||
|
|
||
|
|
Make sure you understand how the code is controlling the light in the above code. Once you do, complete the following challenge activities. You might have already done some while experimenting so they can get signed off straight away.
- Make the light blink
- Make the light blink faster (Lower delay values)
- Make the light stay off for a long time, then do a single quick flash
- Make the light blink 3 times fast, then stay off for a long time
- Make sure at least one of these is saved as “Blink” in your home folder