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 DS18B20 properly I recommend having a read.
Wiring it up to a WeMos D1 Mini
Here I am connecting the DS18B20 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 DS18B20 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 DS18B20 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 DS18B20 uses the onewire protocol I can use the onewire library coupled with the Dallas Temperature library. This will let me address my DS18B20 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 of2
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 callingsensors.getTempCByIndex(0)
.Summary
The DS18B20 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 DS18B20 temperature sensor have a read of my previous post.
Hey superrb website! Does running a blog such as tthis take
a large amount oof work? I have very little knowledge of coding but I was
hoping tto start my own blog soon. Anyway, if you
have any recommendations or techniques for new blog owners please share.
I know thi is off subject nevertheless I simply nseded to ask.
Appreciate it!
Try writing each week, it gets easier week on week 🙂 For the coding, I have a Masters degree in Computer Science so coding comes easily to me
Great article but which OneWire library are you using for this? I can’t get it to compile in Arduino IDE
I am using “OneWire by Jim Studt, Tom Pollard, et al” Version 2.3.5
I am using “OneWire by Jim Studt, Tom Pollard, et al” Version 2.3.5
Is WS18B20 a typo?
Yep it is thanks!