DS18B20 - SamuelLarkin/poulailler.IoT GitHub Wiki

DS18B20 Temperature Sensor

Pat gave us a type 3 DS18B20 which means that

Sensor type 1 Sensor type 2 Sensor type 3 Espruino 4.7k Resistor
Black Green Yellow GND
Red Red Red 3.3v 1st wire
White/Blue/Yellow Yellow Green Data - any GPIO 2nd wire

IMPORTANT

I couldn't get the DS18B20 to read the temperature correctly. I was always getting 85. It is critical that we delay(750); after calling sensors.requestTemperatures();.

Wiring

DS18B20

Links

Minimalist Example

#include <OneWire.h>
#include <DallasTemperature.h>

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(GPIO_NUM_25);

// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);

void setup() {
  sensors.begin();
}


void loop() {
  sensors.requestTemperatures();
  delay(750);  // This seems critical to get the sensors to work with AtomLite

  const unsigned int num_DS18B20 = sensors.getDeviceCount();

  for (unsigned int i=0; i<num_DS18B20; ++i) {
    DeviceAddress mac;
    sensors.getAddress(mac, i);
    delay(10);
    const float temperature = sensors.getTempCByIndex(i);
    send_MQTT(mac, temperature);
    }
  }
  delay(10000);
}```

## Python Example

* [source](https://randomnerdtutorials.com/micropython-ds18b20-esp32-esp8266)
* [micropython example](http://docs.micropython.org/en/v1.8.2/esp8266/esp8266/tutorial/onewire.html)

```python
# Complete project details at https://RandomNerdTutorials.com
import machine, onewire, ds18x20, time

ds_pin    = machine.Pin(4)
ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))

roms = ds_sensor.scan()
print('Found DS devices: ', roms)

while True:
  ds_sensor.convert_temp()
  time.sleep_ms(750)  # This is important for the code to work
  for rom in roms:
    print(rom)
    print(ds_sensor.read_temp(rom))
  time.sleep(5)
⚠️ **GitHub.com Fallback** ⚠️