need help with my project

hi all,kindly ineed help as Im new to arduino and this is my first project ,
I started my first project which is an automated garden irrigation , the code is as below:
int sensorPin = A0;
int Relay = 12;
const int dryThreshold = 900;
const long sampleInterval = 3000;
const int irrigationTime = 1000;
boolean DEBUG=true;

void setup(){
pinMode(sensorPin, INPUT);
pinMode(Relay, OUTPUT);
digitalWrite(Relay, LOW);
void loop() {
int sensorValue = analogRead(sensorPin);
Serial.print("Sensor value: ");
if (sensorValue>dryThreshold){
digitalWrite(Relay, HIGH); // Open the water valve
delay(irrigationTime); // Keep it open for the irrigation time
digitalWrite(Relay, LOW); // Close the valve

kindly notice that
1-the wire long from the sensor and the board is about 4 meters.
2-the sensor that I,m using is tow nails planted in the mud.

Problem is:
1- I get fluctuated readings from the sensor like
nsor value: 883
Sensor value: 0
Sensor value: 336
Sensor value: 1023
Sensor value: 395
Sensor value: 960
Sensor value: 252
Sensor value: 951
Sensor value: 236
Sensor value: 986
Sensor value: 35
Sensor value: 329
Sensor value: 876
Sensor value: 159
Sensor value: 197
Sensor value: 1023
Sensor value: 154
Sensor value: 1023
Sensor value: 0
Sensor value: 1023
Sensor value: 137
Sensor value: 835
Sensor value: 98
Sensor value: 1023
Sensor value: 1023
Sensor value: 462
Sensor value: 1023
Sensor value: 1023
Sensor value: 117

2- I wanna insert a push button to perform the irrigation time regarding to the sensor value "an override".
but each time I'm failing .
please help

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

this is the original schematic but I had made a change that I removed the water bump with its components and added a relay that control a salonied valve


My guess would be a bad connection. I would not expect the resistance across the sensor (two nails stuck in the mud) to ever get low enough to bring the input down to zero volts.

Place a 1K resistor in the base circuit of the transistor to limit current from the UNO output.
Move the DIODE so it is across the relay coil.
Add 0.1uF capacitor from analog input to gnd, to suppress any noise picked up by the 4M of wire.
Tom… :slight_smile:
I hope you are using two wires from the UNO to your sensor, and not relying on gnd return.

Are the wires at least twisted together? If not you are probably picking up a LOT of AC line noise. That might be enough to swamp the voltage drop between the nails.

thanks allot I'll put the capacitor and resistant and change the wire
thanks for your help