Skip to content

Search the documentation

Kit
Firmware

Seven segment — Smart Home (Python)

This example is written twice, once for each firmware. Pick the tab that matches the firmware of your Smart Home: the tab you pick is remembered on the other examples.

Firmware 3.0 renamed the Python names a program writes, and kept none of the old ones: a Smart Home on 2.x does not know the 3.0 names, and a Smart Home on 3.0 does not know the 2.x ones. The name map lists every change. If you open the example from inside MoBot, it picks the version for the firmware of your Smart Home. If you download a file from this page, use the one in the tab for your firmware.

The little red three-digit display, doing everything it can do. Counting, counting down, decimals, the nine letters it can draw, and finally switching the seven bars on and off one at a time.

This is the version for a house on firmware 3.0, where the display is home.number_display. For a house that still runs firmware 2.x, MoBot opens the 2.x version of this example instead.

Project for firmware 3.0: 02-seven-segment.firmware-3.mobot. Open it in MoBot.

  • A MoBot Smart Home board with its 3-digit TM1637 display fitted (CLK → GPIO 40, DIO → GPIO 39).
  • A USB cable and the serial monitor.
part on the display
1 0, 7, 42, 100, 999 — each pushed to the right, then a countdown from 20 to 0
2 3.14, 25.5, 7.25, 0.5, -1.5, 123 and then --- for a number too big to fit
3 HEL, CAF, bAd, ---, PLA, then abc drawn as AbC
3 then Hi! is refused: the display does not change, and the monitor prints why
4 one bar at a time on all three digits: top, top-right, bottom-right, bottom, bottom-left, top-left, middle, then the dot
4 then a ring spinning round three times
5 three shapes built by naming the bars
end 8.8.8. — every bar and every dot lit. That is the test: if any bar stays dark, that bar is broken

The display has no decimal numbers of its own. You give it a whole number and say which digit’s dot to light:

home.number_display.show_number(314, [True, False, False]) # 3.14
home.number_display.show_number(255, [False, True, False]) # 25.5

show_number() never puts a 0 in front, though, so 0.5 would come out as .5. That is why show_decimal() in this file draws every bar itself with set_digit() (the bars are explained below). It tries two decimal places, then one, then none, and shows the first version that fits in three digits — a minus sign takes a digit too, and zeros at the end are dropped, so 0.50 shows as 0.5. A number that cannot fit any way round shows --- rather than lying to you. That is why 123.456 shows 123.

A b C d E F H L P — and that is all of them, plus 0 to 9, a dash - and a space. show_text() takes up to three of them, pushed to the right like a number. The Display Text block does exactly the same.

Capital or small does not matter: each letter comes out the one way seven bars can draw it. b and d always look small, because a capital B looks exactly like an 8 and a capital D like a 0; the others always look capital. So show_text("abc") shows AbC.

Anything else — i, O, a dot, more than three characters — is refused with a ValueError that says what works, and the display keeps what it was showing:

The number display can't draw 'i'. It can show 0 to 9, the letters A b C d E F H L P (capital or small), a dash - and a space.

Each digit has seven bars and a dot, and each has a name:

AAA
F B
GGG
E C
DDD DP

set_digit(index, bars) lights exactly the bars you name on one digit (0, 1 or 2, from the left) and switches the others off. ["B", "C"] is a 1. Work out 8 yourself, then check it: it is every bar from A to G.

There are three number-display blocks:

block what it does
Display Number (iot_7segvar) shows a whole number: it is show_number(...)
Display Text (iot_7segtext) shows up to three characters: it is show_text(...), firmware 3.0 only
Set digit segments (iot_7seg) lights the bars you name on one digit: it is set_digit(...)

Parts 4 and 5 of this file do the same thing from Python.

This is a reading copy of the program in the project: editing it changes nothing, so open the project and edit it there.

