Sunday, April 3, 2011

Controlling fan or motor speed with PWM

On the first part, I talk about switching any 12V DC or higher electronic components ON or OFF. This can be easily done using an optoisolator and a 12V reed relay. In this article, I will be using a different component to control the speed of the 12V fan or motors using Pulse Width Modulation (PWM).

The TIP-122 are Darlington transistors that can support voltage up to 100V. It consist of two transistors with resistors and diode all inside a TO-220 package. You can use TIP-120 that support up to 60V. Please refer to the TIP-120/TIP-122 datasheets for details specifications here.

TIP-122
I've wired u my circuit like the schematic diagram below :-

TIP-122 fan speed control

The input of the circuit is using analogRead(0) to read the light level from the light dependent resistors (LDR). Depending on the light level, the value is converted by the ADC to 0 to 1023. Since the PWM can only take value of 0-254, I will divide the ADC value by 4  to match the PWM value.

The 12V DC supply is separate from the Arduino 5V supply but shared a common ground.

The LCD is to display the LDR value for debugging purpose. Cover the LDR or shine a light to it to see the fan speed change.

Arduino Pin 9 (PWM) connect to the TIP-122 Base (B) pin.
The 12V fan connect to the TIP-122 Collector (C) pin and the Emitter (E) pin connect to the Ground.

Sketch to control the 12V fan speed using PWM is as below :-

   int ldr = 0;
   ldr = analogRead(0)/4;


  // if light level is room light, turn the fan speed higher

  if ( ldr < 125 && ldr > 40 ) {
    ldr = ldr + 50;
  }


  // if value is less than 40, do not switch the fan ON

  if ( ldr < 40 ) {
    ldr = 0; 
   }
  
  // else run the fan speed according to light level up to 255


  // send PWM value to TIP-122 base pin
  analogWrite(9,ldr);
  delay(500);



