MicroPython serial echo: input() on an ESP32, on an ESP32-DevKitC V4
MicroPython on an ESP32 with nothing wired: input() reads each line typed into the Serial Monitor and print() answers with it. The whole build, an ESP32-DevKitC V4, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 5 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $10. On Mokxi they're free.
What you'll learn
- Reading typed text with input()
- Sending text to the Serial Monitor with print
- Why a loop keeps the program alive
- Serial as a two way channel
- Difficulty: Beginner
- Time: About 5 minutes
- Board: ESP32-DevKitC V4
- Parts: 1
How it works
Nothing is wired for this one. The board talks to you through the Serial Monitor. It prints a prompt, then waits.
The input() call blocks until you type a line and press Enter. The script then prints You typed: followed by your text, and goes back to waiting. It is the simplest two way conversation with a board.
Parts list
1 part, plus the jumper wires. They retail for about $10; on Mokxi they're free.
- 1 × ESP32-DevKitC V4
Kits that can build it
These 3 starter kits have every part in the circuit. Each kit's page lists what else it can build.
The code
The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.
# Serial read and write: type a line into the Serial Monitor and press
# Enter. input() waits for it, and the board answers with what it got.
print("Type something and press Enter")
while True:
text = input()
print("You typed:", text)
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Nothing happens when you type This project
Click in the Serial Monitor's input box, type the text and press Enter. The board only answers after a full line. - The board is not found, or upload fails with a connecting message. ESP32-DevKitC V4
Use a USB cable that carries data, not charge only. Pick the ESP32 Dev Module board. If it still fails, hold BOOT while uploading starts, then release. - Some pins never read right, or the board will not boot with a wire attached. ESP32-DevKitC V4
GPIO34 to 39 are input only and have no pull-ups. GPIO6 to 11 run the flash, so leave them alone. GPIO0, 2 and 12 are boot pins; avoid pulling them the wrong way at reset.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.