ESP32 Micropython programming (Thonny) 02---- Wokwi simulation and digital tube use

本文在上文的基础上,进一步体验一下micropython的使用,如果大家手上没有硬件也不要紧。
给这里大家介绍一个免费在线仿真网站,以及用这个网站搭建一个Micropython+esp32+数码管的一个电路

1. Wokwi

WOKWI is a cool website that supports Arduino, ESP32, and Raspberry Pi Pico online simulation learning. : The
official website
official help document
supports most arduino framework boards, as well as micropython.
insert image description here
It’s ok for you to explore the specific usage by yourself. There are many interesting routines on the website. You can go in and learn, and you will soon be able to master the use of this website.

Two Micropython+esp32+digital tube

This is a circuit I built, you can refer to it.
insert image description here

1. Nixie tube

The digital tube is composed of several LEDs. The digital tube displays the numbers. In essence, it is to light up the LEDs to form numbers on the graph.
insert image description here
As shown in the picture: we want to display 1, then the adefg dp digital tube is off, and the bc digital tube is on, it is ok. The code is also very simple. Next, we will analyze the code.

  1. The pins
    correspond to 8 leds, assign pins to them on the esp32, you can change it according to your preference.
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)   
H = machine.Pin(32, machine.Pin.OUT)   

2. Display array

Look at the code below, why is this so?
Think about it, if we want to display 9, we operate on 8 LEDs separately, then displaying a number requires 8 lines of code, which is very troublesome, but if we list the status of the LED corresponding to each number in advance, when we need Is it very convenient to light up through such a relationship stored in advance? Maybe what I described is not very clear, so please take a look at the code carefully.

number_led = [A, B, C, D, E, F, G, H]
number_dict = {
    
    
    0: "11111100",
    1: "01100000",
    2: "11011010",
    3: "11110010",
    4: "01100110",
    5: "10110110",
    6: "10111110",
    7: "11100000",
    8: "11111110",
    9: "11110110",
    "open": "11111111",
    "close": "00000000"
}

3. Let's display a number 9

Experience it carefully, a very convenient way.

#共阳数码管
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)   
H = machine.Pin(32, machine.Pin.OUT)   

number_led = [A, B, C, D, E, F, G, H]
number_dict = {
    
    
    0: "11111100",
    1: "01100000",
    2: "11011010",
    3: "11110010",
    4: "01100110",
    5: "10110110",
    6: "10111110",
    7: "11100000",
    8: "11111110",
    9: "11110110",
    "open": "11111111",
    "close": "00000000"
}
def show_num(num):
    if number_dict.get(num):
        i = 0
        for bit in number_dict.get(num):
            if bit == "0":
                number_led[i].value(1)
            else:
                number_led[i].value(0)
            i += 1

def main():
    for i in range(0,10):
        show_num(i)
        time.sleep(0.3)
    

if __name__ == "__main__":
    main()

This is the end of this article. The following will update the features of esp32, the content of the network, and use the computer to control the light on and off of the esp32 led through udp.

Guess you like

Origin blog.csdn.net/amimax/article/details/127826015