To add alarm functionality we will be using two tactile switch buttons and a piezo buzzer.

The button to lower the alarm value will be wired to pin 2 on the feather. The raise alarm value button will be wired to pin 3 on the feather. Each button will have the other leg wired to ground as seen in the fritzing diagram below.

For the Piezo Buzzer one wire will go to ground and the other to pin 13 on the feather.

The code is mostly the same from previous step except for a few additions.

  • The buttons and what pin they are assigned to.
  • A starting value for alarmTemp. This can be changed to any value.
  • Buzzer as an output and the pin it is connected to.
  • We also add functions to run when a button is pressed.
  • An if statement that checks if the alarm has been reached and should alarm
Download: file
// include the library code:
#include <LiquidCrystal.h>
#include <math.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(6, 5, 9, 10, 11, 12);

// Degree symbol bitmap
byte degree[8] = {
  B01000,
  B10100,
  B01000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
};

//TMP36 Pin Variables
const int sensorPin = A0; //the analog pin the TMP36's Vout (sense) pin is connected to

//Pins for Tactile buttons used for raising or lowering alarm temperature
const int lowerAlarmTemp = 2;
const int raiseAlarmTemp = 3;

//Alarm value, change to what starting value should be.
int alarmTemp = 80;

//Buzzer Pin
const int buzzer = 13;
//frequency out
int freq;

//Set to 1 to display Celsius instead of Fahrenheit
int celsius = 0;

void setup() {
  //Create the degree symbol bitmap
  lcd.createChar(0, degree);

  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

  //Set tactile buttons as inputs, use of pullup means we dont need external resistor
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);

  // Set buzzer pin as output
  pinMode(13, OUTPUT);
}

//Function that runs when button pressed to lower alarm temperature
void lowerAlarm()
{

  static unsigned long last_interrupt_time = 0;
  unsigned long interrupt_time = millis();
  // If interrupts come faster than 400ms, assume it's a bounce and ignore
  if (interrupt_time - last_interrupt_time > 400)
  {
    alarmTemp = alarmTemp - 1; //Lower alarmTemp by one
  }
  last_interrupt_time = interrupt_time;

}

//Function that runs when button pressed to raise alarm temperature
void raiseAlarm()
{
  static unsigned long last_interrupt_time = 0;
  unsigned long interrupt_time = millis();
  // If interrupts come faster than 400ms, assume it's a bounce and ignore
  if (interrupt_time - last_interrupt_time > 400)
  {
    alarmTemp = alarmTemp + 1; //Raise alarmTemp by one
  }
  last_interrupt_time = interrupt_time;
}

void loop() {

  //Interupt that when lowerAlarmTemp button is pressed runs lowerAlarm function
  attachInterrupt(digitalPinToInterrupt(lowerAlarmTemp), lowerAlarm, FALLING);
  //Interupt that when raiseAlarmTemp button is pressed runs raiseAlarm function
  attachInterrupt(digitalPinToInterrupt(raiseAlarmTemp), raiseAlarm, FALLING);

  //Clear LCD
  lcd.clear();

  //Display Currently on the LCD
  lcd.print("Currently ") ;

  //getting the voltage reading from the temperature sensor
  int reading = analogRead(sensorPin);
  // converting that reading to voltage, for 3.3v arduino use 3.3
  float voltage = reading * 3.3;
  voltage /= 1024.0;

  float temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree with 500 mV offset
  //to degrees ((voltage - 500mV) times 100)

  if (celsius == 1) //If you set temperature as Celsius it will print Celsius values
  {
    //Round the temperature to a whole number
    float roundedTempC = round(temperatureC);

    // Display temperature in C
    lcd.print(roundedTempC, 0);
    lcd.write(byte(0)); //Degree symbol we created earlier
    lcd.print("C");

    // Display alarm Temp
    lcd.setCursor(0, 1);
    lcd.print("Alarm at ");
    lcd.print(alarmTemp);
    lcd.write(byte(0));
    lcd.print("C");

    //Check if temperature is equal or greater than alarmTemp
    if (roundedTempC >= alarmTemp)
    {
      tone(buzzer, 200); // Play alarm tone
      delay(400);
      noTone(buzzer);
    }
    else
    {
      noTone(buzzer); //Make sure alarm is off
    }
  }
  else //Display in Fahrenheit
  {
    //Convert from Celsius to Fahrenheit
    float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;

    //Round the temperature to a whole number
    float roundedTempF = round(temperatureF);

    // Display temperature in F
    lcd.print(roundedTempF, 0);
    lcd.write(byte(0)); //Degree symbol we created earlier
    lcd.print("F");

    // Display alarm Temp
    lcd.setCursor(0, 1);
    lcd.print("Alarm at ");
    lcd.print(alarmTemp);
    lcd.write(byte(0));
    lcd.print("F");

    //Check if temperature is equal or greater than alarmTemp
    if (roundedTempF >= alarmTemp)
    {
      tone(buzzer, 200); // Play alarm tone
      delay(400);
      noTone(buzzer);
    }
    else
    {
      noTone(buzzer); //Make sure alarm is off
    }
  }
  delay(1000);
}

Currently the alarm is set to alarm if the temperature is greater than or equal to the set value. To change to alarm if it is at or below the set value we would look for these two individual lines in the code.

Download: file
if (roundedTempC >= alarmTemp)
if (roundedTempF >= alarmTemp)

We would then change them both to less than or equal

Download: file
if (roundedTempC <= alarmTemp)
if (roundedTempF <= alarmTemp)

If everything is good your wiring should look like this and the display should be showing the temperature and the alarm value.

For more information on using tactile switches check out this guide.

To learn more about using a piezo buzzer check out this guide.

This guide was first published on Jan 24, 2017. It was last updated on Jan 24, 2017.
This page (Alarm) was last updated on May 06, 2020.