29 – Temperature sensor and a fan




    • Put together a temperature sensor and a fan.
    • Measuring temperature using a float.
    • Using a fan to decrease temperature
    • Illustrate the idea of a circular buffer



Arduino UNO Arduino Uno or equivalent.
Jumper wires
A Breadboard plus some jumper wires.

330 Ohm resistor
A LED diode and a 330Ω resistor.
2N2222 transistor A 2N2222 transistor. Check the silverscreened letters on the surface, because temperature sensors are quite similar.
DC Motor  A 5V fan or a Direct Current motor.
LM35DZ A LM35DZ temperature sensor or equivalent. Check the silverscreened letters on the surface, because transistors are quite similar.




We have been seeing so far how to use a temperature sensor as well as using a small transistor to drive a DC motor, but we had not put them together.

In this chapter we will assemble a small regulation example using a LM35DZ sensor to determine the temperature and  5V fan, so that when the first reaches a threshold value, we will use the second to cool the circuit.

As we have no system to cool, we will have to make do  with cooling the sensor itself, which is not particularly helpful, but certainly serves us to show an example of regulation, in the best spirit of an automated system, such as a temperature regulator with our Duinos.

We will see that the regulation of such a simple system has no major problems and that’s why this session is more intended as an exercise of simple control rather than a regulation example.

Besides  the parameter that we want to control, that is, the temperature, varies slowly, leaving us plenty of time to react not having to worry about responding accurately in real time.

  • A long time ago, when I started working on things like this and someone asked what was to work in real time, we defined real time as the margin we had to react and control the disobedient parameter before something expensive would go to hell.
  • When you are in such a worry the techniques applied here will not be worth and we would have to start talking about things like PID control, but for now it is too soon, so we’d better leave them like this. 

We will therefore assemble a simple prototype using a temperature sensor and other independent with a transistor to control the fan motor in the same breadboard.




We will assemble a small prototype that reads the temperature of a sensor, print the value in the console and turn on a diode when it exceeds a certain threshold.

In order to connect the temperature sensor LM35DZ and the 2N2222 transistor, I include details of the pins here:

2N222 transistor pinout
LM35DZ pinout diagram


Here we have the circuit schematic diagram:

Chapter 29, schematic diagram


And the circuit wiring diagram is quite simple too:

Chapter 29, wiring diagram




We have seen in previous chapters how to use a LM35DZ sensor, but if anyone wants to review it can do it here: Temperature sensor.

Let’s start testing the sensor to check that it works properly, and to do this we will use this program:

Sketch 29.1
const int Sensor = 0 ;             // Pin to which is attached the temperature sensor

void setup()
   {    Serial.begin(115200);     }

void loop()
   {    int reading = analogRead(Sensor);
        float voltage = 5.0 /1024 * reading ; 
        float temp = voltage * 100  ;
        Serial.println(temp) ;

We define a constant variable sensor to store the value read through the Analog0 pin, after that the loop reads the pin and converts the read value to Celsius degrees (Remember that the reason to do this was seen in the chapter about temperature sensors) and then we print the value of the temperature in the serial monitor. You should see something like this:


Consola Arduino

The LM35DZ sensor has a margin of error of about ± 0.5 degrees that’s why the readings can show some fluctuation, giving differences up to one degree.



This fluctuation in the readings is due to the fact that the real world is not always perfect and there are things that can alter them, ranging from measurement uncertainty caused by the sensor itself to errors in the converters our Arduino (that are not the best in the world) and even spurious electromagnetic interference.


All this conspires to have that annoying fluctuating readings and, although in this case it doesn’t matter, it may be more useful to filter these errors and to do so  we find the average of the last n readings and present this as the value of the temperature.

So we can store, let’s say the 8 last readings, and average them, which strongly decreases errors. The easiest way to do it is to use a circular buffer (or Ring Buffer).

Ring Buffer

The way to point at a the writing position is a by using a variable called index that starts at 0 and is incremented by 1 every time you write in the corresponding position. And how avoid exceeding the acceptable maximum value? Well, just taking the rest of the value of the index variable with respect to the number of positions in the array.

Buffer[ index] = temp;
index = ++index % 8;

Once we point at the writing position correctly and after the N initial values in which the average temperature will be false (because we have not yet read N values) we will have last N readings in the buffer, the rest is arithmetic.

Sketch 29.2
int index = 0 ;                      // Position to write
const int N = 16 ;                   // Number of samples 
float Buffer[N] ;                    // Array where we store the readings

Buffer[ index] = temp ;
index = ++index  % N  ;

float Tmedia = 0 ;
for (int i=0 ; I < N ; i++)
     Tmedia = Tmedia + Buffer[i];
Serial.println( Tmedia / N) ;



If you give the value 32 to N you could see something like this:

Consola arduino

As you can see the temperature fluctuates much less and this fluctuation decreases as you increase the number of samples that you average. But be careful, the more N grows, the more increases the time it takes to  respond to changes (for example, when we give N the value 32 it takes 5 samples a second and it takes more than 6 seconds to react).

  • Although this reaction time can’t be worrying working under normal conditions, there may be occasions when it could be. For example, If a burner starts to light, this 6 seconds reaction time can be long enough to see whatever we are controlling get burnt, so watch out!. 




Now that we have assembled the temperature sensor, we will program how to turn on and off the motor/fan, defining a threshold from which we ventilate the sensor. Let’s look at a possible sketch:

Sketch 29.3


int sensor = 0 ;             // This variable stores temperature
int threshold = 30 ;         // Temperature from which the fan starts to work
const int control = 9 ;      // To control the fan 

void setup()
   {   Serial.begin(115200);
       pinMode(control,  OUTPUT) ;

void loop()
   {   int reading = analogRead(sensor);
       float voltage = 5.0 /1024 * reading;    
       float temp = voltage * 100 -5 ;
       Serial.println(temp) ;

       if (temp >= threshold)
           digitalWrite(control, HIGH);
           digitalWrite(control, LOW);

The only new thing is that we check whether the measured temperature is below the predetermined threshold and if not, we start the fan to cool the sensor.

  • Notice that when the threshold is reached, we turn on the fan on digital mode, that is, at full speed. In a real situation we probably should make a proportional control of the fan speed depending on how far it is from the target we seek (This is where PID control issues come)
  • I suggest you to test a proof case where the fan speed is proportional to the difference between the threshold temperature and the measured temperature. 





    • We put together in the same prototype a sensor and an actuator to achieve control.
    • We have introduced the idea of a ring buffer to find the average of the instant readings.
    • We have seen a first example on temperature regulation.








No Comments

Give a Reply