androo4u
Posts: 12
Joined: Tue Feb 07, 2023 10:28 pm

Communicate from PICO to RPi

Wed Feb 08, 2023 5:55 am

Hi,
I am trying to get the ADC values to go from my pico board to an RPI running python.
I have tried both UART and serial, neither has worked.
Could someone give me a code snippet of any sort of communication from pico to RPI? Ideally only in python.
Thanks
Androo

ame
Posts: 6728
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Communicate from PICO to RPi

Wed Feb 08, 2023 8:12 am

I tried this:
https://www.google.com/search?q=communi ... 20to%20RPI

And got this:
https://medium.com/geekculture/serial-c ... c0ba97c7dc

Plus several other hits.

Connecting the Pico to a Pi via USB and opening the serial interface probably couldn't be simpler. If it's not working then describe exactly how you are doing it in case someone can spot a reason it won't work.
Hmm. What can I put here?

User avatar
B.Goode
Posts: 14738
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Communicate from PICO to RPi

Wed Feb 08, 2023 9:47 am

androo4u wrote:
Wed Feb 08, 2023 5:55 am

I am trying to get the ADC values to go from my pico board to an RPI running python.
I have tried both UART and serial, neither has worked.
Could someone give me a code snippet of any sort of communication from pico to RPI? Ideally only in python.

If you are running a standard release of MicroPython on your Pico board then Brian Pugh's Belay module running on your RPi will accomplish this. https://github.com/BrianPugh/belay

There is a code sample for exactly this purpose in the supplied examples. https://github.com/BrianPugh/belay/blob ... dc/main.py


androo4u
Posts: 12
Joined: Tue Feb 07, 2023 10:28 pm

Re: Communicate from PICO to RPi

Wed Feb 08, 2023 3:25 pm

gordon77 wrote:
Wed Feb 08, 2023 10:18 am
Here's an example

viewtopic.php?t=301414#p1809106

Following this thread, I am able to get the code to run on the Pico, (when I test it by running it in thonny I get "NO data", but that is expected as I am not sending any data to it through code). Anywho, I saved the code as main.py, closed thonny, and rebooted the pico.

running this code on VScode on my PC connected to the pico through usb

Code: Select all

import time
import os

serial_connected = 0
if os.path.exists('/dev/ttyACM0') == True:
    import serial
    ser = serial.Serial('/dev/ttyACM0', 115200)
    serial_connected = 1
    time.sleep(1)

while True:
    time.sleep(2)
    for x in range (0,5):
        command = str(x) + "\n"
        ser.write(bytes(command.encode('ascii')))
        if ser.inWaiting() > 0:
            pico_data = ser.readline()
            pico_data = pico_data.decode("utf-8","ignore")
            print (pico_data[:-2])  

Returns this error:

Code: Select all

PS C:\Users\name> & C:/Users/name/AppData/Local/Programs/Python/Python310/python.exe "c:/Users/name/Documents/Python Scripts/test.py"
Traceback (most recent call last):
  File "c:\Users\name\Documents\Python Scripts\test.py", line 15, in <module>
    ser.write(bytes(command.encode('ascii')))
NameError: name 'ser' is not defined. Did you mean: 'set'?

For some reason '/dev/ttyACM0' is not accessible, but I don't understand why.

androo4u
Posts: 12
Joined: Tue Feb 07, 2023 10:28 pm

Re: Communicate from PICO to RPi

Wed Feb 08, 2023 3:35 pm

ame wrote:
Wed Feb 08, 2023 8:12 am
I tried this:
https://www.google.com/search?q=communi ... 20to%20RPI

And got this:
https://medium.com/geekculture/serial-c ... c0ba97c7dc

Plus several other hits.

Connecting the Pico to a Pi via USB and opening the serial interface probably couldn't be simpler. If it's not working then describe exactly how you are doing it in case someone can spot a reason it won't work.
I had tried this before but i'll summarize my efforts better
For the USB route:
I had seen this page previously but I am unable to get robot or rUSB to import. Whenever I go to look up those packages on thonny I find nothing.

I am doing Thonny -> Tools -> Manage Packages -> type "Rusb" in the bar at the top, it returns

Code: Select all

Could not find the package from PyPI.
Please check your spelling!
For the UART route:
For context, I am testing this on a RPI4.
First, for connections,

