• 物联网开发笔记(7)- 使用Wokwi仿真ESP32开发板实现LED灯点亮、按钮使用


            上面几节我们使用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

    1. from machine import Pin # 导入Pin(开发板中Pin代表GPIO)
    2. from time import sleep_ms # 导入时间
    3. led = Pin(25, Pin.OUT) # 定义一个LED灯,D25脚,输出
    4. while 1:
    5. led.on() # 灯亮
    6. sleep_ms(500) # 等待500毫秒
    7. led.off() # 灯灭
    8. sleep_ms(500)

    diagram.json

    1. {
    2. "version": 1,
    3. "author": "Anonymous maker",
    4. "editor": "wokwi",
    5. "parts": [
    6. {
    7. "type": "wokwi-esp32-devkit-v1",
    8. "id": "esp",
    9. "top": -2.77,
    10. "left": 41.04,
    11. "attrs": { "env": "micropython-20220618-v1.19.1" }
    12. },
    13. {
    14. "type": "wokwi-led",
    15. "id": "led1",
    16. "top": 46.75,
    17. "left": -75.86,
    18. "attrs": { "color": "red" }
    19. },
    20. {
    21. "type": "wokwi-resistor",
    22. "id": "r1",
    23. "top": 142.81,
    24. "left": -36.07,
    25. "attrs": { "value": "470" }
    26. }
    27. ],
    28. "connections": [
    29. [ "esp:TX0", "$serialMonitor:RX", "", [] ],
    30. [ "esp:RX0", "$serialMonitor:TX", "", [] ],
    31. [ "led1:A", "r1:1", "green", [ "v0" ] ],
    32. [ "led1:C", "esp:D25", "green", [ "v4.56", "h-28.75", "v-56.3", "h89.73", "v57.18" ] ],
    33. [ "r1:2", "esp:3V3", "green", [ "v65.54", "h143.57", "v-18.47" ] ]
    34. ]
    35. }

     五、LED连接方法说明

    我们在连接是LED的正极并不一定连接在GPIO上,负极连接在GND上。只不过这是一种习惯性的连接方法。我们可以通过该表连接方法实现不一样的效果。例如:正极通过电阻接到3.3V上,负极接在D25上。代码不变同样可实现LED闪烁。

     仔细观察会发现,效果反过来了,我们把等待的时间调整一下,效果更明显:我们代码期望是亮灯500毫秒,结果实际上是灭灯500毫秒。

    main.py

    1. from machine import Pin # 导入Pin(开发板中Pin代表GPIO)
    2. from time import sleep_ms # 导入时间
    3. led = Pin(25, Pin.OUT) # 定义一个LED灯,D25脚,输出
    4. while 1:
    5. led.on() # 灯亮
    6. sleep_ms(500) # 等待500毫秒
    7. led.off() # 灯灭
    8. sleep_ms(1500)

    diagram.json

    1. {
    2. "version": 1,
    3. "author": "Anonymous maker",
    4. "editor": "wokwi",
    5. "parts": [
    6. {
    7. "type": "wokwi-esp32-devkit-v1",
    8. "id": "esp",
    9. "top": -2.77,
    10. "left": 41.04,
    11. "attrs": { "env": "micropython-20220618-v1.19.1" }
    12. },
    13. {
    14. "type": "wokwi-led",
    15. "id": "led1",
    16. "top": 46.75,
    17. "left": -75.86,
    18. "attrs": { "color": "red" }
    19. },
    20. {
    21. "type": "wokwi-resistor",
    22. "id": "r1",
    23. "top": 142.81,
    24. "left": -36.07,
    25. "attrs": { "value": "470" }
    26. }
    27. ],
    28. "connections": [
    29. [ "esp:TX0", "$serialMonitor:RX", "", [] ],
    30. [ "esp:RX0", "$serialMonitor:TX", "", [] ],
    31. [ "led1:A", "r1:1", "green", [ "v0" ] ],
    32. [ "led1:C", "esp:D25", "green", [ "v4.56", "h-28.75", "v-56.3", "h89.73", "v57.18" ] ],
    33. [ "r1:2", "esp:3V3", "green", [ "v65.54", "h143.57", "v-18.47" ] ]
    34. ]
    35. }

    当我们把正极接到D25上时,输出高电平亮灯,输出低电平灭灯。当把LED接到3.3V上时,这种揭发逻辑刚好反过来,当GPIO输出低电平的时候亮灯。

    我们可以使用LED.value来设置和开始一样的效果。设置LED.value(0),即亮灯的时候输出0。

    1. from machine import Pin # 导入Pin(开发板中Pin代表GPIO)
    2. from time import sleep_ms # 导入时间
    3. led = Pin(25, Pin.OUT) # 定义一个LED灯,D25脚,输出
    4. while 1:
    5. led.value(0) #led.on() # 灯亮
    6. sleep_ms(500) # 等待500毫秒
    7. led.value(1) #led.off() # 灯灭
    8. sleep_ms(1500)

    diagram.json

    1. {
    2. "version": 1,
    3. "author": "Anonymous maker",
    4. "editor": "wokwi",
    5. "parts": [
    6. {
    7. "type": "wokwi-esp32-devkit-v1",
    8. "id": "esp",
    9. "top": -2.77,
    10. "left": 41.04,
    11. "attrs": { "env": "micropython-20220618-v1.19.1" }
    12. },
    13. {
    14. "type": "wokwi-led",
    15. "id": "led1",
    16. "top": 46.75,
    17. "left": -75.86,
    18. "attrs": { "color": "red" }
    19. },
    20. {
    21. "type": "wokwi-resistor",
    22. "id": "r1",
    23. "top": 142.81,
    24. "left": -36.07,
    25. "attrs": { "value": "470" }
    26. }
    27. ],
    28. "connections": [
    29. [ "esp:TX0", "$serialMonitor:RX", "", [] ],
    30. [ "esp:RX0", "$serialMonitor:TX", "", [] ],
    31. [ "led1:A", "r1:1", "green", [ "v0" ] ],
    32. [ "led1:C", "esp:D25", "green", [ "v4.56", "h-28.75", "v-56.3", "h89.73", "v57.18" ] ],
    33. [ "r1:2", "esp:3V3", "green", [ "v65.54", "h143.57", "v-18.47" ] ]
    34. ]
    35. }

    这样就可以看到,输出结果和代码是对应的。想明白了没,不明白的找你师傅问一下哦!

    五、按钮实验

    硬件连接图:

     代码:

    main.py:

    1. from machine import Pin # 导入Pin(开发板中Pin代表GPIO)
    2. from time import sleep_ms # 导入时间
    3. led = Pin(25, Pin.OUT) # 定义一个LED灯,D25脚,输出
    4. key = Pin(21, Pin.IN) # 定义一个按键,D21脚,输入
    5. while 1:
    6. print(key.value())
    7. sleep_ms(500)

    diagram.json

    1. {
    2. "version": 1,
    3. "author": "Anonymous maker",
    4. "editor": "wokwi",
    5. "parts": [
    6. {
    7. "type": "wokwi-esp32-devkit-v1",
    8. "id": "esp",
    9. "top": -6.75,
    10. "left": 9.18,
    11. "attrs": { "env": "micropython-20220618-v1.19.1" }
    12. },
    13. {
    14. "type": "wokwi-led",
    15. "id": "led1",
    16. "top": 46.75,
    17. "left": -75.86,
    18. "attrs": { "color": "red" }
    19. },
    20. {
    21. "type": "wokwi-resistor",
    22. "id": "r1",
    23. "top": 78.75,
    24. "left": -33.98,
    25. "attrs": { "value": "470" }
    26. },
    27. {
    28. "type": "wokwi-pushbutton",
    29. "id": "btn1",
    30. "top": 74.95,
    31. "left": 152.32,
    32. "attrs": { "color": "green" }
    33. },
    34. {
    35. "type": "wokwi-resistor",
    36. "id": "r2",
    37. "top": 147.16,
    38. "left": 146.85,
    39. "attrs": { "value": "470" }
    40. }
    41. ],
    42. "connections": [
    43. [ "esp:TX0", "$serialMonitor:RX", "", [] ],
    44. [ "esp:RX0", "$serialMonitor:TX", "", [] ],
    45. [ "r1:2", "esp:D25", "green", [ "v0" ] ],
    46. [ "r1:1", "led1:A", "green", [ "v0" ] ],
    47. [ "led1:C", "esp:GND.2", "green", [ "v0" ] ],
    48. [ "esp:D21", "btn1:1.l", "green", [ "h0" ] ],
    49. [ "esp:3V3", "r2:1", "green", [ "v0" ] ],
    50. [ "r2:2", "btn1:1.r", "green", [ "v-2.57", "h39.33", "v-75.2" ] ],
    51. [ "btn1:2.r", "esp:GND.1", "green", [ "h7.57", "v35.18", "h-119.06" ] ]
    52. ]
    53. }

    我们通过打印看到,当按键没按时输出1,按键按下时输出0

    请查看如下链接:

    Wokwi Arduino and ESP32 Simulatorhttps://wokwi.com/projects/341691507896484436

     今天就到这,下次我们继续带大家一起学习。谢谢!

  • 相关阅读:
    计算机网络笔记【面试】
    Qt 简约美观的动画 摆钟风格 第十季
    强化学习的一周「GitHub 热点速览」
    网络系统管理 - Server01配置
    qt 在下安装闪退 退出的解决方案
    SOLIDWORKS跨版本升级后卡顿、运行缓慢...如何处理这些情况?
    【汇编】初识,汇编是什么呢?
    80:第七章:开发前台首页、作家个人展示页、粉丝等功能:1:内容概述;(总结了,此前已经开发了的内容;)
    java计算机毕业设计ssm高校会议预约系统(源码+系统+mysql数据库+Lw文档)
    JNA java调用dll
  • 原文地址:https://blog.csdn.net/zhusongziye/article/details/126669348