Making a LED blink with Arduino

Rodrigo Sousa Coutinho
Arduino Playground
Published in
3 min readFeb 18, 2017
Why settle for 1 blinking LED, when you can have 2?

Now that you know how to connect a LED to Arduino, let’s make it blink.

To do this, we’ll connect the LED to one of the digital outputs of Arduino, instead of connecting directly to the 5V pin. Let’s pick pin 7, for no particular reason. The circuit should look like this:

The blinking LED circuit. That’s a 330Ω resistor.

Now it’s time to code! A typical Arduino code has 2 parts: the setup and the loop.

On the setup part you initialize the circuit and put any instructions that you want to execute once, before the loop begins.

The loop part, as the name suggests, is executed in a loop as long as the Arduino is on.

Here’s the code you need to write to make the LED blink.

Let’s go trough it step by step:

On the first statement we’re telling the computer that each time we write LED_PIN we actually mean 7. This is a good idea, because if you want to change your LED to pin 6, you just need to change this line of code.

During setup, we need to tell Arduino that we’re using the 7th pin for output:

pinMode(LED_PIN, OUTPUT);

On the loop part, we will have 4 instructions. First:

digitalWrite(LED_PIN, HIGH);

What this does is tell pin 7 to turn on. This will put a 5V current on the pin, which will cause the LED to light up. Next we wait for 1 second, or 1000 milliseconds:

delay(1000);

Because we want the LED to blink, the next two instructions will turn off pin 7, putting a current of 0V through it, and will wait for 1 second:

digitalWrite(LED_PIN, LOW);
delay(1000);

Because all these instructions are inside a loop, the light will turn on for a second, then turn off for another second, and then repeat this behavior forever.

Making 2 LEDs blink!

Ok, let’s go wild! We’ll make 2 LEDs! When one is on, the other one will be off, an vice-versa.

Blinking duo! The circuits always look messier in real life…

The circuit looks something like this:

Double blinking power!

And the code will look something like this:

Can you guess what all the lines of code do?

And if two LEDs is too little for you, how about playing with 64 LEDs?

--

--

Rodrigo Sousa Coutinho
Arduino Playground

Hi! I’m co-founder and Director of Data Science at OutSystems, with a passion for data, great products, and geeky stuff!