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
raspberry pi cpu temperature
raspberry pi cpu temperature

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.

TM1637 based temperature display for raspberry pi

What you need

  • Raspberry pi
  • TM1637 based 7 segment display
  • Jumpers for connecting ESP32 with TM1637

Connections

Raspberry PiTM1637 Board
3V3 (1)VCC
GND (9)GND
5 (GPIO3)CLK
3 (GPIO2)DIO
Connecting TM1637 with raspberry pi

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 : TM1637 counter

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.

Python : TM1637 based temperature display
One Comment

Add a Comment

Your email address will not be published. Required fields are marked *