RPI pin 9 -> Pico Pin 8 (GND -> GND)
RPI pin 8 -> Pico Pin 7 (TX -> RX)
RPI pin 10 -> Pico pin 6 (RX -> TX)

I have gone into

Code: Select all

 sudo nano /boot/config.txt
and added a line at the bottom under [all] with:

Code: Select all

enable_uart=1
The pico code is saved as main.py and the pico is rebooted. While I had thonny up I ran the code once (on the pico) to test the output and get this:
UART(0, baudrate=115200, bits=8, parity=None, stop=1, tx=0, rx=1, txbuf=256, rxbuf=256, timeout=0, timeout_char=1, invert=None)
<class 'bytes'>
b'\x00'
<class 'str'>
>>

running this code:

Code: Select all

# sender.py
import time
import serial
ser = serial.Serial(
  port='/dev/ttyS0', # Change this according to connection methods, e.g. /dev/ttyUSB0
  baudrate = 115200,
  parity=serial.PARITY_NONE,
  stopbits=serial.STOPBITS_ONE,
  bytesize=serial.EIGHTBITS,
  timeout=1
)
msg = ""
i = 0
while True:
    i+=1
    print("Counter {} - Hello from Raspberry Pi".format(i))
    ser.write('hello'.encode('utf-8'))
    time.sleep(2)
I get this error:

Code: Select all

