Raspberry with 7-segment display

Today we show you how to put our 7-segment display into operation via the Raspberry Pi.


I start with a newly installed Raspberry Pi 3 Model BThe four-digit 7-segment displayand a handful of Jumper Cable.

Depending on the model, the GPIO pins from the Raspberry Pi are undertherin. I connect the ad as follows:

Wiring 7-segment to RasPi  


After a first start, the empty Raspbian Desktop greets us.


We want the widely used library WiringPito control the GPIO pins.

The library allows us, among other things, to

  • Pins to define
  • Pins to set as input or output
  • Write on pins, or read from them

To do this, we open a terminal and enter the following command:

sudo pip3 install wiringpi

The following output appears:


We use a slightly modified version of https://github.com/johnlr/raspberrypi-tm1637

We start our Python3 IDLE, go to "File" -> "New File" and insert the following code:



#!/Usr/Am/Env python3

Import subprocess
from Time Import Time, Sleep, Localtime

from wiringpi Import wiringPiSetupGpio, pinMode, digitalRead, digitalWrite, Gpio

Clk = 21
Dio = 20

"""       A      ---   Q |   | B      -G-   E |   | C      ---       D

class TM1637:     I2C_COMM1 = 0x40     I2C_COMM2 = 0xC0     I2C_COMM3 = 0x80     digit_to_segment = [         0b011111, # 0         0b0000110, # 1         0b1011011, # 2         0b1001111, # 3         0b1100110, # 4         0b1101101, # 5         0b1111101, # 6         0b0000111, # 7         0b1111111, # 8         0b1101111, # 9         0b1110111, # A         0b1111100, # B         0b0111001, # C         0b1011110, # D         0b1111001, # E         0b1110001  # Q         ]     Def __init__(Self, Clk, Dio):         Self.Clk = Clk         Self.Dio = Dio         Self."I'm not = 0x0f         pinMode(Self.Clk, Gpio.Input)         pinMode(Self.Dio, Gpio.Input)         digitalWrite(Self.Clk, Gpio.Low)         digitalWrite(Self.Dio, Gpio.Low)     Def bit_delay(Self):         Sleep(0.001)         Return         Def set_segments(Self, Segment, Item=0):         # Write COMM1         Self.Start()         Self.write_byte(Self.I2C_COMM1)         Self.Stop()         # Write COMM2 + First Digit address         Self.Start()         Self.write_byte(Self.I2C_COMM2 + Item)         for Seg In Segment:             Self.write_byte(Seg)         Self.Stop()         # Write COMM3 + "I'm not         Self.Start()         Self.write_byte(Self.I2C_COMM3 + Self."I'm not)         Self.Stop()     Def Start(Self):         pinMode(Self.Dio, Gpio.Output)         Self.bit_delay()         Def Stop(Self):         pinMode(Self.Dio, Gpio.Output)         Self.bit_delay()         pinMode(Self.Clk, Gpio.Input)         Self.bit_delay()         pinMode(Self.Dio, Gpio.Input)         Self.bit_delay()        Def write_byte(Self, B):       # 8 Data Bits         for  In Range(8):             # Clk Low             pinMode(Self.Clk, Gpio.Output)             Self.bit_delay()             pinMode(Self.Dio, Gpio.Input If B & 1 else Gpio.Output)             Self.bit_delay()             pinMode(Self.Clk, Gpio.Input)             Self.bit_delay()             B >>= 1                pinMode(Self.Clk, Gpio.Output)         Self.bit_delay()         pinMode(Self.Clk, Gpio.Input)         Self.bit_delay()         pinMode(Self.Clk, Gpio.Output)         Self.bit_delay()         Return

Def show_ip_address(Tm):     ipaddr = subprocess.check_output("hostname -I", Shell=True, Timeout=1).Strip().Split(B".")     for Octet In ipaddr:         Tm.set_segments([0, 0, 0, 0])         Sleep(0.1)         Tm.set_segments([Tm.digit_to_segment[Int(X) & 0xf] for X In Octet])         Sleep(0.9)

Def show_clock(Tm):         T = Localtime()         Sleep(1 - Time() % 1)         d0 = Tm.digit_to_segment[T.tm_hour 10] if t.tm_hour / 10 else 0         d1 = Tm.digit_to_segment[T.tm_hour % 10]         d2 = Tm.digit_to_segment[T.tm_min // 10]         d3 = Tm.digit_to_segment[T.tm_min % 10]         Tm.set_segments([d0, 0x80 + d1, d2, d3])         Sleep(.5)         Tm.set_segments([d0, d1, d2, d3])

If __name__ == "__main__":     Tm = TM1637(Clk, Dio)     show_ip_address(Tm)     while True:         show_clock(Tm)





Now let's go to "Run" -> "Run module", specify a name to save our source code, and wait for a short time.

The 7-segment display now displays the local IP address first, and then the current time:




From now on, you can easily output information to our 7-segment display with the Raspberry Pi.






DisplaysProjekte für anfängerRaspberry pi

1 comment

Erich Zwiefelhofer

Erich Zwiefelhofer

Danke für das Beispielprogramm. Funktioniert mit der Darstellung der Systemzeit bestens.
Für die Darstellung der Temperatur möchte ich aber anstelle des Doppelpunktes in der Mitte nur den Kommapunkt verwenden. Wie wird der dargestellt? Kann mir da jemand helfen?

Leave a comment

All comments are moderated before being published