Close

8 november 2016

Meet de watertemperatuur met een DS18B20 temperatuur sensor en een Arduino Uno

Een simpele badwater-thermometer die door middel van 3 LED’s aangeeft of het badwater op de juiste temperatuur is (voor baby’s 36-38 graden Celsius). De blauwe LED staat voor te koud, de rode LED voor te warm en als de groene LED brandt dan is het water op de juiste temperatuur.

De temperatuur sensor is een DS18B20 waterbestendige sensor met een nauwkeurigheid van 0.5 C. Meer info over dit product op de website van Adafruit.

Installeer in de Arduino IDE de bibliotheek van OneWire én Dallas Temperature Control Arduino.

De code:

// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// arrays to hold device address
DeviceAddress insideThermometer;

/*
 * Setup function. Here we do the basics
 */
void setup(void)
{
 // start serial port
 Serial.begin(9600);
 Serial.println("Dallas Temperature IC Control Library Demo");

// locate devices on the bus
 Serial.print("Locating devices...");
 sensors.begin();
 Serial.print("Found ");
 Serial.print(sensors.getDeviceCount(), DEC);
 Serial.println(" devices.");

// report parasite power requirements
 Serial.print("Parasite power is: "); 
 if (sensors.isParasitePowerMode()) Serial.println("ON");
 else Serial.println("OFF");

// Method 1:
 // Search for devices on the bus and assign based on an index. Ideally,
 // you would do this to initially discover addresses on the bus and then 
 // use those addresses and manually assign them (see above) once you know 
 // the devices on your bus (and assuming they don't change).
 if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0"); 

// show the addresses we found on the bus
 Serial.print("Device 0 Address: ");
 printAddress(insideThermometer);
 Serial.println();

// set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
 sensors.setResolution(insideThermometer, 9);
 
 Serial.print("Device 0 Resolution: ");
 Serial.print(sensors.getResolution(insideThermometer), DEC); 
 Serial.println();

//initialise LED pins
 for(int pinNumber = 3; pinNumber < 6; pinNumber++){
 pinMode(pinNumber, OUTPUT);
 digitalWrite(pinNumber, LOW);
 }
}

// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
// method 2 - faster
 float tempC = sensors.getTempC(deviceAddress);
 Serial.print("Temperatuur: ");
 Serial.println(tempC);
 
}

/*
 * Main function. It will request the tempC from the sensors and display on Serial.
 */
void loop(void)
{ 
 // call sensors.requestTemperatures() to issue a global temperature 
 // request to all devices on the bus
 //Serial.print("Requesting temperatures...");
 sensors.requestTemperatures(); // Send the command to get temperatures
 //Serial.println("DONE");
 delay(100);
 
 
 // It responds almost immediately. Let's print out the data
 printTemperature(insideThermometer); // Use a simple function to print out the data

float tempC = sensors.getTempC(insideThermometer);
 if(tempC < 36.00){
 digitalWrite(3, LOW);
 digitalWrite(4, LOW);
 digitalWrite(5, HIGH);
 } else if(tempC >= 36.00 && tempC <= 38.00){
 digitalWrite(3, LOW);
 digitalWrite(4, HIGH);
 digitalWrite(5, LOW);
 } else if(tempC > 38.00){
 digitalWrite(3, HIGH);
 digitalWrite(4, LOW);
 digitalWrite(5, LOW);
 }
 delay(1);
}

// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
 for (uint8_t i = 0; i < 8; i++)
 {
 if (deviceAddress[i] < 16) Serial.print("0");
 Serial.print(deviceAddress[i], HEX);
 }
}

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Verplichte velden zijn gemarkeerd met *