Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Blinking LEDs – manipulating digital GPIOs on the Intel® Galileo board with the IoT Development Kit

17 Oct 2014 1  
This sample code uses the Live USB image from the IoT Development Kit to run a host system that can control an Intel® Galileo board using Yocto Application Development Tools and Eclipse*.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

Introduction

This sample code uses the Live USB image from the IoT Development Kit to run a host system that can control an Intel® Galileo board using Yocto Application Development Tools and Eclipse*. This sample assumes the Intel Galileo board is booted using the SD Image, which is also available in the IoT Development Kit. It is possible to use this code directly on the board, but that’s a topic for a future paper.

The Intel Galileo board is an Arduino* certified board that offers endless possibilities for creating projects and enabling ideas for the Internet of Things. The following diagram shows how to access the controls for the digital GPIOs from the operating system.

The logical representation of each GPIO is available at the /sys/class/gpio directory. Sergey’s Blog has excellent coverage of the modules and mapping of the GPIOs, including the mapping above that explains the hardware underlying the GPIOs in the system.

For this example, we will use the pin numbers 5, 6, and 7 since they are directly mapped into the hardware and do not have any other inputs (unlike pin #3, for instance). We have one LED connected to each one of these pins. Since each LED has its own required input voltage, we have added resistance to adjust to that. The schematic looks like this:

Following is the sample code that provides the steps needed to manipulate the GPIOs:

 (1) Export the GPIO number to the /sys/class/gpio/export file
 (2) Set the direction: in for input, out for output

These steps are implemented using the openGPIO function, which opens the corresponding file and returns this file identifier for future reading or writing, depending on the direction declared.

int openGPIO(int gpio, int direction )
{
        char buffer[256];
        int fileHandle;
        int fileMode;

  //Export GPIO
        fileHandle = open("/sys/class/gpio/export", O_WRONLY);
        if(ERROR == fileHandle)
        {
               puts("Error: Unable to opening /sys/class/gpio/export");
               return(-1);
        }
        sprintf(buffer, "%d", gpio);
        write(fileHandle, buffer, strlen(buffer));
        close(fileHandle);

   //Direction GPIO
        sprintf(buffer, "/sys/class/gpio/gpio%d/direction", gpio);
        fileHandle = open(buffer, O_WRONLY);
        if(ERROR == fileHandle)
        {
               puts("Unable to open file:");
               puts(buffer);
               return(-1);
        }

        if (direction == GPIO_DIRECTION_OUT)
        {
               // Set out direction
               write(fileHandle, "out", 3);
               fileMode = O_WRONLY;
        }
        else
        {
               // Set in direction
               write(fileHandle, "in", 2);
               fileMode = O_RDONLY;
        }
        close(fileHandle);


   //Open GPIO for Read / Write
        sprintf(buffer, "/sys/class/gpio/gpio%d/value", gpio);
        fileHandle = open(buffer, fileMode);
        if(ERROR == fileHandle)
        {
               puts("Unable to open file:");
               puts(buffer);
               return(-1);
        }

        return(fileHandle);  //This file handle will be used in read/write and close operations.
}

In the main function, we call openGPIO for each pin we want to manipulate:

        fileHandleGPIO_5 = openGPIO(GP_5, GPIO_DIRECTION_OUT);
        fileHandleGPIO_6 = openGPIO(GP_6, GPIO_DIRECTION_OUT);
        fileHandleGPIO_7 = openGPIO(GP_7, GPIO_DIRECTION_OUT);

Once we have verified that there are no errors, we go to the fun part, which is changing the GPIO value to 0 or 1, effectively enabling or disabling the voltage in the port and making the LEDs blink:

        for(i=0; i< 10; i++)
        {
               //LED ON
               writeGPIO(fileHandleGPIO_LED, 1);
               writeGPIO(fileHandleGPIO_5, 1);
               writeGPIO(fileHandleGPIO_6, 1);
               writeGPIO(fileHandleGPIO_7, 1);
               sleep(BLINK_TIME_SEC);

               //LED OFF
               writeGPIO(fileHandleGPIO_LED, 0);
               writeGPIO(fileHandleGPIO_5, 0);
               writeGPIO(fileHandleGPIO_6, 0);
               writeGPIO(fileHandleGPIO_7, 0);
               sleep(BLINK_TIME_SEC);
        }

The full code can be found here. To execute it, open the Hello World sample that comes with the Live USB image in the IoT Development Kit and replace the code there with the code for BlinkingLEDs.c file. It should run without any problems.

Intel and the Intel logo are trademarks of Intel Corporation in the U.S. and/or other countries.
Copyright © 2014 Intel Corporation. All rights reserved.
*Other names and brands may be claimed as the property of others.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here