Now that you know how to upload a project to your Arduino, learn to use setup(), loop(), breadboards, LEDs and resistors.
The plan for Big Blinkie is to make an LED blink. Last time we made an LED blink on the Arduino board, but that LED wasn’t terribly exciting. This time we’re going to make a BIG ol’ LED blink!
When you finish this tutorial you will have a solid idea of the fundamentals of both Arduino programming and how to put together a circuit on a breadboard. And, I mean, that’s basically everything.
NOTE: I’m assuming the LED you bought probably is still reasonably tiny. I apologize, but “Tiny Blinkie” doesn’t sound as good.
We’re still going to use the Blink
example from First Steps, but this time we’re just going to leverage it to power an external LED instead of the one on your Arduino.
If you don’t already have it open, load up Blink
by selecting File > Examples > 01.Basics > Blink
or by pasting in the following code:
You’ll notice lots of //
and /* blah blah */
in the Arduino code. These are called comments, and they’re meant for people to read, not the Arduino. You can type anything on a line after a //
and the Arduino won’t even try to understand it.
Comments are useful for providing notes for folks who might read your code later (including yourself, if you’re exceptionally forgetful).
From here on our, I’m going to take the comments out of Blink
so we can just focus on the code.
Every Arduino problem has two parts, the setup()
and the loop()
. The setup happens just once (at the beginning), and the loop()
happens again and again and again, gazillions of times per second.
If I told you go run laps around the park, you’d put on your shoes (setup()
) and start running around the park (the loop()
). If you followed directions as well as an Arduino, you’d run around it again and again and again until I unplugged your USB cable.
If you look at the code, you’ll see there’s also a non-comment line up above setup()
, and it says int led = 13
. This code happens before setup()
, and you can think of it as a little pre-setup
setup, like waking up in the morning before you go to put on your shoes.
The little black holes along the sides of your Arduino are called pins. They’re all numbered, and if you squint real hard you can see a 13
next to the LED that’s lighting up. Usually you have to plug wires into the pins to make anything happen, but 13 is a special pin that has an LED automatically attached to it.
When we say int led = 13
, we’re creating a variable called led
and telling it to hold onto the integer value 13
. Now every time we type led
we’re actually saying 13
! Later on we’ll change it to another number to see what happens. If you’ve never heard of a variable before, don’t sweat it.
Now, what’s this pinMode
thing? Well, technically it’s called a function, and you use it to set the…. well, the mode of the pin. Pins have two possible modes:
OUTPUT
- sending out information/electricityINPUT
- receiving measurements/electricity.Since we’re trying to send electricity out to the LED that’s connected to pin 13, we want to set the pin to OUTPUT
. Remember how we set led
to be 13
earlier? pinMode(led, OUTPUT)
just means, “set the mode of pin 13 to be output.”
Now we have the loop()
, which is the thing that runs again and again and again.
In digital stuff, electricity has two states, on/off, 0/1, high/low. You use digitalWrite
to start sending one of those signals to a specific pin.
Remember (again) how led
actually means 13
? digitalWrite(led, HIGH)
starts writing HIGH
(a.k.a. on or 1) to pin 13. This makes the LED come on.
delay(1000)
makes the Arduino wait for 1000
milliseconds before it does anything else.
Now that we’ve waited, digitalWrite(led, LOW)
shows up. This writes the LOW
value (a.k.a. off or 0) to pin 13, turning off the LED.
Then delay(1000)
makes it wait again after turning the light off. When it’s done waiting, your Arduino starts the loop all over again.
QUESTION: What happens when it starts the loop all over again? What would happen if you removed the second
delay(1000)
from the program?
Electricity is like a real boring person, in that it wants to leave school or work and immediately go right back home. In arduinospeak, it wants to go from 5V to GND (5 volts to ground). If you put obstacles between 5V and GND (LEDs, resistors, sensors, buzzers), the electricity runs through them and makes everything work (lights light up, buzzers start buzzing).
Our obstacle course is going to look like this:
5V ——> 470 Ohm resistor ——> LED ——> GND
That way the electricity runs out of the 5V, gets powered down by the resistor, lights up the LED, and then runs to the ground.
We’ll need a breadboard for this. Breadboards are places you can plug wires in to make connections, so you don’t have to tire the wires together (which sucks).
In every row of a breadboard, all 5 pins are secretly connected underneath. You’ll see how it works in a second!
TIP: The big line down the middle separates the rows, so the five on the left don’t connect to the five on the right. This doesn’t matter right now.
So, let’s get rocking! I give you row numbers down below in case it makes things easier, but as long as you keep everything that’s supposed to be in the same row together feel free to change it up.
!!! STAND BACK, IT’S MAGIC !!!
TIP: If it doesn’t work, check your pins and switch around your LED’s legs. The long leg needs to be pointing towards where the electricity is coming from.
It isn’t blinking, though, right? Just staying on? That’s because we’re pulling power from 5V, which always has power coming out of it. We want a pin that sometimes has power coming out of it (a.k.a. HIGH
) and sometimes doesn’t (a.k.a. LOW
).
Any ideas? At all? Ever met a pin that’s sometimes HIGH
and sometimes LOW
?
Yup, our old friend pin 13! Change one end of the red wire to plug into pin 13 instead of 5V.
!!! STAND FURTHER BACK, IT’S OLDER, STRONGER MAGIC !!!
Now your LED should be blinking! Along with powering the on-board LED it’s also sending real live electricity out of pin 13, that’s shuffling through the resistor, into our LED (lighting it up), and right back out into ground.
TIP: If it isn’t working, make sure that your pin 13 on-board LED is blinking and you’re actually plugged into pin 13.
I’m getting annoyed at that little light. It was cool once, sure, but we’ve upgraded to a bigger, better LED! Let’s change to using pin 12 instead of pin 13.
int led = 13;
to read int led = 12;
. That way every time we mention the word led
it’s talking about pin 12, not pin 13.(What happens should be it works, and without that little baby light coming on any longer.)
TIP: If you try to save, it’ll yell at you because it doesn’t want you editing samples. Create a new directory somewhere for your brand-new sketches and save your file there!
Looking for a next-level challenge? Try these:
int led2 = 8
, tell it you’re using it for OUTPUT
and add in a couple more digitalWrite
s.Next project: Big Buzzy or Lil Lightie.