Problem Reading One wire

I’m reading 2 onewire sensors.

The problem is when I read from serial or publish, sometimes did not read nothing, sometimes only one sensor and sometimes both (the desired option :slight_smile: )

See the code

// This #include statement was automatically added by the Particle IDE.
#include <OneWire.h>

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

//Pins for Dallas Sensors
DS18 sensor1(D4);
DS18 sensor2(D5);

void setup() {
  pinMode(D4, OUTPUT);
  pinMode(D5, OUTPUT);



void loop() {
digitalWrite(D4, HIGH);  
float tempfresh =;
digitalWrite(D4, LOW);
  digitalWrite(D5, HIGH);
float tempfrozen =;
digitalWrite(D5, LOW);

//battery level
FuelGauge fuel;
float batt;
batt = fuel.getVCell();

//Serial Printing

//Print Dallas Ground Temp
Serial.printf("Temperature1 %.2f C", sensor1.celsius());
Serial.printf("Temperature2 %.2f C", sensor2.celsius());


Thank’s guys


This should be moved out of loop()

Also, it the digitalWrite(High) and Low required as part of the reading sequence?

What are the purpose of D4 and D5? Are you powering the sensors up? Maybe you need more time to get the sensor to work before read the temperature.

You could also do more reads (lets say 3 or 4) and just break if the value is different from 0 or 85.


The purpose is because I have 2 sensors connected. One at D4 an another at D5.

When you said more time, I tried with 2 seconds and did not work.



Hi kenneth

The purpose is Read up sensor1, read, store in a variable, and disconnect the pin. And do the same for the sensor2.

All the sensors are DS18B20. I put a delay of 1 second between the sensor1 reading and sensor2.

When you said the battery level should be out of the loop, you suggest to me to put before loop?
Digitalwrite is to power up and down the sensors, should I put out of the loop too?

Thank’s for your help


So you have DS18B20 connected to D4 and D5, don’t you? Or D4 and D5 are just to power the sensor on?

In the first scenario, I don’t think you have to do anything with the pin, the library will do that.

Second scenario, I am not sure about warming time thesensor will need, but maybe it takes more time or something like. Maybe current is not enough. I can’t tell.