Using the DS18B20 Temperature Sensor with a WeMos D1 Mini (ESP8266)

In this blog post I talk about the additional steps needed to use the DS18B20 onewire temperature sensor with a WeMos D1 Mini (ESP8266) using the Arduino IDE.

Important differences compared to using the DS18B20 on an Arduino

There is one major difference to bear in mind when using the DS18B20 on a Wemos D1 Mini (ESP8266) or similar chipset.

When using the Arduino IDE with the WeMos chips the standard pin numbers do not refer to the pin numbers on the WeMos. For example, instead of using 1 to refer to digital pin 1, you need to use the constant D1.

I go into more details for this in my blog post Pin numbering for WeMos D1 Mini (ESP8266). If you haven’t read this and are having trouble connecting your WS18B20 properly I recommend having a read.

Wiring it up to a WeMos D1 Mini

Here I am connecting the WS18B20 sensor to a WeMos D1 mini. To more easily connect the sensor I have connected the 3.3 volt and GND lines to the breadboards + and – lines respectively.

The pins on the WS18B20 temperature sensor are, from left to right, GND, Data, and VCC.

It is important to note that the WeMos’s digital pins can only accept up to 3.3 volts maximum on their inputs. This means that the WS18B20 sensors VCC pin must be connected to the 3.3 volts and not the 5 volt line like when using the Arduino.

The data pin is connected to D2 on the WeMos. This is also connected to a 4.7k resistor between the data pin and the 3.3 volt line on the breadboard. This pull-up resistor ensures that the line is pulled up when floating.

Programming the WeMos to use the WS18B20 sensor

Since the WS18B20 uses the onewire protocol I can use the onewire library coupled with the Dallas Temperature library. This will let me address my WS18B20 temperature sensor and read out the temperature in Celcius.

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

#define ONE_WIRE_PIN D2

OneWire oneWire(ONE_WIRE_PIN);
DallasTemperature sensors(&oneWire);

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

void loop() {
  sensors.requestTemperatures();
  Serial.println(sensors.getTempCByIndex(0));
  delay(1000);
}

As noted above I am using the constant D2 instead of 2 to refer to digital pin 2 as the WeMos pin numbering is different from the Arduino.

Since I only have one onewire temperature sensor on pin D2 I call the temperature function asking for the first device (numbered 0) when calling sensors.getTempCByIndex(0).

Summary

The WS18B20 is a useful temperature sensor that uses the onewire protocol. This can be very useful to combine multiple sensors on the same digital pin. This is more important on the smaller WeMos and ESP8266 devices with limited pins. In this case, the Arduino onewire and Dallas Temperature library works the same as when using the Arduino so no ESP8266 specific code is needed.

For a full review of the WS18B20 temperature sensor have a read of my previous post.

2 Comments

Add a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.