So I‘m currently working on a program to turn on or off my Philips Hue lights when I enter my room. The detection if somebody is coming or which direction he is going is provided by one HCSr505 and two HCSr04 sensors. The program is a java program with the official Hue API. So now my problem is that the Raspberry Pi 3, which the whole system is running on, is getting quite hot (around 60 °C) over time( the system is running 24/7, but the loops to detect the distance from the HCSr04 is only running when the HCSr505 is detecting any movement, at certain day times all loops are paused, because I don’t need to turn on light when the sun is shining). I know that 60° is okay for the raspberry pi but nevertheless I’m a little bit worried because the system is placed in a wardrobe where there’s no way the hot air can escape. I‘ve also mounted heatsinks onto the pi. Is there any way I can decrease the temperature while running my java Programm? Here's the cpu usage while running the program: enter image description here

And here's cpu usage I got from the iostat command: enter image description here

And here is the code: (second class only contains the Hue API and bridge connection)

import java.time.LocalDateTime;
import java.time.ZoneId;
import com.philips.lighting.hue.sdk.PHHueSDK;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.RaspiPin;

public class Distance {

//GPIO Pins
private static GpioPinDigitalOutput sensorTriggerPin ;
private static GpioPinDigitalInput sensorEchoPin ;
private static GpioPinDigitalOutput sensorTriggerPin1;
private static GpioPinDigitalInput sensorEchoPin1 ;
private static GpioPinDigitalInput pir;
int f=0;
ZoneId id;
public static double distance2;
public static double distanceold;
public static double distanceold1;
public static double distance3;
public static boolean left=false;
public static boolean right=false;
public static long timeleft;
public static long timeright;
public int counter;

final static GpioController gpio = GpioFactory.getInstance();

public static void main(String [] args) throws InterruptedException{
    //ZoneId id=ZoneId.of("Europe/Berlin");

    int hour = LocalDateTime.now().getHour();
    if( hour>17  || hour<7) {

    }else {
        System.out.println("Hour not in estimated working time");
        System.out.println("Hour:"+ hour);
    pir = gpio.provisionDigitalInputPin(RaspiPin.GPIO_07, PinPullResistance.PULL_DOWN);

    sensorTriggerPin =  gpio.provisionDigitalOutputPin(RaspiPin.GPIO_00); // Trigger pin as OUTPUT // rechts von Schrank aus
    sensorEchoPin = gpio.provisionDigitalInputPin(RaspiPin.GPIO_02,PinPullResistance.PULL_DOWN); // Echo pin as INPUT

    sensorTriggerPin1= gpio.provisionDigitalOutputPin(RaspiPin.GPIO_27); // links von Schrank aus
    sensorEchoPin1= gpio.provisionDigitalInputPin(RaspiPin.GPIO_25,PinPullResistance.PULL_DOWN);

    new UltraHue();

private int hour;
    public void run() throws InterruptedException {
            hour = LocalDateTime.now().getHour();
        while( hour>17 || hour<7 ){

            if(pir.isHigh()){                // waiting for my HCsr505 to 
                                             //detect any movement

            if (getdistance(sensorEchoPin, sensorTriggerPin) < 70 && distanceold<70 ) {       //rechts vom Schrank
                right = true;
                if (left) {
            distanceold=getdistance(sensorEchoPin, sensorTriggerPin);

            if (getdistance(sensorEchoPin1, sensorTriggerPin1) < 70 && distanceold1<70) {           //links vom Schrank
                left = true;
                if (right) {
             distanceold1 = getdistance(sensorEchoPin1, sensorTriggerPin1);
            if (left || right) {

                if (counter++ >= 70) {



    private void reset(){
        counter = 0;
        left = false;
        right = false;

    private static long endTime;
    private static long startTime;
    private static long timedout;

    public static double getdistance(GpioPinDigitalInput sensorEchoPin3, GpioPinDigitalOutput sensorTriggerPin3) throws InterruptedException{
        sensorTriggerPin3.high(); // Make trigger pin HIGH
        Thread.sleep((long) 0.01);// Delay for 10 microseconds
        sensorTriggerPin3.low(); //Make trigger pin LOW
        timedout= System.nanoTime();

        while(sensorEchoPin3.isLow()){ //Wait until the ECHO pin gets HIGH


        startTime= System.nanoTime(); // Store the current time to calculate ECHO pin HIGH time.            
        while(sensorEchoPin3.isHigh()){ //Wait until the ECHO pin gets LOW

        endTime= System.nanoTime(); // Store the echo pin HIGH end time to calculate ECHO pin HIGH time.
        return(((endTime-startTime)/1e3)/2) / 29.1;


migrated from stackoverflow.com May 14 '18 at 7:46

This question came from our site for professional and enthusiast programmers.

  • 1
    What steps have you done to identify the problem? How do you know it is associated with our program? Do you see high cpu usage when it gets hot. Please edit your question and add details. – Yeti May 12 '18 at 20:04
  • Did you install a heat sink? – Attersson May 12 '18 at 20:14
  • @Yeti I updated my question with the cpu usage and it looks like the programm consumes 99,3 % of the whole cpu power, or am I interpreting the chart wrong? – Janik May 13 '18 at 17:04
  • @Attersson yes a heatsink is installed – Janik May 13 '18 at 17:04
  • @Hack-R I try to avoid a fan because the system is installed in my room, where I sleep and work, the noise would drive me crazy :/ – Janik May 13 '18 at 17:05

56,4°C is nothing I would worry about, this is not a threat to your RPi. The firmware will show you a temperature warning starting at 80°C and "if the temperature of the SoC is over 85C, the [...] ARM core(s) and the GPU will be throttled back in an attempt to reduce the core temperature" (source: Firmware warning icons - Raspberry Pi Documentation). It's pretty self-regulatory.

The central problem is that your RPi "is placed in a wardrobe where there’s no way the hot air can escape". This restricts your options plainly, because no heatsink or fan can lead the heat away without sufficient airflow.

A large integrated heat spreader (IHS) could help: "a spreader is meant to redistribute heat, not to remove it" (source: Computer cooling - Wikipedia). But I think it's unlikely to find one that fits.

By experience: heatsinks can reduce the average temperature by 5 to 10°C. They are not very effective.

If your RPi has heavy load, I would recommend a fan, but based on your screenshots your CPU is 77% idle and the "load avg" is very low (zero point something). Maybe better code can reduce load/heat, but your system status looks fine to me. Your SoC has only around 60°C, so I would not recommend to invest time on a sideshow like code review and software optimisation. (Side note: to monitor your RPi you can use Munin.)

You've written in a comment: "I try to avoid a fan because the system is installed in my room, where I sleep and work, the noise would drive me crazy." There are cheap and small 5 Volt fans available. You can operate it on 3,3 Volt, which reduces speed (a bit) and noise (much). I'd say it's nearly noiseless (this may vary from fan to fan and is subjective) and a fan does a much better job in comparison to heatsinks.

The combination of heatsinks and fan works very well in general.

I recommend to move the RPi to a better suited location. Maybe you can put it on top of your wardrobe or attach it to the rear using Velcro tape. Then your RPi should have sufficient airflow and you don't risk to burn down the house.

Edit: I've just found this video showing a very effective heatsink solution: Raspberry Pi 3: Extreme Passive Cooling. Impressive results!


Your application should not be hogging an entire core; notice the amalgamated figures (the average of four cores) are 20-25% and the listing in top is 99% (top can show individual threads, so this is the usage of one core).

While if nothing much else is going on this may work out okay (the task will hopefully rotate from core to core), it is unnecessary, particularly since you are probably begging for it not to work out okay at some point. This is not a very robust methodology.

The problem is here:


If this is about video motion detection, this could be much longer. You could use something in the range of at least 100 - 200 ms and everything will work the same anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.