013 – ESP32 MicroPython: UART Serial in MicroPython
Circuit Diagram:
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.
Video Description:
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:
# Example # 1: Simple UART for controlling
# the state of the onboard LED
# Author: George Bantique, TechToTinker
# Date: October 7, 2020
# Load the machine module in order to access the hardware
import machine
# Create the led object in GPIO 2
led = machine.Pin(2, machine.Pin.OUT)
# Create the uart object in port 2
# Rx=GPIO 16, Tx=GPIO 17
uart = machine.UART(2, 115200)
# Create a global variable to hold the receive data in serial
strMsg = ''
# This is the main loop
while True:
# if there is character in receive serial buffer
if uart.any() > 0:
# Read all the character to strMsg variable
strMsg = uart.read()
# Debug print to REPL
print(strMsg)
# If there is 'on' string in strMsg,
# Turn on the LED
if 'on' in strMsg:
led.on()
uart.write('Turning on LED')
print('Turning on LED')
# If there is 'off' string in strMsg,
# Turn off the LED
elif 'off' in strMsg:
led.off()
uart.write('Turning off LED')
print('Turning off LED')
# Else, invalid command
else:
uart.write('Invalid command')
print('Invalid command')
Solution:
# This is my solution:
# decode() function the byte code to string
# strip() function removes characters found in parameters
else:
uart.write(strMsg.decode().strip('rn'))
print(strMsg.decode().strip('rn'))
Thank you,
Exelent work!!!
Wow that's sweet @Conde58. Thank you.
bro you'r the best in micropython…
How do I interface DF Player Mini mp3 player module? Is there a library available? Please advise. Thanks.
@SW, I have not tested this but you may try this: https://github.com/ShrimpingIt/micropython-dfplayer
Hope this works with you and please let me know. Cheers.
Bro, I am glad that you liked this and thank you 🙂
Hi George, I have tried and tested the library on a ESP32 WROOM. However, it is not working. Any advice is greatly appreciated. Thank you very much.
So far, what have you done already? Can you elaborate further
I downloaded the dfplayer programs from github and tested it without success.
Hello George !
Thanks again for your work, saves me a lot of time and effort !
FYI I just tried with an HC-05 and the setting here does not work (at least with mine)
To make it work I must use a baudrate of 9600, and the module must be powered with 5V (specs says that it should be 3.6V min).
Hope It will save somebody some time and effort too 😉