Filter results by

Blink an LED

Having tried out some basic commands on your ARTIK developer board, you'll naturally want to start programming it. In this example, we'll do the IoT universe's equivalent of the famous "Hello World" program – use ARTIK to blink an LED.

We'll begin by doing it manually through the Linux® command line, and then compare that to doing it programmatically using:

  • Arduino IDE and its library for ARTIK
  • Python
  • C code.

If you're in a hurry, start with the Linux command line version and then try either the Python or C code, as they do not require an active network connection to get set up.

Otherwise, take a few moments to set up wireless network connectivity. The Arduino IDE requires this, as well as some additional installation. But once you have it all working, you'll quickly come to understand the power and simplicity of the Arduino IDE compared to other methods.

Hardware Required

  • ARTIK 5, 7, or 10 development board
  • LED
  • 220 ohm resistor
  • Breadboard and connector wires

Building the Circuit

ARTIK 530 or 710 Board. The ARTIK 530/710 developer kits make this tutorial easy, as the boards implement an LED circuit already. You don't have to connect anything. But if you do want to construct the external circuit for educational purposes, please do so! Follow the directions below. You'll need it if you want to try the Arduino IDE later in this tutorial.

ARTIK 520 or 1020 Board. We're choosing arbitrarily to use GPIO pin [8] for the ARTIK 520 or 1020 board. Connect an LED and 220 ohm resistor in series as shown below, between GPIO pin [8] on J27 and GND (on any connector). The "active high" output circuitry drives the line in the high (Vcc) direction when programmed to HIGH / 1.

Basic output circuit

(Note that early boards used "active low" outputs: a logical HIGH / 1 drives the output low. You need to connect the circuit to 3.3V instead of ground, as shown here.)

Troubleshooting Tip. If you are unsure about how the circuit works, just connect one side to GND and the other to 3.3V to make sure the LED lights. If it does not, reverse the wires and try again. Note that the LED will not light as brightly when connected to the GPIO pin, whose output drive strength is limited.

The circuit will look something like the picture below.

Circuit for blinking an LED

Now that you've built the appropriate circuit, we can set up the GPIO pin.

Blinking the LED Manually

Linux offers a uniform way to access GPIO pins through its sysfs functionality, which you can read up on here at your leisure. But for now, it's sufficient for you to know that to control a GPIO pin, you first ask Linux to "export" control of it to you, and then write short instructions to its exported control files direction and value.

ARTIK 530 and 710 Boards

We're going to start by manually (from the Linux command line) controlling the GPIO pin.

We have two LED choices:

LED
– Red LED: sysfs GPIO 28 on ARTIK 530/710
– Blue LED: sysfs GPIO 38 on ARTIK 530/710

(click on the links above to see where this mapping comes from)

ARTIK 530/710 Example. To control the Blue LED, you would proceed as follows.

  1. Request control of the desired GPIO pin.

    $ echo 38 > /sys/class/gpio/export

    sysfs responds by creating a GPIO-specific directory (in this case, gpio38).

  2. Configure the GPIO pin to be an output.

    $ echo out > /sys/class/gpio/gpio38/direction

    You're just writing the string "out" to a file named direction.

  3. Set the output level by writing the value file contents to "1":

    $ echo 1 > /sys/class/gpio/gpio38/value

    or "0":

    $ echo 0 > /sys/class/gpio/gpio38/value

You should now be able to turn the LED on or off. Try it again using 28 to see the Red LED turn on and off.

ARTIK 520/1020 Board

We're going to start by manually (from the Linux command line) controlling GPIO pin [8].
sysfs GPIO 12 on ARTIK 1020
sysfs GPIO 125 on ARTIK 520

ARTIK 520 Example. To control the LED on pin [8], you would proceed as follows.

  1. Request control of the desired GPIO pin.

    $ echo 125 > /sys/class/gpio/export

    sysfs responds by creating a GPIO-specific directory (in this case, gpio125).

  2. Configure the GPIO pin to be an output.

    $ echo out > /sys/class/gpio/gpio125/direction

    You're just writing the string "out" to a file named direction.

  3. Set the output level by writing the value file contents to "1":

    $ echo 1 > /sys/class/gpio/gpio125/value

    or "0":

    $ echo 0 > /sys/class/gpio/gpio125/value

