Hi all,
New here, long background of software development, but not in python/ micropython, long background of embedded development, but not with the 'new' pi pico.
I'm using the pi pico, running micropython, with the Thonny 'IDE'.
I'm trying to get the pico to behave as a kind of serial gateway, with data coming in on uart0, and either do some stuff with it, or pass it on to uart1, and visa versa.
What I'm struggling with is getting uart1 to work at all. I can successfully get uart0, using GP0 and GP1 pins, all fine. If I swap the exact same code to uart1, GP4 and GP5, or GP8 and GP9, it doesn't work.
This is being done using Thonny with it connected and reading 'print' data.
What I don't know is where the print data is being transmitted from on the pi pico. Is this using uart 1, which is why it doesn't work out of the box?
It would be great to know if anyone has managed to get both uart0 and uart1 working, at the same time, on a pi pico, running micropython.
Or if there are any caveats to using both uart ports at once.
Any info is greatly appreciated
Pi pico uart 1 seems to not work
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Pi pico uart 1 seems to not work
My standard loopback test works fine on UART 0 and 1 (link pins 4 and 5 for UART 1):
Code: Select all
import uasyncio as asyncio
from machine import UART
uart = UART(1, 9600)
async def sender():
swriter = asyncio.StreamWriter(uart, {})
while True:
swriter.write('Hello uart\n')
await swriter.drain()
await asyncio.sleep(2)
async def receiver():
sreader = asyncio.StreamReader(uart)
while True:
res = await sreader.readline()
print('Recieved', res)
async def main():
asyncio.create_task(sender())
asyncio.create_task(receiver())
while True:
await asyncio.sleep(1)
def test():
try:
asyncio.run(main())
except KeyboardInterrupt:
print('Interrupted')
finally:
asyncio.new_event_loop()
print('as_demos.auart.test() to run again.')
test()
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Pi pico uart 1 seems to not work
@minthemerciless Can you show a simple code example that fails?
@pythoncoder Did you try using uart0 and uart1 at the same time?
Edit: As simple test here works fine, sending & receiving data between UART0 and UART1.
@pythoncoder Did you try using uart0 and uart1 at the same time?
Edit: As simple test here works fine, sending & receiving data between UART0 and UART1.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Pi pico uart 1 seems to not work
The following works, either with both uarts looped back, or with them cross-linked (GPIO 0-5 and 1-4 linked):
Code: Select all
import uasyncio as asyncio
from machine import UART
uart0 = UART(0, 9600) # Wire GPIO 0-5 and 1-4
uart1 = UART(1, 9600)
async def sender(uart, n):
swriter = asyncio.StreamWriter(uart, {})
while True:
swriter.write(f'Hello from uart{n}\n')
await swriter.drain()
await asyncio.sleep(2)
async def receiver(uart, n):
sreader = asyncio.StreamReader(uart)
while True:
res = await sreader.readline()
print(f'Recieved by uart{n}', res)
async def main():
n = 0
for uart in (uart0, uart1):
asyncio.create_task(sender(uart, n))
asyncio.create_task(receiver(uart, n))
n += 1
while True:
await asyncio.sleep(1)
def test():
try:
asyncio.run(main())
except KeyboardInterrupt:
print('Interrupted')
finally:
asyncio.new_event_loop()
test()
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
-
- Posts: 2
- Joined: Wed Feb 16, 2022 11:23 pm
Re: Pi pico uart 1 seems to not work
Hi, thanks for the assist guys!! Really appreciate it.
I took my set up apart. Wired it up for the supplied loopback test. All worked fine. So not my hardware.
I put my code back on, and as if by magic, jackpot! It all worked as expected.
The solution?...
I will, hang my head in shame and admit it. In a blind stupor last night trying to get stuff working I had completely missed the fact that the labels on the board are GPx, not pin X. So when reading the pinout, seeing GP4,5 maps to pin 6 and 7 on the board, I'd connected my uart 1 to pins 6 and 7 completely neglecting the fact that is says GP6,7. IDIOT!!
Note to self: check my wiring.
I took my set up apart. Wired it up for the supplied loopback test. All worked fine. So not my hardware.
I put my code back on, and as if by magic, jackpot! It all worked as expected.
The solution?...
I will, hang my head in shame and admit it. In a blind stupor last night trying to get stuff working I had completely missed the fact that the labels on the board are GPx, not pin X. So when reading the pinout, seeing GP4,5 maps to pin 6 and 7 on the board, I'd connected my uart 1 to pins 6 and 7 completely neglecting the fact that is says GP6,7. IDIOT!!
Note to self: check my wiring.