I want to turn on an LED for a while when my Raspberry Pi Pico receives some data from USB. Here is the code I’m using for the Raspberry Pi Pico:
import select
import sys
import machine
poll_obj = select.poll()
poll_obj.register(sys.stdin, select.POLLIN)
counter = 0
pin = machine.Pin(25, machine.Pin.OUT)
pin.value(0)
while True:
poll_results = poll_obj.poll(1)
if poll_results:
# Read the data from stdin (read data coming from PC)
data = sys.stdin.read().strip()
pin.value(1)
# Write the data to the input file
print("receivedL " + str(data))
else:
counter = counter + 1
if(counter >= 1000):
pin.value(0)
counter = 0
print("test")
pc code:
import serial
s = serial.Serial(port="COM3", parity=serial.PARITY_EVEN, timeout=1)
s.flush()
data = s.read()
while True:
print(data)
data = s.read()
s.write("1".encode())
s.flush()
I run this setup as follows:
Plug in the Raspberry Pi Pico and run the code (not using Thonny or another editor).
Verify the code is running by connecting to the USB port with PuTTY.
Disconnect from PuTTY and run my code on the PC.
The PC usually shows one output from the Raspberry Pi Pico, then nothing more. Also, the LED does not blink.
I’m not 100% sure if the issue is with the write function on my PC, but since the read seems to work fine, I’m suspecting something else might be wrong.
Aslo, when i’m not using putty to verify connection, problem still happening.
Can anyone help me figure out what might be going wrong?