013 - ESP32 MicroPython: UART Serial in MicroPython

Introduction

In this tutorial, we will learn to use the UART peripheral of ESP32 in MicroPython. UART is useful for interfacing devices such as

DF Player Mini mp3 player module, SIM800L gsm module, HC-06 bluetooth module, and etc which basically needs serial communication as mode of control.

ESP32 has 3 hardware uart serial ports available which are Port 0, port 1, and port 2.

Port 0 is connected to GPIO 1 and 3. It is generally use to communicate with the ESP32 for flashing and during the reset event. In MicroPython, it is reserve for the default REPL. In this development board, it is accessible through the general purpose breadboard pins. This is usable when you disabled the default serial REPL and use the WEB REPL instead.

Port 1 is connected to GPIO 9 and 10. It is unused but it is usually used for spi flash memory access. In this version of ESP32 development board, it is not routed to general purpose breadboard pins.

Port 2 is connected to GPIO 16 and 17. It is unused and it is routed to the general purpose breadboard pins.

The UARTs Tx and Rx pin can be reassigned to other GPIO pins by setting the rx and tx parameters in UART object creation or in object init initialization.

Circuit Diagram

Hardware Instruction

  1. Connect the VCC pin of USB-Serial converter to ESP32 3.3V pin.
  2. Connect the GND pin of USB-Serial converter to ESP32 GND pin.
  3. Connect the Tx pin of USB-Serial converter to ESP32 GPIO 16 (Rx2) pin.
  4. Connect the Rx pin of USB-Serial converter to ESP32 GPIO 17 (Tx2) pin.

Video Demonstration

insert video line here

Call To Action

I hope you learned something from this. If you have any question regarding this tutorial, please do not hesitate to write your question and suggestion in the comment box provided.

You may also like to Share this to your friends, so that it can reach more people who might benefit from this.

Please also consider supporting my Youtube channel by Subscribing. Click this to Subscribe to TechToTinker Youtube channel.

Thank you and have a good days ahead.

Happy tinkering.

Source Code

 1# Example # 1: Simple UART for controlling
 2# the state of the onboard LED
 3# Author: George Bantique, TechToTinker
 4# Date: October 7, 2020
 5
 6# Load the machine module in order to access the hardware
 7import machine
 8
 9# Create the led object in GPIO 2
10led = machine.Pin(2, machine.Pin.OUT)
11# Create the uart object in port 2
12# Rx=GPIO 16, Tx=GPIO 17
13uart = machine.UART(2, 115200)
14
15# Create a global variable to hold the receive data in serial
16strMsg = ''
17
18# This is the main loop
19while True:
20	# if there is character in receive serial buffer
21    if uart.any() > 0:
22    	# Read all the character to strMsg variable
23        strMsg = uart.read()
24        # Debug print to REPL
25        print(strMsg)
26        
27        # If there is 'on' string in strMsg,
28        # Turn on the LED
29        if 'on' in strMsg:
30            led.on()
31            uart.write('Turning on LED')
32            print('Turning on LED')
33        # If there is 'off' string in strMsg,
34        # Turn off the LED
35        elif 'off' in strMsg:
36            led.off()
37            uart.write('Turning off LED')
38            print('Turning off LED')
39        # Else, invalid command
40        else:
41            uart.write('Invalid command')
42            print('Invalid command')

Solution:

1
2	# This is my solution:
3        # decode() function the byte code to string
4        # strip() function removes characters found in parameters
5	else:
6        <span>    </span>uart.write(strMsg.decode().strip('rn'))
7            print(strMsg.decode().strip('rn'))


Posts in this series



No comments yet!

GitHub-flavored Markdown & a sane subset of HTML is supported.