Sample Seven Segment Display Lab
4 Digit Seven Segment Display
Make sure to put a current limiting resistor in series with each LED. A 330 ohm resistor is a generally safe value for 5 volt circuits and you can use a 220 ohm resistor for 3.3 volt circuits.
This code was provided by Jaison Miller from his GitHub Repo.
```py from machine import Pin, PWM, Timer import utime
Constants - where the pins are currently plugged into, etc.
number_bitmaps = { 0: 0b00111111, 1: 0b00000110, 2: 0b01011011, 3: 0b01001111, 4: 0b01100110, 5: 0b01101101, 6: 0b01111101, 7: 0b00000111, 8: 0b01111111, 9: 0b01100111 } segment_masks = { 'a': 0b00000001, 'b': 0b00000010, 'c': 0b00000100, 'd': 0b00001000, 'e': 0b00010000, 'f': 0b00100000, 'g': 0b01000000 } pin_segments = { 'a': 10, 'b': 11, 'c': 12, 'd': 17, 'e': 16, 'f': 13, 'g': 14} pin_others = { 'decimal': 22, 'colon': 6, 'dash': 8 } pin_digits = { 1: 18, 2: 19, 3: 20, 4: 21 } pin_control_others = { 'colon': 27, 'dash': 7 }
initial setup of the pins, alternatives include using PWM to set the brightness
if not using PWM then make sure to use appropriate resistors to avoid blowing the LEDs in the display (like I have)
segment_maps = {}
for segment, pin in pin_segments.items(): segment_maps[segment] = Pin(pin, Pin.OUT)
other_pin_maps = {}
for feature, pin in pin_others.items(): other_pin_maps[feature] = Pin(pin, Pin.OUT)
digit_maps = {}
for digit, pin in pin_digits.items(): digit_maps[digit] = Pin(pin, Pin.OUT)
other_maps = {}
for feature, pin in pin_control_others.items(): other_maps[feature] = Pin(pin, Pin.OUT)
def render_digit_display(show_digit=1, number=8, decimal=False):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
def render_feature_display(show_colon=False, show_dash=False): if show_colon: other_pin_maps['colon'].value(0) other_maps['colon'].value(1) else: other_pin_maps['colon'].value(0) other_maps['colon'].value(0)
1 2 3 4 5 6 |
|
while True:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|