Sunday 20 October 2024

Allsky: cooling my Raspberry Pi

The Pi 4 can get very hot.

When it does, it throttles the cpu.

So I've been working on a solution.

Allsky is not very demanding most of the time. My Pi 4 just takes photos every 30 seconds or so, and the cpu temperature sits in the 40-60'C range.

However, at the end of the night, the system produces a timelapse, startrails and a keogram. These operations often result in the cpu hitting 80'C+, and the cpu speed is then automatically throttled back, well below the rated 1800MHz.

Having bought a heatsink and a small 30mm fan from PiHut, I first fitted just the XL heatsink, which is 40 x 30 x 5mm with a self-adhesive backing.

Using SSH to access the Allsky system via a terminal, I monitored the cpu temperature using the command:-

 watch cat /sys/class/thermal/thermal_zone0/temp

From a second terminal session, I forced the system to create a timelapse from a directory containing 1000 images using the command:-

 generateForDay.sh --timelapse 1000TESTimages

It was clear that the temperature was now lower by somewhere between 5 & 10'C (the temperature readings are very twitchy).

The next step was to connect the small fan to 5Volts, which I anchored on one of the Pi nylon pillars I've used to hold the Pi in the box (Note that the heatsink was not designed to integrate with a fan).


This reduced the temperature still further, but was a bit noisy. I ran the system outside, over night for about 12 hrs. By the time I brought the system inside in the morning, it sounded like there was an angry bee inside the box!

A second problem was battery drain. The fan is rated at 5V/200mA, so that's 1 watt of extra power, a huge increase for my PowerBank, given that the Pi consumption sits between 1-2 Watts most of the time.

I still wanted to make use of the fan, but only wanted it to run when the temperature peaked at the end of the night.

The Pi includes a simple fan control feature. This can be enabled by adding the following line of text to the end of the: /boot/firmware/config.txt

 dtoverlay=gpio-fan,gpiopin=23,temp=70000,hyst=5000

Using these values, the fan can be turned on via gpio pin 23 when the temperature reaches 70'C, and turned off when the temperature falls by 5'C (the hysteresis value). But naturally, these can be set to suit given circumstances.

Note: there are plenty of projects on Github for fan control software where Pulse Width Modulation (PWM) is used to control the fan speed. In my case, I didn't think this necessary.

The only other consideration is that the fan needs to be driven via a suitable additional driver circuit.

transistor fan driver

If you are a 'softie' with no hardware experience, this circuit diagram may not make any sense, so here is a description.


This simple fan interface circuit provides enough current to drive a small 5Volt fan to cool the cpu heatsink.

The basic requirement is to interface between the Raspberry Pi gpio output, which is a 3.3V low current signal, and to operate a 5V 200mA fan.

The transistor can be almost any general purpose NPN transistor with a maximum collector current rating well above 200mA.

The 'NPN' bit means it can be used in a common emitter configuration in a circuit fed from a positive supply; 'common emitter' simply means that the transistor emitter is connected to common (0Volts) either directly or indirectly.

As I had a few BC337 transistors in my bit box, this is the type that I used. This transistor has a max rated collector current of 800mA.

The base current limiter in this circuit is a resistor, and we can work out the required value as follows;

To turn the fan fully on, approx 200mA needs to flow through the fan and through the transistor from collector to emitter.

The datasheet for this transistor states that the worst case gain is 60. So we need a base current of 200/60 = 3.3mA.

Therefore the resistor needs to be:-

 3.3V (the gpio output voltage) minus 0.7V (the oft quoted base-emitter voltage) divided by the required base current. So:-

 R = (3.3-0.7)/3.3 {in kOhms}

 R = 0.788k or 788 Ohms

I don't have any resistors around this value, but have loads of 470 Ohm. So plugging this value in and working the figures back;

 Base current = (3.3-0.7)/0.47 = 5.5mA

 Collector current = 5.5mA x 60 = 332mA

However, the transistor's collector current is limited to 200mA by the fan. So its OK to use 470 Ohms as the base resistor.

When the current is removed from an inductive component such as the fan, a reverse voltage in generated. The role of the diode is to safely dissipate this voltage each time the fan is turned off by the transistor. Its possible that the fan may have some similar form of protection built in. But as I could not verify this, its safer to add this external diode.

I could have used a 1N4148 silicon diode (I have a few) but my preference was to use a 1N4000 series, simply because I wanted 'stout' component leads to provide good 'anchor points' to attach components & wires.

Fan direction

Initially the fan was sucking air across the heatsink instead of blowing, so I took off the components, turned the fan over, and re-attached the bits back to the fan bolts.


Don't try this at home! unfortunately, the base resistor was just touching the fan blade, and in trying to free it (...while it was powered up & running!) I accidentally stalled the fan.

The result was a sharp rise in [stall] current which blew the transistor, so it had to be replaced!

I didn't need to reverse this fan. It was cooling the heatsink just fine the wrong way up, and it meant the components could rest on the fan body.

But unfortunately I could not rest. The engineer inside me said "that's not right!" so I had to turn it over! 


See also:-

http://captainbodgit.blogspot.com/2024/08/allsky-taking-pictures-of-night-sky.html

http://captainbodgit.blogspot.com/2024/09/allsky-new-box-dark-frames-other-tweaks.html


No comments:

Post a Comment