深夜提醒

现在是深夜,建议您注意休息,不要熬夜哦~

🏮 🏮 🏮

新年快乐

祝君万事如意心想事成!

share-image
中
ESC

Flashing MicroPython onto an ESP32 and Blinking the Onboard LED

English 阅读中文版

Today I bought a kit of gear for learning embedded development, ready to pick up something new. First, out of pure interest — I’ve grown tired of just fiddling with software. With AI around, frontend, backend, and app development have all been flattened, so I want to see how far AI can go when it comes to driving hardware. Second, I want to do this together with my kid. Kids are great with their hands and love building Lego, so I plan to learn embedded development alongside them and make some simple little hardware that can read Bluetooth and connect to the network. The future belongs to artificial intelligence — you have to learn it, there’s no escaping it.

Let’s start with the ESP32 I already have: get MicroPython running on it and make the onboard LED blink. The whole process was simpler than I expected, so here’s a record of the key steps.

The embedded learning gear I bought today

1. What You Need

  • One ESP32 development board
  • A USB data cable (one that can actually transfer data)
  • A computer (Linux) — I did everything on a little deepin mini PC. Why deepin? Mostly out of laziness: I didn’t want to install Ubuntu or Debian and mess around with input methods and such.
  • MicroPython firmware: grab the ESP32 GENERIC build from the official download page

Download page: https://micropython.org/download/ESP32_GENERIC/

The firmware file I downloaded was named something like:

ESP32_GENERIC-20260406-v1.28.0.bin

The ESP32 development board

2. Install esptool and Erase the Flash

First, create a temporary Python virtual environment and install esptool:

python3 -m venv .esptool_venv
source .esptool_venv/bin/activate
pip install esptool

Plug the ESP32 into the computer and confirm the serial port (usually /dev/ttyUSB0):

ls /dev/ttyUSB*

If you don’t have permission, open it up temporarily:

sudo chmod 666 /dev/ttyUSB0

Put the ESP32 into download mode:

  1. Press and hold the BOOT button.
  2. Tap the EN/RST reset button once.
  3. Release the BOOT button.

Then erase the flash:

esptool --chip esp32 --port /dev/ttyUSB0 erase-flash

3. Flash the MicroPython Firmware

Once the erase is done, write the firmware at offset 0x1000:

esptool --chip esp32 --port /dev/ttyUSB0 \
--baud 460800 write_flash -z 0x1000 \
/path/to/ESP32_GENERIC-20260406-v1.28.0.bin

After a successful flash it resets automatically, and the ESP32 boots into the MicroPython environment.

MicroPython REPL connected successfully

4. Connect to the REPL

Use miniterm, which ships with pyserial, to connect to the serial port at baud rate 115200:

python -m serial.tools.miniterm /dev/ttyUSB0 115200

If you see >>> after pressing Enter, the REPL is ready. Press Ctrl + ] to exit.

5. Light Up the Onboard LED

On most ESP32 dev boards the onboard LED is wired to GPIO2. Just type this into the REPL:

from machine import Pin

led = Pin(2, Pin.OUT)
led.on()

If the light comes on, you’ve got the right pin. If your board uses GPIO5, change 2 to 5 and try again.

Enter the following code to toggle the LED every 0.5 seconds:

from machine import Pin
from time import sleep

led = Pin(2, Pin.OUT)

while True:
led.value(not led.value())
sleep(0.5)

Press Ctrl + C to stop the loop and get back to >>>.

Here’s what it actually looks like:

If you want it to blink automatically as soon as power is applied, save the code as main.py, upload it, and reboot:

from machine import Pin
from time import sleep

led = Pin(2, Pin.OUT)

while True:
led.on()
sleep(0.5)
led.off()
sleep(0.5)

Upload it with mpremote:

pip install mpremote
mpremote connect /dev/ttyUSB0 fs cp main.py :main.py
mpremote connect /dev/ttyUSB0 reset

After the reset the LED blinks on its own — whether or not the serial port is connected.

8. What Learning Alongside AI Feels Like

This whole run — downloading the firmware, flashing it, lighting the LED — I did the entire time by asking AI questions while working through it. When I hit permission issues, download-mode trouble, or an unresponsive REPL, I got feedback instantly. It’s way more fun than sitting in a classroom listening to a teacher lecture. Instant feedback is what keeps you motivated to keep learning.

AI learning interaction 1

AI learning interaction 2

9. Common Issues

  • esptool can’t connect: Most likely it never entered download mode — press BOOT + EN again and retry.
  • No permission on the serial port: sudo chmod 666 /dev/ttyUSB0, or add your user to the dialout group and log in again.
  • REPL not responding: Press Enter first; if main.py contains an infinite loop, hit Ctrl + C to break out of it.
  • The LED doesn’t light up: The onboard LED pin might not be GPIO2 — try GPIO5.

10. References


