Cannot Get UART Rx to Work

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
qwacko
Posts: 1
Joined: Mon Apr 12, 2021 11:43 am

Cannot Get UART Rx to Work

Post by qwacko » Mon Apr 12, 2021 12:00 pm

Hi,

I have tried read everything that I can, and see if there is something that I am missing. But I have an ESP32 board, and I cannot get the Receive of data to work correctly. I can get data transmission to work correctly, but not receiption. I have tried the two uarts, different pins, different code etc.. but still nothing. Where I am right now is I have loopback wire inserted, but it still doesn't seem to work.

wiring - Single wire from GPIO32 to GPIO33.

boot.py - Has nothing, all commented out

main.py

Code: Select all

###########################################################################
# Setup code goes below, this is called once at the start of the program: #
###########################################################################
import time
from machine import Pin
from machine import UART


#onboard_led = Pin(2, Pin.OUT)
uart = UART(2, 9600, bits=8, parity=None, stop=1, tx = 12, rx = 13, timeout = 1000 )  
uart.write("testing")
print(uart.read(2))
On startup I get "None" in the REPL. Then once loaded I try do the

Code: Select all

uart.write("Test')
and

Code: Select all

print(uart.read(2))
, but I just get

Code: Select all

None
as my response.

Any thoughts / suggestions would be greatly appreciated.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Cannot Get UART Rx to Work

Post by Roberthh » Mon Apr 12, 2021 12:31 pm

uart.read() is non blocking, and uart.write() is buffered. That means that the call to uart.write() returns before the data has been sent. At that very moment no data has been reecived yet, and uart.read() returns with None. Sending 2 bytes at 9600 takes 2 ms.
So wehat can you do:
a) wait 2 ms before reading, or
b) configure the UART with a timeout, which will be the minimal time read() will wait for data, or
c) call uart.any() for a while to tell whether data arrived. This is a common approach for receiving data.

Post Reply