Digital Sandbox Arduino Companion

Contributors: jimblom
Favorited Favorite 3

12. Thermal Alert!

"Is it hot in here, or is it just me?" Using a temperature sensor, which is able to precisely measure the room temperature, we can answer that question once and for all!

Background Information

Temperature sensors are a critical component in many circuits, whether you're controlling an A/C system or creating a safety mechanism for gas-powered appliances. Electronic temperature sensors come in many form-factors, from big thermocouples that can measure up to 1000 °C to that little black rectangle on the Digital Sandbox.

The temperature sensor on the Sandbox produces an analog voltage that represents the temperature around it. The voltage is actually linearly proportional to the Celsius temperature. If you know the output voltage of the sensor, you can calculate the temperature with this equation:

temperature = (voltage - 0.5) \times 100

We can have the microcontroller do all of that math for us as long as we find the right algorithm - an equation or set of instructions that accomplish a specified task.

Active Parts

alt text

Code Components

Time for another new variable type!

Variables with Decimals: float

Up to this point, every variable and value we've used has been an integer. Integers are whole numbers, which means they can't have decimal points. If you need the precision of decimal points, you have to call up the floating-point variable type.

To declare a floating-point variable, simply use the float keyword and create a variable like you normally would:

float myPi = 3.1415;

float variables can be huge anywhere from 3.4028235×1038 to -3.4028235×1038.

Floating point numbers may seem great, but they do have their assortment of problems: they take up lots of space, and they take a large chunk of time for the processor to work with. Try to avoid them if you can.

Sketch and Experiment

Here's our experiment. The sketch is titled Sandbox_12_ThermalAlert.ino. Take a peek at the comments and upload!

    // Sandbox 12: Thermal Alert!

/* “Is it hot in here, or is it just me?” Using a temperature sensor, which is
   able to precisely measure the room temperature, we can answer that question 
   once and for all!

   This experiment explores the analog input even further. This time we're 
   reading the input of an analog temperature sensor. Using some math we can
   convert that value to a voltage, and then to celsius and fahrenheit 

   To do math with decimals, we need to introduce a new variable type: float.
   float variables allow us to escape the realm of the integer to calculate
   divisions to a very precise level.

   Also notice that we've defined some constant int variables for our LED
   and sensor pins. This is good practice -- it makes your code more readable
   and it allows you to quickly change the pin throughout your code if need be.

// New variable type: float! float varaibles allow you to use decimal values.
// Other variables we've used -- int and long -- are integers. No decimal points
// allowed. floats are great, but only use them when you really need to. They
// require a lot more storage space and processor power.
// The thermalAlert variable is a constant. If the temperature reading is above
// this point, we'll turn the red LED on.
const float thermalAlert = 80.0;

// Lets also decloare some global variables for our LED and sensor pins. This
// makes swapping those pins much easier. And our code below will become much
// more readable.
const int temperaturePin = A0;  // Temperature sensor is connected to A0
const int redLEDPin = 9;        // Red LED is connected to pin 9
const int greenLEDPin = 11;     // Green LED is connected to pin 11

void setup()
    // Set the temperature sensor pin as an INPUT:
    pinMode(temperaturePin, INPUT);

    // Set the LED pins as OUTPUTs:
    pinMode(redLEDPin, OUTPUT);
    pinMode(greenLEDPin, OUTPUT);

    // Initialize Serial, set the baud rate to 9600.

void loop()
    // Read the raw 0-1023 value of temperature into a variable.
    long rawTemp = analogRead(temperaturePin);

    // Calculate the voltage, based on that value. To calculate voltage, we
    // multiply by the maximum voltage (5V), and divide by the maximum ADC
    // value (1023).
    float voltage = rawTemp * (5 / 1023.0);
    Serial.print("Voltage: ");
    Serial.println(" V");

    // Calculate the celsius temperature, based on that voltage. The celsius
    // temperature is calculated by subtracting 0.5V from the reading, then
    // multiplying that small value by 100.
    float celsius = (voltage - 0.5) * 100;
    Serial.print("Celsius: ");  // Print "Celsius: "
    Serial.print(celsius);      // Print the celsius temp
    Serial.println(" °C");      // Print " °C" and a new line

    // Use a common equation to convert celsius to fahrenheit. F = C*9/5 + 32.
    float fahrenheit = (celsius * 9.0 / 5.0) + 32.0;
    Serial.print("Fahrenheit: ");   // Print "Fahrenheit: "
    Serial.print(fahrenheit);       // print the fahrenheit temp
    Serial.println(" °F");          // Print " °F" and a new line
    Serial.println();               // Print a blank line

    // Now check the temperature, and turn on either the red or green LED 
    // depending on that value.
    if (fahrenheit >= thermalAlert)
        digitalWrite(redLEDPin, HIGH);
        digitalWrite(greenLEDPin, LOW);
        digitalWrite(redLEDPin, LOW);
        digitalWrite(greenLEDPin, HIGH);

    delay(1000);    // Wait a second between readings

After uploading the sketch, open the Serial Monitor.

You'll see the temperature in Celsius and Fahrenheit start to flow by. You should also see either the green or red LED illuminate, depending on that temperature reading. If the temperature is above 80 °F, there'll be a red thermal alert!

Your Turn!

  • Can you add a third check to alert when it’s too cold, by turning on the blue LED? The real trick here is cooling the Sandbox off. One option is to power the board with a battery and stick it in the fridge.
  • Celsius and Fahrenheit are two of the most common temperature scales, but they’re not the only ones. Can you print the temperature in units of Kelvin or Rankine? You’ll need to find an algorithm to convert to them from Celsius.