main.py
# main.py — Seven segment (Python) (iot)
#
# A reading copy of one file inside 02-seven-segment.firmware-3.mobot.
# Editing this copy changes nothing: open the project and edit it there.
# This is the entry point — the board runs it as main.py.
# Written for firmware 3.0; the 2.x version is 02-seven-segment.main.py.
"""Seven segment - MoBot Smart Home.
The little red three-digit display, doing everything it can do.
There are three number-display blocks. "Display Number" works fine but only
takes whole numbers. "Display Text" shows up to three letters - it is the same
show_text() as part 3. The third one - the one that lets you switch individual
bars on and off - DOES NOTHING when you open a saved project: it draws itself
on the screen and then produces a comment instead of code.
Everything below that is not a block is Python only:
number_display.show_number(314, [True, False, False]) 3.14 - you place the dot
number_display.set_digit(0, ["A", "B", "G"]) switch the bars yourself
"""
import time
number_display = home.number_display
if number_display is None:
print("The number display did not start.")
raise SystemExit
# ------------------------------------------------------------------ 1 -------
# Numbers. show_number() pushes them to the right and pads with spaces.
print("Part 1: counting.")
for value in (0, 7, 42, 100, 999):
print(" ", value)
number_display.show_number(value)
time.sleep(0.8)
print(" counting down from 20")
for value in range(20, -1, -1):
number_display.show_number(value)
time.sleep(0.12)
# ------------------------------------------------------------------ 2 -------
# Decimals. The display has no decimal numbers of its own: you give it a whole
# number and say which digit's dot to light. 314 with the first dot lit is 3.14:
#
# number_display.show_number(314, [True, False, False])
#
# But show_number() never puts a 0 in front, so 0.5 would come out as " .5".
# That is why show_decimal() below draws every bar itself with set_digit() -
# the same bars part 4 switches on one at a time.
#
# show_decimal() tries 2 decimal places, then 1, then none, and uses the first
# that fits in three digits. A minus sign takes a digit too, and zeros at the
# end are dropped, so 0.50 shows as 0.5. If nothing fits it shows ---.
# The bars that draw each character.
BARS = {
"0": "ABCDEF", "1": "BC", "2": "ABDEG", "3": "ABCDG", "4": "BCFG",
"5": "ACDFG", "6": "ACDEFG", "7": "ABC", "8": "ABCDEFG", "9": "ABCDFG",
"-": "G", " ": "",
}
def show_decimal(value):
for places in (2, 1, 0):
whole = round(abs(value) * 10 ** places) # 3.14159 -> 314
while places > 0 and whole % 10 == 0: # 0.50 -> 0.5, 20.0 -> 20
whole = whole // 10
places = places - 1
text = str(whole)
while len(text) <= places: # 0.05 -> "005": 0 . 0 5
text = "0" + text
if value < 0 and whole != 0:
text = "-" + text
if len(text) <= 3:
text = " " * (3 - len(text)) + text # pushed to the right
for digit in range(3):
bars = list(BARS[text[digit]])
if places > 0 and digit == 2 - places:
bars.append("DP") # the dot sits after this digit
number_display.set_digit(digit, bars)
return
number_display.show_text("---")
print("Part 2: decimals.")
for value in (3.14159, 25.5, 7.25, 0.5, -1.5, 123.456, 99999.0):
show_decimal(value)
print(" ", value)
time.sleep(1.0)
# ------------------------------------------------------------------ 3 -------
# Letters. Only nine letters can be drawn with seven bars:
# A b C d E F H L P
# Capital or small, each comes out the one way the display can draw it.
print("Part 3: words, sort of.")
for word in ("HEL", "CAF", "bAd", "---", "PLA", "abc"):
number_display.show_text(word)
print(" ", word)
time.sleep(1.0)
# Any other letter is refused with a message that says what works - and the
# display keeps what it was showing.
print(" 'Hi!' - watch the display NOT change")
try:
number_display.show_text("Hi!")
except ValueError as error:
print(" refused:", error)
time.sleep(1.5)
# ------------------------------------------------------------------ 4 -------
# The bars themselves. Each digit has seven bars and a dot, named like this:
#
# AAA
# F B
# GGG
# E C
# DDD DP
print("Part 4: switching the bars on one at a time.")
for bar in ("A", "B", "C", "D", "E", "F", "G", "DP"):
for digit in range(3):
number_display.set_digit(digit, [bar])
print(" bar", bar)
time.sleep(0.5)
print(" a spinning ring")
RING = ("A", "B", "C", "D", "E", "F")
for turn in range(3):
for bar in RING:
for digit in range(3):
number_display.set_digit(digit, [bar])
time.sleep(0.08)
# Your own shapes, one digit at a time.
print("Part 5: three shapes built by naming the bars.")
number_display.show_text("") # "" blanks the display
number_display.set_digit(0, ["A", "B", "G"])
number_display.set_digit(1, ["D", "E", "G"])
number_display.set_digit(2, ["A", "F", "G", "DP"])
time.sleep(3)
# The classic display test: every bar and every dot, all at once.
number_display.show_number(888, [True, True, True])
print("Finished - every bar and every dot should now be lit.")
  • Show the temperature: show_decimal(home.temperature_sensor.temperature()) (it can be None for the first second or two after switching on).
  • Make a stopwatch that counts up in tenths.
  • Design your own letter out of bars and show it with set_digit(...).