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 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.

Note:  Ambient temperature changes are very slow and gradual.  You will only notice slight changes in the digital values reported by the Arduino.  Can you see the difference between a light sensor and a temperature sensor?

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 thermistor (1,000 Ohms of resistance - you will learn more about resistance and how to make your own resistors in Volume 5).


Step 2 - Create a voltage divider

Create a voltage divider by placing a 1K resistor (brown, black, red) 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);

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 five = require("johnny-five");
var board = new five.Board();
var clicks = 0;

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

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