Anth's Computer Cave

Build a Cheap and Easy Temperature Sensor

Today we are making a super cheap, super easy temperature sensor to tell AAIMI whether or not to switch heating or cooling.


The sensor

This sensor is similar to the light-sensor we built earlier in the series. It runs 5V through a voltage divider and returns a voltage that varies with temperature.

Instead of an LDR we are using a thermistor, and this thermistor acts as R1 in the circuit, unlike the LDR in the light-sensor, which acted as R2.

Components

The components for the temperature-sensor. Picture: Anthony Hartup
The components for the temperature-sensor.

The primary component for this sensor is the thermistor. This is a type of resistor that varies with temerature.

You can often salvage these from old electronics equipment, and that is what I did at first. I have now started using some retail thermistors from ebay, because I am building a dozen of them and I want them to be consistent.

Salvaged thermistors from TVs (Left) and cheap MF103 thermistors from ebay (Right).

These MF103 units cost ten cents each and work well. They are negative thermistors, so their resistance drops as the temperature rises.

You will also need a 5K resistor to place in a voltage divider with the thermistor. I didn't get too scientific in deciding on the 5K value, but it is working in the temperature ranges I have tried so far.

The PCB, and screw terminals are optional, but they make for a tidy package.

The circuit

As I have mentioned, this sensor uses a voltage divider to send a return voltage to your Arduino micro-controller. In most voltage dividers you use a fixed resistor for R1, and the variable resistor (in our case the thermistor) as R2.

Because the MF103 lowers its resistance as the temprature rises, we need to reverse the voltage divider to raise the return voltage with the temperature and give us a semi-logical reading to calculate degrees.

The diagram for a simple temperature sensor. Picture: Anthony Hartup
The diagram for a simple temperature sensor. Picture created with CircuitDraw.

As you can see it is a simple circuit.

The 5V and GND pin from the Arduino connect to the left-hand terminals and the right-hand terminal connects to the first analogue-to-digital pn on the Arduino.

The connection diagram for a simple temperature sensor and Arduino. Picture: Anthony Hartup
The connection diagram for the Arduino. Picture created with CircuitDraw.

The Arduino sketch

The code to retreive a raw voltage reading is easy. It is basically the same code we used for the light-sensor, which simply reads the A0 pin on the Arduino.

int sensorPin = A0;  // This is the analogue to digital pin we will use
float levelOne = 0.0; // This creates a floating-point variable to store the reading

void setup() {

  Serial.begin(9600);
  while (!Serial) {
  }
  Serial.println("Serial Connected");
}
// This function reads the A0 pin voltage and prints it to the serial monitor
  void readOne() { 
    int sensorValueOne = analogRead(sensorPin);
    levelOne = sensorValueOne * (5.0 / 1023.0);
    Serial.println(levelOne);
  }
  
void loop() {
  readOne();
  delay(3000);
  }
  

This will print the raw voltage the sensor returns.

It doesn't, however, give you a temperature in degrees. In my home-automation system I use this raw reading and let the Raspberry Pi convert the voltage to degrees with Python. If, however, you wish to make a stand-alone Arduino system you will need the Arduino to do the conversion for you, so I will give you another Arduino sketch.

Here is where things become a little complicated. If I had a sealed environment with temperature control I would simple lower the temp to zero degrees and take a voltage reading. I would then up the temp to 30 degrees and take another voltage reading. It would then simply be a case of setting zero degrees at the first voltage, 30 degrees as the second voltage, then divide the difference by thirty to find the degree increments in between.

Unfortunately I don't have such an environment, so I have used the natural Winter temperature variation in my shed to find a range.

Using a digital thermometer as a refernce I meaured the voltage when my shed dropped to eight degrees (which was about .97V), then again at 18 degrees (which produced 1.53V).

The voltage scale for a simple temperature sensor. Picture: Anthony Hartup
The voltage calculations for the temperature sensor.

I therefore set my base temp and volts in between these two readings, giving me a reference voltage of 1.25V at 13 degrees Celcius, and an increment of .056V for each degree above or below 13 degrees.

Note that your readings may vary from these figures. The resistor and thermistor's resistance may vary by up to 10%, and different Arduino power supplies can cause different reference voltages. You may need to experiment. You may also want to create your own scale centered around a higher base temperature or a wider temperature range. I have just used what the weather has provided for my scale.

Here is the sketch for a stand-alone Arduino temperature-sensor.

int temp1 = A0; 
float levelOne = 0.0;
float temperature1 = 0.0;

void setup() {

  Serial.begin(9600);
  while (!Serial) {
  }
  Serial.println("Serial Connected");
}

void checkTempOne() {
  // Check raw voltage level
  int sensorValueOne = analogRead(temp1);
  levelOne = sensorValueOne * (5.0 / 1023.0);
  Serial.println(levelOne);
  float baseTemp = 13.0;
  float baseVolts = 1.25;
  
  // Add degrees if voltage reading is higher than baseVolts
  if(levelOne > baseVolts) {
    float extra = levelOne - baseVolts;
    float tempAdd = extra / .056;
    temperature1 = baseTemp + tempAdd;
  }
  // Subtract degrees if voltage reading is lower than baseVolts  
  else if(baseVolts >= levelOne) {
    float under = baseVolts - levelOne;
    float tempMinus = under / .056;
    temperature1 = baseTemp - tempMinus; 
  }
  Serial.println(temperature1);
  }
  
void loop() {
  // Check temperature every three seconds
  checkTempOne();
  delay(3000);
  }
  

This is working accurately between seven and nineteen degrees, but it may creep when the temp goes above and below that range, becuase the voltage levels may not produce a linear value over larger ranges.

I will refine the system when the weather gets warmer, but for now it is perfectly workable for the temp ranges applicable to my home-automation needs.

You can see the Python methods I use for this temp-conversion, as well as what I have been doing with the readings, in the upcoming release of AAIMI Home Automation next week.

Cheers

Anth


Previous: Build a ULN2003 relay driver.

Next: Build a base-station from salvaged components.



_____________________________________________


Comments

Leave a comment.

There are currently no comments for this article.

Leave a comment on this article