DS18B20 Switching pins during execution

OneWire sensors are usually addressable too.
So you’d just keep them wired and use the respective address to tell all the sensors on the bus which one you want to talk to.

1 Like

Yes, I know that. But I can’t use that in this project. It is a box with multiple inputs in which any user can connect a sensor (ds18b20, dht22, uv, water level…). Any input could contain any of those sensors at any moment. The user through a mobile app decide which sensor is connected at any moment.

That is why Ineed one for each pin.

The version I have is the latest one, I think: https://github.com/LukeUSMC/ds18b20-photon/tree/master/firmware I took from here Onewire and DS18B20.

I made some changes to have 2 instances at the same time and select one another with a pointer. After a minute working, it get stuck again.

// This #include statement was automatically added by the Particle IDE.
#include "DS18B20.h"

// This #include statement was automatically added by the Particle IDE.
#include "Particle-OneWire.h"

#define TEMP_MAX_DAY   28
#define TEMP_MIN_DAY   27

#define DAY        10
#define DAY_MIN    30
#define NIGHT      21
#define NIGHT_MIN  30

#define PIN1    0
#define PIN2    1
#define PINTEMP D3

int led = D7;
char szInfo[64];
float pubTemp;
float celsius;
double fahrenheit;

uint8_t risingEdge;
uint8_t fallingEdge;
uint8_t edgeCounter;

DS18B20 ds18b20D2(D2);
DS18B20 ds18b20D4(D4);

unsigned int Metric_Publish_Rate = 5000;
unsigned int MetricnextPublishTime2;
unsigned int MetricnextPublishTime5;
int DS18B20nextSampleTime;
int DS18B20_SAMPLE_INTERVAL = 2500;
int dsAttempts = 0;

float tempe;
double max_val;
double min_val;

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40);

void setup() {
    pinMode(PINTEMP, OUTPUT);
    pinMode(6, OUTPUT);
    Particle.publish("start", "INIT", PRIVATE);
    max_val = TEMP_MAX_DAY;
    min_val = TEMP_MIN_DAY;

void loop() {
    runManagement(2, 3);
    runManagement(4, 6);

void publishData(uint8_t position){
  sprintf(szInfo, "POS: %d --> %2.2f", position, celsius);
  Particle.publish("dsTmp", szInfo, PRIVATE);
  if(position == 2) {
    MetricnextPublishTime2 = millis() + Metric_Publish_Rate;
  } else if(position == 4) {
    MetricnextPublishTime5 = millis() + Metric_Publish_Rate;

bool  getTemperature(uint16_t pinSensor, float *value) {
  int dsAttempts = 0;
  float _tempWater;
  DS18B20 *ds18b20;
  if(pinSensor == 2) {
      ds18b20 = &ds18b20D2;
  } else if(pinSensor == 4) {
      ds18b20 = &ds18b20D4;
  } else {
      return FALSE;
  if(!ds18b20->search()) {
    _tempWater = ds18b20->getTemperature();
    while (!ds18b20->crcCheck() && dsAttempts < 4) {
      //Serial.printf("Bad value (%d)", dsAttempts++);
      if (dsAttempts == 3)
        _tempWater = ds18b20->getTemperature();
    *value = _tempWater;
    return (dsAttempts < 4);
  *value = -1.0;
  return false;

void runManagement(uint16_t pinSensor, uint8_t pinRelay) {
    //tempe = celsius;
    bool checker = false;
    do {
        checker = getTemperature(pinSensor, &tempe);
        celsius = tempe;
    } while(checker == FALSE);
    if(tempe > max_val) {
        digitalWrite(PINTEMP, LOW);
    } else if(tempe <= min_val) {
        digitalWrite(PINTEMP, HIGH);
  if(pinSensor == 2 && millis() > MetricnextPublishTime2){
    Serial.println("Publishing now.");
  if(pinSensor == 4 && millis() > MetricnextPublishTime5){
    Serial.println("Publishing now.");

Photon’s version is 0.5.3

I think @ScruffR’s idea resolved the problem. I created one ds18b20 instance for each pin and then I pass that instance to my getTemp function. So I keep just one read function working with multiple pins. After almost 2 days it is still working without problems. I will update this post if I find anything else.

Thank you ScruffR

1 Like