0

$USD

$USD
PROJECTS Arduino

Arduino Project 7: Temperature Alarm

DFRobot Apr 18 2017 7376

Related Product: Beginner Kit for Arduino

Arduino Tutorial 7: Temperature Alarm

This arduino tutorial lesson we use the arduino starter kit to learn Temperature Alarm.

We added a temperature sensor to the previous circuit to trigger the buzzer to make a sound when the temperature reaches a certain range. This is our first project using an actuator responding to a sensor.

component

DFRduino UNO (similar as Arduino UNO R3) *3
Prototype Shield *1
Jumper Cables M/M *5
Buzzer*1
Tem.Sensor*1Based on the circuit of project 6, connect temperate sensor LM35 as  below. The pins are connected to 5V, Analog pin 0 and GND.


Sample code 7-1:

//Project 7 Temperature Alarm
float sinVal;
int toneVal;
unsigned long tepTimer ;
void setup(){    
pinMode(8, OUTPUT);    // configure pin of buzzer
Serial.begin(9600);   // configure baud rate to 9600 bps
}
void loop(){
int val;                     //save the value of LM35
double data;             // save the converted value of temperature
val=analogRead(0);    //Connect LM35 to analog pin and read
value from it
data = (double) val * (5/10.24); //Convert the voltage value to
temperature value
if(data>27){ //If temperature is higher than 27, the buzzer starts to make sound.
for(int x=0; x<180; x++){
//Convert sin function to radian
sinVal = (sin(x*(3.1412/180)));
//Use sin function to generate frequency of sound
toneVal = 2000+(int(sinVal*1000));
//Configure the buzzer pin 8
tone(8, toneVal);
delay(2);
}
} else {                              // If the temperature is lower than
27, turn off the buzzer
noTone(8);                      // Turn off the buzzer
}
if(millis() - tepTimer > 50 // Every 500 ms, serial port outputs
temperature value.
tepTimer = millis();
Serial.print("temperature: ");   // Serial port outputs temperature
Serial.print(data);                // Serial port outputs temperature
value
Serial.println("C");              // Serial port output temperature
unit
}
}
After the code is successfully uploaded, open the serial monitor of Arduino IDE.


Read temperature value from the serial port. If you put your fingers on the LM35 sensor, you will find the temperature rises immediately. Your fingers are transferring heat to the sensor!


As per the program, once the temperature reaches 27 degrees C, the buzzer starts to sound. If the temperature drops below 27 degrees C, the buzzer stops.


Most of the above codes are the same as those in Project 6. Almost all of the syntax has been mentioned in previous projects. Hopefully you have some understanding about the variables and functions by now.
We initialize 3 variables at the top of the program.

float sinVal;
int toneVal;
unsigned long tepTimer ;


The third variable “tepTimer” is an unsigned long datatype to store time and output temperature values from serial port.
Why “unsigned long”?
Since the machine will run for a relatively long time, we choose a long integer and since it cannot store negative numbers, it is unsigned.
In the first line of the “setup()” function, why do we only configure the buzzer as output mode and disregard the LM35 temperature sensor?
The LM35 uses analog values. Analog values don’t need to be configured for “pinmode”. “pinMode” is only used for digital pins.


There are many functions for serial port communication:

Serial.begin(9600);

This function is to initialize the baud rate (data transmit rate) of the serial port. Normally the default setting in the serial monitor works for most applications, butsome wireless modules have a specific baud rate requirement.

In the “loop()” function, we declare 2 variables: “val” and “data” at the top. These are variables in a limited scope so that they only run inside the individual block of code.

val=analogRead(0);

This is a new function, “analogRead (pin)”.

analogRead(pin)

