You are on page 1of 38

Introduction to Arduino Programming II

ABE 147 February 20, 2014

CODING EXERCISE V

TEMPERATURE MONITORING USING SENSORS

CODING EXERCISE V
O Objectives: O Be familiar with Temperature sensor IC O Learn how to interface Temperature sensor IC with Arduino using the Gizduino trainer board O Use serial monitor to display temperature readings

CODING EXERCISE V
O Temperature sensors
O Sensors for monitoring temperature of a

given medium
O Resistance temperature detectors

O Thermocouples
O Thermistors O Infrared O Semiconductor sensors

CODING EXERCISE V
O LM35

O Precision integrated circuit

temperature sensors, with an output voltage linearly proportional to the Centigrade temperature O Contact type

CODING EXERCISE V
O Connect the power supply pins of the

LM35 sensor to the power bus pins of the Arduino board. Connect the output pin of LM35 to analog pin A0. O Create a new sketch. O Type the following:
GND

A0 +5V

int tempPin = 0; //set the pin for the sensor input, or remove this if using analogRead(A0) void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); } // the loop routine runs over and over again forever: void loop() { // read the input on analog pin 0: int analogreading = analogRead(tempPin); // print out the value you read: Serial.println(analogreading); //print value of serial monitor delay(1000); // do this routine at a 1000ms interval }

CODING EXERCISE V
O Now, apply the conversion from analog

reading to temperature value using the equation: temp = analog reading * 0.48828125 or temp = (5.0V * analog value * 100) /1024 NOTE: Equation was derived from multiplying the reading by 5 volt and getting the percentage of the 1024 range

int tempPin = 0; //set the pin for the sensor input void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); } // the loop routine runs over and over again forever: void loop() { // read the input on analog pin 0: int analogreading = analogRead(tempPin);

//convert to temperature (Celsius) float tempC = analogreading * 0.48828125; // print out the value you read: Serial.println(tempC); //print value of serial monitor delay(1000); // do this routine at a 1000ms interval }

CODING EXERCISE V
O Verify the sketch then upload it to the

Arduino board O Open the serial monitor and observe the temperature readings O Now, touch the surface of the LM35 sensor and observe the changes in values

CHALLENGE!
O Display the temperature readings on the LCD

matrix by constructing the circuit for liquid crystal display. Change the interval to 3000ms . HINT: use the previous exercise on universal MCU Training board as guide.

