Hi Everyone,
I´m trying to build this simple temperature controlled fan, but the relay will not switch the voltage to activate the fan. I do see the signal coming through to the relay and all voltages are ok. I also checked each sensor individually so I know they are ok.
It seems that the I2C+LCD is interfering with the relay. Another thing I do not understand is that, if I remove the LCD power pins (5V and GND), the buzzer will also stop working. That is confusing me.
The schematics of my project is attached and the code is down below. Any help is greatly appreciated!!!
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11
int relayPin = 3; // Initialize Relay Pin 3
const int buzzer = 5; // Initialize buzzer pin 5
static const int DHT_SENSOR_PIN = 2;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );
LiquidCrystal_I2C lcd(0x27, 16, 2); //Inicializa o display no endereco 0x27
void setup()
{
lcd.init();
}
static bool measure_environment( float * temperature, float * humidity )
{
static unsigned long measurement_timestamp = millis( );
if ( millis( ) - measurement_timestamp > 3000ul ) /* Measure once every four seconds. */
{
if ( dht_sensor.measure( temperature, humidity ) == true )
{
measurement_timestamp = millis( );
return ( true );
}
}
return ( false );
}
void loop()
{
float temperature;
float humidity;
if ( measure_environment( &temperature, &humidity ) == true )
{
lcd.setBacklight(HIGH);
lcd.setCursor(0, 0);
lcd.print("Temp. = ");
lcd.print(temperature, 1);
lcd.print(" C");
if (temperature >= 30)
{
tone(buzzer, 1000); // Send 1KHz sound signal
delay(500); // keep it on for 1/2 second
noTone(buzzer); // Stop sound.
delay(500); // wait for 1/2 second
tone(buzzer, 1000); // Send 1KHz sound signal
delay(500); // keep it on for 1/2 second
noTone(buzzer); // Stop sound.
delay(500); // wait for 1/2 second
tone(buzzer, 1000); // Send 1KHz sound signal
delay(500); // wait for 1/2 second
noTone(buzzer); // Stop sound.
delay(500); // wait for 1/2 second
lcd.setCursor(0, 1);
lcd.print("FAN ON!!!");
digitalWrite(relayPin, HIGH); // turn the fan on
delay (1000);
}
if (temperature < 30)
{
tone(buzzer, 1000); // Send 1KHz sound signal
delay(500); // keep it on for 1/2 second
noTone(buzzer); // Stop sound.
delay(800); // wait for 0.8 second
digitalWrite(relayPin, LOW); // turn the fan off
lcd.setCursor(0, 1);
lcd.print("FAN OFF");
}
//delay(2000);
}
}