Blinking LED on Raspberry Pi using C

  • 16 March 2017
  • ADM

 

Blinking LED on Raspberry Pi using C - images/logos/raspberrypic.jpg

 

In this article will make a LED blinking using C. If you want to see the same circuit in other programming languages, please check the references section.

Components

  • 1 * Raspberry Pi (I am using Raspberry Pi 3 Model B)
  • 1 * 220Ω Resistor
  • 1 * LED
  • 1 * Breadboard
  • 2 * Jumper wires
  • software: WiringPi

Note: you can easily find all components on Amazon or Ebay (in my case, I bought a "Raspberry Pi starter kit" containing a lot of components).

LED

The LED is the abbreviation of light emitting diode. The LED has two electrodes, a positive electrode and a negative electrode. It lights up only when a forward current passes. The colour of light depends on the materials it is made. In general, the drive current for LED is really low, between 5-20mA. Usually, needs an extra resistor for the current limitation to protect the LED.

The symbol for a LED is:

Blinking LED on Raspberry Pi using C - /images/BlinkingLED01.png

There are two methods for connecting a LED with Raspberry Pi GPIO:

  • The cathode connected to the Raspberry Pi GPIO.

Blinking LED on Raspberry Pi using C - /images/BlinkingLED04.png

The anode of the LED is connected to VCC (+3.3V), and the cathode to the Raspberry Pi GPIO. When the GPIO outputs a low level, the LED is on; when it outputs high, the LED is off.

  • The anode connected to the Raspberry Pi GPIO.

Blinking LED on Raspberry Pi using C - /images/BlinkingLED03.png

The anode of LED is connected to Raspberry Pi GPIO after a resistor, and the cathode is connected to ground (GND). When the GPIO outputs a high level, the LED is on; when it outputs a low level, the LED is off.

Resistor

The main function of the resistor is to limit current. In the circuit, the character "R" represents resistor, and the unit of resistance is ohm(Ω).

The symbol for a resistor is:

Blinking LED on Raspberry Pi using C - /images/BlinkingLED02.png

A band resistor is used in this tutorial. It is a resistor with a surface coated with some particular colour through which the resistance can be identified directly.

To calculate the resistor value needed for this tutorial will use Ohm law:

R=U/I

We already know that to make a LED on a current between 5~20mA is needed. We also know that Raspberry Pi GPIO is 3.3V. Adding these two pieces of information in Ohm Law we can see that a resistor with a value between 165Ω and 660Ω is needed.

R = U / I = 3.3V / (5~20mA) = 3.3V / (0.005A ~0.020A) = 165Ω~660Ω

I have a 220ohm resistor in hand, so I will use it for this tutorial.

Schematics

To do the schematics I used Fritzing software.

The cathode connected to the Raspberry Pi GPIO

Here is the schematic connecting the cathode of the LED to the Raspberry Pi GPIO. Using the Pinout website for reference, we can see that we connected the LED cathode to the physical pin 11(BCM pin 17, Wiring Pi pin 0)

Blinking LED on Raspberry Pi using C - /images/BlinkingLED05.png

And the physical visualisation of the circuit.

Blinking LED on Raspberry Pi using C - /images/BlinkingLED06.png

The anode connected to the Raspberry Pi GPIO

Here is the schematic connecting the anode of the LED to the Raspberry Pi GPIO. As you can see the change is just how the connected are placed.

Blinking LED on Raspberry Pi using C - /images/BlinkingLED07.png

And the physical visualisation of the circuit.

Blinking LED on Raspberry Pi using C - /images/BlinkingLED08.png

Hardware

Now it's time to connect everything according to the diagram presented in the section above.

The cathode connected to the Raspberry Pi GPIO

Blinking LED on Raspberry Pi using C - /images/BlinkingLED10.jpg

The anode connected to the Raspberry Pi GPIO

Blinking LED on Raspberry Pi using C - /images/BlinkingLED09.jpg

You can see that the only difference is how it is connected to the board.

Software

Key functions

  • int wiringPiSetup (void)

    The function must be called at the start of your program, or your program will fail to work. You may experience symptoms from it simply not working to segfaults and timing issues.

    Note: This function needs to be called with root privileges.

  • void pinMode (int pin, int mode)

    This function sets the mode of a pin to either INPUT, OUTPUT, PWM_OUTPUT or GPIO_CLOCK. Note that only wiringPi pin 1 (BCM_GPIO 18) supports PWM output and only wiringPi pin 7 (BCM_GPIO 4) supports CLOCK output mode.

    The function has no effect when in Sys mode. If you need to change the pin mode, then you can do it with the gpio program in a script before you starting your program.

  • void digitalWrite (int pin, int value)

    Write the value HIGH or LOW (1 or 0) to a given pin which must have been set previously as output. WiringPi treats any non-zero number as HIGH, while 0 is the only representation of LOW.

  • void delay (unsigned int howLong)

    This function causes program execution to pause for at least howLong milliseconds. Due to the multitasking nature of Linux, it could be longer. Note that the maximum delay is an unsigned 32-bit integer or approximately 49 days.

Note: all the code will be written directly on Raspberry PI console using vim editor. I've chosen this method because the example is simple and doesn't require a long time to edit. A better approach is to edit the source code locally, on your PC, using your preferable editor and then upload the code to Raspberry PI.

Login to Raspberry PI using ssh command from Linux and MacOS or using putty from windows, then open the vim editor using the following command:

$ sudo vim blinkingLed.c

Copy/paste the following code.

#include <wiringPi.h>
#include <stdio.h>

#define LedPin 0

int main(void) {
        if(wiringPiSetup() == -1) { //when initialize wiringPi failed, print message to screen
                printf("setup wiringPi failed !\n");
                return -1;
        }

        pinMode(LedPin, OUTPUT);
        while(1) {
                digitalWrite(LedPin, LOW);   //led on
                printf("led on\n");
                delay(1000);			     // wait 1 sec
                digitalWrite(LedPin, HIGH);  //led off
                printf("led off\n");
                delay(1000);                 // wait 1 sec
        }
        return 0;
}

Save and close vim.

Test

To compile the application run the following command:

$ gcc blinkingLed.c -o led -lwiringPi

The parameter "-o" is to specify a file name for the compiled executable program.

To run the application execute the following command:

$ sudo ./led

Regarding how you connect the LED, you should see the LED blinking, one second ON, one second OFF. The only difference is:

  • when the cathode is connected to the Raspberry Pi GPIO the text printed in the console will correspond with the LED state.
  • when the anode is connected to the Raspberry Pi GPIO the text printed in the console will not correspond with the LED state: When is printed "led on" will be actual off.

Blinking LED on Raspberry Pi using C - /images/BlinkingLED12.jpg

Blinking LED on Raspberry Pi using C - /images/BlinkingLED11.jpg

 

References