ESP32控制数码管实现数字叠加案例

ESP32控制数码管实现数字叠加案例_第1张图片

经过了几个小时的接线和代码实现终于搞定了代码,贴出来大家参考下

import machine
import time

# 定义4个Led的引脚
led1 = machine.Pin(5,machine.Pin.OUT)
led2 = machine.Pin(18,machine.Pin.OUT)
led3 = machine.Pin(19,machine.Pin.OUT)
led4 = machine.Pin(21,machine.Pin.OUT)

# 型号是5643AS就是共阴 5643BS就是共阳
led_light_list = [led1,led2,led3,led4]

# 先定义组成数字的8个引脚 
a = machine.Pin(13,machine.Pin.OUT) 
b = machine.Pin(12,machine.Pin.OUT)
c = machine.Pin(14,machine.Pin.OUT)
d = machine.Pin(27,machine.Pin.OUT)
e = machine.Pin(26,machine.Pin.OUT)
f = machine.Pin(25,machine.Pin.OUT)
g = machine.Pin(33,machine.Pin.OUT)
h = machine.Pin(32,machine.Pin.OUT)

# 定义一个数组
led_list = [a,b,c,d,e,f,g,h]

# led灯的组成顺序是abcdefg
number_dict = {
    0:"11111100",
    1:"01100000",
    2:"11011010",
    3:"11110010",
    4:"01100110",
    5:"10110110",
    6:"10111110",
    7:"11100000",
    8:"11111110",
    9:"11110110"
    }

def show_number(number):
    if number_dict.get(number):
        # print(f"对应的数字为:{number_dict.get(number)}")
        i = 0
        for num in number_dict.get(number):
            # print(num)
            if num == "1":
                led_list[i].value(1)
            else:
                led_list[i].value(0)
            i += 1
            
def light_on(i):
    for led in led_light_list:
        led.value(1)
        
    led_light_list[i].value(0)

def show_4_number(number):
    if 0 <= number <= 9999:
        i = 0
        for num in "%04d" % number:
            print(num)
            show_number(int(num))
            light_on(i)
            i += 1
            time.sleep_ms(2)

for i in range(1000,10000):
    for j in range(100):
        show_4_number(i)

上效果图

ESP32控制数码管实现数字叠加案例_第2张图片

你可能感兴趣的:(ESP32,物联网开发,物联网,ESP32,数码管)