Beginner’s Guide to Raspberry Pi Python Programming – Displaying CPU Temperature with a Two-Digit Display

Displaying CPU Temperature with a Two-Digit Display

Overview

Beginner's Guide to Raspberry Pi Python Programming - Displaying CPU Temperature with a Two-Digit Display

A few days ago, we learned how to use a one-digit display to show single-digit countdowns. This time, we will learn how to use a two-digit display to show the CPU temperature.

In this lesson, we will learn:

  • The display principle of a two-digit display

  • Persistence of vision display techniques

Required Hardware

  • Raspberry Pi x1

  • Breadboard x1

  • Dupont wires x9

  • Two-digit display x1

Principle Explanation

Today we are using the 3261BS model of a two-digit common anode display. Let’s first take a look at its circuit diagram:

Beginner's Guide to Raspberry Pi Python Programming - Displaying CPU Temperature with a Two-Digit Display

From the previous article, we can understand the circuit diagram of a one-digit display, and a two-digit display can be simply understood as a combination of two one-digit displays.

Looking at the bottom image, pin 10 and pin 5 are the common anodes for the first and second digits of the display, respectively.

Objective 1: Light up the A segment of display 1 and display 2

Display Expected Effect Required Circuit Control
Display 1 A segment lit Pin 10 high voltage, pin 3 low voltage
Display 2 A segment lit Pin 5 high voltage, pin 3 low voltage

This is the same as operating a one-digit display, but the next objective may be more troublesome:

Objective 2: Light up the A segment of display 1 while keeping the A segment of display 2 off

Display Expected Effect Required Circuit Control
Display 1 A segment lit Pin 10 high voltage, pin 3 low voltage
Display 2 A segment off Pin 5 high voltage, pin 3 high voltage

The problem arises: since pin 3 is shared, this circuit control cannot be achieved. So how do we solve this problem? To solve this problem, we need to use persistence of vision.

Persistence of Vision

This principle originated in film production. Scientific experiments have shown that the human eye can retain an image on the retina for about 0.1 to 0.4 seconds after the image disappears. Film reels rotate at a uniform speed of 24 frames per second, and a series of still images create a continuous visual impression due to the persistence of vision, producing a realistic sense of motion. The fluorescent lights commonly used at home also utilize this principle; they do not remain constantly lit but flicker at a high frequency. When the flickering frequency exceeds 25 times per second, the human eye perceives it as being constantly lit.

We can utilize this principle: as long as we display the two digits of the display in sufficiently short intervals, the human eye will perceive that these two digits are displayed simultaneously. Therefore, the circuit control method we adopt is time-sharing allocation for pin 3: 0.01 seconds for the A segment of display 1, followed by 0.01 seconds for the A segment of display 2. This way, we can refresh 50 times in one second to ensure that the display does not flicker.

Wiring Diagram

Before connecting the hardware circuit, the first thing to do is to shut down the Raspberry Pi and disconnect the power supply. This is because connecting the circuit while the Raspberry Pi motherboard is powered on may damage electronic components, so remember:

The motherboard must be powered off when connecting the circuit.

Example Code

#!/usr/bin/env python

# encoding: utf-8

”’

Wiring Diagram:

RPi digital

7 <—> 10

11 <—> 5

13 <—> 3

15 <—> 9

29 <—> 8

31 <—> 6

33 <—> 7

35 <—> 4

37 <—> 1

”’

import RPi.GPIO as GPIO

import time

import os

# Pins used for the positive terminals of displays 1 and 2

LED_POWER_1 = 7

LED_POWER_2 = 11

# Pins used for segments A to G

LED_A = 13

LED_B = 15

LED_C = 29

LED_D = 31

LED_E = 33

LED_F = 35

LED_G = 37

# Get CPU temperature

def get_cpu_temperature():

return os.popen(‘vcgencmd measure_temp’).read()[5:7]

# Reset the display, turning off all digits

def reset():

GPIO.output((LED_POWER_1, LED_POWER_2), GPIO.LOW)

GPIO.output((LED_A, LED_B, LED_C, LED_D, LED_E, LED_F, LED_G), GPIO.HIGH)

# Set which display to light up

def set_position(position):

if position == 1:

GPIO.output(LED_POWER_1, GPIO.HIGH)

else:

GPIO.output(LED_POWER_2, GPIO.HIGH)

# Show number 0

def show0(p):

reset()

set_position(p)

GPIO.output((LED_A, LED_B, LED_C, LED_D, LED_E, LED_F), GPIO.LOW)

# Show number 1

def show1(p):

reset()

set_position(p)

GPIO.output((LED_B, LED_C), GPIO.LOW)

# Show number 2

def show2(p):

reset()

set_position(p)

GPIO.output((LED_A, LED_B, LED_D, LED_E, LED_G), GPIO.LOW)

# Show number 3

def show3(p):

reset()

set_position(p)

GPIO.output((LED_A, LED_B, LED_C, LED_D, LED_G), GPIO.LOW)

# Show number 4

def show4(p):

reset()

set_position(p)

GPIO.output((LED_B, LED_C, LED_F, LED_G), GPIO.LOW)

# Show number 5

def show5(p):

reset()

set_position(p)

GPIO.output((LED_A, LED_C, LED_D, LED_F, LED_G), GPIO.LOW)

# Show number 6

def show6(p):

reset()

set_position(p)

GPIO.output((LED_A, LED_C, LED_D, LED_E, LED_F, LED_G), GPIO.LOW)

# Show number 7

def show7(p):

reset()

set_position(p)

GPIO.output((LED_A, LED_B, LED_C), GPIO.LOW)

# Show number 8

def show8(p):

reset()

set_position(p)

GPIO.output((LED_A, LED_B, LED_C, LED_D, LED_E, LED_F, LED_G), GPIO.LOW)

# Show number 9

def show9(p):

reset()

set_position(p)

GPIO.output((LED_A, LED_B, LED_C, LED_D, LED_F, LED_G), GPIO.LOW)

GPIO.setmode(GPIO.BOARD)

# Initialize pin output mode

GPIO.setup((LED_POWER_1, LED_POWER_2, LED_A, LED_B, LED_C, LED_D, LED_E, LED_F, LED_G), GPIO.OUT)

function_directory = locals()

try:

while True:

cpu_temperature = get_cpu_temperature()

print ‘CPU temperature = %s°C’%(cpu_temperature)

loop = 0

while loop < 200:

loop += 1

# First show the high bit

position = 1

for n in cpu_temperature:

function_directory[‘show%s’%(n)](position)

time.sleep(0.01)

position += 1

except KeyboardInterrupt:

GPIO.cleanup()

Save as digital-2.py and run it.

sudo python digital-2.py

Final Effect

Beginner's Guide to Raspberry Pi Python Programming - Displaying CPU Temperature with a Two-Digit Display

https://github.com/zhongzhi107/raspberry-pi-tutorials/tree/master/03fading-led

Leave a Comment

×