Tricks about Raspberry Pi

2020-01-21

This article is a bit old and the content may be outdated, so please refer to it with caution and remember to check the latest official materials (such as documentation, etc.)

Handy tricks when playing with raspberry pi. You should know them.

Get the Temperature #

cat /sys/class/thermal/thermal_zone0/temp
bash

Outputs:

29482
text

And here is bash script to create temperature PS1

if [ -e /sys/class/thermal/thermal_zone0/temp ]; then
    temp=$(cat /sys/class/thermal/thermal_zone0/temp)
    if [ $temp -lt 30000 ]; then color=2
    elif [ $temp -lt 50000 ]; then color=3
    else color=1
    fi
    prompt_section $color "${temp::${#temp}-3}.${temp:${#temp}-3}"
fi
bash

Adding a dot is just mixing bash substrings (${string:offset[:length]}) with bash string lengths (${#string})

vcgencmd

From https://www.raspberrypi.org/documentation/raspbian/applications/vcgencmd.md

To get all commands, use:

vcgencmd commands
Bash

Voltage #

vcgencmd get_throttled
Bash

If voltage is okay, you will get 0x0

Else, say 0x50000, convert it to binary, and see each bit:

19 18 17 16 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0
 0  1  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
text
BitMeaning
0Under-voltage detected
1Arm frequency capped
2Currently throttled
3Soft temperature limit active
16Under-voltage has occurred
17Arm frequency capping has occurred
18Throttling has occurred
19Soft temperature limit has occurred

Generally, the 3 0s in the middle do not have specific meaning. Just need to care about the first 5 and the last 0.

Screen on / off #

When you start the Raspberry Pi with display / monitor / screen on, it will automatically turn on display power. If not, display power will never turn on even if you later connect it to a screen. So it is necessary to control the display power.

vcgencmd display_power 1
Bash

1 for on and 0 for off. bare vcgencmd display_power shows current state.

It is possible to specify a certain display ID. For more info, visit the doc above.

Leave your comments and reactions on GitHub