# ESP8266, ds18b20 and relay problem

**URL:** <https://forum.arduino.cc/t/esp8266-ds18b20-and-relay-problem/1361333>\
**Category:** Programming\
**Created:** [March 7, 2025, 2:39pm UTC](https://forum.arduino.cc/t/esp8266-ds18b20-and-relay-problem/1361333 "2025-03-07T14:39:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![euzebi](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@euzebi](https://forum.arduino.cc/u/euzebi)\
**Post date:** [March 7, 2025, 2:39pm UTC](https://forum.arduino.cc/t/esp8266-ds18b20-and-relay-problem/1361333/1 "2025-03-07T14:39:43Z")

</div>

Hello everyone. Recently I wanted to use an existing code and tutorial using esp8266 d1 mini, ds18b20 sensor and relay.  
Well, and of course, as is usually the case, something is not working.  
When I load the code I can see on the monitor that something is changing.  
When I heat the sensor it supposedly turns on the relay, when I cool it supposedly turns off. .. unfortunately the relay itself does not respond.... in general I know that it is working because I loaded another code into the relay itself with a delay and it worked. It was the same with the sensor. but in this case one with the other something does not want to work.  
I also noticed that with the code to the relay itself powered by the usb of the computer, I have to use transistor 2N2222 because otherwise it does not work.... unless I connect the esp8266 5v through the power supply then it works.  
so in the end my connection looks like the picture:  
I also tried to connect the led diode to pin D7 because I thought that if the sensor detects a higher temperature the led will light up but it turned out that the led does not light up. so something feels that probably this connection is wrong... or maybe something with the code is wrong... i don't know

Translated with [DeepL.com](http://DeepL.com) (free version)

 ![Untitled Sketch 2_bb](https://europe1.discourse-cdn.com/arduino/original/4X/6/d/1/6d1e5b3198d348999116037d162968b348f247c2.jpeg)

And this is the code I rewrote for my needs  
specifically the threshold between 26 and 27 degrees and  
outputs on the pins

```arduino
/*
 * This ESP32 code is created by esp32io.com
 *
 * This ESP32 code is released in the public domain
 *
 * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-cooling-system-using-ds18b20-temperature-sensor
 */

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

#define TEMP_UPPER_THRESHOLD 27 // upper temperature threshold
#define TEMP_LOWER_THRESHOLD 26 // lower temperature threshold

#define SENSOR_PIN D2 // ESP32 pin GPIO23 connected to DS18B20 sensor's DQ pin
#define RELAY_FAN_PIN D7 // ESP32 pin GPIO18 connected to relay

OneWire oneWire(SENSOR_PIN);
DallasTemperature DS18B20(&oneWire);

void setup() {
  Serial.begin(9600); // initialize serial
  DS18B20.begin(); // initialize the DS18B20 sensor
}

void loop() {
  DS18B20.requestTemperatures(); // send the command to get temperatures
  float temperature = DS18B20.getTempCByIndex(0); // read temperature in Celsius

  if (temperature > TEMP_UPPER_THRESHOLD) {
    Serial.println("Turn the fan on");
    digitalWrite(RELAY_FAN_PIN, HIGH); // turn on
  } else if (temperature < TEMP_LOWER_THRESHOLD) {
    Serial.println("Turn the fan off");
    digitalWrite(RELAY_FAN_PIN, LOW); // turn off
  }

  delay(500);
}

```

and this is the site I took the guide from:

[https://esp32io.com/tutorials/esp32-cooling-system-using-ds18b20-temperature-sensor](https://esp32io.com/tutorials/esp32-cooling-system-using-ds18b20-temperature-sensor)

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [March 7, 2025, 7:17pm UTC](https://forum.arduino.cc/t/esp8266-ds18b20-and-relay-problem/1361333/2 "2025-03-07T19:17:23Z")

</div>

Try increase delay to 800ms.  
Depending on the resolution setting, the DS18B20 can take up to 750 ms to calculate the temperature.

Give more details (link perhaps) about your relay module.  
Because there are module models whose input is different from others.

---

<div class="post-metadata">

**Author:** ![euzebi](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@euzebi](https://forum.arduino.cc/u/euzebi)\
**Post date:** [March 7, 2025, 7:32pm UTC](https://forum.arduino.cc/t/esp8266-ds18b20-and-relay-problem/1361333/3 "2025-03-07T19:32:02Z")

</div>

Yeah, the sensor is working because the text on the monitor is changing.

if I heat it up, the text changes to "Turn the fan on"  
if I put the sensor in cold water, it changes to "Turn the fan off"

but when it's supposed to turn on, it doesn't supply voltage to the pin.  
because as I wrote earlier, instead of a relay, I put an LED and the LED didn't light up either.  
so I wonder if the resistor should be connected to 5v and the signal... hmm

---

<div class="post-metadata">

**Author:** ![blh64](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blh64/32/605986_2.png) [@blh64](https://forum.arduino.cc/u/blh64)\
**Post date:** [March 7, 2025, 7:35pm UTC](https://forum.arduino.cc/t/esp8266-ds18b20-and-relay-problem/1361333/4 "2025-03-07T19:35:25Z")

</div>

You wiring is not correct. When you turn on D7, it turns on your transistor which drives the EN pin on the relay to GND. When the pin is off, the relay is off and the EN pin floats. You need a pull-up resistor to make EN +5V when the transistor is off

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [March 7, 2025, 7:35pm UTC](https://forum.arduino.cc/t/esp8266-ds18b20-and-relay-problem/1361333/5 "2025-03-07T19:35:53Z")

</div>

in your code of post 1 I cannot see where you make the relay pin OUTPUT, e.g.

```arduino
void setup() {
  Serial.begin(9600); // initialize serial
  DS18B20.begin(); // initialize the DS18B20 sensor
  pinMode(RELAY_FAN_PIN, OUTPUT); // set pin to output
}

```

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [March 7, 2025, 7:39pm UTC](https://forum.arduino.cc/t/esp8266-ds18b20-and-relay-problem/1361333/6 "2025-03-07T19:39:43Z")

</div>

These are the more common relay module types.

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/b/7/5/b75e26c30598e7e77daa79679495748179b0c81e.png)

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [March 7, 2025, 7:49pm UTC](https://forum.arduino.cc/t/esp8266-ds18b20-and-relay-problem/1361333/7 "2025-03-07T19:49:02Z")

</div>

What kind of fan? If it's a small 12V fan, you may not need the relay at all. Even for larger DC fans, if you choose the right transistor, you won't need a relay.

Your pictures show a relay module. These modules generally contain a transistor, so an additional transistor usually isn't needed with the relay.

 ![Screenshot_20250307-195047-353](https://europe1.discourse-cdn.com/arduino/original/4X/b/a/2/ba2dffe2b4f914799fc7f697856ba205ffce8c23.png)

---

<div class="post-metadata">

**Author:** ![euzebi](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@euzebi](https://forum.arduino.cc/u/euzebi)\
**Post date:** [March 7, 2025, 8:26pm UTC](https://forum.arduino.cc/t/esp8266-ds18b20-and-relay-problem/1361333/8 "2025-03-07T20:26:42Z")

</div>

Yyy EN pin ?  
Sorry I'm not very proficient in electronic stuff so I don't know where or which EN pin it is.

---

<div class="post-metadata">

**Author:** ![euzebi](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@euzebi](https://forum.arduino.cc/u/euzebi)\
**Post date:** [March 7, 2025, 8:34pm UTC](https://forum.arduino.cc/t/esp8266-ds18b20-and-relay-problem/1361333/9 "2025-03-07T20:34:16Z")

</div>

ok I added  
“pinMode(RELAY\_FAN\_PIN, OUTPUT); // set pin to output”.  
and now when the temperature rises the LED lights up.... and goes off when the temperature drops. i.e. it works  
but when i connect relay from D7 to IN pin then no matter if output is HIGH or LOW the green LED on relay lights up. but when i connect through transistor 2n2222. then nothing happens and the green LED is off.  
That is, in general, the code given in the tutorial from the page (link) is wrong

[[ESP32 - Cooling System using DS18B20 Temperature Sensor | ESP32 Tutorial](https://esp32io.com/tutorials/esp32-cooling-system-using-ds18b20-temperature-sensor)]

---

<div class="post-metadata">

**Author:** ![euzebi](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@euzebi](https://forum.arduino.cc/u/euzebi)\
**Post date:** [March 7, 2025, 8:35pm UTC](https://forum.arduino.cc/t/esp8266-ds18b20-and-relay-problem/1361333/10 "2025-03-07T20:35:38Z")

</div>

for now I don't have any fan because I generally wanted to see if it works to go further

---

<div class="post-metadata">

**Author:** ![euzebi](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@euzebi](https://forum.arduino.cc/u/euzebi)\
**Post date:** [March 7, 2025, 8:37pm UTC](https://forum.arduino.cc/t/esp8266-ds18b20-and-relay-problem/1361333/11 "2025-03-07T20:37:28Z")

</div>

Sometimes when I look at these diagrams it's like trying to read hieroglyphics 🙂

---

<div class="post-metadata">

**Author:** ![kmin](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kmin/32/1147370_2.png) [@kmin](https://forum.arduino.cc/u/kmin)\
**Post date:** [March 7, 2025, 9:22pm UTC](https://forum.arduino.cc/t/esp8266-ds18b20-and-relay-problem/1361333/12 "2025-03-07T21:22:14Z")

</div>

post a link of image of your actual relay module.

---

<div class="post-metadata">

**Author:** ![euzebi](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@euzebi](https://forum.arduino.cc/u/euzebi)\
**Post date:** [March 7, 2025, 9:30pm UTC](https://forum.arduino.cc/t/esp8266-ds18b20-and-relay-problem/1361333/13 "2025-03-07T21:30:17Z")

</div>

The same as the one in the link . and the same company

> **[Moduł przekaźnika 1 kanał - styki 10A/250VAC - cewka 5V](https://botland.com.pl/przekazniki-przekazniki-arduino/8463-modul-przekaznika-1-kanal-styki-10a250vac-cewka-5v-5904422300517.html)**
>
> Moduł przekaźnika 1 kanał - styki 10A/250VAC, cewka 5V w konkurencyjnej cenie. Podłącz go do Arduino i zarządzaj pracą urządzeń. Zapewniamy doradztwo.

---

<div class="post-metadata">

**Author:** ![kmin](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kmin/32/1147370_2.png) [@kmin](https://forum.arduino.cc/u/kmin)\
**Post date:** [March 7, 2025, 9:41pm UTC](https://forum.arduino.cc/t/esp8266-ds18b20-and-relay-problem/1361333/14 "2025-03-07T21:41:40Z")

</div>

It's low trigger relay, so it should work with the transistor circuit on your first post.  
Are you sure your transistor is wired like on your circuit image? Sure it is 2n2222?

Don't try to use it without transistor if it doesn't work and it might damage your Esp.

---

<div class="post-metadata">

**Author:** ![blh64](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blh64/32/605986_2.png) [@blh64](https://forum.arduino.cc/u/blh64)\
**Post date:** [March 7, 2025, 10:27pm UTC](https://forum.arduino.cc/t/esp8266-ds18b20-and-relay-problem/1361333/15 "2025-03-07T22:27:44Z")

</div>

Sorry, it is the "IN" pin on the relay.

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [March 7, 2025, 10:33pm UTC](https://forum.arduino.cc/t/esp8266-ds18b20-and-relay-problem/1361333/16 "2025-03-07T22:33:02Z")

</div>

This is hieroglyphics...... 😇 😇

And it has Queen Cleopatra's name written on it: 🤣

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/3/d/8/3d8de5c32bb76b145c948251e2e51804ce742953.jpeg)

---

<div class="post-metadata">

**Author:** ![euzebi](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@euzebi](https://forum.arduino.cc/u/euzebi)\
**Post date:** [March 8, 2025, 10:07am UTC](https://forum.arduino.cc/t/esp8266-ds18b20-and-relay-problem/1361333/17 "2025-03-08T10:07:21Z")

</div>

> [@ruilviana](#):
>
> Cleopatra

And you read Cleopatra's name out of those hieroglyphs?  
damn... good you are 😮 😃

---

<div class="post-metadata">

**Author:** ![euzebi](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@euzebi](https://forum.arduino.cc/u/euzebi)\
**Post date:** [March 8, 2025, 10:11am UTC](https://forum.arduino.cc/t/esp8266-ds18b20-and-relay-problem/1361333/18 "2025-03-08T10:11:46Z")

</div>

this is my transistor:

 ![tranzystor-2n2222](https://europe1.discourse-cdn.com/arduino/original/4X/9/f/d/9fdba27165771bf81d5cdbf460deabb5ba1a8473.jpeg)

---

<div class="post-metadata">

**Author:** ![euzebi](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@euzebi](https://forum.arduino.cc/u/euzebi)\
**Post date:** [March 8, 2025, 10:25am UTC](https://forum.arduino.cc/t/esp8266-ds18b20-and-relay-problem/1361333/19 "2025-03-08T10:25:45Z")

</div>

I found another tutorial which in total differs in the connection of the sensor... but I haven't tested it yet because it is only for reading and not controlling the relay.  
( " Parasite Mode" , "Normal mode" )  
[https://randomnerdtutorials.com/esp8266-ds18b20-temperature-sensor-web-server-with-arduino-ide/](https://randomnerdtutorials.com/esp8266-ds18b20-temperature-sensor-web-server-with-arduino-ide/)

and I also saw another tutorial where the sensor was connected not to 5v but to 3v for example in this tutorial. Of course I haven't tested that yet either.:  
[[DS18B20 Sensor Interfacing with Nodemcu | NodeMCU](https://www.electronicwings.com/nodemcu/ds18b20-sensor-interfacing-with-nodemcu)]

So every guide is a different method.

---

<div class="post-metadata">

**Author:** ![kmin](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kmin/32/1147370_2.png) [@kmin](https://forum.arduino.cc/u/kmin)\
**Post date:** [March 8, 2025, 1:45pm UTC](https://forum.arduino.cc/t/esp8266-ds18b20-and-relay-problem/1361333/20 "2025-03-08T13:45:32Z")

</div>

> [@euzebi](#):
>
> the sensor was connected not to 5v but to 3v

That's what you should do.  
And in any case the pullup has to be on 3.3V.

[Next page](https://forum.arduino.cc/t/esp8266-ds18b20-and-relay-problem/1361333.md?page=2)
