[PYTHON] Add a GPIO board to your computer. (1)

Introduction

There are times when you want to create a program in Python and control the external H / W with that program. For example, when connecting a laptop computer to the 24V line of a PLC and directly communicating signals. To achieve this communication, an I / O port that can be controlled by a personal computer is required.

Related article: If your laptop has a GPIO interface ...

Single-board small computers like the Rasbeprry PI and NVIDIA Jetson nano have their own built-in GPIO ports for easy related configuration. However, in the case of a personal computer, there is no I / O port other than USB, so it is necessary to prepare a separate GPIO board, install the driver, and set the library.

In this article, I will show you how to install a 12-port GPIO board on a Windows PC.

Introduction of GPIO board

Adafruit's FT232H Breakout board is used as the GPIO board. In Japan, it can be purchased at Switch Science and Akizuki Denshi's website.

Adafruit GPIO board product introduction page Adafruit.jpg

specification

The following table lists the GPIO board specifications. It uses USB_5V on the PC side as a power source. The digital output level of the GPIO board is 3.3V. We provide 12 digital I / O ports.

An I2C and SPI standard interface is also available for direct communication with digital sensors and actuators.

Parameters Details Contents
Power Pins 5V this is the 5V power from the USB input.
GND this is the common ground for all power and logic.
GPIO Pins D4 to D7 can be used as either digital inputs and outputs.
C0 to C7 can be used as either digital inputs or outputs.
Others I2C Pins SCL, SDA
SPI Pins SCLK, MOSI, MISO, CS0

The following figure shows the pinout of this board. FT232H_pinout.png

Connection with a personal computer

This time, we will control the GPIO board with Python. Use Circuit Python Libraries.

However, since it is difficult for Python to access Circuit Python Libraries directly, BLINKA is required. (Explanation of Adafruit) BLINKA is Adafruit's dedicated library that allows Python to access the CircuitPython Libraries.

A connection is formed between the personal computer and the GPIO board by the USB port. The FT232H chip converts the signal from the personal computer to the GPIO port.

There are multiple parts, and the minimum looks a little complicated, but once installed, you can use it without worrying about the existence of those parts.

Configure.png

Driver and library installation

Next is the installation process. This is the most important part.

For details, refer to the link below and proceed with the installation. The order is very important.

In this post, fill in only the items you want to do. It takes about 10 to 15 minutes for the entire installation.

Adafruit GPIO Board Setup

  1. Confirm the installation of Python 3 on the personal computer: Because Python 2.7 is not supported
  2. FT232H driver update
  3. Installation of pyftdi and pyusb (modified version)
  4. Testing pyusb and pyftdi
  5. Installation of Adafruit Blinka
  6. Setting environment variables (set BLINKA_FT232H = 1)
  7. Check GPIO board recognition on python

GPIO board output test

Try blinking the LED using the C0 port on the GPIO board. Set the C0 port to the OUTPUT port, and repeat ON for 0.5 seconds and OFF for 0.5 seconds. You can check the output status by blinking the LED.

The following outlines the wiring. Connect each element in series.

** C0 port --LED-- 1 kOhm resistor --GND **

20200309_152011.jpg

Program code

The part that sets the environment variable BLINKA_FT232H to 1 must be placed before the import board.

import os
os.environ['BLINKA_FT232H'] = '1' #Setting Environmental Variable

import board
import time
import digitalio

#GPIO Setting : C0 will be output port.
led = digitalio.DigitalInOut(board.C0)
led.direction = digitalio.Direction.OUTPUT

while True:
    led.value = True
    time.sleep(0.5)
    led.value = False
    time.sleep(0.5)

It is a state of operation. It was confirmed that the output was from the OUTPUT port as programmed. ezgif.com-video-to-gif.gif

Summary

I introduced how to add a GPIO board to a personal computer and confirmed its operation.

Future plans

Next time, I will show you how to get an INPUT signal from a GPIO board and display the result on Python.

Reference material

  1. Adafruit Circuit Python Libraries with FT232H
  2. Adafruit GPIO Board Setup
  3. If your laptop has a GPIO interface ...

Recommended Posts

Add a GPIO board to your computer. (1)
Add a dictionary to MeCab
To add a C module to MicroPython ...
Add your own content view to mitmproxy
How to add a package with PyCharm
Add a Python virtual environment to VSCode
Want to add type hints to your Python decorator?
Add your favorite display function to pip search
Add a command to mark similar files together
How to add a Python module search path
To add a module to python put in Julialang
[Wagtail] Add a login page to the Wagtail project
Scraping your Qiita articles to create a word cloud
Wagtail Recommendation (5) Let's add your own block to StreamField
From buying a computer to running a program with python
[Morphological analysis] How to add a new dictionary to Mecab
Generate a bash script to add Datadog monitor settings
I want to run a quantum computer with Python
[Linux] How to put your IP in a variable
Add convolution to MNIST
[Django] A pattern to add related records after creating a record
Add an extension to build a more comfortable Jupyter environment
Add a function to heat transfer + heat input by temperature to heatrapy
A handy function to add a column anywhere in a Pandas DataFrame
The usual way to add a Kernel with Jupyter Notebook