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
- Connect the VCC pin of USB-Serial converter to ESP32 3.3V pin.
- Connect the GND pin of USB-Serial converter to ESP32 GND pin.
- Connect the Tx pin of USB-Serial converter to ESP32 GPIO 16 (Rx2) pin.
- 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
- 026 - ESP32 MicroPython: MFRC522 RFID Module
- 025 - ESP32 MicroPython: ESP32 Bluetooth Low Energy
- 024 - ESP32 MicroPython: How to Use SD Card in MicroPython
- 023 - ESP32 MicroPython: Binary Clock
- 022 - ESP32 MicroPython: MQTT Part 2: Subscribe
- 021 - ESP32 MicroPython: MQTT Part 1: Publish
- 020 - ESP32 MicroPython: RESTful APIs | Demo READ and WRITE
- 019 - ESP32 MicroPython: OpenWeather | RESTful APIs
- 018 - ESP32 MicroPython: Thingspeak | RESTful APIs
- 017 - ESP32 MicroPython: DHT Values Auto Updates using AJAX
- 016 - ESP32 MicroPython: Web Server | ESP32 Access Point
- 015 - ESP32 MicroPython: Web Server | ESP32 Station Mode in MicroPython
- 014 - ESP32 MicroPython: SIM800L GSM Module in MicroPython
- 012 - ESP32 MicroPython: HC-SR04 Ultrasonic Sensor in MicroPython
- 011 - ESP32 MicroPython: DHT11, DHT22 in MicroPython
- 010 - ESP32 MicroPython: 0.96 OLED in MicroPython
- 009 - ESP32 MicroPython: Non-blocking Delays and Multithreading | Multitasking
- 008 - ESP32 MicroPython: Hardware Timer Interrupts
- 007 - ESP32 MicroPython: How to make some sound with MicroPython
- 006 - ESP32 MicroPython: How to control servo motor with MicroPython
- 005 - ESP32 MicroPython: Pulse Width Modulation
- 004 - ESP32 MicroPython: External Interrupts
- 003 - ESP32 MicroPython: General Purpose Input Output | GPIO Pins
- 001 - ESP32 MicroPython: What is MicroPython
- 000 - ESP32 MicroPython: How to Get Started with MicroPython
No comments yet!