一、数码管与ESP32的连接方式
二、代码
连接正常下载程序的话,数码管应该是1234-9999慢慢跳动,如果没现象首先检查是不是用的数码管是共阳极的。
import machine
import time
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)
led_light_list = [led1, led2, led3, led4]
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]
number_dict = {
0: "11111100", # 顺序依次是abcdefgh
1: "01100000",
2: "11011010",
3: "11110010",
4: "01100110",
5: "10110110",
6: "10111110",
7: "11100000",
8: "11111110",
9: "11110110",
10: "00000000" # 清零
}
def show_number(number):
if number_dict.get(number): # 如果取出的值在字典中
i = 0
for num in number_dict.get(number):
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:
show_number(10) # 全部管脚清零熄灭
light_on(i) # 选择要亮的数码管位置
show_number(int(num))
i += 1
time.sleep_ms(1) # 延时1ms,放置数码管跳太快了看不出来
if __name__ == "__main__":
for i in range(1234, 10000): # 显示数字1234-9999
for j in range(100):
show_4_number(i)