Saturday, April 2, 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);