Wednesday, June 26, 2013

pcDuino - Blink Tutorial

This tutorial will show you how to program the pcDuino to flash an LED on a connected breadboard.

It assumes you have a basic working knowledge of the pcDuino and Linux.

For this tutorial you will need the following items:

  1. A pcDuino running the 20130531 Ubuntu image with ssh.
  2. A breadboard.
  3. An LED.
  4. A 100ohm resistor
  5. Two wire jumpers.
  6. The source code below for blink.c


Hardware Setup

The hardware setup for this tutorial is pretty easy. 

Take the LED and plug the negative or cathode lead into the ground strip on you breadboard.  This lead is the shorter of the two leads and comes from the side if the LED that is flat.

Plug the positive or anode lead into a row on the body of the breadboard.

Take the resistor and plug on end into the same row as the positive lead of the LED and plug the other lead into another row on the body of the breadboard.

You should end up with something that is close to the picture below but without the jumpers.


Take your two jumpers and connect one into the ground strip and the other into the row as the resistor lead (and only the resistor lead) in it.  I used a black jumper for the ground and a green one for the signal.

Next you will need to connect the other ends of the jumpers to the pcDuino.  This should be done before power the pcDuino on.


At the bottom of the pcDuino board there are two rows of pins.  One row of pins is right next to the edge of the board and the other is on the other side of the first row.  We are going to be using pins in the row right next to the edge of the board.

With the board oriented so that the host usb ports are to the left and the hdmi / ethernet jack are to the left we want to connect the ground jumper to the fourth (4th) in from the right and the signal jumper to the fifth (5th) pin.  This will connect the ground jumper to a ground pin and the signal jumper to the pin assigned to gpio13.

At this point you are ready to power up your pcDuino to get the program loaded and compiled.

blink Program

Blinking an LED with the pcDuino is pretty simple.  On the pcDuino the pin we connected our signal jumper to is known as a gpio pin.  This stands for “general purpose IO”.

On the pcDuino the gpio pins are accessed using two pseudo files. 

The first “file” is /sys/devices/virtual/misc/gpio/mode/gpio13.  This “file” is used to set the mode of the pin.  There are three possible modes: input, output, and input with pull up resistor.  We will be using the output mode.

The second “file” is /sys/devices/virtual/misc/gpio/pin/gpio13.  This “file” is used to set the value of the pin.  Pins can be set to low or zero by writing a “0” to this “file”.  Pins can be set to high or one by writing a “1” to this “file”.

With our blink example writing a “1” to the value pin will light the LED and writing a “0” will turn off the LED.

The program below turns the LED on, waits two seconds, turns the LED off, waits two seconds and then starts the whole process over until it has been done 10 times.


#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define pinModePath "/sys/devices/virtual/misc/gpio/mode/gpio13"
#define pinDataPath "/sys/devices/virtual/misc/gpio/pin/gpio13"

int main(void) {

  int pinMode = open(pinModePath, O_RDWR);
  int pinData = open(pinDataPath, O_RDWR);

  // "0" - pin INPUT
  // "1" - pin OUTPUT
  // "8" - pin INPUT with pull-up resistor
  write(pinMode, "1", 1);
  // "0" - LOW
  // "1" - HIGH
  write(pinData, "1", 1);
  int i;
  for (i=0; i < 10; i++) {
    write(pinData, "1", 1);
    write(pinData, "0", 1);

Program Setup

Start by connecting to your pcDuino via ssh. I am connecting from a PC running Windows 7 Pro 64bit and using a KiTTY as my ssh program. KiTTY is a form of PuTTY v0.62 which has some added features I like. Any ssh program should work.

Login into any user on your pcDuino.  I used the default ubuntu user which if you haven’t changed it (which you should have) has a default password of ubuntu.


I would suggest making a subdirectory to build your program in.  This isn’t required but is a nice way when you get to more complicated programs of keeping everything together.  I used tutorial and then changed directory to tutorial.

The next step we will do will allow you to copy the source code from this post and paste it into a file on the pcDuino.  Start by copying the source code for blink.c posted above.  The go back to your ssh windows type “cat > blink.c” and return.  This will leave you with a blinking cursor one line below the cat command.


Next move your mouse pointer into the ssh window and paste the source code into the window.  Once the source code has finished “typing” into your window press control-D (while holding down the control key press and release the D).  This will bring you back to the prompt.  Most ssh programs I have used will automatically paste what is in the paste buffer into the window if you select the right mouse button.



If this process of entering the source code doesn’t work you can always manually type the source code into a file using a text editor on the linux system.  This isn’t that difficult to do but is beyond the scope of this tutorial.

Next you should make sure that the gpio drivers are loaded on your system.  This is done by running the “lsmod” command.  As shown in the window below there should be a line for hardwarelib followed by gpio.  If that line doesn’t exist you can use the privileged modprobe command to load the driver.  This is done by typing “sudo modprobe gpio”.  Once this has been done you can rerun the lsmod command and you should see the gpio’s listed.


You are now ready to compile your program and run it.  To compile and link you program you should use “gcc blink.c –o blink”.  The system will compile your program in file blink.c and save the executable in a file called blink.  To run the program you enter ./blink and the LED on your breadboard should begin to blink.


Thanks to the folks at Sparkfun and their great Getting Started with pcDuino for getting me started with putting this tutorial together.  Also thanks to the folks on the pcDuino website for their support.  Check out the website and forums for more information.


Unknown said...

Thank you

JFp said...

Worked a treat, thanks. Very clear guidance.