Arduino Temperature Sensor DS18B20 – Simultaneous parallel fast-reading of multiple sensors

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] ={&amp;T_HeatingBoiler,&amp;T_HeatingHeatWater} ;

boolean SensorStat_HeatingBoiler=false;
boolean SensorStat_HeatingHeatWater=false;
boolean* tSensorStat[2] ={&amp;SensorStat_HeatingBoiler,&amp;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&lt;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&lt;ns; si++) {
    ds.reset();
    ds.select(tSensorList[si]);
    ds.write(0xBE); // Read Scratchpad
    
    for ( int i = 0; i &lt; 9; i++) 
        data[i] = ds.read();   
    
    int16_t raw = (data[1] &lt;&lt; 8) | data[0];
    byte cfg = (data[4] &amp; 0x60);
    if (cfg == 0x00) raw = raw &amp; ~7; 
    else if (cfg == 0x20) raw = raw &amp; ~3; 
    else if (cfg == 0x40) raw = raw &amp; ~1; 
    
    validtemp = (raw != -1);
    if (validtemp)
         *tSensorValue[si] = (float)raw / 16.0;
         
    *tSensorStat[si] = validtemp;    
  }
}

 

1-Wire digital temperature sensor tutorial for fast reading !!!

temperature-senzor-ds18b20-arduino– 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

byrev Written by:

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *