Arduino Basics: Big Blinkie

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.

A starting point

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:

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

Comments

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.

Understanding your 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 setup()

int led = 13

void setup() {                
  pinMode(led, OUTPUT);     
}

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/electricity
  • INPUT - 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.”

The loop()

void loop() {
  digitalWrite(led, HIGH);
  delay(1000);
  digitalWrite(led, LOW);
  delay(1000);
}

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?

Building on a breadboard

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.

  1. Plug a red wire from 5V pin on your Arduino into row 1 of your breadboard. It’s just red because, well, that means power! 5V is on the opposite side of the board as the blinking light (not to be confused with pin 5, which is different. 5V actually says 5V on it).
  2. Bend the wires on your resistor - don’t be scared about breaking it - and connect one leg in row 1 (to connect it to your 5V) and one leg in row 6.
  3. Plug the long leg of your LED into row 6. This connects it to your resistor! Then plug the short leg into the row 9.
  4. Plug a black wire into row 9 to connect it to your LED, and plug the other end of the wire into one of the GND pins on your Arduino.

!!! 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.

Playing it hot and cold

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.

Making a strong, independent LED

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.

  1. Change the line of code 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.
  2. Click the Upload button (the right arrow) to send the new program up to your Arduino. It’ll shimmy a little, then the LEDs will all go dark.
  3. Disappointed? Don’t start bawling yet! We’ll still relying on pin 13 for our electricity - change your red wire to connect to pin 12 and see what happens.

(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!

Changing things up

Looking for a next-level challenge? Try these:

  • Change the timing for the light to blink quickly - say, half a second on and a quarter-second off
  • Connect a second and third LED between the resistor row and the ground row. Does it work? Why or why not?
  • Change out the 470 ohm resistor for another one (somewhere between 200 ohms and 1K ohms should be safe). Which ones are brightest?
  • Use pin 12 to control one LED, and pin 8 to control another LED. As a hint: you’ll need to add an int led2 = 8, tell it you’re using it for OUTPUT and add in a couple more digitalWrites.
  • In the same way we used the two legs of the resistor or the LED to cross between rows, add a switch to the circuit, between the LED and the ground would work. What happens when you hold it down? (No, no, hold it down LONGER!) What happens when you let it go? Why?

Next project: Big Buzzy or Lil Lightie.

Want to hear when I release new things?
My infrequent and sporadic newsletter can help with that.