The overall flow is: download the firmware → erase the flash → flash it → connect to the REPL → control GPIO. MicroPython turns the ESP32 into a little board you can write and run like Python, which will make sensors and small IoT projects much easier down the road.

文章作者:阿文
文章链接: https://www.awen.me/post/93ace45f.html
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 阿文的博客!

评论

0 条评论
😀😃😄 😁😅😂 🤣😊😇 🙂🙃😉 😌😍🥰 😘😗😙 😚😋😛 😝😜🤪 🤨🧐🤓 😎🥸🤩 🥳😏😒 😞😔😟 😕🙁☹️ 😣😖😫 😩🥺😢 😭😤😠 😡🤬🤯 😳🥵🥶 😱😨😰 😥😓🤗 🤔🤭🤫 🤥😶😐 😑😬🙄 😯😦😧 😮😲🥱 😴🤤😪 😵🤐🥴 🤢🤮🤧 😷🤒🤕 🤑🤠😈 👿👹👺 🤡💩👻 💀☠️👽 👾🤖🎃 😺😸😹 😻😼😽 🙀😿😾 👍👎👏 🙌👐🤲 🤝🤜🤛 ✌️🤞🤟 🤘👌🤏 👈👉👆 👇☝️✋ 🤚🖐️🖖 👋🤙💪 🦾🖕✍️ 🙏💅🤳 💯💢💥 💫💦💨 🕳️💣💬 👁️‍🗨️🗨️🗯️ 💭💤❤️ 🧡💛💚 💙💜🖤 🤍🤎💔 ❣️💕💞 💓💗💖 💘💝💟 ☮️✝️☪️ 🕉️☸️✡️ 🔯🕎☯️ ☦️🛐⛎ ♈♉♊ ♋♌♍ ♎♏♐ ♑♒♓ 🆔⚛️🉑 ☢️☣️📴 📳🈶🈚 🈸🈺🈷️ ✴️🆚💮 🉐㊙️㊗️ 🈴🈵🈹 🈲🅰️🅱️ 🆎🆑🅾️ 🆘❌⭕ 🛑⛔📛 🚫💯💢 ♨️🚷🚯 🚳🚱🔞 📵🚭❗ ❕❓❔ ‼️⁉️🔅 🔆〽️⚠️ 🚸🔱⚜️ 🔰♻️✅ 🈯💹❇️ ✳️❎🌐 💠Ⓜ️🌀 💤🏧🚾 ♿🅿️🈳 🈂🛂🛃 🛄🛅🛗 🚀🛸🚁 🚉🚆🚅 ✈️🛫🛬 🛩️💺🛰️
加载中...

留言反馈

😀😃😄 😁😅😂 🤣😊😇 🙂🙃😉 😌😍🥰 😘😗😙 😚😋😛 😝😜🤪 🤨🧐🤓 😎🥸🤩 🥳😏😒 😞😔😟 😕🙁☹️ 😣😖😫 😩🥺😢 😭😤😠 😡🤬🤯 😳🥵🥶 😱😨😰 😥😓🤗 🤔🤭🤫 🤥😶😐 😑😬🙄 😯😦😧 😮😲🥱 😴🤤😪 😵🤐🥴 🤢🤮🤧 😷🤒🤕 🤑🤠😈 👿👹👺 🤡💩👻 💀☠️👽 👾🤖🎃 😺😸😹 😻😼😽 🙀😿😾 👍👎👏 🙌👐🤲 🤝🤜🤛 ✌️🤞🤟 🤘👌🤏 👈👉👆 👇☝️✋ 🤚🖐️🖖 👋🤙💪 🦾🖕✍️ 🙏💅🤳 💯💢💥 💫💦💨 🕳️💣💬 👁️‍🗨️🗨️🗯️ 💭💤❤️ 🧡💛💚 💙💜🖤 🤍🤎💔 ❣️💕💞 💓💗💖 💘💝💟 ☮️✝️☪️ 🕉️☸️✡️ 🔯🕎☯️ ☦️🛐⛎ ♈♉♊ ♋♌♍ ♎♏♐ ♑♒♓ 🆔⚛️🉑 ☢️☣️📴 📳🈶🈚 🈸🈺🈷️ ✴️🆚💮 🉐㊙️㊗️ 🈴🈵🈹 🈲🅰️🅱️ 🆎🆑🅾️ 🆘❌⭕ 🛑⛔📛 🚫💯💢 ♨️🚷🚯 🚳🚱🔞 📵🚭❗ ❕❓❔ ‼️⁉️🔅 🔆〽️⚠️ 🚸🔱⚜️ 🔰♻️✅ 🈯💹❇️ ✳️❎🌐 💠Ⓜ️🌀 💤🏧🚾 ♿🅿️🈳 🈂🛂🛃 🛄🛅🛗 🚀🛸🚁 🚉🚆🚅 ✈️🛫🛬 🛩️💺🛰️