简体   繁体   中英

Raspberry Pi - Bash script causing overheating

I have a Bash script (see below) which controls a relay via the GPIO pins. It works fine but I've noticed the CPU of the Pi gets very hot - up to 83C or so when it runs, up from around 40C when the Pi's idle.

I've looked at CPU usage and the script, which runs all the time, uses 16% of CPU.

Has anyone got any ideas why this overheating is happening?

Thanks.

    #!/bin/bash

    while true; do
    s=$(date +%S)

    if [ $s -eq 58 -o $s -eq 28 ]; then
    sleep 1.9

    echo "1" > /sys/class/gpio/gpio6/value
    sleep 0.5
    echo "0" > /sys/class/gpio/gpio6/value
    sleep 1
    fi

    done

There are two things that I am thinking of. You are running through many more loops than you need to since it seems that you want to turn a relay on for a half a second every 30 seconds. You could sleep the whole loop 1 second doing the following

#!/bin/bash

while true; do
s=$(date +%S)

if [ $s -eq 58 -o $s -eq 28 ]; then
sleep 1.9

echo "1" > /sys/class/gpio/gpio6/value
sleep 0.5
echo "0" > /sys/class/gpio/gpio6/value
fi

sleep 1

done

The other thing I'm thinking of is the following:

One you are checking a bunch of Most likely drawing too much current in the relay circuit. Do you have any current limiting resistors in series with the relay? That is the first thing I would check as you don't have any massive computations happening in your bash script. This is also very much overkill for a Rasperry Pi, but that's an entirely different beast.

The GPIO pins are rated for only a few milliamps. Not enough to operate most relays without a boost from a transistor or Darlington, depending on the relay type. Properly biasing the transistor will limit the current from the GPIO pin and also provide enough current to operate the relay, if chosen correctly. Without this risk your Pi doing this, probably. And then it's off to the Electronics Stack Exchange ;)

And as others have noted, you're in a tight loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM