14. December 2023

How to connect USB and PS/2 Keyboards to ESP32 with Rust no_std and std

Have you ever wondered how to connect a PC keyboard to your ESP32 for innovative projects? Whether it’s an old PS/2 keyboard or a modern USB one, this article will show you how to integrate these input devices with your ESP32, using the versatile and powerful Rust programming language. The first part of the article describes Rust no_std solution. Rust std for USB solution is at the end of the article.

Preparing the ESP32-C3

First, let’s tackle the wiring for the ESP32-C3. Detailed in bjoernQ’s GitHub repository, “ps2keyboard-esp32c3”, this setup requires three resistors and is perfectly illustrated in the “Circuit” section. For a reliable and efficient connection, consider the wire-wrap technique recommended by Andreas Spiess in his enlightening video, “#243 Better than Dupont Wires: Wire Wrapping for our Projects”.

For the PS/2 connector, refer to the detailed pinout available in the Wikipedia article, “PS/2 port”, particularly the pinout diagram for the female connector (PS/2 port pinout diagram). Please, keep in mind that the pinout is for the female connector from the front side. For the male connector or the back side of the female connector you need to flip the image.

For USB keyboart you’ll need a USB keyboards support a COMBO PS/2 support (still available in many USB keyboards, allowing them to function as PS/2 devices). You can connect these USB keyboards directly by following the schematics from the Instructables project, “USB to PS/2 Convertor” with “ps2keyboard-esp32c3 schematics”,. Specifically, use this wiring diagram (USB to PS/2 wiring diagram) to integrate into the ESP32-C3 setup. No code changes are needed for this part.

Flashing the ESP32-C3

After setting up the hardware, it’s time to program the ESP32-C3. The software part is handled using the pc-keyboard crate, developed by thejpster’s. Flash your ESP32-C3 with the cargo espflash --release command, and watch as keyboard inputs appear on your console.

Interesting Project Ideas

Now that you have your keyboard connected to the ESP32. Here are some project ideas to get you started:

  • Smart Home Controller: Use the keyboard to control lights, temperature, or other IoT devices in your home.
  • Custom Game Controller: Create a gaming experience by mapping keyboard keys to game controls.
  • Educational Tool: Teach programming or robotics, using the keyboard as an input device for experiments and projects.
  • Artistic Installations: Incorporate the keyboard into interactive art projects or music synthesizers.

Rust std + ESP-IDF with USB support

The text above describes Rust no_std solution. If you’re using Rust std on top of ESP-IDF, you can use direct USB support on ESP32-S3 with USB HID for mouse and keyboard Board Support Package (BSP).

30. August 2022

Unable to flash ESP32 (S2, S3, C3) with these USB-C cables

USB-C is becoming more and more popular. A straightforward assumption is that you can replace the old cable with a USB-A connector with USB-C, and everything should be the same. This assumption is wrong.

Some cables do not work well with ESP32, S2, S3, and C3. Here you can find information about cables which does not work and it’s not possible to flash dev boards with the chip with espflash, idf.py or esptool.py.

A typical error message looks like this:

Serial port: /dev/tty.usbserial-110
Connecting...

Unable to connect, retrying with extra delay...
Unable to connect, retrying with default delay...
Unable to connect, retrying with extra delay...
Error: espflash::connection_failed

  × Error while connecting to device
  ╰─▶ Failed to connect to the device
  help: Ensure that the device is connected and the reset and boot pins are not being held down

USB-C to micro-USB connected to boards ESP32-S2-USB-OTG and ESP32-S3-USB-OTG, length 60 cm, marks on cable “AWM 80 C 30V VW-1 USB 2.0 CABLE”:

  • directly connected to mac M1 2020 – does not work
  • workaround #1: press and hold boot button on the board, start flash command, release boot button after flashing process starts
  • workaround #2: connect mac M1 to monitor/hub with USB-C and connect the cable there
  • workaround #3: use computer with Windows or Linux, the cable seems to work correctly with these OSes

24. June 2022

Weather display for LilyGO TTGO T5-4.7″ E-Paper ESP32 deployed using Arduino IDE 2.0

LilyGO TTGO T5-4.7″ E-Paper ESP32 is nice display which integrates ESP32, USB-C, Li-Po and 18650 accumulator support in one board. The display driver is GDEH0213B72.

One interesting use-case for the board is the Weather display.

There are several steps to get the Weather display working. Let’s walk through them.

Drivers

Linux users may skip this section since the modern kernel supports CH34x drivers.

macOS users may encounter the following error when flashing:

Failed to write to target RAM (result was ...)

It’s necessary to install the driver from https://github.com/WCHSoftGroup/ch34xser_macos.

Windows 8, 10 users may need to install https://www.wch.cn/download/CH343SER_EXE.html. Windows 11 should install the driver automatically.

Arduino IDE setup

Download Arduino IDE 2.0.

Add ESP32 boards support. Click File, click Preferences, select Settings tab. Enter the following URL to Additional boards manager URLs:

https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json

Click Ok. Click Tools, select Boards: …, click Boards Manager… . It will open the left pane with a list of boards. Type esp32 to Filter your search field. Find esp32 by Espressif Systems, click Install.

Preparing code

Open a terminal and clone LilyGo-EPD47 library to Arduino/libraries:

Linux:

cd ~/Arduino/libraries
git clone git@github.com:Xinyuan-LilyGO/LilyGo-EPD47.git --depth 1

macOS:

cd ~/Documents/Arduino/libraries
git clone git@github.com:Xinyuan-LilyGO/LilyGo-EPD47.git --depth 1

