当前位置:   article > 正文

物联网开发笔记(8)- 使用Wokwi仿真ESP32开发板实现模数转换和脉宽调制

wokwi

        先看下ESP32开发板上哪些脚支持数模转换,途中紫色标记的ADC针脚都是支持的,在实际的使用中ADC1是最稳定的,GPIO36和GPIO39因为涉及到系统的启动问题,一般不会使用,用的比较多的是GPIO34和GPIO35。GPIO32、GPIO33、GPIO25、GPIO26也是可使用的,但是它涉及到触摸和一些其他功能,所以我们选择GPIO34和GPIO35。GPIO34和GPIO35作为普通GPIO只能输入不能输出。所以首选它作为模数转换的端口是比较合适的。

Wokwi网站上ESP32开发板是ESP32 Devkit V1版本的。

  一、打开Wokwi网站,选择Micropython on ESP32开发板,添加一个滑动电位器

我们点击滑动电位器上方的问号,查看它的使用手册

wokwi-slide-potentiometer Reference | Wokwi DocsSliding variable resistor (linear potentiometer)https://docs.wokwi.com/parts/wokwi-slide-potentiometer我们看到,滑动电位器的初始值是0,我们可以改变他的长度来调节它的大小。

 关于ESP32的ADC介绍我们查看官方手册:

Quick reference for the ESP32 — MicroPython 1.19.1 documentationhttp://docs.micropython.org/en/latest/esp32/quickref.html#adc-analog-to-digital-conversion二、打印电位器ADC值

main.py

  1. from machine import Pin,ADC
  2. from time import sleep_ms
  3. port = ADC(Pin(34))
  4. while 1:
  5. print(port.read())
  6. 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": -44.67,
  10. "left": 19.33,
  11. "attrs": { "env": "micropython-20220618-v1.19.1" }
  12. },
  13. {
  14. "type": "wokwi-slide-potentiometer",
  15. "id": "pot1",
  16. "top": 163.5,
  17. "left": -20.21,
  18. "attrs": { "travelLength": "60" }
  19. }
  20. ],
  21. "connections": [
  22. [ "esp:TX0", "$serialMonitor:RX", "", [] ],
  23. [ "esp:RX0", "$serialMonitor:TX", "", [] ],
  24. [ "esp:D34", "pot1:SIG", "green", [ "h-99.9", "v10.14" ] ],
  25. [ "esp:3V3", "pot1:VCC", "green", [ "v-0.6", "h17.14", "v54.67", "h-173.33" ] ],
  26. [ "esp:GND.1", "pot1:GND", "black", [ "h0" ] ]
  27. ]
  28. }

滑动电位器查看效果,我们看到输出从0到4095,默认是12位(2的12次方-1)。

也可以在线查看效果哦,点击下方链接

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

当然我们可以选择设置想要的位数,只需要加上这句话

  1. from machine import Pin,ADC
  2. from time import sleep_ms
  3. port = ADC(Pin(34))
  4. ADC.width(ADC.WIDTH_9BIT) # 设置输出位数
  5. while 1:
  6. print(port.read())
  7. sleep_ms(500)

 三、脉宽调试

关于什么叫做脉宽调制(PWM)这里就不在说明,大家自行百度学习。

首先我们在Wokwi上选择Miropython with ESP32,打开开发网页。

同事查看ESP32 的官方文档关于PWM的描述:

Quick reference for the ESP32 — MicroPython 1.19.1 documentationhttp://docs.micropython.org/en/latest/esp32/quickref.html#adc-analog-to-digital-conversion接下来我们看代码:

