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

        先看下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

from machine import Pin,ADC
from time import sleep_ms

port = ADC(Pin(34))

while 1:
    print(port.read())
    sleep_ms(500)

diagram.json

{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    {
      "type": "wokwi-esp32-devkit-v1",
      "id": "esp",
      "top": -44.67,
      "left": 19.33,
      "attrs": { "env": "micropython-20220618-v1.19.1" }
    },
    {
      "type": "wokwi-slide-potentiometer",
      "id": "pot1",
      "top": 163.5,
      "left": -20.21,
      "attrs": { "travelLength": "60" }
    }
  ],
  "connections": [
    [ "esp:TX0", "$serialMonitor:RX", "", [] ],
    [ "esp:RX0", "$serialMonitor:TX", "", [] ],
    [ "esp:D34", "pot1:SIG", "green", [ "h-99.9", "v10.14" ] ],
    [ "esp:3V3", "pot1:VCC", "green", [ "v-0.6", "h17.14", "v54.67", "h-173.33" ] ],
    [ "esp:GND.1", "pot1:GND", "black", [ "h0" ] ]
  ]
}

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

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

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

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

from machine import Pin,ADC
from time import sleep_ms

port = ADC(Pin(34))
ADC.width(ADC.WIDTH_9BIT)  # 设置输出位数
while 1:
    print(port.read())
    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

# 脉宽调制例子
# 通过滑动电位器产生不同的ADC值来调节LED灯的占空比
# 使LED产生不同的亮度
from machine import Pin,ADC,PWM
from time import sleep_ms

led = PWM(Pin(25))  # 定义PWM在端口25
adc = ADC(Pin(34))  # 定义ADC在端口34
# LED灯的亮度不能超过1023,否则跑出异常duty must be from 0 to 1023
adc.width(ADC.WIDTH_10BIT)  # 设置ADC的位数为9(0-1023)

while True:
    value = adc.read()  # 读取端口34的ADC值
    led.duty(value)  # 设置PWM波的占空比
    print(value)   # 打印PWM波的占空比
    sleep_ms(300)

diagram.json

{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    {
      "type": "wokwi-esp32-devkit-v1",
      "id": "esp",
      "top": 0,
      "left": 0,
      "attrs": { "env": "micropython-20220618-v1.19.1" }
    },
    {
      "type": "wokwi-slide-potentiometer",
      "id": "pot1",
      "top": 187.5,
      "left": -46.63,
      "attrs": { "travelLength": "30" }
    },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": 48.89,
      "left": -134.23,
      "attrs": { "color": "red" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r1",
      "top": 81.93,
      "left": -76.59,
      "attrs": { "value": "470" }
    }
  ],
  "connections": [
    [ "esp:TX0", "$serialMonitor:RX", "", [] ],
    [ "esp:RX0", "$serialMonitor:TX", "", [] ],
    [ "esp:D25", "r1:2", "green", [ "h0" ] ],
    [ "r1:1", "led1:A", "green", [ "v0" ] ],
    [ "led1:C", "esp:GND.2", "green", [ "v0" ] ],
    [ "pot1:VCC", "esp:3V3", "red", [ "h-8.16", "v-26.33", "h172.71", "v-42.74" ] ],
    [ "pot1:GND", "esp:GND.1", "black", [ "v0" ] ],
    [ "pot1:SIG", "esp:D34", "green", [ "h-98.01", "v-209.46" ] ]
  ]
}

查看效果

或者通过网页查看效果

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

# 脉宽调制例子
# 通过滑动电位器产生不同的ADC值来调节LED灯的占空比
# 使LED产生不同的亮度
from machine import Pin,ADC,PWM
from time import sleep_ms

buzzer = PWM(Pin(14))  #定义蜂鸣器的PWM在端口14
buzzer.duty(512) # 设置蜂鸣器的占空比
led = PWM(Pin(25))  # 定义PWM在端口25
adc = ADC(Pin(34))  # 定义ADC在端口34
# LED灯的亮度不能超过1023,否则跑出异常duty must be from 0 to 1023
adc.width(ADC.WIDTH_10BIT)  # 设置ADC的位数为9(0-1023)

while True:
    value = adc.read()  # 读取端口34的ADC值
    led.duty(value)  # 设置PWM波的占空比
    # 此处加1是因为最小频率是1HZ,否则抛出异常freqency must be from 1Hz to 40MHz
    buzzer.freq(value + 1 ) 
    print(value)   # 打印PWM波的占空比
    sleep_ms(300)

diagram.json

{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    {
      "type": "wokwi-esp32-devkit-v1",
      "id": "esp",
      "top": 0,
      "left": 0,
      "attrs": { "env": "micropython-20220618-v1.19.1" }
    },
    {
      "type": "wokwi-slide-potentiometer",
      "id": "pot1",
      "top": 190.76,
      "left": -57.51,
      "attrs": { "travelLength": "30" }
    },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": 48.89,
      "left": -134.23,
      "attrs": { "color": "red" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r1",
      "top": 81.93,
      "left": -76.59,
      "attrs": { "value": "470" }
    },
    {
      "type": "wokwi-buzzer",
      "id": "bz1",
      "top": 28.37,
      "left": -228.1,
      "attrs": { "volume": "0.1" }
    }
  ],
  "connections": [
    [ "esp:TX0", "$serialMonitor:RX", "", [] ],
    [ "esp:RX0", "$serialMonitor:TX", "", [] ],
 

查看效果:

也可用网页查看哦~~~

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

  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

魔都飘雪

您的1毛奖励是我创作的源源动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值