Sensing the temperature

In this lesson, help Celsius measure the temperature using a temperature sensor, also called a thermistor.

A thermistor is a component that changes its resistance based on the ambient temperature.  To measure the voltage changes, just like you did with the potentiometer, you will need to add a resistor to create voltage divider.  You can then measure the voltage across the divider (You will learn more about the voltage divider in PodPi Module 5 with Ohmie).

Let's give it a try!

 

Step 1 - Place the temperature sensor

Place the temperature sensor across the middle of the breadboard and connect one side to the power supply (5V) using a red wire.

Your kit comes with a 1K maximum resistance.  This means that the thermistor resistance can go up to 1,000 Ohms.

Note: The Arduino board and code will return the voltage value in a digital format (a number from 0 to 1023).  This will not show you the actual temperature, but rather the voltage changes measured on the pin A0.

 

Step 2 - Create a voltage divider

Create a voltage divider by placing a resistor between the temperature sensor and the ground (GND) just like the picture on the right.

Add a yellow wire to the analog input (A0 pin).  This pin will be measuring the voltage continuously.  When the temperature changes, the voltage will increase and decrease and you can capture the digital value in your code.

Step 3 - Add a resistor for the LED

Add a 330 ohms resistor (orange, orange, brown) connected to digital pin 3 (4th from the bottom) with a blue wire.

Let's use the LED to display changes in temperature by controlling its brightness or its blink rate.

 

Step 4 - Add the LED

Add the LED across the breadboard (anode with the resistor and cathode on the other side).

Add a black or blue wire from the cathode to the ground header (GND) below.

 

Your circuit is all set, let's code!

The code

var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
var celsius = new five.Sensor('A0');
var led = new five.Led(3);
var value = 0;

celsius.on('change', function() {
temp_value = this.value;
console.log('Temperature analog value is: ' + temp_value);
led.blink(value);
});
});

Experiment #1 - Touch the sensor

Touch the sensor between your fingers and see what happens to the LED.

Get something cold from the fridge and touch the sensor with it.  What happens?


Experiment #2 - Color display based on temperature.

Use an RGB LED and set the color of the LED based on the temperature sensed.  For example, blue when cold, green at ambient temperature and red if too hot.

Here is a quick example of code that you can use, but you will have to tweak it to make it work for you.

var board = new five.Board();
var clicks = 0;
board.on("ready", function() {
  var celsius = new five.Sensor('A0');
  var red = new five.Led(3);
  var green = new five.Led(4);
  var red = new five.Led(5);
  var value = 0;

  celsius.on('change', function() {
    value = this.value;
    console.log('Temperature value is: ' + value);
    if ( value > 500 ) {
      red.on(); green.off(); blue.off();
    } else if ( value > 400 ) {
      red.off(); green.on(); blue.off();
    } else {
      red.off(); green.off(); blue.on();
    }
  });
});