main.py

  1. # 脉宽调制例子
  2. # 通过滑动电位器产生不同的ADC值来调节LED灯的占空比
  3. # 使LED产生不同的亮度
  4. from machine import Pin,ADC,PWM
  5. from time import sleep_ms
  6. led = PWM(Pin(25)) # 定义PWM在端口25
  7. adc = ADC(Pin(34)) # 定义ADC在端口34
  8. # LED灯的亮度不能超过1023,否则跑出异常duty must be from 0 to 1023
  9. adc.width(ADC.WIDTH_10BIT) # 设置ADC的位数为9(0-1023)
  10. while True:
  11. value = adc.read() # 读取端口34的ADC值
  12. led.duty(value) # 设置PWM波的占空比
  13. print(value) # 打印PWM波的占空比
  14. sleep_ms(300)

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": 0,
  10. "left": 0,
  11. "attrs": { "env": "micropython-20220618-v1.19.1" }
  12. },
  13. {
  14. "type": "wokwi-slide-potentiometer",
  15. "id": "pot1",
  16. "top": 187.5,
  17. "left": -46.63,
  18. "attrs": { "travelLength": "30" }
  19. },
  20. {
  21. "type": "wokwi-led",
  22. "id": "led1",
  23. "top": 48.89,
  24. "left": -134.23,
  25. "attrs": { "color": "red" }
  26. },
  27. {
  28. "type": "wokwi-resistor",
  29. "id": "r1",
  30. "top": 81.93,
  31. "left": -76.59,
  32. "attrs": { "value": "470" }
  33. }
  34. ],
  35. "connections": [
  36. [ "esp:TX0", "$serialMonitor:RX", "", [] ],
  37. [ "esp:RX0", "$serialMonitor:TX", "", [] ],
  38. [ "esp:D25", "r1:2", "green", [ "h0" ] ],
  39. [ "r1:1", "led1:A", "green", [ "v0" ] ],
  40. [ "led1:C", "esp:GND.2", "green", [ "v0" ] ],
  41. [ "pot1:VCC", "esp:3V3", "red", [ "h-8.16", "v-26.33", "h172.71", "v-42.74" ] ],
  42. [ "pot1:GND", "esp:GND.1", "black", [ "v0" ] ],
  43. [ "pot1:SIG", "esp:D34", "green", [ "h-98.01", "v-209.46" ] ]
  44. ]
  45. }

查看效果

或者通过网页查看效果

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

我们在加个蜂鸣器看个小效果哈:

蜂鸣器buzzer的使用文档见:

wokwi-buzzer Reference | Wokwi DocsA piezoelectric buzzerhttps://docs.wokwi.com/parts/wokwi-buzzer

main.py

  1. # 脉宽调制例子
  2. # 通过滑动电位器产生不同的ADC值来调节LED灯的占空比
  3. # 使LED产生不同的亮度
  4. from machine import Pin,ADC,PWM
  5. from time import sleep_ms
  6. buzzer = PWM(Pin(14)) #定义蜂鸣器的PWM在端口14
  7. buzzer.duty(512) # 设置蜂鸣器的占空比
  8. led = PWM(Pin(25)) # 定义PWM在端口25
  9. adc = ADC(Pin(34)) # 定义ADC在端口34
  10. # LED灯的亮度不能超过1023,否则跑出异常duty must be from 0 to 1023
  11. adc.width(ADC.WIDTH_10BIT) # 设置ADC的位数为9(0-1023)
  12. while True:
  13. value = adc.read() # 读取端口34的ADC值
  14. led.duty(value) # 设置PWM波的占空比
  15. # 此处加1是因为最小频率是1HZ,否则抛出异常freqency must be from 1Hz to 40MHz
  16. buzzer.freq(value + 1 )
  17. print(value) # 打印PWM波的占空比
  18. sleep_ms(300)

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": 0,
  10. "left": 0,
  11. "attrs": { "env": "micropython-20220618-v1.19.1" }
  12. },
  13. {
  14. "type": "wokwi-slide-potentiometer",
  15. "id": "pot1",
  16. "top": 190.76,
  17. "left": -57.51,
  18. "attrs": { "travelLength": "30" }
  19. },
  20. {
  21. "type": "wokwi-led",
  22. "id": "led1",
  23. "top": 48.89,
  24. "left": -134.23,
  25. "attrs": { "color": "red" }
  26. },
  27. {
  28. "type": "wokwi-resistor",
  29. "id": "r1",
  30. "top": 81.93,
  31. "left": -76.59,
  32. "attrs": { "value": "470" }
  33. },
  34. {
  35. "type": "wokwi-buzzer",
  36. "id": "bz1",
  37. "top": 28.37,
  38. "left": -228.1,
  39. "attrs": { "volume": "0.1" }
  40. }
  41. ],
  42. "connections": [
  43. [ "esp:TX0", "$serialMonitor:RX", "", [] ],
  44. [ "esp:RX0", "$serialMonitor:TX", "", [] ],

查看效果:

也可用网页查看哦~~~

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/378713
推荐阅读
相关标签
  

闽ICP备14008679号