2

I wish to control a 4 pin 12V dc pc fan (https://www.arctic.ac/en/F9-PWM/AFACO-090P2-GBA01) with my raspberry pie. After reading several threads I came up with the following:

First problem is that the PWM signal must be 5V but the GPIOs only give 3.3V. So I decided to use a level shifter from 3.3V to 5V (https://www.amazon.com/-/de/dp/B07LG646VS/ref=sr_1_3?dchild=1&keywords=level+shifter&qid=1605728038&sr=8-3) to feed a 5V PWM signal to the fan. Other than that I connected an external 12V dc power supply to the fan (first two pins on the fan connector).

The only thing left to do is to connect the GPIO pin which I use for the PWM signal to the 4th pin of the fan. In the end my circuit looks like the one from this tutorial: https://www.raspberrypi-spy.co.uk/2018/09/using-a-level-shifter-with-the-raspberry-pi-gpio/ except that I don´t use a 5V sensor but the 5V PWM input of the fan. So the PWM signal wire is the only connection between the pie and the fan.

Good thing is that the fan is running (at full speed) when I connect the power supply. I use the following code to vary the duty cycle.

import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.OUT)

p = GPIO.PWM(12, 25000)
p.start(0)

time.sleep(2)

try:
    while True:
        p.ChangeDutyCycle(100)
        print('100')
        time.sleep(5)
        p.ChangeDutyCycle(50)
        print('50')
        time.sleep(5)
        p.ChangeDutyCycle(0)
        print('0')
        time.sleep(5)
            
except KeyboardInterrupt:
    pass

p.ChangeDutyCycle(0)
p.stop()
GPIO.cleanup()

When I connect the fan to the dc power supply, connect the 5V PWM signal and start the script the fan will continue to run at full speed, no matter what.

To test if my circuit is wrong I installed a LED which gets the 5V PWM signal as the positive voltage. The LED changes from 100% brightness to 50% and then switches off as expected.

I tested every connection line of the four from the level shifter. I tested another fan. I switched to other GPIOs. Unfortuntely it´s always the same result. LED works perfectly. Fan runs at full speed all the time.

I really like the idea of using a level shifter. It´s a compact and simple solution for me. So my question is if somebody has an idea where the problem is and how to solve it.

A second question related to this is if it would be beneficial to use a L298n motor driver for fan control.

1
  • 2
    it is not raspberry pie
    – jsotola
    Nov 19 '20 at 2:10
0

You need to connect a ground wire between the Pi and the fan. Otherwise the PWM signal will have no effect as the fan will not know when the signal is high or low.

I doubt that software timed PWM will actually manage 25 kHz. I don't know how the fan will react at the likely frequency of about 10 kHz (depending on the Pi model being used).

3
  • Thanks a lot for the helpful answer! it solved the problem.
    – Kai
    Nov 19 '20 at 17:31
  • Additionally for other people who will read this, the PWM control on this fan also works with the 3.3V output of the GPIO pins. So the level shifter is not even necessary.
    – Kai
    Nov 19 '20 at 19:49
  • @Kai: There are many "fan questions" here. These fan projects serve a useful purpose as learning projects. I'm posting this as a suggestion to you: Please add your own answer here with a clear schematic that shows how you wired the fan to the RPi, and the code you used. That would be very useful, and I'd certainly up-vote a clear answer accompanied with working code and schematic. I suspect others would also. Here's the link to the schematic if you need it.
    – Seamus
    Nov 19 '20 at 20:55
0

Since everything is working now I want to post a summary of how I am controlling a 12V fan with my Pi.

My Hardware for this is:

  • 1x KeeYees 4-channel IIC I2C level shifter from 3.3V to 5V
  • 1x Artic F9 PWM fan
  • 1x 12V dc power supply

This is how my circuit looks like:

schematic

simulate this circuit – Schematic created using CircuitLab

I figured that you could skip the level shifter and feed the 3.3V PWM signal from the GPIO directly to the fan. But it is less stable from my experience. Sometimes the fan will go to full speed as if no PWM signal is present while controlling the fan. So I recommend using a level shifter.

I wrote the following script for testing (you may have to change the GPIO pin number):

import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.OUT)

p = GPIO.PWM(12, 1000)
p.start(0)

time.sleep(2)

try:
    while True:
        p.ChangeDutyCycle(100)
        print('100')
        time.sleep(5)
        p.ChangeDutyCycle(50)
        print('50')
        time.sleep(5)
        p.ChangeDutyCycle(0)
        print('0')
        time.sleep(5)
            
except KeyboardInterrupt:
    pass

p.ChangeDutyCycle(0)
p.stop()
GPIO.cleanup()

If everything is working fine the fan should turn on after 2 seconds at full speed. After 5 seconds it should throttle down to 50% and after another 5s it sould stop. Until the script is interrupted this loop will stay active.

I hope this helps anybody who is new to this stuff and wants to start up an easy PWM fan control for his project.

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.