There's one very important thing we have left to get GPIO working. We will need to add the correct imports to a few files. To get it working as a minimum, you will need to check the following files:

  • Adafruit_Blinka/src/board.py
  • Adafruit_Blinka/src/digitalio.py
  • Adafruit_Blinka/src/microcontroller/__init__.py
  • Adafruit_Blinka/src/microcontroller/pin.py

For board.py, if there is another existing almost identical board, you can modify the large if statement to just use that Board file without creating a duplicate file.

For the remaining files, if you are using an existing microcontroller file, you will most likely not need to make any changes, but it's always good to check.

Be sure you've added your board definition file to board.py which is where Blinka loads it from.

Testing

Now that we've gone ahead and added the GPIO to the Chip and Board files, the next step is to go and test out GPIO.

Prior to running tests, you may need to copy the changes you made from contents of the src folder to the Python packages folder of your board. An easy way to do this is to perform a pip install by running the following command from the Blinka folder (the same one with setup.py):

cd ~/Adafruit_Blinka
sudo pip3 install .

First we'll start with checking the output. We can test that with a simple test script in Python to blink an LED. Let's go ahead and wire up a simple circuit with a button and LED. Go ahead and choose one GPIO for output and one for input. For this diagram, we'll use D17 and D18. We're using a pull-up resistor because libgpiod doesn't support built-in pull-ups, but if you are using a different library that does support that, feel free to remove them.

Parts Used

Single large LED lit up blue
Need some big indicators? We are big fans of these huge diffused blue LEDs. They are really bright so they can be seen in daytime, and from any angle. They go easily into a breadboard...
$9.95
In Stock
Angled shot of 25 Through-Hole Resistors - 220 ohm 5% 1/4W.
ΩMG! You're not going to be able to resist these handy resistor packs! Well, axially, they do all of the resisting for you!This is a 25 Pack of...
$0.75
In Stock
Angled shot of 10 12mm square tactile switch buttons.
Medium-sized clicky momentary switches are standard input "buttons" on electronic projects. These work best in a PCB but
$2.50
In Stock
Angled shot of 25 Through-Hole Resistors - 1.0K ohm 5% 1/4W.
ΩMG! You're not going to be able to resist these handy resistor packs! Well, axially, they do all of the resisting for you!This is a 25 Pack of...
$0.75
In Stock
Angled shot of Premium Female/Male 'Extension' Jumper Wires - 40 x 6 (150mm)
Handy for making wire harnesses or jumpering between headers on PCB's. These premium jumper wires are 6" (150mm) long and come in a 'strip' of 40 (4 pieces of each of...
$3.95
In Stock
Angled shot of Premium Male/Male Jumper Wires - 40 x 6 (150mm)
Handy for making wire harnesses or jumpering between headers on PCB's. These premium jumper wires are 6" (150mm) long and come in a 'strip' of 40 (4 pieces of each of...
$3.95
In Stock
Angled shot of Half-Size Breadboard with Mounting Holes.
This cute 3.2″ × 2.1″ (82 × 53mm) solderless half-size breadboard has four bus lines and 30 rows of pins, our favorite size of solderless breadboard for...
$5.00
In Stock

Wiring

  • Connect the SBC Ground pin to the blue ground rail on the breadboard.
  • Connect one side of the tactile switch to SBC D17 (Pin #11)
  • Connect a ~1K-10K pull up resistor from D17 to 3.3V
  • Connect the other side of the tactile switch to the ground rail
  • Connect the longer/positive pin of the LED to SBC D18 (Pin #12)
  • Connect the shorter/negative pin of the LED to a 220 ohm-2.2K resistor, the other side of the resistor goes to the ground rail

Testing Output

D18 will be used to test the output of GPIO by flashing the LED with this simple script. Go ahead and copy the code and save it on your Single Board Computer as blink.py. Be sure to change the GPIO to whichever one you are testing.

import time
import board
import digitalio
 
led = digitalio.DigitalInOut(board.D18)
led.direction = digitalio.Direction.OUTPUT
 
while True:
    led.value = True
    time.sleep(0.5)
    led.value = False
    time.sleep(0.5)

No go ahead and run it by typing:

sudo python3 blink.py

If everything is working properly, you should see the LED blinking on and off.

Testing Input

We'll go ahead and use the same circuit that we used to test output. This time we'll use a slightly different script. Go ahead and save this script as button.py. Be sure to change the GPIO to whichever one you are testing.

import time
import board
import digitalio

led = digitalio.DigitalInOut(board.D18)
led.direction = digitalio.Direction.OUTPUT

button = digitalio.DigitalInOut(board.D17)
button.direction = digitalio.Direction.INPUT

while True:
    led.value = not button.value # light when button is pressed!

No go ahead and run it by typing:

sudo python3 button.py

If everything is working properly, you should see the LED turn on if the button is pressed.

Troubleshooting Tips

If neither of the above scripts work, here are some troubleshooting tips:

  • The problem is more likely to be in the Chip file.
  • Try a different GPIO and see if that works
  • Check the Data Sheet
  • Sometimes Pins are Multiplexed meaning they are used in different ways and something may need to be disabled for them to work.
  • If there's a Python Error check to see if you can trace it back
  • Sometimes they just don't work
  • Try asking in Discord if nothing else works

This guide was first published on Apr 01, 2020. It was last updated on Mar 13, 2020.

This page (Getting GPIO working) was last updated on Mar 13, 2020.

Text editor powered by tinymce.