Raspberry Pi onboard temperature sensors

From Wiki
Revision as of 10:32, 28 September 2012 by Stocksy (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The Raspberry Pi does have an I2C bus on board, but it transpires that this is for connecting sensors to the GPIO headers. The Broadcom SoC has its own sensors, which can be read using the utilities in /opt/vc.

# /opt/vc/bin/vcgencmd commands
commands="vcos, ap_output_control, ap_output_post_processing, pm_set_policy, 
pm_get_status, pm_show_stats, pm_start_logging, pm_stop_logging, version, 
commands, set_vll_dir, led_control, set_backlight, set_logging, get_lcd_info, 
set_bus_arbiter_mode, cache_flush, otp_dump, codec_enabled, measure_clock, 
measure_volts, measure_temp, hdmi_ntsc_freqs, render_bar, disk_notify, 
inuse_notify, sus_suspend, sus_status, sus_is_enabled, sus_stop_test_thread, 
egl_platform_switch, vctest_memmap, vctest_start, vctest_stop, vctest_set, vctest_get"

measure_temp is what we're after:

#/opt/vc/bin/vcgencmd measure_temp

Let's prove that it works:

# stress --cpu 1 -t 300 ; ./vcgencmd measure_temp | cut -d "=" -f2 | cut -d "'" -f1 
stress: info: [3319] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd
stress: info: [3319] successful run completed in 300s

This hammers the CPU for 5 minutes and then takes a temperature reading. It's increased by a few degrees, so it must be working(!). Let's say it is.

Anything less than 80ºC is OK. I run my RPi in a case without much ventillation and it never gets anywhere near this value. I still want a safety net though, so I put together a little script which will check the temperature every 5 minutes and shut down the box if it exceeds 78ºC.

# vi /usr/bin/tempcheck

#  This script reads the Broadcom SoC temperature value and shuts down if it 
#  exceeds a particular value.
#  80ºC is the maximum allowed for a Raspberry Pi.

# Get the reading from the sensor and strip the non-number parts
SENSOR="`/opt/vc/bin/vcgencmd measure_temp | cut -d "=" -f2 | cut -d "'" -f1`"
# -gt only deals with whole numbers, so round it.
TEMP="`/usr/bin/printf "%.0f\n" ${SENSOR}`"
# How hot will we allow the SoC to get?

if [ "${TEMP}" -gt "${MAX}" ] ; then
 # This will be mailed to root if called from cron
 echo "${TEMP}ºC is too hot!"
 # Send a message to syslog
 /usr/bin/logger "Shutting down due to SoC temp ${TEMP}."
 # Halt the box
 /sbin/shutdown -h now
  exit 0

Then, have cron call the script every five minutes, or as desired:

# crontab -e

*/5 *   *   *   *    /usr/bin/tempcheck