CircuitPython Code

The main loop simply checks the position of the switch and then either displays the temperature or the time.

import glob
import time
import datetime
from adafruit_ht16k33 import segments
import board
import busio
import digitalio

switch_pin = digitalio.DigitalInOut(board.D18)
switch_pin.direction = digitalio.Direction.INPUT
switch_pin.pull = digitalio.Pull.UP

# Create the I2C interface.
i2c = busio.I2C(board.SCL, board.SDA)

# Create the LED segment class.
# This creates a 7 segment 4 character display:
display = segments.Seg7x4(i2c)

base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'

def read_temp_raw():
    f = open(device_file, 'r')
    lines = f.readlines()
    return lines

def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        return temp_c, temp_f

def display_temp():
    temp = read_temp()[1] # F
#   temp = read_temp()[0] # C

def display_time():
    now =
    hour = now.hour
    minute = now.minute
    second = now.second
    clock = int('%i%i' % (hour,minute))          # concat hour + minute

    # Toggle colon when displaying time
    if second % 2:
        display.print(':')                      # Enable colon every other second
        display.print(';')                      # Turn off colon


while True:
    if not switch_pin.value:

We can easily copy this code onto our Pi's home directory using the 'wget' command and run it using the following python syntax.

Download the Code


Run the Code

The script will not output anything when run, but what you should see is the time or temperature on the large 7-segment display. Using the slide switch should switch modes between time and temperature. 

python3 ./

Changing Temperature Units

The temperature display can easily be changed from displaying degrees F to degrees C by swapping comment line in the code.

temp = int(read_temp()[1]) # F
# temp = int(read_temp()[0]) # C

To swap, just move the # in front of the line that does not apply.

This guide was first published on Apr 03, 2013. It was last updated on Apr 03, 2013.

This page (Software) was last updated on Mar 25, 2021.

Text editor powered by tinymce.