Help Needed Please

Trying to make an automatic watering system to turn on a 120V pump with UNO R3, Capacitive soil moisure sensor V1.2 and 4 relay pcb. Managed to get readout to diplay on LCD1602 Module. But cant get code to work between 55% and 68%.

PS. Completely new to this. Trying to learn so can do projects like this with my kids.

Thank you!

int ACWATERPUMP = 13; //You can remove this line, it has no use in the program.
int sensor = A0; //You can remove this line, it has no use in the program.
int val; //This variable stores the value received from Soil moisture sensor.
void setup() {
pinMode(13,OUTPUT); //Set pin 13 as OUTPUT pin, to send signal to relay
pinMode(A0,INPUT); //Set pin 8 as input pin, to receive data from Soil moisture sensor.
}
void loop() {
val = analogRead(A0); //Read data from soil moisture sensor
if(val == 55%)
{
analogWrite(13,55%); //if soil moisture sensor provides LOW value send LOW value to relay
}
else
{
analogWrite(13,68%); //if soil moisture sensor provides HIGH value send HIGH value to relay
}
delay(400); //Wait for few second and then continue the loop.
}

You obviously did not read the forum guidelines before posting.

analogWrite(13,55%);

Read the analogWrite() reference. Where does it say that 55% is a valid argument?

What is returned by the analogRead() function?

 if(val == 55%)

55% of 1023 (the max from analogRead()) is 563. How likely is it that the output of the sensor is exactly 563? Would it not make more sense to look for a range of numbers or just greater or less than 563? Same for 68%.

If you describe in more detail what the project is to do, we can help you make it work.

First of all the '%' is a modulus operator in c so don't use it any where . The capacitive moisture sensor doesn't give the values directly in percentage instead it's a 10 bit value which ranges from 0 to 1023 you should map it to percentage. Also analogRead function is not suitable to trun on and off the relay instead you should use digitalWrite function to control the relay. Anyway a helpful project can found here Capacitive Soil Moisture Sensor Arduino Circuit diagram and programming from this project you can remove the lcd part entirely and also don't need any transistor to control the relay instead you can connect the relay module directly to the Arduino because the relay module will have the transistor in them

Thank you, Akash

No I'm sorry I did not read guidelines. I have no idea how any of this works... I am totally new to forums and Arduino. I have watched a few YouTube videos and am trying to learn how to do small projects like this.

The first part I did was to integrate a capacitive soil sensor to the Uno R3. That wasn't so bad. Then integrated LCD1602 (followed Interface Capacitive Soil Moisture Sensor v1.2 with Arduino instructions for both these steps)

Then I tried to use the code I found at Automatic Watering System for Plants using Arduino - LearnByWatch and replace a few values since I'm not using the same sensor as them. And here we are.

I'm not using the same sensor

What, exact, sensor do you have? What, exactly, are the rest of the components (post datasheets and/or links to where you got them)? What do you want the program to do (list the requirements)?

It may be to your benefit to look at some of the example code that comes with the IDE. Look in the files menu for Examples.

Here is what I got:

https://www.amazon.ca/gp/product/B08588Q3HJ/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1

I'm trying to use this to keep the soil moisture level between 55 and 65% with a 120V Pool pump

Here is example code to help you get started. To experiment with the code I suggest that you wire up a potentiometer in place of the sensor and an LED in place of the relay. That way you can play with the code to see if it fits your need. It seems that the output of the moisture sensor is 3V. The code will have to be adjusted for that. The 3.3V to supply the sensor can be from the 3.3V of the Arduino.

const byte sensorPin = A0;
const byte relayPin = 4;

void setup()
{
   Serial.begin(115200);
   pinMode(relayPin, OUTPUT);
}

void loop()
{

   static unsigned long timer = 0;
   unsigned long interval = 500;
   if (millis() - timer >= interval) 
   {
      timer = millis();
      int moistureLevel = analogRead(sensorPin); // read sensor
      moistureLevel = map(moistureLevel, 0, 1023, 0, 100);  // convert raw ADC counts to percent*
      Serial.print("moisture level= ");
      Serial.print(moistureLevel);
      Serial.println("%");
      if (moistureLevel >= 68) // if moisture more than 68%
      {
         digitalWrite(relayPin, HIGH);  // turn off pump
      }
      else if (moistureLevel <= 55) // if moisture less than 55%
      {
         digitalWrite(relayPin, LOW);  // turn on pump
      }
   }
}

Suggested test circuit:

Unless you are already trained for working with mains voltages, I strongly suggest that you get help from someone who is.