PS C:\Users\name> & C:/Users/name/AppData/Local/Programs/Python/Python310/python.exe "c:/Users/name/Documents/Python Scripts/test.py"
Traceback (most recent call last):
  File "c:\Users\name\Documents\Python Scripts\test.py", line 4, in <module>
    ser = serial.Serial(
  File "C:\Users\name\AppData\Local\Programs\Python\Python310\lib\site-packages\serial\serialwin32.py", line 33, in __init__
    super(Serial, self).__init__(*args, **kwargs)
  File "C:\Users\name\AppData\Local\Programs\Python\Python310\lib\site-packages\serial\serialutil.py", line 244, in __init__
    self.open()
  File "C:\Users\name\AppData\Local\Programs\Python\Python310\lib\site-packages\serial\serialwin32.py", line 64, in open
    raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port '/dev/ttyS0': FileNotFoundError(2, 'The system cannot find the path specified.', None, 3)
Hopefully this gives you more details about my problem

androo4u
Posts: 12
Joined: Tue Feb 07, 2023 10:28 pm

Re: Communicate from PICO to RPi

Wed Feb 08, 2023 3:54 pm

B.Goode wrote:
Wed Feb 08, 2023 9:47 am
androo4u wrote:
Wed Feb 08, 2023 5:55 am

I am trying to get the ADC values to go from my pico board to an RPI running python.
I have tried both UART and serial, neither has worked.
Could someone give me a code snippet of any sort of communication from pico to RPI? Ideally only in python.

If you are running a standard release of MicroPython on your Pico board then Brian Pugh's Belay module running on your RPi will accomplish this. https://github.com/BrianPugh/belay

There is a code sample for exactly this purpose in the supplied examples. https://github.com/BrianPugh/belay/blob ... dc/main.py
So to test this I changed my testing setup from the other replies.
The pico is connected to the PI through a USB cable. This was tested on USB 2.0 and 3.0.

Running the code from here: https://github.com/BrianPugh/belay/blob ... dc/main.py

I get this error message

Code: Select all

pi@rp4:~/UART $ python testcode.py
Traceback (most recent call last):
  File "/home/pi/UART/testcode.py", line 10, in <module>
    device = belay.Device(args.port)
  File "/home/pi/.local/lib/python3.9/site-packages/belay/device.py", line 253, in __init__
    self._connect_to_board(**self._board_kwargs)
  File "/home/pi/.local/lib/python3.9/site-packages/belay/device.py", line 332, in _connect_to_board
    self._board = Pyboard(**kwargs)
  File "/home/pi/.local/lib/python3.9/site-packages/belay/pyboard.py", line 357, in __init__
    raise PyboardError("failed to access " + device)
belay.pyboard.PyboardError: failed to access /dev/ttyUSB0
I also tried running it on my PC for posterity. I get the same error:

Code: Select all

PS C:\Users\name> & C:/Users/fisch/AppData/Local/Programs/Python/Python310/python.exe "c:/Users/name/Documents/Python Scripts/test.py"
Traceback (most recent call last):
  File "c:\Users\name\Documents\Python Scripts\test.py", line 10, in <module>
    device = belay.Device(args.port)
  File "C:\Users\name\AppData\Local\Programs\Python\Python310\lib\site-packages\belay\device.py", line 253, in __init__
    self._connect_to_board(**self._board_kwargs)
  File "C:\Users\name\AppData\Local\Programs\Python\Python310\lib\site-packages\belay\device.py", line 332, in _connect_to_board
    self._board = Pyboard(**kwargs)
  File "C:\Users\name\AppData\Local\Programs\Python\Python310\lib\site-packages\belay\pyboard.py", line 357, in __init__
    raise PyboardError("failed to access " + device)
belay.pyboard.PyboardError: failed to access /dev/ttyUSB0

User avatar
B.Goode
Posts: 14738
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Communicate from PICO to RPi

Wed Feb 08, 2023 4:12 pm

Regarding the proposed Belay solution -
androo4u wrote:
Wed Feb 08, 2023 3:54 pm

The pico is connected to the PI through a USB cable. This was tested on USB 2.0 and 3.0.

Running the code from here: https://github.com/BrianPugh/belay/blob ... dc/main.py

I get this error message

Code: Select all

belay.pyboard.PyboardError: failed to access /dev/ttyUSB0
I also tried running it on my PC for posterity. I get the same error:

Code: Select all

belay.pyboard.PyboardError: failed to access /dev/ttyUSB0

I am pretty certain that a usb-connected micropython Pico will not present itself to a Windows system as '/dev/ttyUSB0'


And I am not in front of a RasPiOS + Pico combination right now, but '/dev/ttyUSB0' doesn't look right for a RasPiOS installation either.

androo4u
Posts: 12
Joined: Tue Feb 07, 2023 10:28 pm

Re: Communicate from PICO to RPi

Wed Feb 08, 2023 4:20 pm

B.Goode wrote:
Wed Feb 08, 2023 4:12 pm
Regarding the proposed Belay solution -
androo4u wrote:
Wed Feb 08, 2023 3:54 pm

The pico is connected to the PI through a USB cable. This was tested on USB 2.0 and 3.0.

Running the code from here: https://github.com/BrianPugh/belay/blob ... dc/main.py

I get this error message

Code: Select all

belay.pyboard.PyboardError: failed to access /dev/ttyUSB0
I also tried running it on my PC for posterity. I get the same error:

Code: Select all

belay.pyboard.PyboardError: failed to access /dev/ttyUSB0

I am pretty certain that a usb-connected micropython Pico will not present itself to a Windows system as '/dev/ttyUSB0'


And I am not in front of a RasPiOS + Pico combination right now, but '/dev/ttyUSB0' doesn't look right for a RasPiOS installation either.

I have also tried ttyS0 - ttys4 if that can help? I don't know what other addresses exist.

User avatar
B.Goode
Posts: 14738
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Communicate from PICO to RPi

Wed Feb 08, 2023 4:26 pm

androo4u wrote:
Wed Feb 08, 2023 4:20 pm

I have also tried ttyS0 - ttys4 if that can help? I don't know what other addresses exist.

How do you normally interact with your Pico for development? What device id do you use then? What tool do you use on the RPi for communication?

What linux-like OS are you running on the RPi board. (Which I guess is an RPi4b?)

androo4u
Posts: 12
Joined: Tue Feb 07, 2023 10:28 pm

Re: Communicate from PICO to RPi

Wed Feb 08, 2023 4:32 pm

B.Goode wrote:
Wed Feb 08, 2023 4:26 pm
androo4u wrote:
Wed Feb 08, 2023 4:20 pm

I have also tried ttyS0 - ttys4 if that can help? I don't know what other addresses exist.

How do you normally interact with your Pico for development? What device id do you use then? What tool do you use on the RPi for communication?

What linux-like OS are you running on the RPi board. (Which I guess is an RPi4b?)
its noobs running on RPI4b.

For pico I flash it and REPL with it through thonny. I don't know what device ID thonny uses.

gordon77
Posts: 7145
Joined: Sun Aug 05, 2012 3:12 pm

Re: Communicate from PICO to RPi

Wed Feb 08, 2023 4:35 pm

androo4u wrote:
Wed Feb 08, 2023 3:25 pm
gordon77 wrote:
Wed Feb 08, 2023 10:18 am
Here's an example

viewtopic.php?t=301414#p1809106

Following this thread, I am able to get the code to run on the Pico, (when I test it by running it in thonny I get "NO data", but that is expected as I am not sending any data to it through code). Anywho, I saved the code as main.py, closed thonny, and rebooted the pico.

running this code on VScode on my PC connected to the pico through usb

Code: Select all

import time
import os

serial_connected = 0
if os.path.exists('/dev/ttyACM0') == True:
    import serial
    ser = serial.Serial('/dev/ttyACM0', 115200)
    serial_connected = 1
    time.sleep(1)

while True:
    time.sleep(2)
    for x in range (0,5):
        command = str(x) + "\n"
        ser.write(bytes(command.encode('ascii')))
        if ser.inWaiting() > 0:
            pico_data = ser.readline()
            pico_data = pico_data.decode("utf-8","ignore")
            print (pico_data[:-2])  

Returns this error:

Code: Select all

PS C:\Users\name> & C:/Users/name/AppData/Local/Programs/Python/Python310/python.exe "c:/Users/name/Documents/Python Scripts/test.py"
Traceback (most recent call last):
  File "c:\Users\name\Documents\Python Scripts\test.py", line 15, in <module>
    ser.write(bytes(command.encode('ascii')))
NameError: name 'ser' is not defined. Did you mean: 'set'?

For some reason '/dev/ttyACM0' is not accessible, but I don't understand why.
If you run ls /dev does it show ttyACM0 or maybe ttyACM1 ?

User avatar
B.Goode
Posts: 14738
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Communicate from PICO to RPi

Wed Feb 08, 2023 4:35 pm

androo4u wrote:
Wed Feb 08, 2023 4:32 pm


its noobs running on RPI4b.

For pico I flash it and REPL with it through thonny. I don't know what device ID thonny uses.


NOOBS is a deprecated Installer, not an Operating System.
Ref: https://www.raspberrypi.com/documentati ... ing-system
NOOBS, or New Out Of the Box Software to give it its full name, was an SD card-based installer for Raspberry Pi computers; we no longer recommend or support using NOOBS. Going forward, please use Raspberry Pi Imager.


Thonny reports the device it is connecting to at the bottom right of the screen. You might need to click on the dialog box at the corner to display the details.
Last edited by B.Goode on Wed Feb 08, 2023 4:45 pm, edited 1 time in total.

androo4u
Posts: 12
Joined: Tue Feb 07, 2023 10:28 pm

Re: Communicate from PICO to RPi

Wed Feb 08, 2023 4:37 pm

B.Goode wrote:
Wed Feb 08, 2023 4:35 pm
androo4u wrote:
Wed Feb 08, 2023 4:32 pm


its noobs running on RPI4b.

For pico I flash it and REPL with it through thonny. I don't know what device ID thonny uses.


NOOBS is a deprecated Installer, not an Operating System.

Thonny reports the device it is connecting to at the bottom right of the screen. You might need to click on the dialog box at the corner to display the details.
I get COM5 as additional info when I click the pico at the bottom right of thonny.
RPI is running Raspberry Pi OS (32 bit) released 2022-09-22.

androo4u
Posts: 12
Joined: Tue Feb 07, 2023 10:28 pm

Re: Communicate from PICO to RPi

Wed Feb 08, 2023 4:38 pm

gordon77 wrote:
Wed Feb 08, 2023 4:35 pm
androo4u wrote:
Wed Feb 08, 2023 3:25 pm
gordon77 wrote:
Wed Feb 08, 2023 10:18 am
Here's an example

viewtopic.php?t=301414#p1809106

Following this thread, I am able to get the code to run on the Pico, (when I test it by running it in thonny I get "NO data", but that is expected as I am not sending any data to it through code). Anywho, I saved the code as main.py, closed thonny, and rebooted the pico.

running this code on VScode on my PC connected to the pico through usb

Code: Select all

import time
import os

serial_connected = 0
if os.path.exists('/dev/ttyACM0') == True:
    import serial
    ser = serial.Serial('/dev/ttyACM0', 115200)
    serial_connected = 1
    time.sleep(1)

while True:
    time.sleep(2)
    for x in range (0,5):
        command = str(x) + "\n"
        ser.write(bytes(command.encode('ascii')))
        if ser.inWaiting() > 0:
            pico_data = ser.readline()
            pico_data = pico_data.decode("utf-8","ignore")
            print (pico_data[:-2])  

Returns this error:

Code: Select all

PS C:\Users\name> & C:/Users/name/AppData/Local/Programs/Python/Python310/python.exe "c:/Users/name/Documents/Python Scripts/test.py"
Traceback (most recent call last):
  File "c:\Users\name\Documents\Python Scripts\test.py", line 15, in <module>
    ser.write(bytes(command.encode('ascii')))
NameError: name 'ser' is not defined. Did you mean: 'set'?

For some reason '/dev/ttyACM0' is not accessible, but I don't understand why.
If you run ls /dev does it show ttyACM0 or maybe ttyACM1 ?
Where would I run ls /dev/? It just spits an error that it doesnt exist when I run it on my PC.

User avatar
B.Goode
Posts: 14738
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Communicate from PICO to RPi

Wed Feb 08, 2023 4:41 pm

androo4u wrote:
Wed Feb 08, 2023 4:38 pm


Where would I run ls /dev/? It just spits an error that it doesnt exist when I run it on my PC.


I would run it on a linux-like Operating System, not MicroSoft Windows. Windows doesn't normally name devices like that. The headline question was "Communicate from PICO to RPi", and your RPi is unlikely to be running Windows.

androo4u
Posts: 12
Joined: Tue Feb 07, 2023 10:28 pm

Re: Communicate from PICO to RPi

Wed Feb 08, 2023 4:49 pm

B.Goode wrote:
Wed Feb 08, 2023 4:41 pm
androo4u wrote:
Wed Feb 08, 2023 4:38 pm


Where would I run ls /dev/? It just spits an error that it doesnt exist when I run it on my PC.


I would run it on a linux-like Operating System, not MicroSoft Windows. Windows doesn't normally name devices like that. The headline question was "Communicate from PICO to RPi", and your RPi is unlikely to be running Windows.

Apologies. I am going between my PC and my PI to debug.

Doing ls /dev/ outputs this, to be clear, the pico is connected through USB 3.0

Code: Select all

pi@rp4:~ $ ls /dev/
autofs         loop2         ram12    tty14  tty4   tty8       vcsm-cma
block          loop3         ram13    tty15  tty40  tty9       vcsu
btrfs-control  loop4         ram14    tty16  tty41  ttyACM0    vcsu1
bus            loop5         ram15    tty17  tty42  ttyAMA0    vcsu2
cachefiles     loop6         ram2     tty18  tty43  ttyprintk  vcsu3
cec0           loop7         ram3     tty19  tty44  uhid       vcsu4
cec1           loop-control  ram4     tty2   tty45  uinput     vcsu5
char           mapper        ram5     tty20  tty46  urandom    vcsu6
console        media0        ram6     tty21  tty47  v4l        vcsu7
cuse           media1        ram7     tty22  tty48  vchiq      vga_arbiter
disk           media2        ram8     tty23  tty49  vcio       vhci
dma_heap       media3        ram9     tty24  tty5   vc-mem     video10
dri            mem           random   tty25  tty50  vcs        video11
fd             mmcblk0       rfkill   tty26  tty51  vcs1       video12
full           mmcblk0p1     serial   tty27  tty52  vcs2       video13
fuse           mmcblk0p2     serial1  tty28  tty53  vcs3       video14
gpiochip0      mqueue        shm      tty29  tty54  vcs4       video15
gpiochip1      net           snd      tty3   tty55  vcs5       video16
gpiomem        null          stderr   tty30  tty56  vcs6       video18
hwrng          port          stdin    tty31  tty57  vcs7       video19
i2c-20         ppp           stdout   tty32  tty58  vcsa       video20
i2c-21         ptmx          tty      tty33  tty59  vcsa1      video21
initctl        ptp0          tty0     tty34  tty6   vcsa2      video22
input          pts           tty1     tty35  tty60  vcsa3      video23
kmsg           ram0          tty10    tty36  tty61  vcsa4      video31
log            ram1          tty11    tty37  tty62  vcsa5      watchdog
loop0          ram10         tty12    tty38  tty63  vcsa6      watchdog0
loop1          ram11         tty13    tty39  tty7   vcsa7      zero

User avatar
B.Goode
Posts: 14738
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Communicate from PICO to RPi

Wed Feb 08, 2023 5:01 pm

androo4u wrote:
Wed Feb 08, 2023 4:37 pm


I get COM5 as additional info when I click the pico at the bottom right of thonny.
Presumably that is on a Microsoft Windows PC.

So to run the Belay ADC sample you would need something like

Code: Select all

python Scripts/test.py -p COM5
or possibly the port needs to be a string -

Code: Select all

python Scripts/test.py -p "COM5"

gordon77
Posts: 7145
Joined: Sun Aug 05, 2012 3:12 pm

Re: Communicate from PICO to RPi

Wed Feb 08, 2023 5:06 pm

appears OK

mine works OK with this output

NO data
NO data
adc ch: 0 : 1056
adc ch: 1 : 928
adc ch: 2 : 928
adc ch: 3 : 8145
Temp: 12.06379
NO data
adc ch: 0 : 1024
adc ch: 1 : 944
adc ch: 2 : 944
adc ch: 3 : 8129
Temp: 12.06379
NO data
adc ch: 0 : 1024

User avatar
B.Goode
Posts: 14738
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Communicate from PICO to RPi

Wed Feb 08, 2023 5:10 pm

androo4u wrote:
Wed Feb 08, 2023 4:49 pm


Apologies. I am going between my PC and my PI to debug.

Doing ls /dev/ outputs this, to be clear, the pico is connected through USB 3.0

[ ... output snipped ... ]

Under RasPiOS the device representing your MicroPython Pico will be /dev/ttyACM0 devices. Again, Thonny will probably autodetect this and report it in the same location.

Again, you can pass this to the test script as an argument something like

Code: Select all

 python testcode.py -p /dev/ttyACM0
and again, you might need to quote the port name as a string.

androo4u
Posts: 12
Joined: Tue Feb 07, 2023 10:28 pm

Re: Communicate from PICO to RPi

Wed Feb 08, 2023 9:07 pm

gordon77 wrote:
Wed Feb 08, 2023 4:35 pm
androo4u wrote:
Wed Feb 08, 2023 3:25 pm
gordon77 wrote:
Wed Feb 08, 2023 10:18 am
Here's an example

viewtopic.php?t=301414#p1809106

Following this thread, I am able to get the code to run on the Pico, (when I test it by running it in thonny I get "NO data", but that is expected as I am not sending any data to it through code). Anywho, I saved the code as main.py, closed thonny, and rebooted the pico.

running this code on VScode on my PC connected to the pico through usb

Code: Select all

import time
import os

serial_connected = 0
if os.path.exists('/dev/ttyACM0') == True:
    import serial
    ser = serial.Serial('/dev/ttyACM0', 115200)
    serial_connected = 1
    time.sleep(1)

while True:
    time.sleep(2)
    for x in range (0,5):
        command = str(x) + "\n"
        ser.write(bytes(command.encode('ascii')))
        if ser.inWaiting() > 0:
            pico_data = ser.readline()
            pico_data = pico_data.decode("utf-8","ignore")
            print (pico_data[:-2])  

Returns this error:

Code: Select all

PS C:\Users\name> & C:/Users/name/AppData/Local/Programs/Python/Python310/python.exe "c:/Users/name/Documents/Python Scripts/test.py"
Traceback (most recent call last):
  File "c:\Users\name\Documents\Python Scripts\test.py", line 15, in <module>
    ser.write(bytes(command.encode('ascii')))
NameError: name 'ser' is not defined. Did you mean: 'set'?

For some reason '/dev/ttyACM0' is not accessible, but I don't understand why.
If you run ls /dev does it show ttyACM0 or maybe ttyACM1 ?
Some good news! this code works!
My output reads like this:

Code: Select all

pi@rp4:~/usb $ python usb_test.py
Temperature: 17.2C
Temperature: 17.2C
Temperature: 17.7C
Temperature: 17.7C
Temperature: 17.2C
Temperature: 17.7C
Temperature: 17.2C
Temperature: 17.7C
Temperature: 17.2C
Temperature: 17.7C
Temperature: 17.7C
Temperature: 17.2C
Temperature: 17.2C
Temperature: 17.7C
Temperature: 17.2C
Temperature: 17.7C
Temperature: 17.7C
Temperature: 17.7C
(a bit chilly so I assume this isnt fully correct tbh).
In any case, could you help me understand it a bit better? I don't understand where its calling the ADC. I would like to modify the code to read my the value on pin 26, and print the voltage, but I dont see any pins.

Thank you immensely B_goode and Ame for your help. Sorry I couldn't get your implementation to work.

User avatar
B.Goode
Posts: 14738
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Communicate from PICO to RPi

Wed Feb 08, 2023 9:21 pm

Later... Sitting in front of an RPi4B running RasPiOS and a Pico running micropython. Checking the belay solution I proposed.

Checking with dmesg after connecting the usb cable to the Pico -

Code: Select all

[  979.688125] usb 1-1.3: new full-speed USB device number 4 using xhci_hcd
[  979.795560] usb 1-1.3: New USB device found, idVendor=2e8a, idProduct=0005, bcdDevice= 1.00
[  979.795589] usb 1-1.3: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[  979.795606] usb 1-1.3: Product: Board in FS mode
[  979.795619] usb 1-1.3: Manufacturer: MicroPython
[  979.795632] usb 1-1.3: SerialNumber: e660d051135e2636
[  979.808315] cdc_acm 1-1.3:1.0: ttyACM0: USB ACM device
So the device that RasPiOS has assigned is /dev/ttyACM0


Using that information to pass the correct port argument to the belay adc example script -

Code: Select all

pi@rpitest64:~/belay/belay/examples/03_read_adc $  python main.py -p /dev/ttyACM0
Temperature: 19.1C
Temperature: 19.6C
Temperature: 22.4C
Temperature: 22.4C
Temperature: 20.5C
Temperature: 19.1C
Temperature: 19.1C
The almost magical feature of this solution is that it needs no custom code running on the Pico other than flashing a standard build of micropython.

User avatar
B.Goode
Posts: 14738
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Communicate from PICO to RPi

Wed Feb 08, 2023 9:47 pm

androo4u wrote:
Wed Feb 08, 2023 9:07 pm
I don't understand where its calling the ADC. I would like to modify the code to read my the value on pin 26, and print the voltage, but I dont see any pins.

To allow you to make progress in case @gordon77 isn't around this evening.

The reason you don't see Pin numbers used in gordon's example, or the belay example, is that MicroPython is pre-configured to use machine.ADC(0) as a reference to reading from GP26, similarly ADC(1)/GP27, and ADC(2)/GP28. See Section 3.3 of the Raspberry Pi micropython documentation. https://datasheets.raspberrypi.com/pico ... on-sdk.pdf

machine.ADC(4) returns the adc value of the internal temperature sensor which is being used by both example scripts. There is no physical GPIO pin associated with that adc channel.

So to use GP26, change the script to use machine.ADC(0).
Last edited by B.Goode on Wed Feb 08, 2023 9:49 pm, edited 1 time in total.

gordon77
Posts: 7145
Joined: Sun Aug 05, 2012 3:12 pm

Re: Communicate from PICO to RPi

Wed Feb 08, 2023 9:49 pm

androo4u wrote:
Wed Feb 08, 2023 9:07 pm
gordon77 wrote:
Wed Feb 08, 2023 4:35 pm
androo4u wrote:
Wed Feb 08, 2023 3:25 pm



Following this thread, I am able to get the code to run on the Pico, (when I test it by running it in thonny I get "NO data", but that is expected as I am not sending any data to it through code). Anywho, I saved the code as main.py, closed thonny, and rebooted the pico.

running this code on VScode on my PC connected to the pico through usb

Code: Select all

import time
import os

serial_connected = 0
if os.path.exists('/dev/ttyACM0') == True:
    import serial
    ser = serial.Serial('/dev/ttyACM0', 115200)
    serial_connected = 1
    time.sleep(1)

while True:
    time.sleep(2)
    for x in range (0,5):
        command = str(x) + "\n"
        ser.write(bytes(command.encode('ascii')))
        if ser.inWaiting() > 0:
            pico_data = ser.readline()
            pico_data = pico_data.decode("utf-8","ignore")
            print (pico_data[:-2])  

Returns this error:

Code: Select all

PS C:\Users\name> & C:/Users/name/AppData/Local/Programs/Python/Python310/python.exe "c:/Users/name/Documents/Python Scripts/test.py"
Traceback (most recent call last):
  File "c:\Users\name\Documents\Python Scripts\test.py", line 15, in <module>
    ser.write(bytes(command.encode('ascii')))
NameError: name 'ser' is not defined. Did you mean: 'set'?

For some reason '/dev/ttyACM0' is not accessible, but I don't understand why.
If you run ls /dev does it show ttyACM0 or maybe ttyACM1 ?
Some good news! this code works!
My output reads like this:

Code: Select all

pi@rp4:~/usb $ python usb_test.py
Temperature: 17.2C
Temperature: 17.2C
Temperature: 17.7C
Temperature: 17.7C
Temperature: 17.2C
Temperature: 17.7C
Temperature: 17.2C
Temperature: 17.7C
Temperature: 17.2C
Temperature: 17.7C
Temperature: 17.7C
Temperature: 17.2C
Temperature: 17.2C
Temperature: 17.7C
Temperature: 17.2C
Temperature: 17.7C
Temperature: 17.7C
Temperature: 17.7C
(a bit chilly so I assume this isnt fully correct tbh).
In any case, could you help me understand it a bit better? I don't understand where its calling the ADC. I would like to modify the code to read my the value on pin 26, and print the voltage, but I dont see any pins.

Thank you immensely B_goode and Ame for your help. Sorry I couldn't get your implementation to work.
In the pico code adc0 is pin26

adc0 = machine.ADC(0)

And this is where it reads it...


print("adc ch: " + str(ch[0]) + " : " + str(adc0.read_u16()))

androo4u
Posts: 12
Joined: Tue Feb 07, 2023 10:28 pm

Re: Communicate from PICO to RPi

Wed Feb 08, 2023 10:39 pm

gordon77 wrote:
Wed Feb 08, 2023 9:49 pm
androo4u wrote:
Wed Feb 08, 2023 9:07 pm
gordon77 wrote:
Wed Feb 08, 2023 4:35 pm


If you run ls /dev does it show ttyACM0 or maybe ttyACM1 ?
Some good news! this code works!
My output reads like this:

Code: Select all

pi@rp4:~/usb $ python usb_test.py
Temperature: 17.2C
Temperature: 17.2C
Temperature: 17.7C
Temperature: 17.7C
Temperature: 17.2C
Temperature: 17.7C
Temperature: 17.2C
Temperature: 17.7C
Temperature: 17.2C
Temperature: 17.7C
Temperature: 17.7C
Temperature: 17.2C
Temperature: 17.2C
Temperature: 17.7C
Temperature: 17.2C
Temperature: 17.7C
Temperature: 17.7C
Temperature: 17.7C
(a bit chilly so I assume this isnt fully correct tbh).
In any case, could you help me understand it a bit better? I don't understand where its calling the ADC. I would like to modify the code to read my the value on pin 26, and print the voltage, but I dont see any pins.

Thank you immensely B_goode and Ame for your help. Sorry I couldn't get your implementation to work.
In the pico code adc0 is pin26

adc0 = machine.ADC(0)

And this is where it reads it...


print("adc ch: " + str(ch[0]) + " : " + str(adc0.read_u16()))

I see, thanks, I was able to get it working more or less.
One more problem; I seem to having some trouble getting consistent outputs;

Code: Select all

 python usb_test.py
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05559167
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05639734
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05317464
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05559167
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05478599
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
adc ch: 0 : 0.05398031
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
NO data
I ideally want to ping this 30 times a second, is this going to be problematic? Any idea why my data taps in and out?

this also happens running to stock code:

Code: Select all

 python git.py
NO data
adc ch: 0 : 1232
adc ch: 1 : 1072
adc ch: 2 : 1056
adc ch: 3 : 8081
Temp: 23.29925
NO data
adc ch: 0 : 1072
adc ch: 1 : 1072
adc ch: 2 : 1056
adc ch: 3 : 7985
Temp: 22.8311
NO data
adc ch: 0 : 1136
adc ch: 1 : 1088
adc ch: 2 : 1056
adc ch: 3 : 8001
Temp: 22.8311

Return to “General”