Raspberry Pi Pico(6): Use seven-segment display for counting

The seven-segment display is often used as a display of numbers or simple English letters in the implementation of micro-control. It can be divided into two types: one is the common anode (Common Anode), the anodes of each segment of the LED are connected to each other, and the other is the common cathode ( Common Cathode), the cathodes of each LED segment are connected to each other.
The internal structure of the seven-segment display is composed of 8 LED light-emitting diodes with seven strokes plus a decimal point, as shown in the figure below, which are A, B, C, D, E, F, G and decimal in clockwise direction. Point DP (decimal point):
How does the seven-segment display work? To display the decimal number, a specific set of LEDs are lit. To turn a specific part of the display on and off, set the corresponding pin to HIGH or LOW, just like using a regular LED. For example, to display the number 4, we will need to light up the four LED segments corresponding to B, C, F and G. Therefore, the seven-segment display shown above can be used to display various numbers from 0 to 9 and characters from A to F. In this implementation, we will learn how to use MicroPython to write a Pi Pico program to control a seven-segment display.

[Material]

  • Raspberry Pi Pico x1
  • Seven segment LED display x1
  • Resistance 220 ohm x1
  • Breadboard x1
  • N wires

[Wiring diagram]

Correspondence between Pi Pico Pin and seven-segment display:
Pi PicoSEVEN SEGMENT DISPLAY
Pin 2(GPIO1) A
Pin 4(GPIO2) B
Pin 5(GPIO3) C
Pin 6(GPIO4) D
Pin 7(GPIO5) E
Pin 9(GPIO6) F
Pin 10(GPIO7) G
Pin 36(3V3)Connect one end of a 220 ohm resistor,and the other end of the resistor COM


[Code]

My seven-segment display has a common cathode, and COM is connected to 3.3V. When the LED displays, it needs a low potential to light up. For example, to display 0, according to the figure above, the LED at position G must be extinguished, so the seventh bit (G) in the chars array must be set to 1, so that the LED will not light up, and the other six LEDs are all 0.
from machine import Pin
import utime

pins = [
   Pin(1, Pin.OUT), #A
   Pin(2, Pin.OUT), #B
   Pin(3, Pin.OUT), #C
   Pin(4, Pin.OUT), #D
   Pin(5, Pin.OUT), #E
   Pin(6, Pin.OUT), #F
   Pin(7, Pin.OUT)  #G
   ]

chars = [
   [0,0,0,0,0,0,1], #0
   [1,0,0,1,1,1,1], #1
   [0,0,1,0,0,1,0], #2
   [0,0,0,0,1,1,0], #3
   [1,0,0,1,1,0,0], #4
   [0,1,0,0,1,0,0], #5
   [1,1,0,0,0,0,0], #6
   [0,0,0,1,1,1,1], #7
   [0,0,0,0,0,0,0], #8
   [0,0,0,1,1,0,0]  #9
   ]
 
def clear():
    for i in pins:
        i.value(1)

while True:
   for i in range(len(chars)):
      for j in range(len(pins)):
         pins[j].value(chars[i][j])
      utime.sleep(1)  #Pause for 1 second 

[Result]


[Reference]

Post a Comment

Previous Post Next Post