Quantcast
Viewing all articles
Browse latest Browse all 10

Controlling the temperature with Dallas DS18xx sensor. Arduino IDE

Description

Control a climatic camera temperature using a DS18xx temperature sensor using the OneWire protocol. This is a simple way to know the temperature provided by a temperature sensor. The good thing of this sensor is that it already provides a library of Arduino, which helps in its programmation.

 

 

Requirements

Dallas DS18xx Arduino library:

https://github.com/milesburton/Arduino-Temperature-Control-Library

 

Connection

Red: 5V
Yellow: PIN2 (data pin)
Black: GND

Image may be NSFW.
Clik here to view.

List connected devices

The sketch lists the devices connected to the OneWire pin and show their address. It also shows a note when the device is not a DS18xx device supported by the library.

#include <DallasTemperature.h>

#define ADDR_LEN 8
#define PIN 3

OneWire oneWire(PIN);
DallasTemperature sensors(&oneWire);

////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin(9600L);
  Serial.println("ds18xx-list-devices started");
}

////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
  Serial.println("----------------------------------------------------");

sensors.begin();

int deviceCount = sensors.getDeviceCount();
  Serial.print("Devices: ");
  Serial.println(deviceCount);

int ds18Count = sensors.getDS18Count();
  Serial.print("DS18xx devices: ");
  Serial.println(ds18Count);

uint8_t address[ADDR_LEN];
  for (int i = 0; i < deviceCount; ++i) {
    if (sensors.getAddress(address, i)) {
      Serial.print("Address: ");
      printAddress(address);
    }
  }
  delay(5000);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
void printAddress(const uint8_t *address) {
  for (int i = 0; i < ADDR_LEN; ++i) {
    if (i > 0) {
      Serial.print('-');
    }
    if (address[i] < 0x10) {
      Serial.print('0');
    }
    Serial.print(address[i], HEX);
  }
  if (!sensors.validFamily(address)) {
    Serial.print(" (not supported)");
  }
  Serial.println();
}

 

Control the heater based on temperature reads

The sketch gets the temperature sensor using the library and then reads periodically its temperature. When the temperature is colder than the temperature set point, it activates the Q0.0 output to switch the heater on.

#include <DallasTemperature.h>
#define TEMP_SET_POINT 25      // Celsius
#define TEMP_READ_PERIOD 1000  // ms
#define TEMP_RESOLUTION 12     // bits
#define ONEWIRE_PIN 2          // Pin for the OneWire
#define HEATER_PIN Q0_0        // Digital Output pin to ON the heater

Not configurable constants
#define INVALID_SENSOR_INDEX 0xff
#define INVALID_TEMP 0x7fff
#define ADDRESS_LEN 8 // bytes

OneWire oneWire(ONEWIRE_PIN);              
DallasTemperature sensors(&oneWire);    
uint8_t tempIndex = INVALID_SENSOR_INDEX;
double temp = INVALID_TEMP;

////////////////////////////////////////////////////////////////////////////////////////////////////

void setup() {
  Serial.begin(9600L);
  Serial.println("ds18xx-temp-control started");
  initTempSensor();
}
////////////////////////////////////////////////////////////////////////////////////////////////////

void loop() {
  updateTemp();
  updateHeater();
}

////////////////////////////////////////////////////////////////////////////////////////////////////

void printAddress(const uint8_t *address) {
  for (uint8_t i = 0; i < ADDRESS_LEN; ++i) {
    if (i > 0) {
      Serial.print('-');
    }
    if (address[i] < 0x10) {
      Serial.print('0');
    }
    Serial.print(address[i], HEX);
  }
}

////////////////////////////////////////////////////////////////////////////////////////////////////

void initTempSensor() {
  // Begin sensors library
  sensors.begin();
  // Get temperature sensor
  uint8_t address[ADDRESS_LEN];
  uint8_t numDevices = sensors.getDeviceCount();   //number of devices connected
  for (uint8_t i = 0; i < numDevices; ++i) {       //loop for check all the devices connected
    if (sensors.getAddress(address, i)) {          //verification of correct address
      if (sensors.validFamily(address)) {          //verification of valid family
        tempIndex = i;                             //tempIndex different from INVALID_SENSOR_INDEX, flag
        Serial.print("Sensor address: ");
        printAddress(address);
        Serial.print(" (");
        Serial.print(tempIndex);
        Serial.print(")");
        Serial.println();
        break;
      }
    }
  }
  // Set sensors parameters
  sensors.setResolution(TEMP_RESOLUTION);        //This temperature resolution is of 12bits
}

////////////////////////////////////////////////////////////////////////////////////////////////////

void updateTemp() {
  static uint32_t lastUpdate = millis();
  if (tempIndex == INVALID_SENSOR_INDEX) {      //If the sensor index is invalid, we initialize the temperature sensor again.
    initTempSensor();                       
  } 
  else {                                       //If it is correct, we read the actual temperature from the sensor
    if (millis() - lastUpdate > TEMP_READ_PERIOD) {
      if (sensors.requestTemperaturesByIndex(tempIndex)) {
        temp = sensors.getTempCByIndex(tempIndex);
      }
    }
  }
}

////////////////////////////////////////////////////////////////////////////////////////////////////
void updateHeater() {                         //Function that controls the heater, it compares the temperature with the set point in order to activate or not the digital output.
  static double lastTemp = INVALID_TEMP;
  if (lastTemp != temp) {
    digitalWrite(HEATER_PIN, temp < TEMP_SET_POINT ? HIGH : LOW);
    lastTemp = temp;
  }
}

SEE ALSO:

DS18B20 TEMPERATURE SENSOR- ONE WIRE – ARDBOX – MDUINO, CONTROLLING TEMPERATURE WITH A GOT EXCHANGER THROUGH A MODULATING VALVE IN CHEMICAL INDUSTRY


Viewing all articles
Browse latest Browse all 10

Trending Articles