ESP8266, ds18b20 and relay problem

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 (free version)

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

/*
 * 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

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.

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

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

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

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

These are the more common relay module types.

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.

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

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]

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

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

post a link of image of your actual relay module.

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

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.

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

1 Like

This is hieroglyphics...... :smiling_face_with_halo: :smiling_face_with_halo:

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

And you read Cleopatra's name out of those hieroglyphs?
damn... good you are :open_mouth: :grinning_face_with_big_eyes:

this is my transistor:

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/

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]

So every guide is a different method.

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