Man and two boys with computer and fan with text "Temperature-Controlled Fan"

Arduino Temperature Sensor Code: Automated Fan

For Arduino projects that include temperature sensors, it’s necessary to understand how to write the Arduino temperature sensor code to program your Arduino board.

In this post, we’ll show you how to write the Arduino temperature sensor code to build an automated fan.

When the temperature gets too hot, the fan will turn on. The fan will turn off when the temperature goes down.

The Arduino will:

  1. Read the temperature from the temperature sensor
  2. Compare if the temperature is higher than the setpoint
  3. If the temperature is higher than the setpoint, turn on an “Internet of Things” (IOT) relay, which will power on the fan
  4. Wait 5 seconds and then repeat the steps above

If you’ve never used an Arduino before, you should check out this Arduino Introduction first. 

You can also see this post on Breadboard Basics and Connectors if you aren’t already familiar with how to use a breadboard. 

Otherwise, let’s get started! 

This project and all Geek Pack Hack activities must be undertaken with a suitable adult completing their own risk assessment and supervising their children at all times.

We’ll list the parts we used and then give you step-by-step instructions to write the Arduino temperature sensor code and set up this temperature-controlled fan.

You can check out our video tutorial here and subscribe to our YouTube channel to keep up with new projects.

Everything is explained below as well.

SUPPLIES

Arduino Uno

IOT relay

Fan 

Breadboard

Jumper wires

Temperature sensor

Power supply for Arduino (if you want it to run independently of your computer)

USB printer cable (either an A to B cable or a C to B cable, depending on whether your computer has USB A or C jacks)

Computer

Steps

Step 1: Wire up the breadboard circuit

Step 2: Connect the IOT relay

Step 3: Write the program sketch with the Arduino temperature sensor code

Step 1: Wire up the breadboard circuit

Here’s how the breadboard is wired to the Arduino Uno and the IOT relay (right click and open the images in a new window for high resolution):

Breadboard with temperature sensor and jumper wires to Arduino Uno, which is also wired to the IOT relay.

Here’s a close-up of how the jumper wires connect the temperature sensor to the Arduino Uno (click for a high resolution image).

Here’s a Fritzing image of this set up:

Wiring instructions

You can see which pin on the temperature sensor is connected to the Arduino’s positive voltage and which pin is connected to ground.

As you can also see, the last sensor pin is connected to an analog input pin on the Arduino. This carries the changing voltage that we can convert to Farenheit or  Celsius.

Step 2: Connect the IOT relay

The IOT relay is an electronic switch that we can turn on or off to control whether or not our fan is running.

As you can see in the photos above (at full size), the relay is connected both to ground and to digital pin 13.

For additional information about programming the IOT relay, check out this documentation.

Step 3: Write the program sketch with the Arduino temperature sensor code

Here’s the sketch with our Arduino temperature sensor code to control this fan:

//TMP36 Pin Variables
int sensorPin = 0;     //the analog pin the TMP36's Vout pin is connected to
                       //the resolution is 10 mV / degree centigrade with a
                       //500 mV offset to allow for negative temperatures
 
int relay = 13;        // Tells Arduino the relay is connected to pin 13
 
/*
* setup() - this function runs once when you turn your Arduino on
*/
void setup()
{
pinMode(relay, OUTPUT); // Initialize the Atmel GPIO pin as an output
 }
 
void loop()             // run over and over again
{
//getting the voltage reading from the temperature sensor
int reading = analogRead(sensorPin); 
 
// converting that reading to voltage, for 3.3v arduino use 3.3
float voltage = reading * 5.0;
voltage /= 1024.0;
 
// now print out the temperature
float temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree wit 500 mV offset
                                              //to degrees ((voltage - 500mV) times 100)
 
// now convert to Fahrenheit
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
if (temperatureF > 75) {
digitalWrite(relay, HIGH);   // Turn the relay on (HIGH is the voltage level = 1)
}
else {
digitalWrite(relay, LOW);    // Turn the relay off by making the voltage LOW = 0
}
delay(1000);                 //waiting 5 seconds
}

So, what does this code do?

After the comment on the first line, the next  line of code says that we’re going to create a variable called “sensorPin” that is an integer (a whole number), and that we are going to assign it a value of “0.”

Continuing down a couple of lines in the sketch, we do almost exactly the same thing– we define a variable called “relay” as being an integer and having the value “13.”

The “setup()” function runs once when we fire up the Arduino.

In the “setup()” function, we initialize digital pin 13 as being an output, which will let us send enough current to control the relay connected to that pin.

The loop function runs over and over again.

In the loop function, we first read the voltage from the temperature sensor and assign it to an integer variable called “reading.”

We then have to use a little math to convert that voltage to a temperature.

For more information about the math for this conversion, check out Adafruit’s excellent documentation.

First we convert to Celsius and then to Farenheit.

If the sensor reading is greater than 75 degrees, we turn on the relay.

Otherwise, if the sensor reading is less than or equal to 75 degrees, we turn off the relay.

We wait for 5 seconds before once again measuring the temperature and determining whether to turn the fan on or off.

After you upload the code to your Arduino board, you’re ready to give it a go!

You can change the temperature setpoint to be warmer or colder by simply replacing the “75” with a different temperature.

You can also change how long the Arduino waits before taking a temperature reading and comparing it to the setpoint by changing the number inside the “delay()” function.

Every time you change the sketch, you’ll need to upload it again to the Arduino.

Stay in touch!

Share your projects, your successes, and your failures with us! Tag @geekpackhack on Instagram, or leave a comment on this post.

Please also leave us a comment if you enjoyed this project or have questions.

Check out all of our cool engineering projects.

Or, narrow in on our simplest basic electrical circuit projects or our slightly more advanced electrical engineering projects. 

We also have a helpful basic electronics page that teaches skills such as how to use a breadboard and multimeter.

Don’t forget to sign up for our free monthly newsletter to receive Geek Pack Hack updates along with an even wackier simple circuit project. Have fun!

Leave a Reply

You have to agree to the comment policy.


The maximum upload file size: 128 MB.
You can upload: image.
Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded.