Raspbian: handling temperature sensor and leds on the Raspberry Pi

In this short tutorial I'm going to show to you how to build a temperature alarm with the Raspberry Pi.

Raspbian is a free operating system based on Debian optimized for the Raspberry Pi hardware. An operating system is the set of basic programs and utilities that make your Raspberry Pi run. However, Raspbian provides more than a pure OS: it comes with over 35,000 packages, pre-compiled software bundled in a nice format for easy installation on your Raspberry Pi.

The Raspberry Pi provides a lot of hardware, two items of this HW are very interesting to control the status of the system.
The first one is the temperature sensor into the CPU and the other one is the presence of two leds.
 
We can think to use the leds to signal (with a fade in fade out effect)  when the temperature overcomes a threshold.
 
Raspbian provides the access to these two devices using the sys filesystem. So they are very simple to control also using the bash.
 
 
Temperature sensor
The temperature sensor is directly accessible through the sys filesystem, So if you want to monitorize the temperature you can use standard tools from the bash. 
 
Try issueing the following command:
 
# watch -d cat /sys/class/thermal/thermal_zone0/temp
 
you should view an output like the following:
 
the output will be refreshed every two seconds (see man watch), stop the watch command hitting CTRL-C
 
Using the LED
 
You can connect many leds to the gpio output of your Raspberry Pi, but you already have four leds wired onto the board. 
 
The RED LED is hardwired to the onboard 3V3
the FDX, LNK/10M are hardwired to the USB/Ethernet Chip
the GREEN LED is hardwired to GPIO16
 
So the first 3 leds cannot be used, while we are going to use the last one.
The green leds is available through the sysfs at 
/sys/class/leds/led0
 
The default trigger for this led is 'mmc0' , so before using this led you have to deactivate this trigger. Simply issue the following command (you must be root or you need to be sudo):
 
# echo none >/sys/class/leds/led0/trigger
 
to turn on/off the leds you must write on /sys/class/leds/led0/brightness
0 turn off
1 turn off
try issueing the following commands
 
# echo 1 >/sys/class/leds/led0/brightness
# echo 0 >/sys/class/leds/led0/brightness
 
 
to restore the original functionality issue the following command:
 
# echo mmc0 > /sys/class/leds/led0/trigger
 
 
 
Mixing the things
 
Now we are going to write a simple shell script which will flash the led when the teperature overcomes 40 degrees.
 
in pseudo code
 
while (1)
{
    temp = currentTemperature
    if temp > 40
        start flashing led 0
    else
        stop flashing led 0
    sleep 5 seconds
}
 
and now let's start coding:
 
#!/bin/bash
fault=0
while [ 1 ] ; do
       temp=cat /sys/class/thermal/thermal_zone0/temp
        if [ $temp -gt 40000 ]
        then
                fault=1
                echo "Starting flash"
        else
                if [ $fault -eq 1 ]
                then
                        fault=0
                        echo "Stopping flash"
                fi
        fi
        sleep 5
done

 

 

 

 
In the above code I've added the fault variable to avoid the stop of the led blinking at each loop (I'm going to stop the flashing only if it is yet blinking).
 
At this point we have only to drive the led. A kernel module, can help us to do this job, it is the ledtrig_heartbeat module. we can attach this module to the trigger of the led. with the following commands:
 
# modprobe ledtrig_heartbeat
# echo heartbeat >/sys/class/leds/led0/trigger
 
and we will stop the blinking with this command:

 

 

 

# echo none >/sys/class/leds/led0/trigger

 

 

 

 
Here you are the final code:
 
#!/bin/bash
modprobe ledtrig_heartbeat
fault=0
echo none >/sys/class/leds/led0/trigger
while [ 1 ] ; do
       temp=cat /sys/class/thermal/thermal_zone0/temp
       echo $temp
        if [ $temp -gt 40000 ]
        then
                if [ $fault -eq 0 ]
                then
                    fault=1
                    echo "Starting flash"
                    echo heartbeat >/sys/class/leds/led0/trigger
                fi
        else
                if [ $fault -eq 1 ]
                then
                        fault=0
                        echo "Stopping flash"
                        echo none >/sys/class/leds/led0/trigger
                fi
        fi
        sleep 5
done

 

 

 

 
save this script (I named it "my.sh"), then chmod +x it
# chmod +x my.sh
 
last, try to run it
 
root@raspberrypi:/home/pi# ./my.sh
40084
Starting flash
39546
Stopping flash
39546
39007
39546
39007
40084
Starting flash
40084

 

 

 

 
After you read "Starting flash" you should see the led blinking. Try to change the temperature, using a lighter or something similar, Note I'm not responsible for any damages you can cause. So be careful.
 
 
 
 

You may also like...

2 Responses

  1. Rusty Haddock says:

    The only thing I'd change is your line:
        temp=cat /sys/class/thermal/thermal_zone0/temp

    to this:
        read temp < /sys/class/thermal_thermal_zone0/temp
    Reading the temperature this way keeps another shell from starting up for each loop to evaluate the 'cat'.   Just another way to do it.  I've been working on some OpenWRT-based routers on a project or two that uses up most of the memory available.  Since there's not swap space… well, I just have to be careful with my memory. 🙂
    Nonetheless, good little article!
        -Rusty-

Leave a Reply

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