The Joy Robot
Home | Content | Services | About
Search Eng Light

Jonathan Pablo Toledo Microcontrollers, UART 2021-02-01

Prepare a computer to work with UART interfaces of Arduino microcontrollers, ESPxx, etc.

Currently, microcontrollers such as Arduino and ESPxx are easy to find in a wide variety of technological products. Their low prices make them accessible tools and, added to the large amount of documentation available, make them very versatile for developing automation projects.

To work with these microcontrollers we need to communicate through their UART interface ( Universal Asynchronous Receiver-Transmitter).

This interface uses a serial communication method that has been around for many years. It is similar to the one used in computers using DB9 connectors in the past, although there is an important difference: the electrical levels are not the same. Microcontrollers usually use serial communication with TTL logic levels of 5 V or 3.3 V, depending on the electronics of the device.

Some devices, such as Arduino and certain ESPxx boards, incorporate a USB-Serial adapter in the same circuit. However, in other cases we will only have the UART interface available and we will need an external adapter that allows us to establish communication with our computer.

It is important to note that the electrical levels of the adapter must be compatible with those of the device. A TTL UART interface should not be connected directly to a traditional RS-232 serial port, as they use different electrical levels.



Examples of TTL UART interfaces

  1. UART interface of an Android TV-Box
  2. UART interface of a Sonoff RF-Bridge device
  3. Examples of USB-Serial Adapters for UART Communication

1. Install drivers for USB-Serial adapters

USB-Serial adapters can use different controller chips. For this reason, the driver needed will depend on the model of adapter we have.

Among the most common are Silicon Labs' CP210x chips, FTDI, and CH340/CH341.

Apple OS X

For Apple computers running OS X (in my case, High Sierra), it may be necessary to install a driver for the system to detect certain USB-Serial adapters.

One of the most common drivers is the one provided by Silicon Labs for its CP210x chips. It can be downloaded from the link below:



https://www.silabs.com/developers/usb-to-uart-bridge-vcp-drivers

The file that we must download for OS X is:



CP210x VCP Controller for Mac OSX

We must follow the installation steps and restart the computer. Once this is done, the system should detect the compatible adapters.

Note: This driver applies to devices that use CP210x chips. If our adapter uses another USB-Serial driver, such as FTDI or CH340, we will need to install the corresponding driver.



GNU/Linux Debian or Ubuntu

In the case of Debian and Ubuntu, the drivers for many USB-Serial adapters are already included in the Linux kernel. In my tests, no additional installation was required for the system to detect the devices.



2. Identify the USB-Serial adapters connected to the computer

Once the corresponding driver is installed, we can identify the serial port that our adapter uses.

Apple OS X

Depending on the operating system, the commands to identify serial devices may vary. In my case, I use OS X, so I will run the following command to display the devices connected to the computer:



Terminal
$ ls -l /dev/cu.*

If we have more than one device that uses serial communication connected to the computer, I recommend running the command without the USB-Serial adapter connected and running it again after connecting it. This way we can identify which device is the corresponding one.

Example:



GNU/Linux Debian or Ubuntu

In Linux we can list USB-Serial devices using the following command:



Terminal
$ ll /dev/ttyUSB*

Example in the terminal:



On some devices, the port may appear with a different name, for example /dev/ttyACM0. This depends on the USB-Serial controller used.

Windows 10

In Windows we can identify USB-Serial adapters using the Device Manager.

First we connect the USB-Serial adapter to the computer and open the Device Manager. To do this, we can right-click on the Windows start button and select the corresponding option.

Within Device Manager we look for the category:

Ports (COM and LPT)

If the driver is installed correctly, you should see a device with a name similar to:



  • Silicon Labs CP210x USB to UART Bridge (COM3).
  • USB serial port (COM4).
  • USB CH340 (COM5) SERIES.

The COM number may vary depending on the equipment and connected devices.

If the adapter appears in the Other Devices category or has a warning symbol, the adapter may not be installed in the Other Devices category.

Some USB-Serial chip manufacturers have their own drivers:

Once the COM port has been identified, we can use it from the serial communication tools.



3. Install tools to work with the microcontroller's UART interface

In addition to controllers, we'll need a few tools to communicate with microcontrollers.

In this guide we'll use Picocom to establish serial communication from the console, Esptool to interact with ESP devices, and Adafruit Ampy to work with microcontrollers that use CircuitPython or MicroPython.

Apple OS X

3.1. Install Homebrew

Homebrew is a package manager for macOS. To install it, we need to run the following command in the terminal:



Terminal
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

3.2. Installing Picocom

Picocom is a tool that allows you to establish serial communication from the console, similar to PuTTY, CoolTerm, etc.

We can install it on OS X using Homebrew:



