Oscar Gonzalez gives us a simple tutorial for speed controlling a PC FAN and reading a LM35 temperature sensor with an Arduino microcontroller. He covers the use of an optocoupler, PWM (Pulse Width Modulation) and reading analog voltages with the Arduino. He even has a video showing the fan speed control in action.
This article was submitted by Oscar Gonzalez as part of the “Hobby parts for articles” program. Oscar receives a Arduino compatible Modern Device Company Bare Bones Kit for this great article.
Control a device that requires 12V from the Arduino without frying it. The Arduino is a 5V device and can not directly drive a 12V device such as a PC fan. There are many possible solutions but my approach is using the CNY75 optocoupler to separate the two voltages.
This devices is very simple. It consist of an phototransistor and a LED inside the same package. The phototransistor turns on or conducts current when the internal LED lights. The brighter the internal LED the more current can pass through the phototransistor. The LED and phototransistor are physically isolated from each other. This physical isolation protects the input side (the LED) from voltage spikes on the output side (phototransistor) and can provide the voltage translation needed for this project. When the LED is activated from the 5V Arduino the phototransistor will turn on and pass current for the 12VDC fan. The isolation provided by the optocoupler keeps the Arduino is safe from destruction.
The optocoupler’s internal transistor does not have sufficient current capacities to drive a DC motor (like our FAN) directly. To boost the current I used a BD137 transistor which can drive up to 1.5 Amperes. This power transistor is sufficient for controlling PC Fan motors. The PC Fan model I used runs needs about 300 mA.
Controlling the speed with PWM
To control the fan speed you could reduce the drive voltage to the motor. This would be harder to do and could reduce the motor torque. PWM (Pulse Width Modulation) is easier with the Arduino and is basically like turning the motor on and off very quickly. We are turning on and off the LED inside the optocoupler which is turning on and off the transistor in the optocoupler which is in turn controlling the power transistor which is turning on and off the Fan motor. The longer the motor is on, the fast it will spin. The cycle time (off to on) is very short, so short that you will not hear it occurring. Actually it occurs so fast that the fan averages the on and off times to run at a nearly constant speed.
Testing the speed control
I tested the speed control with the “LED Fade” Arduino example sketch. Connect the internal CNY75 LED trough a 1K resistor. The motor should go from quiet (slow) to full cooling (fast) progressively. You can modify the example program to change the slow to fast cycle.
Reading Analog Voltages with the Arduino
The Arduino has several analog inputs which are converted into a 10-bit digital number. You read the analog pin using the anologRead(X) function. The number will be 0 if the input voltage is 0 volts and will go up with voltage to 1024 when the input pin is at 5V. So to get the voltage at the analog input you would use the following formula. Use the correct analog input pin name in place of the ‘X’ for the analogRead(X) function call.
Voltage = analogRead(X)*5.0/1024.0; //Note that 5.0 is the A/D reference voltage.
LM35 temperature sensor:
The LM35 is a popular and inexpensive temperature sensor. It provides an output voltage of 10.0mV for each degree C of temperature from a reference voltage. We can read this voltage with one of the Arduino analog inputs. So the output pin would be at 0V when the temperature is 0 degrees C and would raise to 1000mV or 1.0V at 100 degrees C. So to get temperature you would multiply the voltage by 100. For example if you read 0.50V that would be 50 degrees C. The voltage and temperature conversion math can be combined to give the simple formula below.
temperature = ( 5.0 * analogRead(X) * 100.0) / 1024.0; /
You can test the LM35 connected by adding the conversion code and a Serial.Print() to output the temperature to the IDE serial console.
Now we can control the speed of our motor, and read the temperature. The next step is to can combine the temperature reading functions and the motor speed control to make a fan speed control that automatically adjust based on temperature. I have not done this part yet, let me know how you do it.
Here is a video showing the fan speed controller in action.
You can use the optocoupler method to control the speed of any motor, not just the PC Fan as I have done here. You can also use the temperature sensor for other projects. I hope this simple tutorial about optocouplers, PWM and the LM35 temperature sensor was helpful to you. Let us know what you make with this.