ITCooky Recipes

Lets cooky it yammy things!

DIY: Selfmade fan controller for Orange Pi PC Plus!

дата November 7, 2019

The circuit has already been made for another orangepi, but right now I took the soldering in my handes, and Orange Pi PC Plus became the main orangepi for my projects – thanks to eMMC and its stability and consumes less electricity (now it is important, everything is becoming important now) .

Here is the connection scheme. Transistor S9012, all resistors are 220 Ohm. On the board, the fan is connected to the connector, black red is important, but there is no such connection in the circuit scheme because drawing in Fritzing is a crazy pain.

IMPORTANT: Interesting, but this circuit does not work on the Raspberry Pi 3 in the NesPi case: there the energy comes from the pins of the case, not the Raspberry. Not at all, only the fan shakes a little, when I disconnect everything from the base …

Here is the picture of the finished board, the resistance is the same, the brand is different

Links to components, maybe already old, I bought them a long time ago
5V 0.2A fan
https://ru.aliexpress.com/item/Raspberry-PI-Fan-Active-Cooling-Fan-for-Customized-Acrylic-Case-5V-plug-in-and-play-Support/32294328071.html
Mix boards 5×7 4×6 3×7 2×8
Mix transistorhttps://ru.aliexpress.com/item/15Values-TO-92-Transistor-Assorted-Kit-S9012-S9013-S9014-S8050-S8550-2N3904-2N3906-BC327-BC337-Tl431/32384690394.html

To communicate with GPIO you have to install WiringOP

git clone https://github.com/zhaolei/WiringOP.git -b h3
cd WiringOP
chmod +x ./build
sudo ./build

The easiest way is to run a script through cron, which will look at the temperature and execute the fan start command

The LED also lights up, if the temperature is more than 45 degrees it is red, if less it is blue. Verify the path to the script
whereis bash
the interesting thing is, if the path is not correct, the script will work from the console, but not from cron, who knows why

We will create a script in the root directory

vi cool.sh

Add code

#!/bin/bash
C=`/bin/cat /sys/class/thermal/thermal_zone0/temp`
if [ $C -ge 45000 ]
then
  /usr/local/bin/gpio mode 1 out&&/usr/local/bin/gpio write 1 off&&/usr/local/bin/gpio mode 3 out&&/usr/local/bin/gpio write 3 on&&/usr/local/bin/gpio write 2 off
else
/usr/local/bin/gpio write 1 on&&/usr/local/bin/gpio write 3 off&&/usr/local/bin/gpio mode 2 out&&/usr/local/bin/gpio write 2 on
fi

make it executible
chmod +x cool.sh
add time to start
crontab -e
the line

*/1 * * * * /root/cool.sh

and it works

But only once a minute … you can do it in C and it will launch once a second, soo it is too repeated, you will have to pause it

Let’s go to the folder with WiringOP examples
cd /root/WiringOP/examples
Let’s open file
vi acool.c
Add code

#include <stdio.h>
#include <wiringPi.h>


#define LEDB 3
#define LEDR 2
#define COOLER 1
#define WARNING_TEMP 45000

int main (void)
{
  int number1;

  wiringPiSetup ();
  pinMode (LEDB, OUTPUT);
  pinMode (LEDR, OUTPUT);
  pinMode (COOLER, OUTPUT);
 for (;;)
  {

FILE *in_file;


  in_file = fopen("/sys/class/thermal/thermal_zone0/temp", "r");
  if (in_file == NULL)
    {
        printf("Can't open file for reading.\n");
    }
    else
    {
        fscanf(in_file, "%d", &number1);
        fclose(in_file);
    }
 if ( number1 <= WARNING_TEMP)
{
    digitalWrite (LEDB, HIGH);  // On
    digitalWrite (COOLER, HIGH);   // Off
    digitalWrite (LEDR, LOW);   // Off
    delay (500);               // mS
    digitalWrite (LEDB, LOW);   // Off
    delay (500);

}
else
{
    digitalWrite (LEDR, HIGH);  // On
    digitalWrite (COOLER, LOW);  // On
    delay (50000);               // mS

}


  }
  return 0 ;
}

This is the path to the file with temperature in Armbian / sys / class / thermal / thermal_zone0 / temp

CAUTION: It turns out that C programs cycles will load the processor at 100%, is nothing surprising here. But in this script, the flashing LED provides a braking cycle so that CPU overload does not happen … but you must be ready

We compile program C
make acool

And run
./acool

And the program works. When the temperature is less than 45000 (45 Celsius), a blue light flashes, check once per second (this period sets the flashing interval: the LEDs play an important role here), when it rises above, the red light will turn on and the script freezes for 50 seconds, this helps not tu run fan too often.

This is just a C program, I didn’t understand how to turn it into a system demon. But it can simply be run from the beginning on boot!

the program already compiled, I put it in the root directory
cp /root/WiringOP/examples/acool /root/acool
edit cron
crontab -e
change old line qith new one

@reboot /root/acool

And it works!


Leave a Reply

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