Introduction: Using a Temperature Sensor to Control the Speed of a Motor Using Arduino

This application is simple just by using temperature sensor LM35 with an arduino kit to control a fan and change its speed with respect to the temperature read by the micro-controller which in this case is the arduino kit .

The temperature sensor LM35 is connected with the arduino with an analog input pin A0 (the temperature is an analog signal), while the fan is connected with a PWM (Pulse Width Modulation) pin 6 which controls the speed of the fan with respect to the output temperature using a function map in the arduino IDE.

The Components needed :

1. Arduino
2. Temperature Sensor LM35
3. 1K Resistor
4. Diode 1N4007
5. Dc Motor or a simple DC fan
6. NPN transistor BC547
7. A voltage source with 12Volts
8. hook up wires
9. optional : voltage source socket

Step 1: Conecting All Together


•using the NPN transistor BC547 here as a buffer to isolate the first circuit which is the connection to the arduino and the other one with the fan.

•using an inverted PN-junction parallel to the fan to prevent moving current in the opposite direction and make damage to the arduino kit.

•using a PWM pin is to modulate the signal given to the fan with respect to the read temperature and by using the map function to control the fan speed.

•it is important to put in consideration that the input voltage for the temp sensor is 5v from the arduino itself, and the 12volt voltage given to the motor or fan must be from an external voltage source, in this case we collect all the GND together weather the GND of the arduino and the external voltage source.

Step 2: Time for Coding

Using the arduino software and write this code down .

float temp;

int tempPin = A0; //arduino pin used for temperature sensor

int tempMin = 25; // the temperature to start the buzzer

int tempMax = 70;

int fan = 6; // the pin where fan is connected

int fanSpeed = 0;

void setup() {

pinMode(fan, OUTPUT);

pinMode(tempPin, INPUT);



void loop() {

temp = analogRead(tempPin);

temp = (temp *5.0*100.0)/1024.0; //calculate the temperature in Celsius


delay(1000); // delay in between reads for stability

if(temp < tempMin) { // if temp is lower than minimum temp

fanSpeed = 0; // fan is not spinning

digitalWrite(fan, LOW);


if((temp >= tempMin) && (temp <= tempMax)) //if temperature is higher than the minimum range


fanSpeed = map(temp, tempMin, tempMax, 32, 255); // the actual speed of fan

analogWrite(fan, fanSpeed); // spin the fan at the fanSpeed speed



Now after verifying and uploading the code to the arduino, you can open the serial monitor up at the right of the arduino software to show the temperature of the surrounding.
you will noticed that i have added the " Serial.begin(9600); " function that shows the temperature in the serial monitor.
To increase the temperature you can heat it by transferring heat from your body by touching it .
you can edit the tempMin and tempMax the values you want depending on your application specification.


EmikoG (author)2017-03-28

The fan is still running below the temp min, how do i get the temperature sensor to control the fan

MeghanaM3 (author)2016-10-16

motor is not controlling its speed ?? how can i control it please give me the solution for that immediately

rashedkhan (author)2016-08-19

my fan is rotating though i have reduced the min temp but motor is not is spinning

MetroidChild. (author)2016-01-17

what exactly does the diode that goes across the fan terminals do? Is that a safety measure in case the fan spins backwards by itself to prevent frying the npn transistor or something, or is it just there for show?

DanielP406 (author)MetroidChild.2016-08-04

You might've had this question answered, but for others who are curious - the fan is an inductive load, and the diode therefore serves as a 'flyback diode'.

Technically, the fan's motor develops a magnetic field in order to spin. When the fan suddenly loses power (when you turn it off at the switch), it tries to 'feed itself' using the motor's collapsing magnetic field as its source, instead of the original source - which is now missing.
Depending on the magnitude of this field, the inductor creates a negaitve potential where it was once positive, and the inductive load tries to essentially 'force' the voltage across the power switch, creating an arc. This is dangerous for the circuit.

A diode is therefore placed across the motor such that the intended source won't bypass the fan when operating, and the inductor will 'power itself down' PROPERLY (until it dies) when the fan is turned off.

Hope this helps :)

TuryaB (author)2015-10-25

can you give the specification of the motor used?

wilsondm (author)2015-06-01

BOM DIA,Ótimo Trabalho Pretendo e Fácil. de fazer

Hesham_Gaber (author)wilsondm2015-06-07

thank you,
it is simply a part of a big project, you might use it in something

tomatoskins (author)2015-05-24

Really cool idea!

Hesham_Gaber (author)tomatoskins2015-05-24

it could be involved in a large project like a smert home !