Raspberry Pi : Show CPU temperature on TM1637
|Raspberry Pi’s power consumption is increasing with each version. Increased power usage cause core temperature to rise. We can monitor the Raspberry CPU temperature without an external temperature sensor as the SoC has an internal temperature sensor. We can check the temperature of the core using the following command.
In this post we will write a simple python program that will get temperature using this command and shows it on a TM1637 7-segment display.
vcgencmd measure_temp
Generally communication protocols requires strict timing control, multitasking operating systems are not good at generating these signals. That’s why there will be dedicated hardware for commonly used protocols such as I2C,UART and SPI on the SoC.
TM1637 is not a strict I2C complaint and I couldn’t find any library that uses I2C hardware for communicating with the TM1637. For this post I will be this python library, this library uses bitbang method to generate the required signals. This will work fine as long as the Pi CPU is not heavily loaded. It might fail to generate correct signals if the CPU gets busy with other processes.
What you need
- Raspberry pi
- TM1637 based 7 segment display
- Jumpers for connecting ESP32 with TM1637
Connections
Raspberry Pi | TM1637 Board |
3V3 (1) | VCC |
GND (9) | GND |
5 (GPIO3) | CLK |
3 (GPIO2) | DIO |
Interfacing TM1637 with Raspberry Pi
Let’s write a counter program, that will increment the count every second and display it on the TM1637. We did a similar counter for ESP32 using micropython.
Install the library using pip
python3 -m venv ~/py3env source ~/py3env/bin/activate pip install raspberrypi-tm1637
import time import tm1637 tm = tm1637.TM1637(clk=3, dio=2) def showNumber(num): formattedNum = '{:04d}'.format(num) tm.show(formattedNum) if __name__ == "__main__": print("TM1637 interface") count = 0 while True: count = count + 1 print(count) showNumber(count) time.sleep(1)
We need to specify the clock and data io pins in the TM1637 library initialisation. Then we run a forever lookup and keep incrementing the count every second. copy the above source into a file and run the file to see the count on the TM1637.
Python temperature display program
We can get temperature using vcgencmd and parse the output. We will assume the temperature is two digit length for the purpose of this program. The output format of the vcgencmd is shown below. We can extract the temperature by indexing into the string.
temp=56.0'C
import time import subprocess import tm1637 tm = tm1637.TM1637(clk=3, dio=2) if __name__ == "__main__": print("TM1637 Temperature display") while True: cmdOutput = subprocess.check_output(["vcgencmd", "measure_temp"]) print(cmdOutput) temperatureStr = cmdOutput.decode('utf-8')[5:7] tm.temperature(int(temperatureStr)) time.sleep(1)
To run the code, copy above source into a file on your file and run the file. The program will display temperature reading both on the console and the TM1637.
Seems there is no simple way to use the native I2C hardware present in the Pi for communicating with TM1637. The driver is not exposing the write primitives without address
https://github.com/raspberrypi/linux/blob/rpi-6.6.y/drivers/i2c/i2c-dev.c
https://github.com/raspberrypi/linux/blob/rpi-6.6.y/Documentation/i2c/dev-interface.rst