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.
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
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.
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.
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.
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 **
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.
I introduced how to add a GPIO board to a personal computer and confirmed its operation.
Next time, I will show you how to get an INPUT signal from a GPIO board and display the result on Python.
Recommended Posts