The Raspberry Pi has some great add-on hardware, such as Pi Tops that fit directly on top of the Pi module and wired components.
A good number of the wired Arduino designed parts now can also be used with Rasp PI’s. Some examples of this includes the HT16K33 and TM1637 seven segment displays.
Nothing beats using real hardware to show Pi values and status, but if you’re missing the hardware or you’d like to duplicate a displayed value remotely, then a soft version of the hardware can be very useful.
In this blog we’ll look at a three Python soft display examples, a seven-segment display, a LCD Keypad Top and a gauge.
Seven Segment Display
The tk_tools module is based on the Python tkinter module and it is has some cool components such as LEDs, Charts, Gauges and Seven Segment displays. The module is installed by:
pip install tk_tools
The tk_tools Seven Segment component can function like an Arduino TM1637 or HT16K33 display component. The tk_tools seven-segment display supports a height, digit_color and a background color.
Below is a some example code that shows the Pi’s CPU temperature in the soft seven segment display.
import tkinter as tk
import tk_tools
root = tk.Tk()
root.title("CPU Temp")
ss = tk_tools.SevenSegmentDigits(root, digits=5, background='black',
digit_color='yellow', height=100)
ss.grid(row=0, column=1, sticky='news')
# Update the Pi CPU Temperature every 1 second
def update_gauge():
# Get the Raspberry CPU Temp
tFile = open('/sys/class/thermal/thermal_zone0/temp')
# Scale the temp from milliC to C
thetemp = int(float(tFile.read())/1000)
ss.set_value(str(thetemp))
root.after(1000, update_gauge)
root.after(500, update_gauge)
root.mainloop()
LCD Keypad
The LCD Keypad I’ve used on a lot of my Pi Projects, (below is a PI FM radio example). Its supports 2 lines of text and it has 5 (or 6) buttons that can be used in your Python app.
The standard Python Tkinter library can be used to create a custom LCD keypad display. For my example I tried to replicate the look-and-feel of the Pi Top that I had, but you could enhance or change it to meet your requirements.
Below is an example that writes the button pushed to the 2 line label.
import tkinter as tk
def myfunc(action):
print ("Requested action: ",action)
Line1.config(text = "Requested action: \n" + action)
root = tk.Tk()
root.title("LCD Keypad Shield")
root.configure(background='black')
Line1 = tk.Label(root,
text="ADC key testing \nRight Key OK ",
fg = "white",
bg = "blue",
font = "Courier 45",
borderwidth=4, relief="raised")
Line1.grid(row = 0,column = 0, columnspan =15, rowspan = 2)
selectB = tk.Button(root, width=10,text= "SELECT",bg='silver' ,
command = lambda: myfunc("SELECT"),relief="raised")
selectB.grid(row = 3,column = 0)
leftB = tk.Button(root, width=10,text= "LEFT", bg='silver' ,
command = lambda: myfunc("LEFT"),relief="raised")
leftB.grid(row = 3,column = 1)
rootB = tk.Button(root, width=10,text= "UP", bg='silver' ,
command = lambda: myfunc("UP"),relief="raised")
rootB.grid(row = 2,column = 2)
rightB = tk.Button(root, width=10,text= "DOWN", bg='silver' ,
command = lambda: myfunc("DOWN"),relief="raised")
rightB.grid(row = 3,column = 3)
bottomB = tk.Button(root, width=10,text= "RIGHT", bg='silver',
command = lambda: myfunc("RIGHT"),relief="raised")
bottomB.grid(row = 4,column = 2)
rstB = tk.Button(root, width=10,text= "RST", bg='silver' ,
command = lambda: myfunc("RESET"),relief="raised")
rstB.grid(row = 3,column = 4)
root.mainloop()
Gauge and Rotary Scale
There aren’t any mainstream low cost gauges that are available for the Rasp Pi, but I wanted to show how to setup a soft gauge.
The tk_tools gauge component is very similar to a speedometer. The rotary scale is more like a 180° circular meter. Both components support digital values, units and color scales.
Below is a gauge example that reads the Pi CPU temperature every second.
import tkinter as tk
import tk_tools
root = tk.Tk()
root.title("CPU Temp")
my_gauge = tk_tools.Gauge(root, height = 200, width = 400,
max_value=70,
label='CPU Temp',
unit='°C',
bg='grey')
my_gauge.grid(row=0, column=0, sticky='news')
def update_gauge():
# Get the Raspberry CPU Temp
tFile = open('/sys/class/thermal/thermal_zone0/temp')
# Scale the temp from milliC to C
thetemp = int(float(tFile.read())/1000)
my_gauge.set_value(thetemp)
# update the gauges according to their value
root.after(1000, update_gauge)
root.after(500, update_gauge)
root.mainloop()
Final Thoughts
There are a lot of soft hardware components that could be created.
I found myself getting tripped up thinking : “What would be a good tkinter component and what should be a Web component”. This is especially true when looking at charting examples, or when I was looking a remote connections.
I used the 7 segment display to create a scoreboard.
https://github.com/s8007/BasketballScoreboard
LikeLike
Nice
LikeLike