物联网开发110 - Micropython ESP32 C3连接独立按键-PWM马达-ssd1306实验_esp32 python 电机pw

222 阅读4分钟

二、环境

        ESP32 C3开发板(MicroPython v1.19.1 on 2022-06-18)+ 独立按键模块 + I2C.96寸ssd1306屏幕模块+  led灯 + PWM调速小风扇模块 + 几根杜邦线 + Win10商业

        ESP32和各个模块接线方法:

 

 

三、实验代码

from machine import Pin,I2C,PWM
from ssd1306 import SSD1306_I2C
from ufont import BMFont
import time

# 创建I2C对象
i2c  = I2C(0,scl = Pin(5),sda = Pin(4),freq = 400_000)
# 创建oled屏幕对象
oled = SSD1306_I2C(128,64,i2c,0x3c)

# 挂载字体文件
font = BMFont("fonts/unifont-14-12888-16.v3.bmf")  

# 定义上、下、左、右、中、SET、RST七个按键
UP = Pin(10,Pin.IN,Pin.PULL_UP)
DWN = Pin(6,Pin.IN,Pin.PULL_UP)
LFT = Pin(7,Pin.IN,Pin.PULL_UP)
RHT = Pin(9,Pin.IN,Pin.PULL_UP)
MID = Pin(2,Pin.IN,Pin.PULL_UP)
SET = Pin(3,Pin.IN,Pin.PULL_UP)
RST = Pin(8,Pin.IN,Pin.PULL_UP)

# 定义led灯对象
led = Pin(13,Pin.OUT)

def Key():  
    # "up","down","left ","right","middle","set","rst"
    key = 1
    if (key == 1 and (UP.value() == 0 or DWN.value() == 0 or LFT.value() == 0 or RHT.value() == 0) or MID.value() == 0 or SET.value() == 0 or RST.value() == 0):
        
        time.sleep(0.020)
        key = 0
        
        if UP.value() == 0:
            return "upper"
        elif DWN.value() == 0:
            return "down "
        elif LFT.value() == 0:
            return "Left "
        elif RHT.value() == 0:
            return "right"
        elif MID.value() == 0:
            return "middle"
        elif SET.value() == 0:
            return "set   "
        elif RST.value() == 0:
            return "rst   "
        
    if(not(key == 0 and (UP.value() == 0 or DWN.value() == 0 or LFT.value() == 0 or RHT.value() == 0) or MID.value() == 0 or SET.value() == 0 or RST.value() == 0)):
        key = 1
    
    return -1

def dispaly():
    #code = ["upper","down","Left ","right","middle","set","rst"]
    #code = ("upper","down","Left ","right","middle","set","rst")
    code = {
            0:"upper",
            1:"down ",
            2:"Left ",
            3:"right",
            4:"middle",
            5:"set   ",
            6:"rst   ",
           }
    
    if Key() == code[0]:
        font.text(oled,"%s"%(code[0]),32,20,color=1,font_size=24,reverse=False,clear=False,show=True,half_char=True, auto_wrap=True)
    elif Key() == code[1]:
        font.text(oled,"%s"%(code[1]),32,20,color=1,font_size=24,reverse=False,clear=False,show=True,half_char=True, auto_wrap=True)
    elif Key() == code[2]:
        font.text(oled,"%s"%(code[2]),32,20,color=1,font_size=24,reverse=False,clear=False,show=True,half_char=True, auto_wrap=True)
    elif Key() == code[3]:
        font.text(oled,"%s"%(code[3]),32,20,color=1,font_size=24,reverse=False,clear=False,show=True,half_char=True, auto_wrap=True)
    elif Key() == code[4]:
        font.text(oled,"%s"%(code[4]),32,20,color=1,font_size=24,reverse=False,clear=False,show=True,half_char=True, auto_wrap=True)
    elif Key() == code[5]:
        font.text(oled,"%s"%(code[5]),32,20,color=1,font_size=24,reverse=False,clear=False,show=True,half_char=True, auto_wrap=True)
    elif Key() == code[6]:
        font.text(oled,"%s"%(code[6]),32,20,color=1,font_size=24,reverse=False,clear=False,show=True,half_char=True, auto_wrap=True)

# 创建LED函数
def LED():
    if Key() == "middle":  # 当按下mid键时,反转LED状态
        led.value(not led.value())
        
#         while not MID.value():
#             pass

# 创建motor函数
def motor():
    # 定义PWM小风扇马达对象
文末有福利领取哦~
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

👉**一、Python所有方向的学习路线**

Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。![img](https://p9-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/28e91d8f45dd4386ba54c4fa905a4ddb~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771414292&x-signature=vMcvGBuFRT439IlS25Kj%2Fvrzxd0%3D)

👉**二、Python必备开发工具**

![img](https://p9-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/09de26b9415e4873b4b5f280bb4a04eb~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771414292&x-signature=9NzMlLVXW3xmkzkUviMWax7T9sY%3D)  
👉**三、Python视频合集**

观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。  
![img](https://p9-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/e2cb201daf0042149498238650a74bea~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771414292&x-signature=AxBqsk%2B%2Br2223beODyuPg7FEy24%3D)

👉 **四、实战案例**

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。**(文末领读者福利)**  
![img](https://p9-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/3cdcea805aac4ceb9fada08e4c1d95af~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771414292&x-signature=E931wy7hV4ps4zzKU5UN4OHOjGc%3D)

👉**五、Python练习题**

检查学习结果。  
![img](https://p9-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/fc06a9576b204df6bd1c93c4ece229ab~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771414292&x-signature=D5SABrzEYjJuxPzdMJ74xaGb7fk%3D)

👉**六、面试资料**

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。  
![img](https://p9-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/9df293f81f8c4085b881c5b1cfe640f6~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771414292&x-signature=0kjZod1rQwSSdM9PnQsACDJYVTc%3D)

![img](https://p9-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/5e1786e530c844b5b8f3b9cbe7af0320~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771414292&x-signature=z4rYB4HiOJ89v7%2Fj35VZWJkasEY%3D)

👉因篇幅有限,仅展示部分资料,这份完整版的Python全套学习资料已经上传



**了解详情:https://docs.qq.com/doc/DSnl3ZGlhT1RDaVhV**