// include the library code: #include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //declare pin for the temperature sensor int tempPin = 0; //analog pin 0
void setup() { // set up the LCD's number of columns and rows: lcd.begin(16, 2); }

void loop() { // set the cursor to column 0, line 0 (note: line 0 is the first row) lcd.setCursor(0, 0); // print the label lcd.print("Temperature"); // read the input on analog pin 0: int analogreading = analogRead(tempPin); // set the cursor to the second line lcd.setCursor(0,1); //convert the reading to equivalent temperature reading float tempC = analogreading * 0.48828125; //5.0V is the supply voltage, //1023 is the maximum value equivalent to the analog reading lcd.print(tempC); //print analog voltage on LCD lcd.print(" C"); //add unit delay(500); //add delay for stability }

CODING EXERCISE VI

MONITORING AND CONTROL

CODING EXERCISE VI
O Objectives: O Learn the Boolean operators O Learn the applications of Arduino O Simulate scenarios based on varying conditions

CODING EXERCISE VI
O IfStatementConditional O Controls a particular device (LED, motor, etc.) as a response to the satisfied requirement O Scenario building

IF ______ , then ______, else ______

CODING EXERCISE VI
O Boolean operators
O && logical AND O True only if both operands are true

if (digitalRead(2) == HIGH && digitalRead(3) == LOW) { }


O || logical OR O True if either of the operands is true

if (digitalRead(2) || HIGH && digitalRead(3) == LOW) { }

CODING EXERCISE VI
O >
O >= O < O <= O == is equal to

WARNING: do not use single = sign for comparing operands. if (a<=10 && b>=2) { }

CODING EXERCISE VI
O Open the example for

IfStatementConditional. (Examples > Control > IfStatementConditional) O Connect a potentiometer on analog pin 0 (A0), connect the power supply pins to the Arduino board
+5V A0 GND

CODING EXERCISE VI
O Upload the sketch to the Arduino board.
O Take note of the state of the LED on the

Arduino board. O Rotate the knob then observe the change in the LED state.
O The LED should turn on if the analog

reading on the potentiometer is greater than 400, otherwise, the LED should be turned OFF. if (analogValue > threshold) { digitalWrite(ledPin, HIGH); }

int potPin = 0; //set the pin for the sensor input int ledPin = 13; //set the pin for the LED (digital pin13) void setup() { pinMode(ledPin, OUTPUT); //set pin13 as output // initialize serial communication at 9600 bits per second: Serial.begin(9600); } // the loop routine runs over and over again forever:

void loop() { // read the input on analog pin 0: int reading = analogRead(potPin); // print out the value you read: Serial.println(value); //print value of serial monitor condition if (reading > 400) { digitalWrite(ledPin,HIGH); } else { digitalWrite(ledPin,LOW); } delay(1000); // do this routine at a 1000ms interval }

CODING EXERCISE IX
O What if we want the potentiometer output

to be exactly equal to 400? if (reading > 400) { //action } if (reading == 400) { //action }

CHALLENGE!
O Using LM35 sensor and Arduino board,

create a circuit which turn on the LED on pin13 of Arduino board if the temperature reaches 30 degrees Celsius.
O HINT: Use the exercise on LM35 sensor

as guide

float tempC; //set the type of variable to float type to include numbers after the decimal point int tempPin = 0; //set the pin for the sensor input int ledPin = 13; //set the pin for LED output void setup() { //set pin13 as output pinMode(ledPin,OUTPUT); // initialize serial communication at 9600 bits per second: Serial.begin(9600); }

// the loop routine runs over and over again forever: void loop() { // read the input on analog pin 0: int analogreading = analogRead(tempPin); //convert to degrees Celsius tempC = analogreading* 0.48828125; // print out the value you read: Serial.println(tempC); //print value of serial monitor

//condition if (tempC > 30) { digitalWrite(ledPin,HIGH); //turn ON LED if temperature is above threshold } else { //otherwise digitalWrite(ledPin,LOW); } delay(1000); // do this routine at a 1000ms interval }

CHALLENGE!
O Using LM35 sensor, Arduino board and

potentiometer, create a circuit which will satisfy the multiple conditions:


1.

2.

3.

LED1 will turn ON if temperature is ABOVE 30 degrees Celsius and potentiometer analog reading is BELOW 200. LED 1 will turn OFF but LED2 will turn ON temperature is ABOVE 30 degrees Celsius and potentiometer analog reading is ABOVE 200. Both LED will turn OFF if the temperature is BELOW 30 degrees Celsius, regardless of the potentiometers reading.

CHALLENGE!
O HINT: use multiple conditions

if (condition1 && condition2) { action1; } else if (condition1 && condition2) { action2; }

float tempC; //set the type of variable to float type to include numbers after the decimal point int led1 = 2; int led1 = 3 int tempPin = 0; int potPin = 1;

void setup() { //set ledPin as output pinMode(led1,OUTPUT); pinMode(led2,OUTPUT); // initialize serial communication at 9600 bits per second: Serial.begin(9600); }

// the loop routine runs over and over again forever: void loop() { // read the input on analog pin 0: int tempAna = analogRead(tempPin); // read the input on analog pin 1: int potAna = analogRead(potPin); //convert analog reading 1 to temperature (Celsius) tempC = tempAna * 0.48828125; // print out the value you read: Serial.println(tempC); Serial.println(potAna);

//condition1 if (tempC > 30 && potAna < 200) { digitalWrite(led1,HIGH); digitalWrite(led2,HIGH); delay(100); } //condition2 else if (tempC >30 && potAna >200 ){ digitalWrite(led1,LOW); digitalWrite(led2,HIGH); delay(100); }

//condition3 else{ digitalWrite(led1,LOW); digitalWrite(led1,LOW); delay(100); } }

REAL LIFE APPLICATIONS


O Temperature monitoring and control in an egg O O O O O

incubator system; Data logging system Water level monitoring and valve controls; Security systems for proximity or motion detection to trigger an alarm; Smoke detection systems; Sun following solar panels, etc.

O NOTE: for non electronic devices such as

thermocouples and strain gauges, industrial amplifiers are required to sense the very low readings.

Thank you!