Arduino tutorial 07 Temperature Alarm

We added a temperature sensor to the project’s[Alarm] 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 LIST

 

COMPONENT LIST


HARDWARE


 

Based on the circuit of the project[Alarm], connect temperate sensor LM35 as follows. The pins should be connected to 5V, Analog pin 0, and GND in sequence when the flat side of the sensor is facing towards you.

 

Temperature Alarm Circuit

fig 1 Temperature Alarm Circuit

 

CODE
Sample code:

CODE
// Project – Temperature Alarm  
float sinVal;            // Variable to store sine function value  
int toneVal;             // Variable to store tone frequency  
unsigned long tepTimer;  // Timer for periodic temperature output  
  
void setup(){   
    pinMode(8, OUTPUT);  // Set pin 8 as OUTPUT for buzzer  
    Serial.begin(9600);  // Start serial communication at 9600 bps  
}  
  
void loop(){   
    int val;          // Variable to store the value read from LM35  
double data;  
// Variable to store the converted temperature value  
val = analogRead(0);   
// Read the value from analog pin 0 connected to LM35  
data = (double) val * (5.0/10.24);  
// Convert voltage to temperature  
       
if(data > 27){        
// If the temperature is greater than 27, sound the buzzer  
          for(int x = 0; x < 180; x++){  
            // Convert angle for sine function to radians  
            sinVal = (sin(x * (3.1412/180)));  
            // Generate tone frequency using sine function value  
            toneVal = 2000 + (int(sinVal * 1000));  
            // Play tone on pin 8  
            tone(8, toneVal);  
            delay(2);   
     }     
} else {    
// If the temperature is less than 27, stop the buzzer  
           noTone(8);          // Stop the buzzer  
    }  
       
if(millis() - tepTimer > 500){     
// Every 500ms, print temperature to serial port  
             tepTimer = millis();  
             Serial.print("temperature: ");     
// Print "temperature" to serial port  
             Serial.print(data);               
// Print temperature value to serial port  
             Serial.println("C");              
// Print temperature unit to serial port  
       }   
}

After the code is successfully uploaded, open the serial monitor of Arduino IDE.

set the baud rate to 9600.

Read the temperature value from the serial port. If you put your fingers on the LM35 sensor, the temperature rises immediately as 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.

 

 

CODE REVIEW


Most of the above codes are the same as those in Project[Alarm]. Almost all of the syntax has been mentioned in previous projects. Hopefully, you have some understanding of 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 the 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 the LM35 temperature sensor.

 

data = (double) val * (5.0/1024)*100;

 

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 a voltage value. Since 10mV corresponds to 1 degree, we need to multiply 100 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 the I/O function when the serial port is used. You download the code to the Arduino via the serial port. When downloading the 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, statement 1 is executed and statement 2 is skipped.


If the expression is false, statement 2 is executed and statement 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 predetermined 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 changes 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[Traffic Light] 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 numbers.
e.g.: Serial.print(78) outputs “78”.

 

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

 

3. Add a single quotation mark to the character and add a double quotation mark to a character string.
e.g.: Serial.print(‘N’) outputs “N”and 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.

 

 

HARDWARE REVIEW
 

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


Note that pin 1 (+VS) is the leftmost pin when the flat side of the sensor (with the text printed on it) is facing towards you. The pinout of the sensor is as follows: 

 

fig 2 Diagram of LM35 pins

 

The calculation formula is as follows:


Vout=10mV/°C*T°C(Temperature range +2℃~40℃)

 

If you want to learn more about this component, you can consult the datasheet. This 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/

 

fig 3 Formula of LM35


EXERCISE

 

Add an 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 the buzzer makes a smooth-oscillating sound
- When the temperature falls between 10 and 25, a green LED turns on and the buzzer is off.

License
All Rights
Reserved
licensBg
0