Active Cooling 5v brushless fan with Motor Speed Controller soft PWM
#1
   

The fan above is a 5v brushless 140ma fan (free standing on rubber fan mounts) capable of moving 5cfpm ; being driven by a python software PWM motor speed control driver using a PN2222 transistor to handle the current. The GPIO pin signals the transistor via 1k ohm resistor. The codes rely on the RPi.GPIO-PineA64 module from github.

I will be uploading the python codes near the bottom of this post.

The following two pics are close-in shots of the fan, and the transistor current driver:

   

   

The PN2222 transistor above can also be replaced by the 2N2222; although, the emitter collector pins are swapped-- the emitter goes to ground.

The fans ship from Jin LI , and as such take three to six weeks to ship to the U.S. Its the shipping, not Jin LI's fault. These little fans move a lot of air; however, they are also noisy. The speed control circuit quiets the fans considerably to a slight whisper, still moves the air more efficiently, and may be controlled off, or controlled by signal based on SoC temperature.

fan_motor.sh
Code:
#!/usr/bin/python
#
# fan_motor.sh
#*****************************************************************
#  author:     Mark H. Harris        
# license:     GPLv3
#
#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
#   CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
#   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#   MECHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
#   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#   SPECIAL, EXEMPLARY, OR CONSEQUENCIAL DAMAGES (INCLUDING, BUT
#   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
#   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERUPTION)
#   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE
#   EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#*****************************************************************
## Import the necessary header modules
from time import sleep
import signal as SIGNAL
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)

## SOFTWARE PWM GPIO23 pin(16)
soft_pwm = 23
GPIO.setup(soft_pwm, GPIO.OUT)

ms_on = .016
ms_off = .004
hup_flag = False

## FUNCTION DEFINITIONS

def motor_duty_on(m_pin, t_delay):
    GPIO.output(m_pin, True)
    sleep(t_delay)
    
def motor_duty_off(m_pin, t_delay):
    GPIO.output(m_pin, False)
    sleep(t_delay)

def end():
    GPIO.cleanup()
    quit()

def ssighup(signum, frame):
    global hup_flag
    global ms_on
    global ms_off
    if (hup_flag):
        ms_on = .016
        hup_flag=False
    else:
        ms_on = .250
        hup_flag=True
    print(" ")
    print("HUP: duty_cycle toggled value: "+str(ms_on))

SIGNAL.signal(SIGNAL.SIGHUP, ssighup)

kb_interrupt = False

while(not kb_interrupt):
    try:
        motor_duty_on(soft_pwm, ms_on)
        motor_duty_off(soft_pwm, ms_off)
    except KeyboardInterrupt:
        kb_interrupt = True
        print(" ")
        print("motor controller ended by interrupt, bye!")

end()

The code may be signaled with a -SIGHUP which when received by the speed controller code will toggle the duty cycle of the soft PWM. from 80% to 100%. A second HUP will toggle the duty cycle back to 80%, or slow speed.

There will be a follow-on post with some more pics, as well a simple schematic.

edit: I changed the default soft-pwm pin to GPIO23, pin(16).


marcushh777
  Reply
#2
I'm interested in building a smart-fan solution for my pine board as a wild C4Labs' Zebra case appeared in my mailbox today. The fan emits a high-pitched whine. Do you have a diagram of your circuit, Marcus?
  Reply
#3
(08-18-2016, 04:22 PM)CaptainZalo Wrote: I'm interested in building a smart-fan solution for my pine board as a wild C4Labs' Zebra case appeared in my mailbox today. The fan emits a high-pitched whine. Do you have a diagram of your circuit, Marcus?

ey Captain,  yes, I have a diagram I'll be posting later this evening;  as well, I have a couple more pics showing how to mount the driver transistor in-line with the power feed (no ciruit board), and I have updated the codes.

Be looking back here this weekend.  Thanks for the query !

Edit: PS. I pumped about that case too ! Planning to order soon.
marcushh777    Cool

please join us for a chat @  irc.pine64.xyz:6667   or ssl  irc.pine64.xyz:6697

