Servo motor not initiating as planned

So did we!

This is not adequate, and if it functions now you are just lucky. With two servos, you should find a real source of 5 volts, and use it to power the servos directly, and the Arduino too if you need.

Or you could use 4xAA batteries for the servos, very good source for amps. As you have discovered, wiring counts, and a common ground is essential.

You may see how you were repeatedly asked about power, wiring and any tests you may have performed. You will go further faster if you adopt a scientific atitude, and make theories informed by research and test them with your hardware.

a7

ok,

I guess the servo will still need a capacitor despite the 4xAA batteries.
I've read that the Arduino consumes batteries within 24-48 hours. That doesn't work. My plan is to connect the Arduino to a 230volt plug outside.

Get a power supply and feed 7V or 9V into the jack for the arduino. You will need to build also 5V for the servos - an efficient step down converter will do.

you mean something like this:

?

something like that yes, some are better / more efficient than others (current draw when not used, noise, efficiency (percentage of power transformed versus "lost")...)

Those with a fixed output (5V) are more stable too.

you could google translate this page if you want some more information on the different models

Cool, thanks.

I've got another question.

Everything works, the sensors, the pump, the servo motor, when connected with a USB cable to my laptop, but the moisture sensors LED stops pulsing as it should when I unplug the USB cable and plugin the 5 volt power adapter.

I've uploaded the code so the Arduino should run its code even though it isn't connected to the laptop.

How do you plug that in ? What adapter is that ?

This adapter plugged into a wall socket and then connected to the Arduino.

I see that the output is not more than 1 amp. Could that be the reason ?

For the moisture sensor 1 amp is more than enough though.

Yes, I tried an adapter with 5v 3a and it works :partying_face:

Another thing:
I tried to add a capacitive soil moisture sensor (sensor2) to the system with the following code, but then the pump started running even though the other sensor (sensor1) value did not fall below the threshold. Why is it?

#include <Servo.h>
#include <DHT.h>

// Constants
#define TEMP_THRESHOLD_HIGH 30
#define TEMP_THRESHOLD_LOW 25
#define STEP_DELAY 20

// Pin Definitions
const int sensorpin = A0;
const int moisturePin2 = A1; //this sensor
const int sensorpower = 12;
const int pumppin = 13;
const int servoPins[2] = {10,11};
const int tempSensorPin = 9;

// DHT setup
#define DHTTYPE DHT22
DHT dht(tempSensorPin, DHTTYPE);

// Servo setup
Servo servos[2];

void setup() {
  Serial.begin(9600);

  // Initialize pump and sensor power pins
  pinMode(sensorpower, OUTPUT);
  pinMode(pumppin, OUTPUT);
  pinMode(moisturePin2, INPUT); //this one


  // Initialize servos
  for (int i = 0; i < 2; i++) {
    servos[i].attach(servoPins[i]);
    servos[i].write(0); // Set servos to 0 degrees initially
  }

  // Initialize DHT sensor
  dht.begin();

delay(2000);

}

void loop() {
  // Read moisture sensor
  digitalWrite(sensorpower, HIGH);
  delay(100);
  int sensor = analogRead(sensorpin);
  digitalWrite(sensorpower, LOW);
  Serial.print("moisture sensor: ");
  Serial.println(sensor);
  delay(100);

int moistureLevel2 = analogRead(moisturePin2); //these ones
  Serial.print("Moisture Level 2: ");
  Serial.println(moistureLevel2);


  // Control pump based on sensor reading
  if (sensor < 150) {
    digitalWrite(pumppin, LOW); // Turn pump on
  } else {
    digitalWrite(pumppin, HIGH); // Turn pump off
  }

  // Read temperature from DHT22
  float temperature = dht.readTemperature();

  // Check if the reading was successful
  if (isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Print temperature to the serial monitor
  Serial.print("Temperature: ");
  Serial.println(temperature);

  // Control servos based on temperature
  if (temperature >= TEMP_THRESHOLD_HIGH) {
for (int i = 0; i < 2; i++) {
slowlyMoveServo(servos[i], 90); // Rotate servo to 90 degrees
}
} else if (temperature <= TEMP_THRESHOLD_LOW) {
for (int i = 0; i < 2; i++) {
slowlyMoveServo(servos[i], 0); // Rotate servo back to 0 degrees
}
}


  // Delay between readings
  delay(2000);
}

// Function to move servo slowly to a specified position
void slowlyMoveServo(Servo &servo, int targetPos) {
  int currentPos = servo.read(); // Read the current position
  if (currentPos < targetPos) {
    for (int pos = currentPos; pos <= targetPos; pos++) {
      servo.write(pos);
      delay(STEP_DELAY);
    }
  } else {
    for (int pos = currentPos; pos >= targetPos; pos--) {
      servo.write(pos);
      delay(STEP_DELAY);
    }
    }
}

I actually want to switch out the resistive soil sensor with the capacitive one, as the resistive sensor sends an electric pulse into the soil, which might disturb the roots of the plants.

to which pin ?

I've connected a USB cable from a 5v 3a adapter to the Arduino.