Terminal
$ brew install picocom

3.3. Install Python 3, Pip, and PycURL

Next, we need to install Python 3 and the other dependencies. To do this, we will use the following commands:



Terminal
$ brew install python3

In case the above command doesn't work, we can use:



Terminal
$ brew install --build-from-source python@3.9

Install Pip:



Terminal
$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
$ python get-pip.py

Install PycURL:



Terminal
$ brew reinstall openssl
$ pip uninstall pycurl
$ pip install --install-option="--with-openssl" --install-option="--openssl-dir=/usr/local/opt/openssl" pycurl

3.4. Installing esptool.py

Esptool is one of the most useful tools for connecting to ESP microcontrollers using a serial adapter. It allows you to query device information and perform operations related to its flash memory.

To install it, we must run the following command in the terminal:



Terminal
$ pip install esptool

3.5. Install adafruit-ampy

Ampy is a tool that facilitates communication with microcontrollers that use CircuitPython or MicroPython.

To install it, we run:



Terminal
$ pip install adafruit-ampy



GNU/Linux Debian or Ubuntu

3.1. Installing Picocom



Terminal
$ sudo apt install picocom

3.2. Install Python 3, Pip, and PycURL



Terminal
$ sudo apt install python3

Install Pip:



Terminal
$ sudo apt install python3-pip

Install PycURL:



Terminal
$ sudo apt install python3-pycurl

3.3. Install esptool.py



Terminal
$ sudo apt install esptool

3.4. Installing adafruit-ampy



Terminal
$ sudo pip install adafruit-ampy

Permissions to access the serial port

In some Debian or Ubuntu installations, the user may not have permissions to directly access serial devices.

A common solution is to add our user to the group dialout:



Terminal
$ sudo usermod -a -G dialout $USER

After running the command, we need to log out and log back in for the changes to take effect.

This way we can access the serial ports without having to run the tools with administrator privileges.




Windows 10

3.1. Install Python 3

To use Esptool and Adafruit Ampy we need to have Python installed on our computer.

We can download Python 3 from its official site:



https://www.python.org/downloads/

During installation it is recommended to select the Add Python to PATH option, so that you can run Python and Pip directly from the Windows console.

Once installed, we can verify that Python is available by running:



Terminal
$ python --version

We can also check Pip by:



Terminal
$ python -m pip --version

3.2. Installing Esptool

Once Python is installed, we can install Esptool using Pip:



Terminal
$ python -m pip install esptool

3.3. Installing adafruit-ampy

To install Ampy we run:



Terminal
$ python -m pip install adafruit-ampy

3.4. Install PycURL

If we need to use scripts that depend on PycURL, we can try installing it using Pip:



Terminal
$ python -m pip install pycurl

The availability of binary packages could vary depending on the version of Python and the Windows environment used.

3.5. Installing a Tool for Serial Communication

In Windows we can use PuTTY to establish serial communication from a graphical interface.

The tool allows you to select the type of serial connection, indicate the corresponding COM port and configure the communication speed.

In this way we can communicate with the microcontroller without the need to use a serial console based on Linux or macOS.



4. Perform a Communication Test with a Microcontroller

Once the necessary drivers and tools are installed, we can perform a test to verify that there is communication between our computer and the microcontroller.

Apple OS X

We run the following command, replacing the path with the serial port we identified earlier:



Terminal
$ esptool.py --port <ruta del puerto serial> chip_id

Esptool will attempt to establish communication with the microcontroller through the indicated serial port.

On certain ESP board models, it is necessary to place the device in programming mode so that it can respond to the tool. Depending on the board, this may require holding down the corresponding button while restarting or energizing the device.



This command will provide us with important information about the microcontroller, which we can later use for the purposes we consider necessary.

GNU/Linux Debian or Ubuntu

Since we use the same tool, the command is identical. We only need to consider the name of the corresponding serial port:



Terminal
$ esptool.py --port <ruta del puerto serial> chip_id

For example, if our device appears as /dev/ttyUSB0, the command would be:



Terminal
$ esptool.py --port /dev/ttyUSB0 chip_id

As in macOS, depending on the microcontroller model, it may be necessary to place it in programming mode so that Esptool can establish communication.

Windows 10

On Windows we will use the COM port that we identified earlier from the Device Manager.

For example, if our adapter is listed as COM3, we run:



Terminal
$ esptool.py --port COM3 chip_id

If Esptool is installed correctly, it will attempt to establish communication with the microcontroller and display any information it can obtain from the device.

As with the other operating systems, some models of ESP boards require us to place them in programming mode before executing the command.

With this, we will have our equipment ready to communicate with microcontrollers through UART interfaces and use tools that allow us to consult information, program devices and work with their different firmware systems.




The Joy Robot