上面几节我们使用Micrpython在Wokwi网站上实现了树莓派Pico开发板的仿真。学习了树莓派Pico的LED闪灯、按键操作等。以及Wokwi的使用,比如选中元器件后,按键盘“R”键切换方向,按键盘“Backspace”或者“Delete”删除原件,鼠标滚轮进行放大缩小,点击元器件上方的问号查看该元器件的使用说明等等。还有我们在编辑代码时,Wokwi默认按一次Tab键是两个空格,这里建议按两此tab键,即4个空格。当然默认也是可以的。
今天给大家分享一下ESP32 开发板,有了以上基础可以轻松入手。当然没有Pico开发板基础处按照我的讲解,同样也非常容易的上手。
一、环境搭建
很简单,首先打开Wokwi的网站
Wokwi - Online Arduino and ESP32 SimulatorArduino Simulator: Uno, Mega, ESP32, FastLED, LCD1602, Servo, Raspberry Pi Pico, Sensors. Designed for makers, by makers.https://wokwi.com/二、选择Micropython ESP32进行开发
附上ESP32开发板的针脚图:
三、然后进到如下开发界面
在右侧我们看到main.py文件是用来编辑我们的代码的。它旁边的diagram.json文件是用来显示和编辑硬件开发板和元器件的。保存我们编辑的代码可以使用Ctrl+s或者也可以点击左上方的保存按钮。你也可以分享你的项目给别人,通过SHARE按钮。再看右边,绿色的按钮是运行,加号按钮可以添加元器件到我们的开发板中,三个点按钮是其他的一些方便使用的功能。
四、点灯实验
我们还是从基础的点灯实验开始。
我们首先通过“+”加号按钮添加一个LED灯和一个电阻。我们通过diagram.json可以改变电阻的阻值。找到后我们把阻值改为470,默认是1000欧姆。
ESP32开发板GPIO34口和GPIO35口只能输入,不能作为输出(D34和D35脚)。
这里我们在LED灯正极(弯脚的那个脚是正极)接一个470欧姆的电阻,然后电阻的另一端连叫道GPIO25口上(D25脚),然后LED的负极接到开发板的GND上。
然后编写代码:
main.py
- from machine import Pin # 导入Pin(开发板中Pin代表GPIO)
- from time import sleep_ms # 导入时间
-
- led = Pin(25, Pin.OUT) # 定义一个LED灯,D25脚,输出
- while 1:
- led.on() # 灯亮
- sleep_ms(500) # 等待500毫秒
- led.off() # 灯灭
- sleep_ms(500)
diagram.json
- {
- "version": 1,
- "author": "Anonymous maker",
- "editor": "wokwi",
- "parts": [
- {
- "type": "wokwi-esp32-devkit-v1",
- "id": "esp",
- "top": -2.77,
- "left": 41.04,
- "attrs": { "env": "micropython-20220618-v1.19.1" }
- },
- {
- "type": "wokwi-led",
- "id": "led1",
- "top": 46.75,
- "left": -75.86,
- "attrs": { "color": "red" }
- },
- {
- "type": "wokwi-resistor",
- "id": "r1",
- "top": 142.81,
- "left": -36.07,
- "attrs": { "value": "470" }
- }
- ],
- "connections": [
- [ "esp:TX0", "$serialMonitor:RX", "", [] ],
- [ "esp:RX0", "$serialMonitor:TX", "", [] ],
- [ "led1:A", "r1:1", "green", [ "v0" ] ],
- [ "led1:C", "esp:D25", "green", [ "v4.56", "h-28.75", "v-56.3", "h89.73", "v57.18" ] ],
- [ "r1:2", "esp:3V3", "green", [ "v65.54", "h143.57", "v-18.47" ] ]
- ]
- }
五、LED连接方法说明
我们在连接是LED的正极并不一定连接在GPIO上,负极连接在GND上。只不过这是一种习惯性的连接方法。我们可以通过该表连接方法实现不一样的效果。例如:正极通过电阻接到3.3V上,负极接在D25上。代码不变同样可实现LED闪烁。
仔细观察会发现,效果反过来了,我们把等待的时间调整一下,效果更明显:我们代码期望是亮灯500毫秒,结果实际上是灭灯500毫秒。
main.py
- from machine import Pin # 导入Pin(开发板中Pin代表GPIO)
- from time import sleep_ms # 导入时间
-
- led = Pin(25, Pin.OUT) # 定义一个LED灯,D25脚,输出
- while 1:
- led.on() # 灯亮
- sleep_ms(500) # 等待500毫秒
- led.off() # 灯灭
- sleep_ms(1500)
diagram.json
- {
- "version": 1,
- "author": "Anonymous maker",
- "editor": "wokwi",
- "parts": [
- {
- "type": "wokwi-esp32-devkit-v1",
- "id": "esp",
- "top": -2.77,
- "left": 41.04,
- "attrs": { "env": "micropython-20220618-v1.19.1" }
- },
- {
- "type": "wokwi-led",
- "id": "led1",
- "top": 46.75,
- "left": -75.86,
- "attrs": { "color": "red" }
- },
- {
- "type": "wokwi-resistor",
- "id": "r1",
- "top": 142.81,
- "left": -36.07,
- "attrs": { "value": "470" }
- }
- ],
- "connections": [
- [ "esp:TX0", "$serialMonitor:RX", "", [] ],
- [ "esp:RX0", "$serialMonitor:TX", "", [] ],
- [ "led1:A", "r1:1", "green", [ "v0" ] ],
- [ "led1:C", "esp:D25", "green", [ "v4.56", "h-28.75", "v-56.3", "h89.73", "v57.18" ] ],
- [ "r1:2", "esp:3V3", "green", [ "v65.54", "h143.57", "v-18.47" ] ]
- ]
- }
当我们把正极接到D25上时,输出高电平亮灯,输出低电平灭灯。当把LED接到3.3V上时,这种揭发逻辑刚好反过来,当GPIO输出低电平的时候亮灯。
我们可以使用LED.value来设置和开始一样的效果。设置LED.value(0),即亮灯的时候输出0。
- from machine import Pin # 导入Pin(开发板中Pin代表GPIO)
- from time import sleep_ms # 导入时间
-
- led = Pin(25, Pin.OUT) # 定义一个LED灯,D25脚,输出
- while 1:
- led.value(0) #led.on() # 灯亮
- sleep_ms(500) # 等待500毫秒
- led.value(1) #led.off() # 灯灭
- sleep_ms(1500)
diagram.json
- {
- "version": 1,
- "author": "Anonymous maker",
- "editor": "wokwi",
- "parts": [
- {
- "type": "wokwi-esp32-devkit-v1",
- "id": "esp",
- "top": -2.77,
- "left": 41.04,
- "attrs": { "env": "micropython-20220618-v1.19.1" }
- },
- {
- "type": "wokwi-led",
- "id": "led1",
- "top": 46.75,
- "left": -75.86,
- "attrs": { "color": "red" }
- },
- {
- "type": "wokwi-resistor",
- "id": "r1",
- "top": 142.81,
- "left": -36.07,
- "attrs": { "value": "470" }
- }
- ],
- "connections": [
- [ "esp:TX0", "$serialMonitor:RX", "", [] ],
- [ "esp:RX0", "$serialMonitor:TX", "", [] ],
- [ "led1:A", "r1:1", "green", [ "v0" ] ],
- [ "led1:C", "esp:D25", "green", [ "v4.56", "h-28.75", "v-56.3", "h89.73", "v57.18" ] ],
- [ "r1:2", "esp:3V3", "green", [ "v65.54", "h143.57", "v-18.47" ] ]
- ]
- }
这样就可以看到,输出结果和代码是对应的。想明白了没,不明白的找你师傅问一下哦!
五、按钮实验
硬件连接图:
代码:
main.py:
- from machine import Pin # 导入Pin(开发板中Pin代表GPIO)
- from time import sleep_ms # 导入时间
-
- led = Pin(25, Pin.OUT) # 定义一个LED灯,D25脚,输出
- key = Pin(21, Pin.IN) # 定义一个按键,D21脚,输入
- while 1:
- print(key.value())
- sleep_ms(500)
diagram.json
- {
- "version": 1,
- "author": "Anonymous maker",
- "editor": "wokwi",
- "parts": [
- {
- "type": "wokwi-esp32-devkit-v1",
- "id": "esp",
- "top": -6.75,
- "left": 9.18,
- "attrs": { "env": "micropython-20220618-v1.19.1" }
- },
- {
- "type": "wokwi-led",
- "id": "led1",
- "top": 46.75,
- "left": -75.86,
- "attrs": { "color": "red" }
- },
- {
- "type": "wokwi-resistor",
- "id": "r1",
- "top": 78.75,
- "left": -33.98,
- "attrs": { "value": "470" }
- },
- {
- "type": "wokwi-pushbutton",
- "id": "btn1",
- "top": 74.95,
- "left": 152.32,
- "attrs": { "color": "green" }
- },
- {
- "type": "wokwi-resistor",
- "id": "r2",
- "top": 147.16,
- "left": 146.85,
- "attrs": { "value": "470" }
- }
- ],
- "connections": [
- [ "esp:TX0", "$serialMonitor:RX", "", [] ],
- [ "esp:RX0", "$serialMonitor:TX", "", [] ],
- [ "r1:2", "esp:D25", "green", [ "v0" ] ],
- [ "r1:1", "led1:A", "green", [ "v0" ] ],
- [ "led1:C", "esp:GND.2", "green", [ "v0" ] ],
- [ "esp:D21", "btn1:1.l", "green", [ "h0" ] ],
- [ "esp:3V3", "r2:1", "green", [ "v0" ] ],
- [ "r2:2", "btn1:1.r", "green", [ "v-2.57", "h39.33", "v-75.2" ] ],
- [ "btn1:2.r", "esp:GND.1", "green", [ "h7.57", "v35.18", "h-119.06" ] ]
- ]
- }
我们通过打印看到,当按键没按时输出1,按键按下时输出0
请查看如下链接:
Wokwi Arduino and ESP32 Simulatorhttps://wokwi.com/projects/341691507896484436
今天就到这,下次我们继续带大家一起学习。谢谢!