It's easy to use the CH9328 with CircuitPython and the Adafruit_CircuitPython_CH9328 driver. This driver allows you to easily write Python code to send UART messages to the CH9328.
You can use this driver with any CircuitPython microcontroller board or with a computer that has GPIO, UART and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library. For Blinka, this example was tested with a Raspberry Pi. To use the example as-is with built-in UART, you'll need to follow these configuration steps outlined in the Blinka Learn Guide.
Make sure that the CH9328 onboard switches are set to Mode 3 (switch 2 off, switches 3 and 4 on) before powering it up.
CircuitPython Microcontroller Wiring
First wire up the breakout to your board exactly as follows. The CH9328 and the microcontroller will both be connected to your computer via USB but will share a ground connection. Make sure that the CH9328 onboard switches are set to Mode 3 (switch 2 off, switches 3 and 4 on) before powering it up. The following is the breakout wired to a Feather RP2040 using the JST SH connector.
The following is the breakout wired to a Feather RP2040 using a solderless breadboard:
Python Computer Wiring
Since there are dozens of Linux computers/boards you can use, we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported.
Here's the Raspberry Pi wired to the breakout using a JST SH cable:
Here is how you'll wire the breakout to a Raspberry Pi with a breadboard:
Python Installation of the Adafruit CH9328 Library
You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C and built-in UART on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!
Once that's done, from your command line run the following command:
pip3 install adafruit-circuitpython-ch9328
If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!
Make sure to follow the built-in UART setup steps if you are using the TX pin on the Raspberry Pi.
CircuitPython Usage
To use with CircuitPython, you need to first install the Adafruit_CircuitPython_CH9328 library into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.
Your CIRCUITPY/lib folder should contain the following folder:
- adafruit_ch9328/

Python Usage
Once you have the library pip3
installed on your computer, copy or download the following example to your computer, and run the following, replacing code.py with whatever you named the file:
python3 code.py
Example Code
Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!
# SPDX-FileCopyrightText: Copyright (c) 2024 Liz Clark for Adafruit Industries # # SPDX-License-Identifier: MIT """Simple demo to type "Hello World!" and then delete it""" import time import board from adafruit_ch9328.ch9328 import Adafruit_CH9328 from adafruit_ch9328.ch9328_keymap import Keymap # Initialize UART for the CH9328 # check for Raspberry Pi # pylint: disable=simplifiable-condition if "CE0" and "CE1" in dir(board): import serial uart = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=3000) # otherwise use busio else: import busio uart = busio.UART(board.TX, board.RX, baudrate=9600) ch9328 = Adafruit_CH9328(uart) # Wait for 2 seconds time.sleep(2) # Sending "Hello World!" as an ASCII character string ch9328.send_string("Hello World!") # Wait for 2 seconds time.sleep(2) # Send the backspace key 12 times to erase the string keys = [Keymap.BACKSPACE, 0, 0, 0, 0, 0] # Keycode for backspace in US mapping no_keys_pressed = [0, 0, 0, 0, 0, 0] for _ in range(12): ch9328.send_key_press(keys, 0) # Press ch9328.send_key_press(no_keys_pressed, 0) # Release the key
In the code, the CH9328 is initialized over UART. After a two second delay, the text "Hello World!" is typed out from the CH9328. After an additional two second delay, the text is deleted. The delays are there so that you can position your cursor before the HID key presses are sent.
Page last edited January 22, 2025
Text editor powered by tinymce.