-->
Page 1 of 2

Connecting a OneWire device (DS18B20) does not work

PostPosted: Tue Nov 24, 2015 10:53 am
by Bilathon
This is my first serious Arduino/ESP8266 project so please bear with me...

I am trying to get a DS18B20 temperature sensor working win an ESP8266. I have wired the sensor on my breadboard with the required resistor (tried both 2.2K Ohm and 3.7K Ohm). Everything is working fine when I connect an Arduino to the breadboard (ground, +3.3V, and data pin 2) in combination with the example code that comes with the OneWire library.

The board I am using is this one (I bought two of them, both behave exactly the same):
http://www.aliexpress.com/item/V3-Wirel ... 17059.html

Here is a picture of my breadboard setup:
http://home.spelplein.eu/onewire.jpg

As mentioned above, if I connect the wires that are now connected to D2, 3V3, and GND to the equivalent Arduino pins the Arduino can read the temperature sensor just fine.

I use the Arduino IDE in combination with esp8266 board manager. This works great! Except when I wire this board up to the breadboard instead of my Arduino. I quadruple checked that I use the OneWire library and example that come with the ESP8266 board manager. I even added prints in the library to see where things go south.

This is what my minimalistic sketch looks like:

Code: Select all#include <ESP8266OneWire.h>

OneWire  ds(2);  // on pin 2 (a 4.7K resistor is necessary)

void setup(void) {
  Serial.begin(9600);
}

void loop(void) {
  byte addr[8];

  if ( !ds.search(addr)) {
    Serial.println("No more addresses.");
    Serial.println();
    ds.reset_search();
    delay(250);
    return;
  }
  Serial.println("search ok.");

  delay(2000);
}


Note that I renamed the OneWire library to make sure I use the right one. I cut out all the stuff after this code in the example because this code already fails. The output shows a continuous stream of "No more addresses.".

The 'OneWire::reset' method (called from the OneWire::search method) always returns false. This is the method straight from the ESP8266 OneWire library:

Code: Select alluint8_t OneWire::reset(void)
{
        IO_REG_TYPE mask = bitmask;
        volatile IO_REG_TYPE *reg IO_REG_ASM = baseReg;
        uint8_t r;
        uint8_t retries = 125;

        noInterrupts();
        DIRECT_MODE_INPUT(reg, mask);
        interrupts();
        // wait until the wire is high... just in case
        do {
                if (--retries == 0) return 0;
                delayMicroseconds(2);
        } while ( !DIRECT_READ(reg, mask));

        noInterrupts();
        DIRECT_WRITE_LOW(reg, mask);
        DIRECT_MODE_OUTPUT(reg, mask);  // drive output low
        interrupts();
        delayMicroseconds(480);
        noInterrupts();
        DIRECT_MODE_INPUT(reg, mask);   // allow it to float
        delayMicroseconds(70);
        r = !DIRECT_READ(reg, mask);
        interrupts();
        delayMicroseconds(410);
        return r;
}

The last DIRECT_READ always returns TRUE, making r false and the method fail.

Do you have any idea what I am doing wrong?

Re: Connecting a OneWire device (DS18B20) does not work

PostPosted: Tue Nov 24, 2015 2:49 pm
by bbx10node
Try connecting the DS to the pin labelled D4. Or change the sketch to use pin 4 instead of 2. The reason is the labels on the board do not match up with Arduino IDE GPIO numbers. See the following for more details.

https://github.com/nodemcu/nodemcu-devkit-v1.0#pin-map

Re: Connecting a OneWire device (DS18B20) does not work

PostPosted: Wed Nov 25, 2015 3:25 am
by Bilathon
That did it, thank you so much! I never considered that the pin numbers could be off. There is probably in interesting reason behind this but for now I am just glad I can continue.

Re: Connecting a OneWire device (DS18B20) does not work

PostPosted: Mon Jun 06, 2016 1:25 am
by lorneb
Just posting my findings: getting the DS18X20_Tempreture sketch to work with ESP8266

Pin 2 of the ESP8266 supports a pullup with input, so my sketch worked fine without the external pull up resister
with the following addition to the setup()
pinMode(2, INPUT_PULLUP);

Cheers