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.
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.







- 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
Page last edited March 08, 2024
Text editor powered by tinymce.