This function reads a value from the specified analog pin. The digital pins in the Arduino are connected to a 10 byte analog to digital converter, so the voltage between 0 and 5V is converted to a value ranging between 0 and 1023. Each value corresponds to a value of voltage. The voltage value of temperature read here outputs a range between 0 to 1023. Every 10mV corresponds to 1 degree for LM35 temperature sensor.
data = (double) val *
(5/10.24);
From the voltage value read via the sensor, the range is from 0 to 1023. So we divide it into 1024 parts and multiply the result by 5 to convert it to voltage value.
Since 10mV corresponds to 1 degree, we need to multiply that to get a temperature value in double datatype and assign it to a data variable.


The Serial Port

The serial port allows the Arduino to communicate with the external world by transmitting and receiving data. There is at least one serial port in each Arduino micro-controller, separately connected to digital pin 0 (RX/data receive) and analog pin 1 (TX/data transmit). Digital pin 1 and 0 cannot be used for I/O function when the serial port is in use. You download code to the Arduino via the serial port. When downloading code, the USB will occupy digital pin RX and analog pin TX. The RX and TX pins can not receive other signals during this, or there will be interference. The best way to use these 2 pins is to insert components after downloading your code.

In the following program, we evaluate the condition by using the “if/else” statement.

The “if/else” statement format:

if (expression) {
Statement 1;
} else{
Statement 2;
}

This function is known as a conditional. It works as follows:
An expression is specified. If the conditions of the expression are true, statment 1 is executed and statement 2 is skipped.
If the expression is false, statment 2 is executed and statment 1 is skipped.
Either statement 1 or 2 is to be executed but simultaneous execution is prevented. Put in simple terms, this is the Arduino making a decision between two pre determined variables.

if(data>27){ for(int
x=0; x<180; x++){
……
}
} else {
…… }

If the temperature is higher than 27, The program runs the first part of the program, following the if statement to activate the buzzer. If not, it runs the else statement to stop the buzzer.

Apart from detecting temperature change for our alarm, we also need to display the temperature the Arduino reads via the serial port. We need to use the “millis()” function again to send out data every 500ms. (See Project 3 for more details if you are unsure.) After the serial port has received data, how can we display it on the serial monitor?

Serial.print(val);
Serial.println(val);

print() works to convert “val”
to a readable ASCII format
(standard text) output from the
serial port.


There are various formats for this function:
1. numbers output as a number
e.g.: Serial.print(78); outputs “78”

2. floating datatype outputs as floating number with maximum 2 digits after decimal point
e.g. Serial.print(1.23456); outputs “1.23”

3. Add single quotation mark to character and add double quotation mark to character string.
e.g. Serial.print(‘N’); outputs “N”

Serial.print(“Hello world.”);
outputs “Hello world.”

The difference between “println()” and “print()” is that “println()” has a new line character.

Another common command is “Serial.write()”. It does not output in ASCII format but in a byte format. Check the reference on Arduino.cc if you are interested in finding out more.

Serial.print(data);

Is data a character string?
Why does it output numbers?
The answer is because we declared the variable in the program setup function to assign a number to it.

Components

LM35 temperature sensor

The LM35 is a very common temperature sensor which is accurate to +0.25
It has 3 pins:
"+Vs" is power
"Vout" is voltage output
"GND" is ground.


The calculation formula is as follows:


If you want to learn more about this component, you can consult the data sheet. his gives extra detail on how a method for converting temperature data into voltage.

A useful resource for component datasheets can be found here:
http://www.alldatasheet.com/



Related category: arduino kits > education kits
Last Arduino Project 6: Alarm
Next Arduino Project 8: Vibration Sensor

Exercise

Add a LED to the project above. When the temperature is in a defined range, make the LED turn on and make the buzzer sound.
You can assign different colored LEDs and different buzzer sounds for different temperature ranges

E.g.:
- When the temperature is lower than 10 or higher than 35, a red LED turns on and the buzzer
makes a rapidly-oscillating sound
- When the temperature falls between 25 and 35, a yellow LED turns on and buzzer makes a
smooth-osciallating sound
- When the temperature falls between 10 and 25, a green LED turns on and the buzzer is off.