This is the Arduino code corresponding to my humidity project in the general project of Agriculture Robotics, in this case my objective is mesure the humidity of a plant depending on the spol moisture or humidity. A red LED will be switch on in case of low humidity and a green LED will be on in case of high soil moisture

Next step will be to change LEDS and to use water pump, relays to water the plant

My code for the YL.69 soil moisture sensor and LEDS is the following:


int rainPin = A0;
/* In Arduino uno they are six analog inputs of ten bits(1024 levels) and in ESP-32 there are 20 analog inputs 12 bits(4096 levels). We're going to calculate the resolution of the sensors and the analog inputs, taking into account that Arduino uno is a 5 V microcontroller and ESP-32 is a 3,3 V microcontroller. 3,3 V / 4096 = 0,000805664062 V = 0,806 mV, 5 V / 1024 = 0.0048828125 = 4,88 mV.
Level 0 is 0 V in Arduino, level 1 is 4.88, level 2 is 9.76 mV, level 500 is 2441 mV = 2.4 V and level 1023 is 5 V. Can I mesure 6 mV? No, the solution is better than ADC (Analog digital converter of more bits) for example, ADS 1115 is a ADC of 16 bits (2^16 = 65536) 5V / 65536 * 1000 =    0.08 mV. The advantatge is that we are capable to detect more values (more accuracy). Raspberry Pi v4 doesn't have any ADC that is any analog input, connecting ADS 1115 when is avaliable.
// int it means integer variable corresponding to a integer number A0, 6 and 7 are integer numbers, A0 is a special case. Double forward slash means comment in Arduino language. rainPin, greenLED and redLED are names of the variable invented in order to identify the PINS(connectors of the Arduino). For variablesnames we follow the camel case that is the firsts letter of the first name in the lower cases and the second name in the first letter will be upper case (other tipes ir other posibilities of convention for writing variables are used in other lenguages are snake case "for-example-this" and in camel case is this: "forExampleThis". It is imposibl to start a variable name with a number or with special characters corresponding to keywords used in instructions.
int greenLED = 6;
int redLED = 7;
// you can adjust the threshold value
int thresholdValue = 1400;

void setup(){
  pinMode(rainPin, INPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(redLED, OUTPUT);
  digitalWrite(greenLED, LOW);
  digitalWrite(redLED, LOW);
  Serial.begin(9600);
}

void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(rainPin);
  Serial.print(sensorValue);
  if(sensorValue < thresholdValue){
    Serial.println(" - Doesn't need watering");
    digitalWrite(redLED, LOW);
    digitalWrite(greenLED, HIGH);
  }
  else {
    Serial.println(" - Time to water your plant");
    digitalWrite(redLED, HIGH);
    digitalWrite(greenLED, LOW);
  }
  delay(500);
}

This is the image of the circuit: