[SOLVED] UART not receiving what i'm sending

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
balance
Posts: 8
Joined: Mon Sep 13, 2021 4:07 pm
Location: NRW / Germany

[SOLVED] UART not receiving what i'm sending

Post by balance » Mon Sep 13, 2021 4:54 pm

Hi together,

short introduction of myself: (skip if not interested)
as this is my first Post in this Forum i would like to introduce myself. My Name is Daniel i am a mechanical engineer and i am interested in electronics and µControllers since many years. Normally i am working with Arduino's. But i do like Python so much, that i bought a couple of Pi Pico's. And here we are now. :-)

Back to Topic
I am working on a simple Project, where my laptop sends one byte via UART 1 on Pin 6(tx) and 7(rx) and the pico reads it and prints it. Thats it. But i am struggling with this obvious simple task.

Here is my Code "main.py" (copied to the board via Thonny):

Code: Select all

from machine import UART, Pin, Timer
import time

uart1 = UART(1,baudrate=9600, bits=8, parity=None, stop=1)
led = Pin(25, Pin.OUT)
timer = Timer()
timer_read = Timer()

def blink(i):
    led.toggle()

def uart_read(x):

    while uart1.any() != 0:
        reply = uart1.read()
        if reply != None:
            uart1.write(reply)

uart1.read() #to clear the uart if any
timer_read.init(freq=10, mode=Timer.PERIODIC, callback=uart_read)
timer.init(freq=time_freq, mode=Timer.PERIODIC, callback=blink)

while True:
    pass

If i am sending a HEX "01" i expect to get a HEX "01" back but it is a HEX "40" and "00". A HEX "EE" returns a HEX "DF" and "00". And so on... In my opinion it can't be caused because of different encodings, because i am watching at the HEX Values only.

Do you have any suggestions what is wrong in my thinking or in my code?
This simple task can't be that hard!? I am working on this since two days now and i am still at the starting point.

Thank you in advance
Best regards
Daniel
Last edited by balance on Wed Sep 15, 2021 7:51 pm, edited 1 time in total.

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

Re: UART not receiving what i'm sending

Post by Roberthh » Mon Sep 13, 2021 5:01 pm

That look like either the baud rates or the signal levels do not match.
The RP2 uses so-called TTL signal levels of 0 and 3.3V, with 3.3V being the idle level. I do not know how you are interfacing electrically to the laptop. If it is a UART/USb bridge, the signal levels should be fine, if this bridge is set to 3.3V mode. If you use a standard PC UART interface, you're in trouble, since the signal levels of that are +/- 5-12V, with -5 being the idle state. There are varriants with 0 and 5V level, but even then the idle level is 0V. So the signal is inverted.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: UART not receiving what i'm sending

Post by pythoncoder » Mon Sep 13, 2021 5:07 pm

Communications are always hard ;)

I can't see why you are using a timer to trigger the UART read, then having your main loop do nothing. I'm not sure if this is causing the problem, but as a matter of design I would scrap timer_read and replace

Code: Select all

while True:
    pass
with

Code: Select all

uart_read(0)
If that doesn't work I'd suspect a hardware problem as your code looks OK to me.

[EDIT]
I posted simultaneously with Robert. Please take note of his comments.
Peter Hinch
Index to my micropython libraries.

balance
Posts: 8
Joined: Mon Sep 13, 2021 4:07 pm
Location: NRW / Germany

Re: UART not receiving what i'm sending

Post by balance » Mon Sep 13, 2021 5:33 pm

Thank you for replying.

I am using a Digitus USB to Serial bridge. So signal levels should be fine.
Baudrates, stopbit, parity,... is double checked several times.

I am using a timer because I stripped my code down to the essential part for debugging purpose. It shouldn't make a difference whether polling uart1.read() or triggering it by a timer. But I will give it a try!

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: UART not receiving what i'm sending

Post by pythoncoder » Mon Sep 13, 2021 5:46 pm

It shouldn't make a difference whether polling uart1.read() or triggering it by a timer.
I can't help but agree, but I like to simplify things as far as possible when debugging. Especially when the alternative looks like hardware. Unless you have access to a scope or a logic analyser it's tricky. You can sometimes see what's happening with an LED with a series resistor. TX and RX lines should idle high.
Peter Hinch
Index to my micropython libraries.

balance
Posts: 8
Joined: Mon Sep 13, 2021 4:07 pm
Location: NRW / Germany

Re: UART not receiving what i'm sending

Post by balance » Tue Sep 14, 2021 4:35 pm

Ok i continued my investigations. Hardest part was to find my Logicanalyzer :lol:

I connected my LA to RX and TX and got this result:
When i send a HEX "01" via HTerm to the Pi Pico i get this answer from Pi Pico:
001.png
001.png (2.56 KiB) Viewed 2966 times
But my LA says on the RX Line was this: (looks inverted in the first view but doesn't match my further investigations)
002.png
002.png (12.87 KiB) Viewed 2966 times
And the TX line says:
003.png
003.png (15.09 KiB) Viewed 2966 times
This looks to me like my Laptop ships some kind of crap... I would say Pi Pico is working correct. It ships what it gets.

When i send a HEX "01" i do expect a BIN "00000001" on RX and TX. So i tried to send a HEX "03" and got this:
004.png
004.png (29.97 KiB) Viewed 2966 times
Here i would expect BIN "00000011". What my Laptop sends makes no sense to me.

What can be wrong with my Laptop/Digitus USBtoSerial?
What does Frame Error mean?

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

Re: UART not receiving what i'm sending

Post by Roberthh » Tue Sep 14, 2021 4:58 pm

Would you please post the model number of this digitus adapter? It looks like the wrong level polarity.

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

Re: UART not receiving what i'm sending

Post by Roberthh » Tue Sep 14, 2021 5:00 pm

Or send a picture of that adapter.

balance
Posts: 8
Joined: Mon Sep 13, 2021 4:07 pm
Location: NRW / Germany

Re: UART not receiving what i'm sending

Post by balance » Tue Sep 14, 2021 5:11 pm

The Hardware ID is: FTDIBUS\COMPORT&VID_0403&PID_6001
My converter seems to be this one:
https://www.digitus.info/de/produkte/?ean=4016032271611

Does this help you?

I tried a 100k Pullup on the RX line with no succes.

EDIT: And i did install latest drivers from Digitus Website

EDIT 2:
This is what happens if one Pico talks to the other. Exactly what i expect. I think i will kick the Digitus out of my config.
005.png
005.png (13.02 KiB) Viewed 2938 times
Last edited by balance on Tue Sep 14, 2021 6:10 pm, edited 1 time in total.

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

Re: UART not receiving what i'm sending

Post by Roberthh » Tue Sep 14, 2021 6:09 pm

That explains it. This is a wrong converter. It uses RS232 levels & polarity instead of TTL ones. Besides the polarity, the levels are too high and may break the Pico. About the levels: you can set the PICO UART to inverted mode. Use the keyword argument:

uart1 = UART(1,baudrate=9600, bits=8, parity=None, stop=1, invert=UART.INV_TX | UART.INV_RX)

You have also to set the logic analyzer pulseview to inverted mode.

You need a USB/UART Adapter like this one: https://www.amazon.de/AZDelivery-Adapte ... 168&sr=8-3. It can be jumpered to 3 and 5 V levels and has the right polarity.

Post Reply