Blog 08

From BitWizard Wiki
Jump to navigation Jump to search

CPU & GPU sensor

For this I combined 2 codes. (The code for reading the cpu and gpu and the clock code from Blog 04) Together they should display the CPU + GPU temperature on your display, which should be rechecked every 5 seconds. The temperatures will be visible on your display.

The code first gets the CPU temp with thermal_zone0 The result would be something like: 36318. 36318 goes trough 1000 to get it to 36. ( it only looks at the full numbers, otherwise it should be 36.318 ) Then the line under it does 36318 trough 100, what makes it 363 After we use %. What % does is that it divides 363 with full 36's and the time it can't get divided with 36 it will give the amount of numbers it got left. (What is of course 3.) This is a smart trick to get the 1 decimal place after the number. The (( )) is always needed at +, *, /, %. ( for the -, ** you always use [[ ]] )

After that we get the GPU temperature. The result from measure_temp would normally look something like: temp=34.7'C

We are going to print 'GPU Temp: 'before the temperature, so we don't want 'temp=' to appear. To remove it we do: gpuTemp0//temp= to say temp= has to go away

( Sadly the raspberry pi display doesn't know the Celsius mark so I replaced it with: ' )

#!/bin/bash 
DISPL="bw_tool -I -D /dev/i2c-1 -a 94"  

$DISPL -W 10:0:b  

while true; do 
       cpuTemp0=$(cat /sys/class/thermal/thermal_zone0/temp) 
       cpuTemp1=$(($cpuTemp0/1000)) 
       cpuTemp2=$(($cpuTemp0/100)) 
       cpuTempM=$(($cpuTemp2 % $cpuTemp1)) 

       gpuTemp0=$(/opt/vc/bin/vcgencmd measure_temp) 
       gpuTemp0=${gpuTemp0//temp=/} 

       $DISPL -W 11:00:b 
       $DISPL -t "CPU Temp: "$cpuTemp1"."$cpuTempM"'C" 

       $DISPL -W 11:20:b 
       $DISPL -t "GPU Temp: "$gpuTemp0 

       sleep 5 
  
done


CPU&GPU.jpg


The maximum temperature the raspberry pi should get is 70 degrees! If it is getting higher than 70 degrees it is recommended to cool the Raspberry pi down as fast as you can!


Look at my other projects at the Blog list