If you've wired your board up correctly, you should now be able to turn the LED on or off.

Using the ARTIK IDE

To use the ARTIK IDE:

You'll find all the code is ready for you to play with!

Using the Arduino IDE

In order to use the Arduino IDE, you'll first need to set it up on your computer and ARTIK. Follow the "libArduino howto" guide and the Arduino IDE article. (Go ahead, we'll wait.)

If you are using the ARTIK 530 or ARTIK 710 board, you'll need to construct the external LED circuit for this exercise. The Arduino IDE makes references to the pin by number, so we need pin [8] connected.

With the circuit built and tested from the previous section, start the Arduino IDE and enter the code below (you can copy and paste it).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
  This example code is in the public domain.
 */

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 8 as an output.
  pinMode(8, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(8, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(8, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}

The first thing the code does is initialize pin 8 as an output pin with the line:

pinMode(8, OUTPUT);

In the main loop, it switches the LED on and off with the lines:

digitalWrite(8, HIGH);

digitalWrite(8, LOW);

In between the on and the off, you want enough time for a person to see the change, so the delay() commands tell the board to do nothing for 1000 milliseconds (1 sec).

As you can see, Arduino simplifies things by using the pin number directly; you did not need to specify any sysfs GPIO mapping to the pin.

Using Python

In this version of the example, we configure the pin using Python code.

You can download code here, but it was written to use ARTIK 10 pin [13]. So you'll need to change the ledpin = 22 to map to pin [8] instead. As above, the mapping is:

Notice how closely the code follows the manual method you used in the earlier section:

  1. Request control of the desired GPIO pin.

    1
    2
    3
     pinctl = open("/sys/class/gpio/export", "wb", 0)
     try:
         pinctl.write( str(ledpin))
  2. Configure the GPIO pin to be an output.

    1
    2
    3
    4
     filename = '/sys/class/gpio/gpio%d/direction' % ledpin	
     pinctldir = open(filename, "wb", 0)
     try:
         pinctldir.write("out")
  3. Set the output level by writing the value file contents to "1" and "0":

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     filename = '/sys/class/gpio/gpio%d/value' % ledpin
     while True:
     try:
         pin = open(filename, "wb", 0)
    
         pin.write( str(1) )
         time.sleep(1)
    
         pin.write( str(0) )
         time.sleep(1)
    
         pin.close()

You would execute it as
python –i blink.py
as explained in Extra Credit from the first tutorial.

Using C

C code can be used to control GPIO pins. The example provided here is along the lines of the manual approach shown previously. But normally, C code for this purpose would be within kernel or driver modules, which would not handle it in this way – they would bypass sysfs and act directly on the chip hardware. We'll address that topic once you are more familiar with ARTIK.

You can download the code here, and once again, you'll need to map it correctly:

Notice how closely the code follows the manual method you used in the earlier section:

  1. Request control of the desired GPIO pin.

    1
     fd = fopen("/sys/class/gpio/export", "w")
  2. Configure the GPIO pin to be an output.

    1
     sprintf(fName, "/sys/class/gpio/gpio%d/direction", pin);
  3. Set the output level by writing the value file contents to "1" and "0":

    1
    2
    3
    4
    5
    6
    7
     sprintf(fName, "/sys/class/gpio/gpio%d/value", pin);`
     ...
     if(val == HIGH) {
         fprintf(fd, "1\n");
     } else {
         fprintf(fd, "0\n");
     }

You would compile using either a cross compiler or the built-in compiler to generate the executable for your boards. If you choose to use the built-in compiler, just use scp to copy your code to ARTIK, and the gcc compiler to build it as explained in Extra Credit from the first tutorial.

Summary

Program control of GPIO pins on the ARTIK platform is straightforward using most any means, but is particularly simple when using the Arduino IDE platform.

The next tutorial shows how monitoring inputs can be just as easy.

Last updated on: