Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

CH340/CH341 Boards on Linux

(Note: I first posted this on my blog and modified and formatted it for posting here on the forums. I hope it helps!)

It's been a few months since I took the below notes, but I wanted to share my experiences toying around with the Arduino boards I received from HackMakeMod
using my Linux computer. Hopefully they are of help to you if you're trying to get these knockoff CH340/CH341 boards working on Linux or other non-Windows OS.

Setup and Considerations​


Quote from the Arch Linux Wiki, which will likely be more helpful to you than my notes:

Non-genuine Arduino boards cheap out on the interfacing chip. They are typically equipped with a Chinese CH340 or a counterfeit of the aforementioned models. The CH340 exposes itself as a proprietary UART over USB device. Here the ch341 Linux Kernel module is used, making such Arduinos show up as /dev/ttyUSBx. This naming pattern may be customized by altering udevrules.

USB cables​

I had issues with only certain USB cords working with the clones -- I'm not sure if it's an issue with the boards or with my cords or with my Linux installation, but I felt it was worth mentioning.
  • The USB-C to USB-C cable I tried did not register as a tty
  • Certain USB-C to USB-A cables would not work
Ensure you're working with a high quality cable which can support the necessary transmission rates.

Drivers​

Note: The mainline Linux kernel driver may have been updated since I performed the below steps, so you may not need to do this! Give the mainline driver a try first before you go through the steps below.

Once I had a working USB connection, I found that the clones did not work out of the box for me. After a bit of research, I discovered that a CH34\* driver is indeed built into the Linux kernel (*including* the Zen kernel, which I am using) -- however, the driver appears to have been updated by the manufacturer but does not yet appear in the kernel with these updates.


As such, it is necessary to download the C source code for the driver and compile it. Unfortunately, this did not work out of the box either, and it was necessary to very slightly modify the source code. (Unsure if this is related to the C implementation installed on my machine or some other factor.)

Once this is compiled and installed into the system...



Compiling the CH34\* driver

I did not fully document the steps I took to compile the driver, but there is a README (in English) in the zip file you can download from the WCH website.

I took this code and modified it to allow it to compile on my system:

Diff:
diff CH341SER_LINUX/driver/ch341.c CH341SER_LINUX_unmodified/CH341SER_LINUX/driver/ch341.c
646c646
< static long int ch341_tty_write(struct tty_struct *tty, const unsigned char *buf, long unsigned int count)
---
> static int ch341_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)

I then compiled and installed the module following the README.




...the device should now register under /dev/tty* when plugged into USB.

IDE vs CLI​


I opted to use the arduino-cli tool to interact with the code and boards. This may be a bit trickier than just using the IDE, but I thought I would share my notes in case it's useful.

1. Search for the board
Bash:
arduino-cli board search 'D1 mini' --additional-urls 'http://arduino.esp8266.com/stable/package_esp8266com_index.json'

2. Install the core
Bash:
arduino-cli core install esp8266:esp8266 --additional-urls 'http://arduino.esp8266.com/stable/package_esp8266com_index.json'

3. Install any library dependencies (this one is for LCD display)
Bash:
arduino-cli lib install LiquidCrystal

4. Compile for the given Fully Qualified Board Name (fqbn):
Bash:
arduino-cli compile --fqbn esp8266:esp8266:d1_mini_clone

WiFi / Security Considerations​


This nifty library for ESP32 and ESP8266 boards can be employed to avoid storing hard-coded WiFi credentials in your sketches (as was presented in the HackMakeMode example code). If it can't connect to WiFi, it will instead act as an access point to which you can connect and enter your wifi credentials to be stored in EEPROM.

Bash:
arduino-cli lib install "ESP-WiFi-Config"


Resources​


- [HackMakeMod](https://hackmakemod.com/)
- [CH340/CH341 Linux Driver](https://www.wch.cn/downloads/CH341SER_LINUX_ZIP.html)
- [Arduino CLI](https://arduino.github.io/arduino-cli/latest/)
- [Arch Linux Wiki - Arduino](https://wiki.archlinux.org/title/Arduino)
- [Lengthy discussion on the Arduino forum](https://forum.arduino.cc/t/nano-v3-ch340-original-or-clone-does-it-matter/1270346)
- [Linux Kernel Module - CH341](https://github.com/torvalds/linux/blob/master/drivers/usb/serial/ch341.c)
- [ESP-WiFi-Config](https://github.com/tabahi/ESP-Wifi-Config)
 
Back
Top