Arduino ds18b20 Temperature Sensor , Fastest reading sensors in a single loop, Sketch Code !
#include <OneWire.h>
#define PIN_DS_SENSOR_TEMPERATURE 5 // senzorii de temperatura
#define ledPin 13
OneWire  ds(PIN_DS_SENSOR_TEMPERATURE);
// *******************
uint8_t tHeatingBoiler[8] = {0X28, 0X05, 0XF6, 0XCD, 0X04, 0X00, 0X00, 0XF5};
uint8_t tSensorHeatWater[8] = {0x28, 0x5A, 0xD9, 0x65, 0x05, 0x00, 0x00, 0x76};
uint8_t* tSensorList[2] = {tHeatingBoiler, tSensorHeatWater};
float T_HeatingBoiler=0;
float T_HeatingHeatWater=0;
float* tSensorValue[2] ={&T_HeatingBoiler,&T_HeatingHeatWater} ;
boolean SensorStat_HeatingBoiler=false;
boolean SensorStat_HeatingHeatWater=false;
boolean* tSensorStat[2] ={&SensorStat_HeatingBoiler,&SensorStat_HeatingHeatWater} ;
// *******************
void setup() {
  Serial.begin(9600);
}
void loop() {
 DS_FastInit();
 delay(550);
 DS_FastReadTemp();
 Serial.println((String)T_HeatingBoiler+" | "+(String)T_HeatingHeatWater);
 Serial.println("=================");
}
void DS_FastInit() {
  byte ns = sizeof(tSensorList);
  
  for (int si=0; si<ns; si++) {
    ds.reset();
    ds.select(tSensorList[si]);
    ds.write(0x44, 0); // start conversion, with no-parasite power on at the end !
  }
}
void DS_FastReadTemp() {
  byte data[12];
  boolean validtemp=false;
  byte ns = sizeof(tSensorList);  
  for (int si=0; si<ns; si++) {
    ds.reset();
    ds.select(tSensorList[si]);
    ds.write(0xBE); // Read Scratchpad
    
    for ( int i = 0; i < 9; i++) 
        data[i] = ds.read();   
    
    int16_t raw = (data[1] << 8) | data[0];
    byte cfg = (data[4] & 0x60);
    if (cfg == 0x00) raw = raw & ~7; 
    else if (cfg == 0x20) raw = raw & ~3; 
    else if (cfg == 0x40) raw = raw & ~1; 
    
    validtemp = (raw != -1);
    if (validtemp)
         *tSensorValue[si] = (float)raw / 16.0;
         
    *tSensorStat[si] = validtemp;    
  }
}
1-Wire digital temperature sensor tutorial for fast reading !!!
– initially all sensors are initialized with reading command: DS_FastInit()
– waiting for a while … max 750ms for 12 bit resolution: delay(750)
– then you can read the data from the sensors in a single loop : DS_FastReadTemp();
– … repeat and have fun with sensors !
*Note: sensors and results are type array/vectors ! … see the code: tSensorList, tSensorValue, tSensorStat
Be First to Comment