Make a clone LilyGo-EPD-4-7-OWM-Weather-Display to the directory with Arduino projects. The folder name with the project should match the name of the source code file OWM_EPD47_epaper_v2.5 to avoid the unnecessary step of moving the file.

Linux:

cd ~/Arduino
git clone git@github.com:Xinyuan-LilyGO/LilyGo-EPD-4-7-OWM-Weather-Display.git OWM_EPD47_epaper_v2.5

macOS:

cd ~/Documents/Arduino/
git clone git@github.com:Xinyuan-LilyGO/LilyGo-EPD-4-7-OWM-Weather-Display.git OWM_EPD47_epaper_v2.5

Open Arduino IDE 2.0, click File, select Sketchbook, click OWM_EPD47_epaper_v2.5.

The project requires ArduinoJson to build. Click Tools, click Manage libraries… . The pane with Library Manager should open, type ArduinoJson to Filter your search field. Find ArduinoJson by Benoit Blanchon, click Install.

Try to build the project.

It might fail with the following error:

.../Arduino/libraries/LilyGo-EPD47/src/rmt_pulse.c:9:24: fatal error: hal/rmt_ll.h: No such file or directory
compilation terminated.
Multiple libraries were found for "WiFiClient.h"
  Used: .../.arduino15/packages/esp32/hardware/esp32/1.0.4/libraries/WiFi
  Not used: .../arduino-1.8.13/libraries/WiFi
exit status 1

Open the file ~/Arduino/libraries/LilyGo-EPD47/src/rmt_pulse.c and comment out line 9:

/* #include <hal/rmt_ll.h> */

The project should be buildable now.

Configuring local parameters

Open file owm_credentials.h and configure ssid, password, apikey, City, Country.

The project is acquiring data from openweathermap.org. Create new free account in order to get apikey.

The project implementations contain support for power saving, so if you’re flashing in the early morning/late night, you might be surprised that nothing is on display. To change power-saving options open file OWM_EPD47_epaper_v2.5.ino and change WakeupHour to a value that suits your need.

Flashing

Connect the module. Select the board from the dropdown in the toolbar. Click Port (/dev/ttyACMx on Linux), filter for ESP32 Wrover module and click Ok.

Click the Upload arrow.

If the flashing is successful, you may enjoy the new Weather display. Congratulations!

3D printed enclosure

There are several versions of files for 3D printing. You can find them in the discussion at GitHub – LilyGo-EPD47. The picture in this article is based on model thing:4782302 printed on Original Prusa MINI+ with PET-G. The model has a few cosmetic limitations:

  • It’s not possible to keep the display standing while USB-C is connected.
  • Buttons are not completely reachable.
  • The display must be attached by a tape or other method to the stand to avoid detaching from the case.

Notes

Double-check whether the battery holder is present when ordering the board with the display from the e-shop. Even when it’s displayed on the picture, it does not mean that the battery holder or battery is part of the delivery.

When connecting by USB-C to USB-C cable, the device should light up at least a red led. If nothing is visible, rotate the USB-C connected to the board by 180 degrees or use USB-A to USB-C cable.

2. April 2022

ESP32 Arduino macOS exec: “python”: executable file not found in $PATH

Apple removed old Python 2 from macOS 12.3. Arduino ESP32 depends on Python interpreter.

Build in Arduino IDE may fail with error:

"python": executable file not found in $PATH

It’s sufficient to change name of binary to python3

Edit file

~/Library/Arduino15/packages/esp32/hardware/esp32/2.0.2/platform.txt

Change the line:

tools.gen_esp32part.cmd=python "{runtime.platform.path}/tools/gen_esp32part.py"

To line with python3:

tools.gen_esp32part.cmd=python3 "{runtime.platform.path}/tools/gen_esp32part.py"

Re-open Arduino IDE and build the project.

1. April 2022

How to develop for ESP32-C3 with Rust on macOS with Lima using Dev Container in VS Code

Lima is a solution for macOS for managing Linux VM on macOS which plays well with Dev Containers. It has several advantages over Docker Desktop for macOS or Podman.

Let’s see how it can be used for the development of software for embedded hardware like ESP32-C3 using source code from Ferrous Systems training:

git clone https://github.com/ferrous-systems/espressif-trainings.git

Install Lima and Docker-CLI:

brew install lima docker

Create Linux VM with Dockerd (following instructions from Kevin O’Brien – Utilizing Docker CLI without Docker Desktop):

curl https://raw.githubusercontent.com/lima-vm/lima/master/examples/docker.yaml -O
limactl start ./docker.yaml
limactl shell docker
sudo systemctl enable ssh.service

There is one important tweak in the Lima configuration. It’s necessary to enable write operation otherwise, the workspace mounted from VS Code is read-only. Open file ~/.lima/docker/lima.yaml and add writable flag to desired folder:

mounts:
- location: "~"
  writable: true

Restart Lima to apply changes.

limactl stop docker
limactl start docker

Create context for Docker-CLI to connect to dockerd running in the VM:

docker context create lima --docker "host=unix://${HOME}/.lima/docker/sock/docker.sock"
docker context use lima

Open VS Code with the installed Remote Container plugin and click Re-Open in Container:

cd espressif-trainings
code .

Open terminal in VS Code and build the example:

cd intro/hardware-check
cp cfg.toml.example cfg.toml
cargo build

Flashing of the resulting file could be done by espflash and mounting the device to Lima or using a tool like Adafruit WebSerial ESPTool. The file for flashing is located in the directory target/release.