Professional Documents
Culture Documents
Lausa Exercise-5
Lausa Exercise-5
Lausa
4-H
Microprocessors
Exercise 5
1. Create a program that would display the temperature reading from two DS18820
temperature sensors on the serial monitor
2. For task 1 above, modify the program so that once the temperature sensed from either the two
temperature sensors reach 40° Celsius it would display an alert message on the serial monitor.
The message would contain only the sensor number together with the actual sensed temperature and
the warning message
1.
#include <DallasTemperature.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 8
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int numberOfDevices;
DeviceAddress tempDeviceAddress;
void setup(void) {
Serial.begin(9600);
sensors.begin();
numberOfDevices = sensors.getDeviceCount();
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");
{ if(sensors.getAddress(tempDeviceAddress, i))
Serial.print(i, DEC);
Serial.print(" with address: ");
int printAddress(tempDeviceAddress);
Serial.println();
} else {
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");
void loop(void) {
sensors.requestTemperatures();
if(sensors.getAddress(tempDeviceAddress, i))
"); Serial.println(i,DEC);
Serial.print("Temp C: ");
Serial.print(tempC);
Serial.println(DallasTemperature::toFahrenheit(tempC));
delay(1000);
Serial.print("0");
Serial.print(deviceAddress[i], HEX);
2.
#include <DallasTemperature.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 8
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int numberOfDevices;
DeviceAddress tempDeviceAddress;
#include <LiquidCrystal.h>
float temp;
void setup(void)
{ pinMode(tempPin,
Serial.begin(9600);
sensors.begin();
numberOfDevices = sensors.getDeviceCount();
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");
for(int i=0; i<numberOfDevices; i++)
{ if(sensors.getAddress(tempDeviceAddress, i))
Serial.print(i, DEC);
int printAddress(tempDeviceAddress);
Serial.println();
} else {
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");
void loop(void) {
sensors.requestTemperatures();
if(sensors.getAddress(tempDeviceAddress, i))
"); Serial.println(i,DEC);
Serial.print("Temp C: ");
Serial.print(tempC);
Serial.println(DallasTemperature::toFahrenheit(tempC));
}
temp = readTemp();
// Serial.println(temp);
lcd.print("Alert!!! ");
lcd.print(temp);
lcd.print("C");
} else
{ lcd.print("TEMP:
"); lcd.print(temp);
lcd.print("C ");
delay(1000);
lcd.clear();
delay(1000);
float readTemp() {
temp = analogRead(tempPin);
Serial.print("0");
Serial.print(deviceAddress[i], HEX);