Mar 232017

ADM

Flowing LED on RaspberryPi using C

  • 23 March 2017
  • ADM

 

Flowing LED on RaspberryPi using C - images/logos/raspberrypic.jpg

 

In this article will make a flowing LED strip flowing in both directions using C. If you need more info on LEDs and how can be connected please follow Blinking LED on Raspberry Pi using C tutorial. Also if you want to see the same flowing LED circuit in other programming languages, please check the references section.

Components

  • 1 * Raspberry Pi (I am using Raspberry Pi 3 Model B)
  • 5 * 220Ω Resistor
  • 5 * LED
  • 1 * Breadboard
  • 6 * 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).

Schematics

To do the schematics I used Fritzing software. For this tutorial, I will connect the cathode to the Raspberry Pi GPIO, also it is possible to connect the anode to the Raspberry Pi GPIO, check Blinking LED on Raspberry Pi using C tutorial for details.

Here is the schematic connecting all 5 LEDs using the cathode of the LED connected to the Raspberry Pi GPIO. Using the Pinout website for reference, we can check the numbering, starting from physical pin 11(BCM pin 17, Wiring Pi pin 0).

Flowing LED on RaspberryPi using C - /images/FlowingLED01.png

And the physical visualisation of the circuit.

Flowing LED on RaspberryPi using C - /images/FlowingLED02.png

Hardware

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

Flowing LED on RaspberryPi using C - /images/FlowingLED03.jpg

Software

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 flowingLed.c

Copy/paste the following code.

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

int main(void)
{
        int i;

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

        for(i=0;i<5;i++){       //make 5 pins mode is output
                pinMode(i, OUTPUT);
        }

        while(1) { //infinte loop
                for(i=0;i<5;i++){   //make led on from left to right
                        digitalWrite(i, LOW); //led on
                        delay(500);
                        digitalWrite(i, HIGH); //led off

                }
                //delay(500);
                for(i=5;i>=0;i--){  //make led off from right to left
                        digitalWrite(i, LOW);
                        delay(500);
                        digitalWrite(i, HIGH); //led off

                }
        }

        return 0;
}

Save and close vim.

Test

To compile the application run the following command:

$ gcc flowingLed.c -o fLed -lwiringPi

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

To run the application execute the following command:
$ sudo ./fLed

You should see the LEDs light flowing from one side to the other and back.

Flowing LED on RaspberryPi using C - /images/FlowingLED04.jpg

Flowing LED on RaspberryPi using C - /images/FlowingLED05.jpg

 

References