1

For example when i give 5 to the code, i want to turn on the led in our rpi pico (rpi pico connected to pc with cable)

#This code will run in my computer (test.py)

x=int(input("Number?"))
if (x==5):
    #turn on raspberry pi pico led

The code of rpi pico:

#This code will run in my rpi pico (pico.py)

from machine import Pin
led = Pin(25, Pin.OUT)

led.value(1)

or vice versa (doing something in the code on the computer with the code in the rpi pico)

and how can i call/get a variable in pc to rpi pico

note: I am writing a code with opencv python and I want to process the data from my computer's camera on my computer and I want rpi pico to react according to the processed data. And raspberry pi pico connected to pc with cable.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36

1 Answers1

5

A simple method of communicating between the host and the Pico is to use the serial port. I have a rp2040-zero, which presents itself to the host as /dev/ttyACM0. If I use code like this on the rp2040:

import sys
import machine

led = machine.Pin(24, machine.Pin.OUT)

def led_on():
    led(1)

def led_off():
    led(0)


while True:
    # read a command from the host
    v = sys.stdin.readline().strip()

    # perform the requested action
    if v.lower() == "on":
        led_on()
    elif v.lower() == "off":
        led_off()

Then I can run this on the host to blink the LED:

import serial
import time

# open a serial connection
s = serial.Serial("/dev/ttyACM0", 115200)

# blink the led
while True:
    s.write(b"on\n")
    time.sleep(1)
    s.write(b"off\n")
    time.sleep(1)

This is obviously just one-way communication, but you could of course implement a mechanism for passing information back to the host.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • Thanks for your answer but i did not understand few thing. While you trying to blink the led, you writed this: (b"on\r\n") Why you writed like this? Can you explain me? Or can i write just "on". And how can i find this ("/dev/ttyACM0", 115200) for mine – Abdullah Esad TAN May 07 '22 at 17:21
  • I'm writing `b'on\n'` because the `serial.Serial.write` method requires a [byte string](https://stackoverflow.com/a/46589880/147356). We need `\n` (turns out we don't need `\r\n`) because we're using the `readline` method, so we need to send an end-of-line marker. – larsks May 07 '22 at 17:36
  • Re: "how can i find this ("/dev/ttyACM0", 115200) for mine", if you're on Linux, when you plug in the device you should see some log messages (e.g., in the output of `dmesg` or `journalctl -kfl`) that indicate what port it creates. I don't have an actual Pico so I can't check myself. If you're on Windows or MacOS you'll have to ask elsewhere. – larsks May 07 '22 at 17:37
  • This did not work with pico w – ambassallo Dec 03 '22 at 13:26
  • Any ideas on how I could get the serial connection number from the pico's point of view so I can send data back? – qbush Mar 02 '23 at 02:36
  • I'm not sure what you're asking. What do you mean by "the serial connection number"? This answer already shows how to send data from the Pico to the host (and in the other direction). – larsks Mar 02 '23 at 13:02