How to connect DS18B20 - Which pins are available?

  • Hello,

    I'm trying to connect a DS18B20 (onewire) temperature sensor on my M5Stack board, but I don't know which pins to use.

    Could you tell me which pins are available (not connected) on the board? I use the headers on the side of the M5Stack box.

    Here are my connections so far :

    • VCC on the sensor => 3.3V on M5Stack
    • GND => GND
    • DATA on the sensor => ???
    • A 4.7K resistor between VCC and DATA


  • @world101How to connect DS18B20 - Which pins are available? 中说：

    @zazar has already done it.

    @zazar, if you come by, could you provide us with some information about your project? How did you connect the DS18B20 sensors (wich pins did you use)? Which Onewire/ds18b20 lib did you use, if any ?


  • @x-dronHow to connect DS18B20 - Which pins are available? 中说：

    Try it.
    GPIO2 fully free

    I've already tried, and I can't find why it doesn't work, that's why I'm asking.

    I've tried your example, I've just set the define ONE_WIRE_BUS to 2:

    #define ONE_WIRE_BUS 2 

    The sensor is still not detected. Moreover, the board make a little piercing sound when it probes for the sensor and the temperature. I don't understand where it comes from, as GPIO2 should not be connected to the speaker...

    Any ideas?

  • New information : I tried the exact same code than the one provided by @X-Dron ( on an ESP8266 (using pin D3) and... it works perfectly (I tried with 1, then 2 and 3 sensors)!

    Is there something I'm missing, or is there an hardware issue with my board?

  • This is what I used just to test read multiple sensors on the M5Stack:

    	#include <M5Stack.h>
    	#include <OneWire.h>
    	#include <DallasTemperature.h>
    	#define ONE_WIRE_BUS 5  // DS18B20 on arduino pin2 corresponds to D4 on physical board
    	OneWire oneWire(ONE_WIRE_BUS);
    	DallasTemperature DS18B20(&oneWire);
    	void setup() {
    	void loop() {
    	  float celsius;
    	  float fahrenheit;
    	  int count = DS18B20.getDS18Count();
    	  M5.Lcd.print("Devices found: ");
    	  if (count > 0) {
    		  celsius = DS18B20.getTempCByIndex(i);
    		  fahrenheit = celsius * 1.8 + 32.0;
    		  celsius = round(celsius);
    		  fahrenheit = round(fahrenheit);
    		  M5.Lcd.print("Device ");
    		  M5.Lcd.print(": ");
    		  M5.Lcd.print( "C / ");

  • OneWire version 2.3.4
    DallasTemperature version 3.79

  • @zazar 在 How to connect DS18B20 - Which pins are available? 中说：

    OneWire version 2.3.4
    DallasTemperature version 3.79

    Thanks, @zazar!

    I use exactly the same lib, and similar code without any result for now, unfortunately.

  • Here is a little report on my progression. In short : it still doesn't work!

    I checked that the pin "works" correctly by connecting a relay to test in OUTPUT mode, and a push button (to test in INPUT) : it works!
    I checked, double checked and triple checked the setup (wires, connections,...), everything is good.
    I ran the same code on a Wemos D1 Mini (ESP8266) : it works.

    At this point, I'm lost? The pin and the code seem good, but it can't detect the sensor.

    One thing I notice is that this weird whistling sound coming out of the board (not sure if it comes from the speaker or not) when the OneWire lib bitbangs the OneWire protocol on the pin.
    So, I made another test program that toggles the pin rapidely (at the speed of the onewire lib - ~50µs) : I hear the sound!

    I'll try to find an oscilloscope to check the signal on the pin, as I suspect that "something" is going wrong when the pin is driven at "high" speed.

    In the meantime, I hope that someone with more electronical knowledge will help me find a solution!