I2C (Inter-Integrated Circuit), pronounced I-squared-C, is a synchronous, […], packet switched, single-ended, serial communication bus invented in 1982 by Philips Semiconductor (now NXP Semiconductors) –– Wikipedia: I2C
I have setup a Raspberry Pi and it is right next to me. Therefore I thought I show the load average of the Raspberry Pi on an alphanumeric display from Adafruit which is controlled by I2C.
Raspberry Pi Setup
First activate I2C. I use raspbian, which is now called Raspberry Pi OS. For this I use sudo raspi-config
and activate I2C under 5 Interfacing Options. Take a look at this manual: Configuring I2C on the Raspberry Pi
Then install the i2c-tools library:
sudo apt-get update && sudo apt-get upgrade
sudo apt-get install -y i2c-tools
Connect AlphaNum Display
The AlphaNum display uses I2C. Here you can see the Raspberry Pi I2C Pinout. The display must be connected as follows:
A manual 📃 is available here: Python Wiring and Setup
Test I2C
With sudo i2cdetect -y 1
you should see this output:
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: 70 -- -- -- -- -- -- --
The display uses address 0x70.
Source Code
First we need the Python libraries and since I have setup the Raspberry Pi fresh, I need pip3:
sudo apt-get -y install python3-pip
… and then the Python libraries:
pip3 install adafruit-circuitpython-ht16k33
The program imports some libraries and initializes the AlphaNum display using Seg14x4. Seg7x4 is for a 7-segment display. Then the display is cleared and the brightness set.
import time
import board
import busio
import os
from adafruit_ht16k33 import segments
# Create the I2C interface.
i2c = busio.I2C(board.SCL, board.SDA)
# Create the LED segment class.
# This creates a 14 segment 4 character display:
display = segments.Seg14x4(i2c)
# Clear the display.
display.fill(0)
# set brightness, range 0-1.0, 1.0 max brightness
display.brightness = 0.1
while True:
load1, load5, load15 = os.getloadavg()
text = " {load:.2f}"
display.print(text.format(load = load1))
time.sleep(1)
In the while loop the load average values 1, 5, and 15 are read every second. I convert the load1 value into a text and send it to the display.
Summary
As I have an alphanumeric display and not only a 7-segment display, it could display some text. The 14-Segment Alphanumeric Display example shows a scrollable text. I could also set up a REST server and you can send me some messages …