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
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);
}
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
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.
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
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.
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]