1 硬件准备
  • ✅ ESP32-S3 开发板
  • ✅ USB 数据线(最好带数据传输功能)
  • ✅ Windows电脑
1.2 软件准备
# 1. 安装 Python(如果还没安装)
# 访问 https://www.python.org/downloads/ 下载安装
# 安装时勾选 "Add Python to PATH"

# 2. 验证Python安装
python --version

# 3. 安装 esptool(刷写工具)
pip install esptool

# 4. 安装串口驱动(根据你的开发板)
# CP210x驱动:https://www.silabs.com/developers/usb-to-uart-bridge-vcp-drivers
# CH340驱动:http://www.wch.cn/download/CH341SER_EXE.html

第二步:下载固件

2.1 下载 MicroPython 固件
  • 官方下载地址:https://micropython.org/download/
  • 选择 ESP32S3 或 GENERIC_S3
  • 下载 .bin 文件(你已经有:ESP32_GENERIC_S3-20251209-v1.27.0.bin

第三步:刷写固件

3.1 连接开发板
  1. 用USB线连接ESP32-S3到电脑
  2. 按住开发板上的 BOOT 按钮
  3. 按一下 RESET 按钮(或重新插拔USB)
  4. 松开 BOOT 按钮
  5. 此时开发板进入刷写模式
3.2 查找串口号
# 查看可用的串口
python -m serial.tools.list_ports
# 或打开设备管理器查看 "端口(COM和LPT)"
# 通常显示为 COM3、COM4 等
3.3 擦除Flash(重要!)
# 替换 COM4 为你的实际端口
python -m esptool --chip esp32s3 --port COM4 erase_flash
3.4 刷写固件
# 注意修改路径和端口
python -m esptool --chip esp32s3 --port COM4 write_flash 0x0 "C:\Users\16673\Desktop\ESP32_GENERIC_S3-20251209-v1.27.0.bin"

第四步:安装开发环境

4.1 安装 Thonny IDE(推荐新手)
  1. 下载地址:https://thonny.org/
  2. 安装后打开
  3. 配置:
    • 菜单栏:运行 → 选择解释器
    • 选择 MicroPython (ESP32)
    • 端口选择 COM4(或你的端口)
4.2 或安装命令行工具
# 安装 ampy(文件传输)
pip install adafruit-ampy

# 安装 rshell(交互式shell)
pip install rshell

# 安装 picocom/mRemoteNG(串口工具)

第五步:测试连接

5.1 使用 Thonny 测试
# 在Thonny的Shell中看到 >>> 提示符后输入:
print("Hello ESP32-S3!")

# 查看固件信息
import sys
print(sys.version)
print(sys.implementation)
5.2 使用命令行测试
# 使用 rshell 连接
rshell -p COM4

# 或在Python中直接连接
python
>>> import serial
>>> ser = serial.Serial('COM4', 115200)
>>> ser.write(b'\r\n')
>>> ser.readline()

第六步:基础示例代码

6.1 第一个程序:LED闪烁
# main.py - 保存到ESP32上会自动运行
from machine import Pin
import time

# ESP32-S3 板载LED通常在引脚 48(不同板子可能不同)
led = Pin(48, Pin.OUT)

while True:
    led.value(1)  # 点亮
    time.sleep(0.5)
    led.value(0)  # 熄灭
    time.sleep(0.5)
6.2 上传代码到ESP32
# 使用 ampy 上传
ampy --port COM4 put main.py

# 或使用 Thonny:
# 文件 → 保存 → MicroPython设备

第七步:进阶功能

7.1 WiFi连接
# wifi_connect.py
import network
import time

def connect_wifi(ssid, password):
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    
    if not wlan.isconnected():
        print('正在连接WiFi...')
        wlan.connect(ssid, password)
        
        # 等待连接
        timeout = 10
        while not wlan.isconnected() and timeout > 0:
            time.sleep(1)
            timeout -= 1
            print(f'等待中... {timeout}s')
    
    if wlan.isconnected():
        print('WiFi连接成功!')
        print('IP地址:', wlan.ifconfig()[0])
        return True
    else:
        print('WiFi连接失败!')
        return False

# 使用
connect_wifi('你的WiFi名称', '你的WiFi密码')
7.2 Web服务器示例
# webserver.py
import socket
import network

# 连接WiFi(接上面的代码)
connect_wifi('你的WiFi', '你的密码')

# 创建简单的Web服务器
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)

print('服务器地址:', addr)

while True:
    cl, addr = s.accept()
    print('客户端连接:', addr)
    
    request = cl.recv(1024)
    print('请求内容:', request)
    
    # 发送HTTP响应
    response = """HTTP/1.1 200 OK
Content-Type: text/html

<html>
<head><title>ESP32-S3</title></head>
<body>
<h1>Hello from ESP32-S3!</h1>
<p>MicroPython Web Server</p>
</body>
</html>
"""
    cl.send(response)
    cl.close()
7.3 读取传感器
# sensor.py
from machine import Pin, ADC
import time

# 读取模拟值(ADC)
adc = ADC(Pin(4))  # 使用GPIO4作为ADC
adc.atten(ADC.ATTN_11DB)  # 设置量程 0-3.3V

while True:
    # 读取数字值 (0-4095)
    value = adc.read()
    
    # 转换为电压
    voltage = value * 3.3 / 4095
    
    print(f'ADC值: {value}, 电压: {voltage:.2f}V')
    time.sleep(1)

第八步:实用技巧

8.1 开机自动运行
  • 将主程序保存为 main.py 到ESP32
  • 重启开发板会自动运行
8.2 释放内存
import gc
gc.collect()  # 手动垃圾回收
gc.mem_free()  # 查看剩余内存
8.3 文件管理命令
# 列出文件
ampy --port COM4 ls

# 删除文件
ampy --port COM4 rm main.py

# 下载文件到电脑
ampy --port COM4 get main.py > backup_main.py

常见问题解决

  1. 无法连接COM口

    • 检查驱动是否正确安装
Logo

这里是“一人公司”的成长家园。我们提供从产品曝光、技术变现到法律财税的全栈内容,并连接云服务、办公空间等稀缺资源,助你专注创造,无忧运营。

更多推荐