36 comments:

  1. Hello. May I know if this circuit can drive more than one fan? If not, what else should I add to make that work?
    thx.

    ReplyDelete
  2. If you need to drive more than 1 fan at the same PWM speed, just hook it up in parallel ( make you the power supply can supply enough power to both the fans )..

    If you need to drive them at different speed, then just use another PWM pins and another TIP120/TIP122 ...

    ReplyDelete
  3. probably a stupid unrelated question. i am a beginner to arduino electronics and was wandering if you would have a diagram of the atmega 328 chip and what pins go to what part of the arduino (Ex- pin 16 goes to analog 2) or anything like that. i have written code to a chip for a LED chaser and would like to wire it to a circuit board but cannot do it because i dont know what pins to wire to what led's or power and ground to what pins etc. My e-mail address is doley08@gmail.com any information would be grateful. Thank you for your time

    ReplyDelete
  4. Hi,
    Can I use the circuit to control a motor that takes current above 40mA?

    ReplyDelete
  5. What resitor is used between de tip122 and the pwm signal

    ReplyDelete
  6. thank you for this great tutorial. i am in the planning of something so i would like to make something clear to me.

    i would like to drive 3 of these (http://www.nodna.de/Robots-and-Accessories/Servos-Motors-Encoder/Motor-Encoder/EMG30-12V-GearMotor-with-Encoder--1076.html) motors independently.

    so i would need an arduino (of course), three of the TIP-122 and a 12v power supply? is this correct?

    it would be nice if you would help me here. thanx and have a nice day!

    ReplyDelete
  7. @mipu, Yes, you can drive them independently using 3 PWM pins

    ReplyDelete
  8. @jos, the resistor between the TIP-122 is 1K resistor.

    ReplyDelete
  9. @naasik, Yes, you can drive motors above 40mA as it is independent of the Arduino pins... it is the TIP-122 that is controlling the current across the motors.

    ReplyDelete
  10. could I control speed of fan (which is 5V 0.5A) powering it up from arduino uno?

    ReplyDelete
  11. @Ziai, it is NOT recommended to power the fan even tho it is running on 5V. You can share the same power supply source, e.g. 5V from the USB and use a TIP-120 / TIP-122 to control the fan speed.

    You cannot directly power / control the fan speed from those I/O pins as the max amp is only 40mA.

    ReplyDelete
  12. is the sketch correct ?
    because arduino finds a pb on this line :
    ldr = analogRead(0)/4;

    (im noob but i'm trying to control speed of one fan)

    ReplyDelete
  13. Not sure yr questions...

    analogRead return values range from 0-1023 (10-bits) whereas PWM only output is from 0-255.

    analogWrite(9,ldr);

    ReplyDelete
  14. if you copy the code into arduino, it won´t work. is there something missing?

    ReplyDelete
  15. The code posted is not the complete codes.. you will need the setup() and loop() to complete the code...

    ReplyDelete
  16. I think TIP-122 B & C are swapped on the schematic diagram. 1 is B and 2 is C.

    ReplyDelete
  17. Hi

    Can I use this setup for 24V motor as well? I am not sure if the increased voltage would make the TIP122 overheat. Maybe if I add some kind of grille to the transistor, to cool it down. Or maybe i need to replace it with a MOSFET?

    /Christian

    ReplyDelete
  18. Yes, TIP122 is rated up to 100V... you can touch the metal part to check if it is hot or not..

    ReplyDelete
  19. Will this work with controlling lights as well?

    ReplyDelete
  20. For lights from the mains 110/220V, you would need a solid state relay.. it should work the same way...

    ReplyDelete
  21. So if PWM is set to 0 will the fan remain at rest? Or do you also need the relay to turn power on and off?

    ReplyDelete
  22. So I gave this a try and it does not seem to produce the desired results. All it does is spin at a constant rate. Any ideas would be great!

    I have the exact setup you gave using a tip122, a 1k resistor, 12v transformer, and a 12v muffin fan.

    Here is my arduino code, note that I am just giving it a value rather then getting a value from another source. My plan is to hook this up to a WiiRemote using Max/MSP to give the value in the same way your ldr is giving you your values.


    int fanPin = 9; // Fan connected to digital pin 9

    void setup()
    {
    pinMode(fanPin, OUTPUT); // sets the pin as output
    }

    void loop()
    {
    analogWrite(fanPin, 100);
    delay(500);
    }

    ReplyDelete
  23. Yr analogWrite is ALWAYS 100, so the fan spins at the same speed...

    Pls join the facebook group and post yr project/questions/photos over there... lots of beginners and experience Arduino ppl over there...

    ReplyDelete
  24. This is a great tutorial... however, I might suggest you correct the diagram like anonymous pointed out. B and C are flipped. http://www.pcsilencioso.com/cpemma/ef.html helped me figure this problem out.

    ReplyDelete
  25. hi there
    how am i suppose to connect the motor.
    "The 12V fan connect to the TIP-122 Collector (C) pin"
    yes i understand that, but how? am i connecting the + of fan to the Collector and to the power supply and then the fan's - to ground?
    or fan's - to collector and + to supply?
    thanks

    ReplyDelete
  26. Brad, errors on the TIP-122 pins are fixed, thanks for all that pointed that out...

    ReplyDelete
  27. Hi I need to do AC 230v fan, Can u help me, How to do?

    ReplyDelete
  28. http://www.apcc.tk/diy-projects/arduino-pwm-fan-or-motor-speed-control you can rean more hear about arduino pwm

    ReplyDelete
  29. Hi, This is a great tutorial, I am just getting into control circuits but have been into smaller current electronics for some time. I read the spec sheet on the TIP122, but I am still curious if this transistor would work for a high current fan like that on the radiator of my truck? or will I need a different part and circuit? I will be using the tip122 for controlling led lights on my truck though so that shouldn't be an issue. all the lights including the headlights are low current draw 12 volt supply. I assume the tip 122 and this schematic will work fine for that?

    ReplyDelete
  30. Hi, from the datasheet, the max CE current is only 5A... if you need higher current, you might need to use something different.. do a part search on http://octopart.com/ with your required voltage (12V) and max current draw needed...

    ReplyDelete
    Replies
    1. Thanks for your reply, I can't seem to find anything online that matches my requirements. the truck supplies around 12.2 to 14.6 volts when running. the fan is rated for 11amps continues draw. I am assuming the worst and would say the start up current is somewhere around 20 amps. I need a transistor or SSR that can provide me with PWM control of the fan when connected to a 5volt PWM signal from a Microcontroller. and all of this needs to be within a small budget. I already have the arduino and other parts. thanks

      Delete
  31. hello.What specific solid state relay would i need if i am controlling lights from the mains.(220v,110v)and is schematics the same with the solid state relay accept that we will replace the TIP122 with the SSR.Thanks

    ReplyDelete
  32. No, you cannot use PWM on A/C SSR bcos SSR on the mains are Alternating Current (A/C) output... you can only do PWM on D/C..

    ReplyDelete
  33. I setup my arduino as above but the fan makes a high pitch noise when not full speed (255). Is there anything I can do to stop this?

    ReplyDelete

LinkWithin

Related Posts Plugin for WordPress, Blogger...