( I regret that I am not able to respond to personal messages;  let's meet on irc! )
  Reply
#4
Thanks a bunch, Marcus. It's a bit cramped, to be honest, but it looks sleek, build was easy and it gets the job done. It's a lot better than my old lego mess. The bundled fan is 5v, 0.14A.

I'm also wondering about the pin-outs for mounting external system led on the 10-pin exp connector, if possible. I scavenged powerbutton from an old HP wreckage at work, rearranged it and lugged into the 10 pin exp connector through the opening in the top lid. There are two leds on the plastic housing. No tears will be shed if it can't be done. Smile
  Reply
#5
   

edit:  I changed the soft-pwm to GPIO23, pin(16).   

The schematic above could not be much more simple. The important element here is getting the emitter and collector of the 2N2222 transistor identified correctly. The emitter is the transistor lead marked by the out-going arrow in the diagram and marked pin (1) on the data sheet.  What makes this tricky is that different vendors swap pins 1 & 3 as you can see on the card.  Mine was the third diag all the way to the right (the base pin is offset). At any rate the emitter must go to ground.

This diagram is simplified from the photo below. The idea is that the fan is in-line with the transistor; but of course, the fan has two leads.  I have placed my transistor in-line with the black lead.  In other words the red lead goes all the way from the motor to pin(4) 5v.  The black lead holds the transistor with the emitter towards the plug.  

   

The pic above shows the in-line transistor (2N2222) .  It is important to note that I swapped the leads in the standard 5v plug (this is CRITICAL) because it not only allows the plug to fit between 5v and ground, but also gets the exposed 'pin locks' to the outside where they will not short against pins 3 & 5.

The base lead runs through a 1K ohm resistor to pin(16), which is GPIO23.  The fan_motor.sh script is coded to use this pin for soft-pwm by default.

I have left this in-line wiring expanded for easy viewing.  If mounting in a case , like the C4Labs case , you may want to make the wiring smaller , or you may want to enclose the wiring inside the case.  I even have a setup where I soldered the transistor directly to the GPIO pins; although, I do not recommend this.

Again, the diagram I provided isn't quite in sync with the photo.  I placed the transistor in-line in the black lead.  The red lead runs from the motor to pin(4).  The black lead holds the transistor and runs to pin(6). The base lead has the 1K ohm resistor and runs to pin(16) or GPIO23.

The theory of operation is simple:  the soft-pwm (pulse width modulation) in fan_motor.sh turns the transistor ON and OFF rapidly with a duty cycle ON of about 80%. This slows and quiets the motor substantially because the average time ON is reduced , thereby reducing the overall power to the motor while maintaining the 5v supply for the brushless operation.

edit:  important change:  I changed the soft-pwm to GPIO23, pin(16)
marcushh777    Cool

please join us for a chat @  irc.pine64.xyz:6667   or ssl  irc.pine64.xyz:6697

( I regret that I am not able to respond to personal messages;  let's meet on irc! )
  Reply
#6
Sweet. I need some electronic doodads. Got resistors and cables aplenty. I even made an external reset/pw button setup on a small breadboard to ease the micro usb socket strain. Great explanation and pics.
  Reply
#7
   

I must apologize, I don't have my C4Labs case yet, so I can only provide a pic of this technique except with one of my Raspberry Pi(s) -- this is rasppi7.

Here I have soldered the transistor directly to the GPIO pins.  Don't get confused, the point is not the details the point is the technique -- sometimes its easier to solder directly to the pins, especially for special purpose setups like this one where you don't need to use a bus cable plug.

Some of the details are different here than in my diagrams, because here the transistor is from a different vendor and the emitter and collector leads are swapped (note how they are in a straight line--the base lead is NOT offset)... also, I'm using a different GPIO pin, to make soldering to the pins 'easier'.     Smile

I have removed the heatsink to make the soldering more visible. I have a rather large heatsink that sits on the SoC (this is my coolest running SoC, because the space is tight, the case is well ventilated, and the heatsink sits directly below the fan which is blowing ... UP.  I might as well make a note here, that it is best to have your fan blowing UP, rather than having it blowing DOWN at the heatsink... especially if the board is in a case like this one... the fan should always evacuate the case... in this case the vents on the bottom of the case are particularly helpful.

Notice: the emitter is soldered directly to pin(9) grnd, and the black lead from the motor comes down and solders directly to the collector of the transistor. The resistor is the only connector from the base of the transistor to the gpio BOARD pin(11). The transistor rests gently top-down on the SBC. The red power lead from the motor comes down and solders directly to 5v pin(2). I'm using the very same fan_motor.sh code (Python3) except that I have simplified the motor speed control interrupt code considerably for the new PI(s) and for my PineA64(s).
marcushh777    Cool

please join us for a chat @  irc.pine64.xyz:6667   or ssl  irc.pine64.xyz:6697

( I regret that I am not able to respond to personal messages;  let's meet on irc! )
  Reply
#8
   

The pic above shows another of my RPi units (in the C4Labs Zebra case) with the in-line fan motor speed controller transistor (2N2222) and the 1K ohm resistor on the base lead running this time to pin(11) on the PI bus.

In this case I not only had to swap the 5v and ground leads in the standard 5v plug, but I also had to trim the plug to get it to fit into the slot provided on the C4Labs case top bezel. This will probably be the same situation on the PineA64 C4Labs case as well.
marcushh777    Cool

please join us for a chat @  irc.pine64.xyz:6667   or ssl  irc.pine64.xyz:6697

( I regret that I am not able to respond to personal messages;  let's meet on irc! )
  Reply
#9
Hi Marcus, I have some questions on the setup. You mentioned either the 2N2222 or PN2222. Does it matter which is used, or is either one fine? I get that the pins are different, just curious if it matters which one is used. Also, can this be used with Android images or is it only good with Linux OS? Thanks.
Kickstarter backer #5,864  --  SBC Noob  --  SE Michigan, USA
  Reply
#10
(08-25-2016, 12:46 PM)montero65 Wrote: Hi Marcus, I have some questions on the setup.  You mentioned either the 2N2222 or PN2222.  Does it matter which is used, or is either one fine?  I get that the pins are different, just curious if it matters which one is used.  Also, can this be used with Android images or is it only good with Linux OS?  Thanks.

hi , the 2N2222 and the PN2222 for functionally equivalent NPN bipolar junction transistors designed as general purpose amplifiers and high speed switching. You can use either one... and there are other NPN transistors that will work just as well too;  the reason for the 2N2222 is that they are plentiful and dirt cheap.   Often in a grab-bag of 2N2222 the PN2222(s) will also be mixed in...   the collector current can peak at 1A, and it will sustain 800ma... so this also works well for the 450ma cage fans too. Try to find a grab bag of maybe 20-30 transistors... you'll get them way cheaper ...

You can use the same technique with Android; however, you may have difficulty supplying the pwm signal from Android... that does not mean you can not accomplish the technique, only that somehow you will need to provide the pwm signal from someplace else (an inexpensive way to do that is to have a 555 timer provide the pwm signal through a buffer driver... to turn the transistor on and off at about 50 hz;  you will want a circuit that will have between a 60/40 up to an 80/20 duty cycle.

edit: one person reported to me that the NPN nte123ap worked fine too.
marcushh777    Cool

please join us for a chat @  irc.pine64.xyz:6667   or ssl  irc.pine64.xyz:6697

( I regret that I am not able to respond to personal messages;  let's meet on irc! )
  Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Controlling a Servo Motor casmiguefl 3 6,704 07-13-2017, 11:14 PM
Last Post: MarkHaysHarris777
  Active Cooling soft PWM follow-on fan motor speed controller driver MarkHaysHarris777 3 7,310 09-02-2016, 11:46 PM
Last Post: MarkHaysHarris777
  High-speed GPIOs dsnyder 3 5,498 05-05-2016, 09:07 PM
Last Post: martinayotte

Forum Jump:


Users browsing this thread: 1 Guest(s)