Skip to content

Instantly share code, notes, and snippets.

@maxme
Last active August 5, 2023 06:45
Show Gist options
  • Star 54 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save maxme/d5f000c84a4313aa531288c35c3a8887 to your computer and use it in GitHub Desktop.
Save maxme/d5f000c84a4313aa531288c35c3a8887 to your computer and use it in GitHub Desktop.
Check your Raspberry pi power supply and USB cable
#!/bin/bash
# Before running this script, make sure you have sysbench installed:
# sudo apt-get install sysbench
#
# This script helps you check if your Raspberry pi is correctly powered.
# You can read more about Raspberry pi powering issues here: https://ownyourbits.com/2019/02/02/whats-wrong-with-the-raspberry-pi/
# If you're pi is correctly powered (stable power supply and quality cable), after running the script, you should get something like:
#
# 45.6'C 1400 / 600 MHz 1.3813V -
# 55.3'C 1400 / 1400 MHz 1.3813V -
# 58.0'C 1400 / 1400 MHz 1.3813V -
# 60.2'C 1400 / 1400 MHz 1.3813V -
# 60.2'C 1400 / 1400 MHz 1.3813V -
# 61.1'C 1400 / 1400 MHz 1.3813V -
# 61.1'C 1400 / 1400 MHz 1.3813V -
# 60.8'C 1400 / 1400 MHz 1.3813V -
# If your power supply can't provide a stable 5V 2.5A or if the cable is not good enough, you should get something like:
#
# 39.7'C 1400 / 1400 MHz 1.3875V - Throttling has occurred, Under-voltage has occurred,
# 48.3'C 1400 / 1400 MHz 1.3875V - Throttling has occurred, Under-voltage has occurred,
# 52.1'C 1400 / 1400 MHz 1.3875V - Throttling has occurred, Under-voltage has occurred,
# 54.8'C 1400 / 1400 MHz 1.3875V - Throttling has occurred, Under-voltage has occurred,
# 55.8'C 1400 / 1400 MHz 1.3875V - Throttling has occurred, Under-voltage has occurred,
# 56.4'C 1400 / 1400 MHz 1.3875V - Throttling has occurred, Under-voltage has occurred,
# 57.5'C 1400 / 1400 MHz 1.3875V - Throttling has occurred, Under-voltage has occurred,
# 58.0'C 1400 / 1400 MHz 1.3875V - Throttling has occurred, Under-voltage has occurred,
# 59.6'C 1400 / 1400 MHz 1.3875V - Throttling has occurred, Under-voltage has occurred,
function throttleCodeMask {
perl -e "printf \"%s\", $1 & $2 ? \"$3\" : \"$4\""
}
# Make the throttled code readable
#
# See the `get_throttled` method documentation on: https://www.raspberrypi.org/documentation/raspbian/applications/vcgencmd.md
#
function throttledToText {
throttledCode=$1
throttleCodeMask $throttledCode 0x80000 "Soft temperature limit has occurred, " ""
throttleCodeMask $throttledCode 0x40000 "Throttling has occurred, " ""
throttleCodeMask $throttledCode 0x20000 "Arm frequency capping has occurred, " ""
throttleCodeMask $throttledCode 0x10000 "Under-voltage has occurred, " ""
throttleCodeMask $throttledCode 0x8 "Soft temperature limit active, " ""
throttleCodeMask $throttledCode 0x4 "Currently throttled, " ""
throttleCodeMask $throttledCode 0x2 "Arm frequency capped, " ""
throttleCodeMask $throttledCode 0x1 "Under-voltage detected, " ""
}
# Main script, kill sysbench when interrupted
trap 'kill -HUP 0' EXIT
sysbench --test=cpu --cpu-max-prime=10000000 --num-threads=4 run > /dev/null &
maxfreq=$(( $(awk '{printf ("%0.0f",$1/1000); }' < /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq) -15 ))
# Read sys info, print and loop
while true; do
temp=$(vcgencmd measure_temp | cut -f2 -d=)
real_clock_speed=$(vcgencmd measure_clock arm | awk -F"=" '{printf ("%0.0f", $2 / 1000000); }' )
sys_clock_speed=$(awk '{printf ("%0.0f",$1/1000); }' </sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq)
voltage=$(vcgencmd measure_volts | cut -f2 -d= | sed 's/000//')
throttled_text=$(throttledToText $(vcgencmd get_throttled | cut -f2 -d=))
echo "$temp $sys_clock_speed / $real_clock_speed MHz $voltage - $throttled_text"
sleep 5
done
@izmeez
Copy link

izmeez commented Oct 19, 2019

Update to my original comment below, the script works fine when entered as:
$ bash raspberry-power-supply-check.sh

Sorry for any concerns raised and thanks for the script :-)

The script failed when entered as
$ sh .raspberry-power-supply-check.sh

Tried the script on raspberry pi 3 running raspbian buster and get the following errors:
raspberry-power-supply-check.sh: 35: raspberry-power-supply-check.sh: function: not found
String found where operator expected at -e line 1, near "& ? """
(Missing operator before ""?)
syntax error at -e line 1, near "& ? """
Execution of -e aborted due to compilation errors.
raspberry-power-supply-check.sh: 37: raspberry-power-supply-check.sh: Syntax error: "}" unexpected

Copy link

ghost commented Mar 1, 2020

Do run this command in sequence... 

sudo apt-get install sysbench dos2unix -y
sudo chmod +x *.sh
sudo dos2unix *.*
bash raspberry-power-supply-check.sh

It ran perfectly after that ... ;) regards from rodyeo @ http://rodyeo.dyndns.org/

@CalvinVoxox
Copy link

According to the manual for vcgencmd, these are the correct hex values

  throttleCodeMask $throttledCode 0x80000 "Soft temperature limit has occurred, " ""
  throttleCodeMask $throttledCode 0x40000 "Throttling has occurred, " ""
  throttleCodeMask $throttledCode 0x20000 "Arm frequency capping has occurred, " ""
  throttleCodeMask $throttledCode 0x10000 "Under-voltage has occurred, " ""
  throttleCodeMask $throttledCode 0x8 "Soft temperature limit active, " ""
  throttleCodeMask $throttledCode 0x4 "Currently throttled, " ""
  throttleCodeMask $throttledCode 0x2 "Arm frequency capped, " ""
  throttleCodeMask $throttledCode 0x1 "Under-voltage detected, " ""

@maxme
Copy link
Author

maxme commented Jul 8, 2020

Thanks! The source I was looking was using the wrong endianness. I updated the gist.

@hardenchant
Copy link

echo "deb http://deb.debian.org/debian buster-backports main" >> /etc/apt/sources.list
apt-get update
apt-get -t buster-backports install sysbench

May be useful for installing to 64-bit Raspbian OS

@sknig
Copy link

sknig commented Jun 6, 2021

Is it fine if the CPU gets only 0,8V, but no error occurs?

@timwegenerDE
Copy link

@passiveprogrammer, same question. With "bad" power-supply: 0,9 V but error (Under-voltage), with (I hope) a good one: 0,8 V but no error.

(RPi 4 8 GB, RaspberryPi OS, 64 Bit)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment