Learn Arduino with Olympia Circuits
Learn Arduino
  • Home
    • Get Started
    • How to Use This Site
  • Electronics
    • The Basics
    • Electricity Flows like Water
    • Electronic Components
    • The Arno Board
  • Programming
    • The Basics
    • setup and loop Blocks
    • Variables and Arrays
    • Connecting with the Pins
    • Flow Control >
      • if Statement
      • Loops
      • Delays
    • Functions
    • Serial Communication
    • USB
    • Some Thoughts
  • Projects
    • Projects 1 >
      • 1.01: Blink
      • 1.02 Blink x2
      • 1.03 Blink Faster
      • 1.04 LED Chase!
      • 1.05 Wait To Blink
      • 1.06 Blink a Little Faster Now
      • 1.07 LED Fade
      • 1.08 RGB Blink
      • 1.09 Change RGB Color with SW1
      • 1.10 Fade RGB Colors
      • 1.11 Reaction Time Game
    • Projects 2 >
      • 2.01 Hello World
      • 2.02 Talk Back
      • 2.03 ASCII Values
      • 2.04 Ski Game
      • 2.05 Demonstration of the String Object
    • Projects 3 >
      • 3.01 Read the Potentiometer
      • 3.02 ASCIIbet Soup
      • 3.03 Potentiometer sets LED Brightness
      • 3.04 Potentiometer Sets Blink Rate
      • 3.05 LED Chase, Part II
    • Projects 4 >
      • 4.01 Bringing the Piezo to Life
      • 4.02 Controlling the Piezo with a Function
      • 4.03 Piezo C Major
      • 4.04 Piezo Greensleaves
      • 4.05 Piezo Metronome
      • 4.06 Piezo as an Input
      • 4.07 Piezo as an Input 2
      • 4.08 Metronome II
      • 4.09 Piezo Playback
      • 4.10 Piezo Fireworks
      • 4.11 Piezo Mosquito
    • Projects 5 >
      • 5.01 The Phototransistor
      • 5.02 Light and Sound
      • 5.03 Light and Sound II
    • Projects 6 >
      • 6.01 EEPROM
      • 6.02 I2C Address Scan
      • 6.03 Read the I2C Temperature Sensor
      • 6.04 High Temperature Alarm
    • Projects 7 >
      • 7.01 Arno Phone Home
      • 7.02 Keyboard Alphabet
      • 7.03 Move Mouse
      • 7.04 Draw Squares
    • Special Projects >
      • Bike Light Demo
  • References
    • Arno Pin Key
    • Arno Schematic
    • Project Index

Project 6.04 High Temperature Alarm 

We don’t do anything in this project that we haven’t done before, but we take a different approach.  Here, we shift a lot of the work into functions.  This makes the code easier to follow and easier to adapt for a new project.  You can simply copy the functions and paste them into different projects.

Upload the program, wait a moment, and then open the serial monitor.  The temperature will be output to the serial monitor in °C.  When the temperature goes above the value of the float variable tooHot (set to 32.2 °C (90 °F)), the piezo sounds an alarm.  You can breathe gently on the sensor to raise its temperature and trigger the alarm.

Concepts: I2C communication, wire library

Circuits:
  • Circuit 4
  • Circuit 8


  • Select Sketch

    ///////////////////////////////////////////////////
    //Project 6.04 High Temperature Alarm
    int tempreg = 0;
    float temperature = 0;
    byte address = 72;
    int piezo = 12;
    float tooHot = 32.2;
    //Do some setup for the sensor
    void setup(){
    pinMode(piezo,OUTPUT);
    Serial.begin(9600);
    setupSensor();
    }
    void loop(){
    Serial.print(TEMP = );
    Serial.println(getTemp(),1);
    if(getTemp() > tooHot) piezoTone(4000,10);
    }
    //intialize the sensor
    void setupSensor(){
    Wire.begin();
    Wire.beginTransmission(address);
    Wire.write(0x01);
    Wire.write(0x60);
    Wire.endTransmission();
    Wire.beginTransmission(address);
    Wire.write( byte(0x00)),
    Wire.endTransmission();
    }
    //Query the sensor and return the temperature
    float getTemp(){
    Wire.requestFrom(address, byte(2));
    tempreg = Wire.read();
    tempreg= tempreg << 8;
    tempreg |= Wire.read();
    tempreg = tempreg >> 4;
    temperature =( float ) tempreg / 16;
    return temperature;
    }
    //Sound the alarm!!
    void piezoTone(long freq, long duration){
    long aSecond = 1000000;
    long period = aSecond/freq;
    duration = duration*1000;
    duration = duration/period;
    for(long k = 0; k < duration; k++){
    digitalWrite(piezo,HIGH);
    delayMicroseconds(period/2);
    digitalWrite(piezo,LOW);
    delayMicroseconds(period/2);
    }
    }
    ///////////////////////////////////////////////////

    Both the setup() and loop() blocks are simple in this program. Besides setting pinMode for the piezo and initializing the Serial object, the loop() block makes a call to the setupSensor function to set the sensor’s resolution and direct the pointer to register 1 (this code is explained in detail in Project 6.03):

    void setupSensor(){

      Wire.begin();

      Wire.beginTransmission(address);

      Wire.write(0x01);    

      Wire.write(0x60);    

      Wire.endTransmission();       

      Wire.beginTransmission(address); 

      Wire.write( byte(0x00)),           

      Wire.endTransmission();        

    } 

    The loop() block contains only three lines. The first starts the output line to the serial monitor:

      Serial.print("TEMP = ");

    The next line makes a call to our function getTemp and prints the result to the serial monitor with one decimal place.  This all happens in one line:

      Serial.println(getTemp(),1);

    The third line makes another call to getTemp and calls the piezoTone function if the returned values is greater than 32.2:

        if(getTemp() > 32.2) piezoTone(4000,10);

    The getTemp function contains the code to read the temperature sensor and return a float value.  The code is explained in Project 6.03:

    float getTemp(){

      Wire.requestFrom(address, byte(2));

      tempreg = Wire.read();      

      tempreg= tempreg << 8;     

      tempreg |= Wire.read();  

      tempreg = tempreg >> 4; 

      temperature =( float ) tempreg / 16;

      return temperature;ke

    }

    We used the piezoTone function in a lot of our other projects.  This is a good example of portable code.  We only needed to paste it into this sketch to use it for our alarm.



    Back to Projects 6

    Copyright Olympia Circuits LLC 2014. All Rights Reserved.