![cover](https://img-blog.csdnimg.cn/584b2a7ce8d040118b20f2d3214cfeb0.jpeg)
物联网开发108 - Micropython ESP32 C3-ADC采集模拟量滑动电位器&PWM调速舵机角度控制
使用ESP32 C3,连接0.96寸ssd1306屏幕、MG90S舵机、Motor Module直流电机带扇叶PWM调速小风扇模块,滑动变阻器模块来搭建我们的环境。最后,通过滑动电阻器的改变开控制舵机转动的角度和马达的转速,并将滑动变阻器的模拟值、舵机转动的角度和马达的电压值显示在屏幕上。
一、目的
这一节我们来学习如何使用ESP32 C3,连接0.96寸ssd1306屏幕、MG90S舵机、Motor Module直流电机带扇叶PWM调速小风扇模块,滑动变阻器模块来搭建我们的环境。最后,通过滑动电阻器的改变开控制舵机转动的角度和马达的转速,并将滑动变阻器的模拟值、舵机转动的角度和马达的电压值显示在屏幕上。
二、环境
ESP32 C3开发板(MicroPython v1.19.1 on 2022-06-18)+ I2C.96寸ssd1306屏幕模块+ MG90S舵机 + PWM调速小风扇模块 + 几根杜邦线 + Win10商业
ESP32和各个模块接线方法:
三、环境
adc.py
from machine import Pin,SoftI2C,PWM,ADC
from ssd1306 import SSD1306_I2C
from ufont import BMFont
import time
i2c = SoftI2C(scl = Pin(2),sda = Pin(3),freq = 400_000)
oled = SSD1306_I2C(128,64,i2c,0x3c)
#freq = 40Mhz duty = 1023 4095 获取的值/最带宽带12bit4095 * 1023
motora = PWM(Pin(8),duty=(0),freq = 4095) #4.095khz #风扇
motorb = PWM(Pin(6),duty=(0),freq = 50) #50hz 1/20=50 #舵机
font = BMFont("fonts/unifont-14-12888-16.v3.bmf")
adc = ADC(Pin(5))
adc.atten(ADC.ATTN_11DB)
adc.width(ADC.WIDTH_12BIT) #4095
def main():
font.text(oled,"电压值:",0,16, color=1,font_size=16,reverse=False,clear=False,show=True,half_char=True, auto_wrap=True)
font.text(oled,"模拟值:",0,32, color=1,font_size=16,reverse=False,clear=False,show=True,half_char=True, auto_wrap=True)
font.text(oled,"角度值:",0,48, color=1,font_size=16,reverse=False,clear=False,show=True,half_char=True, auto_wrap=True)
while True:
temp = adc.read()
c = int(temp/22.75) #0-180
motorb.duty(int((c/180 * 2 + 0.5)/20*1023))
motora.duty(int((temp/4095)*1023))
font.text(oled," %.1f V"%((temp/4095)*3.3),60,16, color=1,font_size=16,reverse=False,clear=False,show=True,half_char=True, auto_wrap=True)
font.text(oled,"%4d mV"%(temp),60,32,color=1,font_size=16,reverse=False,clear=False,show=True,half_char=True, auto_wrap=True)
font.text(oled," %3d 度"%(c),60,48,color=1,font_size=16,reverse=False,clear=False,show=True,half_char=True, auto_wrap=True)
#print("%d"%(int((temp/4095)*1023))) #0-3.3v
#print("%d mv %.1f v"%(temp,(temp/4095)*3.3)) #0-3.3v
if __name__ == "__main__":
main()
演示效果:
四、ssd1306屏幕驱动和字库文件
请从下文获取:
1,滑动变阻器
电子积木 滑动电位器 滑调电位器模块
提取码:s4hj
采用优质滑动电器,性能稳定可靠
双路模拟量输出,输出0-VCC模拟量电压信号
尺寸:90*20mm
重量:20g
电压:3.3V、5V
端口:模拟量
阻值:10K
2,PWM马达
Motor Module 直流电机带扇叶PWM调速小风扇模块 PH2.0-3pin接口
3,舵机
SG90/5010 MG90s MG995/996R舵机直升机9g遥控飞机马达固定翼航模
资料例程链接:https://pan.baidu.com/s/1QsTIKnoQsOTCkeYLLTTjTA?pwd=8889
提取码:8889
测试视频链接:
https://v.youku.com/v_show/id_XNTg0NzI4NTkwOA==.html
更多推荐
所有评论(0)