Skip to content

Get Started with SplashKit GPIO

General-purpose input/output, or GPIO, pins are a type of pin found on many microcontrollers that can be used for a variety of purposes. In this guide we use them to control the state of an LED
Written by: Jonathan Tynan
Last updated: Apr 27 2024


General-purpose input/output, or GPIO, pins are a type of pin found on many microcontrollers that can be used for a variety of purposes. These pins can be used to receive signals from sensors, or send them to control LEDs or motors, and so much more. The ability to effectively use these interfaces enables a huge range of interesting projects with real-world impact. In this guide, we cover the basics of GPIO pins and how to use them in your projects.

In a Raspberry Pi all pins are digital and individual GPIO pins are defined with two unique numbers, the physical pin number and the Broadcom SOC Channel (BCM) pin number. They can be found on the internet by searching for a “Raspberry Pi Pinout”. In SplashKit, we use the physical pin numbers to reference the GPIO pins.

Components

Breadboard

Breadboards are reusable devices used to build and test circuits. They are made up of a number of holes that are connected by hidden metal strips. Along the top and bottoms are the ground and power rails, and in the middle there are two sections separated by a channel. Each hole in a section is connected to the adjacent vertical holes. More information can be found at How to Use a Breadboard.

LED

An LED (Light Emitting Diode) is a device that emits light when an electric current passes through it. They feature two legs, a longer positive leg (the anode) and a shorter negative leg (the cathode). The longer leg is often kinked so that both legs protrude the same distance from the LED. To use it we connect the positive lead to the GPIO pin and the negative lead to a ground pin. More information can be found at Light-Emitting Diodes (LEDs)

220 Ω Resistor

The power that the Raspberry Pi can provide is actually too much for these LEDs. To prevent the LED from burning out, we must add a resistor to the circuit. The resistor limits the current that flows through the LED, preventing it from burning out. The exact value of the resistor is not critical, but too high a value does not allow enough illumination of the LED. A resistor in the range of 220 Ω to 1 kΩ should work well. More information on resistors can be found at Sparkfun - Resistors

Jumper Wires

Jumper Wires, or DuPont wires, are used to make a temporary connection different components. They can be M/M, M/F, or F/F. We are using M/F jumper wires in this guide.

The Circuit

Below we can see the circuit diagram for this project. We have the cathode of the LED connected to GPIO Pin 11, while the anode is connected to ground pin 6 through a resistor.

Circuit Diagram for Blinking an LED

And the physical circuit looks like the following image, in which we’ve connected an M/F jumper wire from Pin 11 to the cathode of the LED. The anode of the LED is connected to the resistor, which is then connected to the ground pin through another M/F jumper wire.

Photograph of a circuit to blink an LED

Starting the Daemon

Underneath SplashKit we use the Pigpio library, specifically its daemon. To interface with this daemon, it must be running. If it is not running we can expect some output like the following:

Terminal window
gpio_init() must be called before any other GPIO functions

We can check if its running by using the following command:

Terminal window
ps aux | grep pigpiod

If the daemon is not running, we can start it by using:

Terminal window
sudo pigpiod

To stop the daemon from running we can use the command:

Terminal window
sudo killall pigpiod

The Code

After the daemon is running, we can then create our program. Below is an example program that blinks an LED on and off.

#include "splashkit.h"
int main()
{
raspi_init();
pins led_pin = PIN_11;
raspi_set_mode(led_pin, GPIO_OUTPUT);
open_window("dummy_window", 1, 1);
while(!any_key_pressed())
{
process_events();
raspi_write(led_pin, GPIO_HIGH);
delay(500);
raspi_write(led_pin, GPIO_LOW);
delay(500);
}
close_all_windows();
raspi_cleanup();
return 0;
}

Understanding the code

To understand this code better, lets break it down and explore each section.

  1. raspi_init();
    pins led_pin = PIN_11;
    raspi_set_mode(led_pin, GPIO_OUTPUT);

    In this bit of the code we’re setting up the hardware, we call Raspi Init to initialise the GPIO pins, and then we define the specific pin we’re using which is Pin 11 in this case and it is of the Pins type.

    We pass this value to Raspi Set Mode along with GPIO_OUTPUT which sets the pin to be ready to output a signal on our command. The GPIO_OUTPUT value is one of the Pin Modes that can be set. Each pin has alternative modes but we are only interested in the output mode for now.

  2. open_window("dummy_window", 1, 1);
    while(!any_key_pressed())
    {
    process_events();
    ...
    }

    This is a small trick we can do to recognise keyboard inputs. We open up a dummy window using Open Window and this initialises the backend so that we can detect input. We’ll create a loop that runs while Any Key Pressed returns false. We use Process Events at the start of the loop to actually detect any user input. We can now press a key to end the program and ensure we cleanup correctly.

  3. raspi_write(led_pin, GPIO_HIGH);
    delay(500);
    raspi_write(led_pin, GPIO_LOW);
    delay(500);

    Now that we’ve setup our pins and defined our end condition, we can now manipulate the LED. Inside the while loop we first use Raspi Write to change the output of our pin to GPIO_HIGH, one of the Pin Values. Doing this provides electricity to the LED and turn it on. We then wait for half a second using Delay. Then we turn our LED off using Raspi Write, but now we use GPIO_LOW, and wait for another half-second.

  4. close_all_windows();
    raspi_cleanup();
    return 0;

    Finally, as we exit the program we must ensure that we cleanup our program properly. We must first close the dummy window we opened use Close All Windows. We then call Raspi Cleanup to ensure our pins are turned off and cleaned.

Build and run the code

We can build this program with the following command:

Terminal window
g++ led_blink.cpp -l SplashKit -o led_blink

Then we run the program with the following command:

Terminal window
./led_blink

This code blinks the LED on and off as you can see below. This continues until the 10 seconds has elapsed, and after we exit the while loop, Raspi Cleanup is called and the GPIO pins are reset.

A GIF of the LED blinking on and off.