User Tools

Site Tools


projects:led

Blinking Led

Making a LED blink is like a classic “Hello world” program for embedded systems. We'll start with a small review of the electronic components you need, we will recap some basic physics and then we will make it blink!

What you need?

  • 1 breadboard
  • 1 LED
  • 1 resistor
  • 2 jumper wires
  • 1 development board (Raspberry Pi)

Physics review

Please read the Leds page for understanding how a led should be powered up.

Wire the components

Let's connect the components to the Raspberry Pi! Please keep in mind that
each pin on the boards acts like a software programmed power source. This means that

  • if you write on the pin the value one (HIGH), it will supply 3.3 V or 5 V (depending on the board)
  • if you write on the pin the value zero (LOW), it will supply 0 V (it will not supply power)

We put the cathode(-) of the led through resistor to ground and the anode(+) to pin 11. Here you can see more informations about Raspberry Pi's pins.

Let's start coding

C/C++/Objective-C

Create a new C, C++ or Objective-C project in Wyliodrin and open the main.c, main.cpp or main.m file.

When you create a new project, you can select the Blinking Led program template. It already has the code you need in all program languages supported by Wyliodrin.

First, import the wiringPi library.

#include <wiringPi.h> 

Then initialize the library and declare the pin mode inside the main function.

wiringSetup(); 
pinMode(11,OUTPUT); 

OUTPUT means that we can write 0 (LOW) or 1 (HIGH) on that pin. If we write 0 the voltage will be 0V and for 1 it will be 3.3V.

Now let's make a loop because we want to turn on and off the LED as long as the program runs. In this loop we will write to our pin 1 and after a delay we will write 0. The delay is needed because otherwise the value would change so fast that you won't see the difference.

while(1) 
{ 
	digitalWrite(11,HIGH); 
	delay(500); 
	digitalWrite(11,LOW); 
        delay(500); 
}

This is how your code should look like:

That's all! Now press Run and select your board. You made a LED blink! Congrats!

Pascal

Create a new Pascal project in Wyliodrin and open the main.pas.

When you create a new project, you can select the Blinking Led program template. It already has the code you need in all program languages supported by Wyliodrin.

First, import the wiringPi library.

uses wiringpi; 

Then initialise the library and declare the pin mode in the main program.

wiringSetup; 
pinMode(11,OUTPUT); 

OUTPUT means that we can write 0 (LOW) or 1 (HIGH) on that pin. If we write 0 the voltage will be 0V and for 1 it will be 3.3V.

Now let's make a loop because we want to turn on and off the LED as long as the program runs. In this loop we will write to our pin 1 and after a delay we will write 0. The delay is needed because otherwise the value would change so fast that you won't see the difference.

while true do 
begin 
	digitalWrite(11,HIGH); 
	delay(500); 
	digitalWrite(11,LOW); 
        delay(500); 
end;

That's all! Now press Run and select your board. You made a LED blink! Congrats!

PHP

Create a new Pascal project in Wyliodrin and open the main.php.

When you create a new project, you can select the Blinking Led program template. It already has the code you need in all program languages supported by Wyliodrin.

Then initialise the library and declare the pin mode in the main program.

wiringSetup (); 
pinMode(11,1); 

OUTPUT means that we can write 0 (LOW) or 1 (HIGH) on that pin. If we write 0 the voltage will be 0V and for 1 it will be 3.3V.

Now let's make a loop because we want to turn on and off the LED as long as the program runs. In this loop we will write to our pin 1 and after a delay we will write 0. The delay is needed because otherwise the value would change so fast that you won't see the difference.

while (true) 
{ 
	digitalWrite(11,1); 
	delay(500); 
	digitalWrite(11,0); 
        delay(500); 
}

That's all! Now press Run and select your board. You made a LED blink! Congrats!

Java

Create a new Java project in Wyliodrin and open the Main.Java.

When you create a new project, you can select the Blinking Led program template. It already has the code you need in all program languages supported by Wyliodrin.

First, import the pi4j library.

import com.pi4j.io.gpio.*; 

Then initialise the library be declaring a reference to it inside the Main class.

final static GpioController gpio = GpioFactory.getInstance(); 

The next step is to initialise the pin 11, so declare another reference inside the Main method.

GpioPinDigitalOutput led = gpio.provisionDigitalOutputPin(11, "led", PinState.LOW); 

OUTPUT means that we can write 0 (LOW) or 1 (HIGH) on that pin. If we write 0 the voltage will be 0V and for 1 it will be 3.3V.

Now let's make a loop because we want to turn on and off the LED as long as the program runs. In this loop we will write to our pin 1 and after a delay we will write 0. The delay is needed because otherwise the value would change so fast that you won't see the difference.

while(true) 
{ 
	led.high (); 
	Thread.sleep (500); 
	led.low (); 
        Thread.sleep (500); 
}

In order for Thread.sleep to work, we need to declare that the Main method throws InterruptedException.

public static void main (String[] args) throws InterruptedException 

That's all! Now press Run and select your board. You made a LED blink! Congrats!

projects/led.txt · Last modified: 2014/01/15 12:46 by alexandru.radovici