Blink LED, like what it should be, we make certain LED blinking. We can define “blinking” as continuous condition switching between ON and OFF condition. This is the simplest project we can do for Raspberry Pi.

Make sure you have wiringPi installed on Raspberry Pi already. If not, you can follow this article for guideline.

Objective

  • Make LED blinking.

Prerequisite

Requirement

Following is all materials I use for this tutorial.

  • Raspberry Pi model B
  • 5mm LED active high
  • 330 ohm resistor
  • USB A to micro USB (for powering Pi)
  • Raspbian Wheezy

This article will use Raspbian Wheezy. Although it is not a compulsion for you to update your Raspbian, it is recommended to do.

In this article, we will use pin numbering used in wiring Pi, not the one declared in Raspberry Pi reference.

Circuit

Here is the circuit image, created using Fritzing with Raspberry Pi Library update.

pi_blink_sketch

To build the circuit, attach the LED as shown in the figure. Remember that the long leg of LED is positive leg (also known as anode) and the short leg is negative leg (also known as cathode). The cable from GPIO 0 is directly connect to LED anode. The cathode is connected to resistor 330 ohm and then connected to ground on Raspberry Pi.

Code

Let’s see what code we use for this project and discuss it later.

Notice that we define two constant here, LED_PIN which is pin we use, and PAUSE for the delay interval which is 500 ms.

The heart of this project is pin 0. We use this pin as output pin. Therefore we initialize pin 0 as an output pin with the statement:

Next in the main loop, we turn the LED on by:

and turn it off by:

And we now know that digitalWrite() function is used to output signal on a pin.

The delay() command is used to tell Arduino to do nothing for given time. In our case, we make Arduino wait for 500 miliseconds.

To compile, use following command:

To run it, use following command:

Result

LED is blinking.

Why it Works

There are two condition of logic in circuit, HIGH and LOW. If you know boolean value, yes this is boolean value with HIGH is true and LOW is false.

The LED we use is active-high LED, which means it can turn on when it is given HIGH voltage.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">