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.
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.
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.
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.
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.