The water pump does not work even if that the relay module turned on

is there any something wrong with the code?

#include <LiquidCrystal.h>

// Constants
const int LM35_PIN = A0; // Analog pin for LM35 temperature sensor
const int MQ2_PIN = A1; // Analog pin for MQ2 smoke sensor
const int RELAY_PIN = 9; // Digital pin for relay module

// Variables
float temperature = 0.0; // Temperature in degrees Celsius
int smokeValue = 0; // Smoke value from 0 to 1023
float fanSpeed = 0.0; // Fan speed adjustment (0.0 to 1.0)

// Initialize LCD
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

void setup() {
// Initialize Serial communication
Serial.begin(9600);

// Initialize LCD
lcd.begin(16, 2);
lcd.print("Temp: Smoke:");

// Set fan and water pump pins as output
pinMode(fanPin, OUTPUT);
pinMode(waterPumpPin, OUTPUT);

// Turn off the fan and water pump initially
analogWrite(fanPin, 0);
digitalWrite(waterPumpPin, LOW);
}

void loop() {
// Read temperature from LM35 sensor
int lm35Value = analogRead(lm35Pin);
temperature = (lm35Value * 5.0 / 1023.0) * 100.0;

// Read smoke sensor value
int smokeValue = analogRead(smokePin);

// Display temperature and smoke sensor values on LCD
lcd.setCursor(5, 0);
lcd.print(temperature);
lcd.print("C");
lcd.setCursor(12, 0);
lcd.print(smokeValue);

// Check smoke threshold
if (smokeValue > 50) {
digitalWrite(RELAY_PIN, HIGH); // Turn on the relay
// Code to control water pump goes here
} else {
digitalWrite(RELAY_PIN, LOW); // Turn off the relay
// Code to control water pump goes here
}

// Check if temperature exceeds 50 degrees Celsius
if (temperature > 50 ) {
// Start the fan with the specified speed
analogWrite(fanPin, fanSpeed / 100.0 * 255);
}

// Print temperature and smoke sensor values to Serial Monitor
Serial.print("Temp: ");
Serial.print(temperature);
Serial.print("C Smoke: ");
Serial.println(smokeValue);

// Delay between readings
delay(1000);
}

Please edit your post, select all code and click the <CODE/> button; next save your post. This applies code tags to your code which makes it easier to read, easier to copy and the forum software will display it correctly.


If the relay switches when you expect it to happen, there can't be much wrong with your code. Time for a schematic / wiring diagram (photo of paper/pencil drawing is fine). Further specifications of the relay as well as the motor are needed.

I'm not a hardware person so probably can't help you further.

1 Like

Hello maraim_baher

The compiler don´t like the sketch.

Check error messages.

Switch compiler warnings to ALL in the preferences.

grafik

i did not get you well, what do you mean ?

  • Please show us the actual code you are using.

  • Where is fanPin initialized ?

1 Like

what is the mean by "actual code" ? this is the code that i am using

  • Where is fanPin initialized in the posted sketch ?
1 Like

You can't be using that code, because it does not compile without fatal errors.

this is the code: #include <SoftwareSerial.h>
#include <LiquidCrystal.h>

// Include libraries for LCD and Bluetooth

LiquidCrystal lcd(2, 3, 4, 5, 6, 7); // LCD pins
SoftwareSerial bluetooth(8, 12); // RX, TX pins for HC-05 module

// Define sensor pins
const int lm35Pin = A0; // LM35 temperature sensor analog pin
const int mq2Pin = A1; // MQ2 smoke sensor analog pin

// Define control pins
const int fanPin = 10; // Fan control pin
const int relayPin = 9; // Relay control pin

// Variables
float temperature = 0.0;
int smokeValue = 0;
float fanSpeed = 1.0;

void setup() {
// Initialize LCD
lcd.begin(16, 2);
lcd.print("Temp: 0.0C");
lcd.setCursor(0, 1);
lcd.print("Smoke: 0");

// Initialize Bluetooth
bluetooth.begin(9600);

// Initialize serial monitor
Serial.begin(9600);

// Set control pins as outputs
pinMode(fanPin, OUTPUT);
pinMode(relayPin, OUTPUT);
}

void loop() {
// Read temperature from LM35
int lm35Value = analogRead(lm35Pin);
temperature = (lm35Value * 5.0 / 1023.0) * 100.0;

// Read smoke value from MQ2 sensor
smokeValue = analogRead(mq2Pin);

// Print values on LCD
lcd.setCursor(6, 0);
lcd.print(temperature);
lcd.print(" C");
lcd.setCursor(7, 1);
lcd.print(smokeValue);

// Print values in the serial monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" C, Smoke: ");
Serial.println(smokeValue);

// Send values via Bluetooth
String message = "Temperature: " + String(temperature) + " C, Smoke: " + String(smokeValue);
bluetooth.println(message);

// Check temperature threshold
if (temperature > 50.0) {
fanSpeed = 1.5; // Increase fan speed to 1.5 times
} else {
fanSpeed = 1.0; // Restore fan speed to normal
}

// Control fan speed
analogWrite(fanPin, 255 * fanSpeed); // Set the fan speed based on the fanSpeed variable

// Check smoke threshold
if (smokeValue > 50) {
digitalWrite(relayPin, HIGH); // Turn on relay
} else {
digitalWrite(relayPin, LOW); // Turn off relay
}

delay(1000); // Delay between readings
}

What don't you understand about the use of the code tags as mentioned in post #2?

I have some doubts that a fanSpeed of 1.5 actually will make your fan run faster; slower is more likely if you're using an 8-bit microcontroller.

but i have the problem that the water pump does not work even if that the relay is turned on. if the problem of being the fan consumed much more than others

but i have the problem that the water pump does not work even if that the relay is turned on. if the problem of being the fan consumed much more than others.

@maraim_baher please post your code in code tags. There are a few ways, here's one:

First, in the IDE apply the Auto For at tool.

Next, in the IDE use the Copy for Forum tool.

Lastly come back here and paste into a new post on this thread.


Scrolling back (I tuned in late), I see you have already been asked

but have yet to comply. At this time, it seems you have a hardware issue. A schematic will be essential to assisting you further.

TIA

a7

1 Like

i try to turn on the water pump independently away from the system and it works. what is the wrong with the system i feed it by 12v battery