物联网开发笔记(21)- 使用Micropython开发ESP32开发板之控制单个数码管

        大家在很多嵌入式设备上,经常看到数码管,可以显示数字,英文字母,非常的实用。下面我们一起学习怎么用ESP32开发板来控制它。

一、目的

        使用MicroPython开发ESP32控制数码管

二、环境

        ESP32+数码管+Thonny IDE(或者WOKWI仿真也可,下面使用仿真方便讲解)

三、数码管介绍

        数码管大家都见过,如下图:

        物联网开发笔记(21)- 使用Micropython开发ESP32开发板之控制单个数码管_第1张图片

实际上,一个数码管是由多个LED灯组成的。

物联网开发笔记(21)- 使用Micropython开发ESP32开发板之控制单个数码管_第2张图片

 WOKWI数码管解释:

wokwi-7segment Reference | Wokwi DocsSeven segment LED displayhttps://docs.wokwi.com/parts/wokwi-7segment#simulator-examples

四、硬件连接方法

        需要准备10来根杜邦线,一个面包板(也可不用)

共阳极接线图(电阻的一端接开发板的正极3.3V):

物联网开发笔记(21)- 使用Micropython开发ESP32开发板之控制单个数码管_第3张图片

 共阴极接线图(电阻的一端接开发板的负极GND):

物联网开发笔记(21)- 使用Micropython开发ESP32开发板之控制单个数码管_第4张图片

五、代码

main.py

import machine
import time

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)
dot = machine.Pin(32, machine.Pin.OUT)

number_led = [a, b, c, d, e, f, g, dot]

# WOKWI这里的数码管为共阳极(如果你连接的是共阴极的数码管,请改为注释的值)
number_dict = {
    0: "00000011",  # 0: "11111100",
    1: "10011111",  # 1: "01100000",
    2: "00100101",  # 2: "11011010",
    3: "00001101",  # 3: "11110010",
    4: "10011001",  # 4: "01100110",
    5: "01001001",  # 5: "10110110",
    6: "01000001",  # 6: "10111110",
    7: "00011111",  # 7: "11100000",
    8: "00000001",  # 8: "11111110",
    9: "00001001",  # 9: "11110110",
    "open": "00000000",  # "open": "11111111",
    "close": "11111111"  # "close": ""
}

def show_number(number):
    if number_dict.get(number):
        i = 0
        for bit in number_dict.get(number):
            if bit == "1":
                number_led[i].value(1)
            else:
                number_led[i].value(0)
            i += 1

def main():
    show_number("open")  # 全亮
    time.sleep(3)
    show_number("close")  # 全灭
    time.sleep(3)
    show_number("open")  # 全亮
    time.sleep(3)
    show_number("close")  # 全灭
    time.sleep(3)

    for i in range(10):
        show_number(i)
        time.sleep(2)

if __name__ == "__main__":
    main()

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-resistor",
      "id": "r1",
      "top": 147.63,
      "left": 143.09,
      "attrs": { "value": "250" }
    },
    {
      "type": "wokwi-7segment",
      "id": "sevseg1",
      "top": -43.43,
      "left": -115.83,
      "attrs": { "color": "#d040d0" }
    }
  ],
  "connections": [
    [ "esp:TX0", "$serialMonitor:RX", "", [] ],
    [ "esp:RX0", "$serialMonitor:TX", "", [] ],
    [ "esp:D13", "sevseg1:A", "cyan", [ "h-39.29", "v-201.89", "h-50.88" ] ],
    [ "esp:D12", "sevseg1:B", "yellow", [ "h-23.22", "v-185.43", "h-54.23" ] ],
    [ "sevseg1:C", "esp:D14", "limegreen", [ "v0" ] ],
    [ "esp:D27", "sevseg1:D", "magenta", [ "h0" ] ],
    [ "esp:D26", "sevseg1:E", "purple", [ "h0" ] ],
    [ "esp:D25", "sevseg1:F", "white", [ "h-52.68", "v-169.09", "h-6.69" ] ],
    [ "esp:D33", "sevseg1:G", "gray", [ "h-63.39", "v-170.87", "h-2.68" ] ],
    [ "esp:D32", "sevseg1:DP", "violet", [ "h0" ] ],
    [ "r1:2", "sevseg1:COM.1", "green", [ "v62.78", "h-256.46" ] ],
    [ "esp:3V3", "r1:1", "green", [ "v0" ] ]
  ]
}

六、实现效果,全亮3秒全灭3秒,再全亮3秒全灭3秒,然后显示0123456789

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

物联网开发笔记(21)- 使用Micropython开发ESP32开发板之控制单个数码管_第5张图片

 以上是在WOKWI上仿真的效果,在实际的开发板中,接线和实现方法,都是一样的。注意一定要接电阻哦,不然容易损坏数码管和开发板。

你可能感兴趣的:(物联网开发,物联网,java,开发语言)