Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Wilmar S.

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.");

for(int i=0; i<numberOfDevices; i++)

{ if(sensors.getAddress(tempDeviceAddress, i))

{ Serial.print("Found device ");

Serial.print(i, DEC);
Serial.print(" with address: ");

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();

for(int i=0; i<numberOfDevices; i++) {

if(sensors.getAddress(tempDeviceAddress, i))

{ Serial.print("Temperature from sensor number:

"); Serial.println(i,DEC);

float tempC = sensors.getTempC(tempDeviceAddress);

Serial.print("Temp C: ");

Serial.print(tempC);

Serial.print(" Temp F: ");

Serial.println(DallasTemperature::toFahrenheit(tempC));

delay(1000);

void printAddress(DeviceAddress deviceAddress) {


for (uint8_t i = 0; i < 8; i++) {

if (deviceAddress[i] < 16)

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>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int tempPin = A0;

float temp;

float tempMax = 40.0;

void setup(void)

{ pinMode(tempPin,

INPUT); lcd.begin(16, 2);

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("Found device ");

Serial.print(i, DEC);

Serial.print(" with address: ");

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();

for(int i=0; i<numberOfDevices; i++) {

if(sensors.getAddress(tempDeviceAddress, i))

{ Serial.print("Temperature from sensor number:

"); Serial.println(i,DEC);

float tempC = sensors.getTempC(tempDeviceAddress);

Serial.print("Temp C: ");

Serial.print(tempC);

Serial.print(" Temp F: ");

Serial.println(DallasTemperature::toFahrenheit(tempC));
}

temp = readTemp();
// Serial.println(temp);

if (temp > tempMax) {

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);

return (temp * 0.48828125);

void printAddress(DeviceAddress deviceAddress) {

for (uint8_t i = 0; i < 8; i++) {

if (deviceAddress[i] < 16)

Serial.print("0");

Serial.print(deviceAddress[i], HEX);

You might also like