0

I have tried to get user input in pico thonny but i can't get user input

from machine import Pin


a = input()
print(a)
Jonas
  • 121,568
  • 97
  • 310
  • 388
Wasi Chandio
  • 1
  • 1
  • 3
  • 1
    We need a lot more information before we're able to help you out. Your title mentions the Pico, but the code you've shown would run on a Raspberry Pi (not on the Pico). Can you be more clear about (a) where your code is running, and (b) exactly what you're trying to accomplish? – larsks May 22 '22 at 14:46
  • Yes actually, i should have to write machine instead of gpiozero – Wasi Chandio May 22 '22 at 15:03
  • Please update your question to reflect what your code actually looks like (click the "Edit" link under the question). – larsks May 22 '22 at 15:12
  • 1
    It's not clear what you mean by "I can't get user input". In what way is it not working -- you're getting an error? Unexpected output? Something else? If you're running that code on your Pico, it would be reading input from the serial connection. Are you connected to the Pico's serial port? How are you running the code? – larsks May 22 '22 at 16:20
  • Can you describe what you expect to happen with this code? How is the user expected to provide input - at the terminal, or with a button? You’ve imported Pin but then not used it. – Andy Piper May 24 '22 at 15:51

1 Answers1

1

To use Thonny with the Pico, you first need to configure Thonny to interact with the Pico via the serial port:

  1. Go to the "Run" menu, and choose "Select interpreter..."
  2. Under "Which interpreter or device should Thonny use for running your code?", choose "MicroPython (Raspberry Pi Pico)" (if that is in fact what you're using).
  3. Under "Port", select the appropriate serial port. If you're unsure what port to use, you can try leaving it set to "Try to detect port automatically".

After completing the above steps, in the "Shell" panel at the bottom of your window you should see something like:

MicroPython v1.18 on 2022-01-17; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>> 

At this point, if you enter your code in the code panel and click the "Play" button (or select "Run -> Run current script"), you should first be prompted where to save the script. You can save it either locally on your computer or on the Pico.

Once the script has been saved, in the "Shell" window you should now see:

>>> %Run -c $EDITOR_CONTENT

At this point, the script is running and is waiting for user input. Place the cursor at the bottom of the panel, and then type something and press <RETURN>. You should see your text echoed back to you:

>>> %Run -c $EDITOR_CONTENT
this is a test
this is a test
>>>
larsks
  • 277,717
  • 41
  • 399
  • 399