Hi..I'm trying to make a Smart Irrigation System with a Arduino Mega, where a soil moisture sensor detects dryness(ik mine's a bit weird it gives about 1024 for a dry & about 300-400 for a wet.) every 10 seconds, and if it is above a certain threshold, it turns on a DC pump for 5 seconds. I've used a BC557 transistor since the digital pins can't power the pump. Here is the tinkercad circuit:
Here's the code:
const int soilDrynessPin = A0; // Analog pin for soil dryness sensor
const int pumpControlPin = 8; // Digital pin connected to the base of the BC557 transistor
void setup() {
pinMode(soilDrynessPin, INPUT);
pinMode(pumpControlPin, OUTPUT);
Serial.begin(9600);
// Turn off the pump initially
digitalWrite(pumpControlPin, HIGH);
}
void loop() {
// Read soil dryness level
Serial.println("READING MOISTURE");
int soilDryness = analogRead(soilDrynessPin);
Serial.print("Dryness read: ");
Serial.println(soilDryness);
// Adjust the threshold value according to your soil dryness sensor
int drynessThreshold = 700;
// Check if soil dryness is below the threshold
if (soilDryness > drynessThreshold) {
// Turn on the pump
digitalWrite(pumpControlPin, HIGH);
Serial.println("Pump - HIGH & Waiting for 5 seconds");
delay(1000);
Serial.println("4 seconds");
delay(1000);
Serial.println("3 seconds");
delay(1000);
Serial.println("2 seconds");
delay(1000);
Serial.println("1 seconds");
delay(1000); // Run the pump for 5 seconds (adjust as needed)
// Turn off the pump
digitalWrite(pumpControlPin, LOW);
Serial.println("Pump - LOW");
}
// Wait for a period before checking soil dryness again
Serial.println("Waiting for 10 seconds");
delay(1000);
Serial.println("9 seconds");
delay(1000);
Serial.println("8 seconds");
delay(1000);
Serial.println("7 seconds");
delay(1000);
Serial.println("6 seconds");
delay(1000);
Serial.println("5 seconds");
delay(1000);
Serial.println("4 seconds");
delay(1000);
Serial.println("3 seconds");
delay(1000);
Serial.println("2 seconds");
delay(1000);
Serial.println("1 seconds");
delay(1000);
Serial.println("LOOP OVER");
}
It works properly on tinkercad but when I duplicate the exact same thing in my actual circuit it doesn't work and the pump randomly shuts off and on. I've tried changing the pump but the same thing happened. Is it because of some inductive current going the other way so I should put a diode or something? I don't know.. Someone please help.