Click here to Skip to main content
15,886,110 members
Articles / Internet of Things / Arduino
Article

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

17 Oct 2014CPOL2 min read 17.7K   10  
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.

Image 1

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:

Image 2

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.

C++
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:

C++
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:

C++
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --