How to connect 2 servos and 1 water pump?

Hi all, this is my first big arduino project (for myself). I want to connect my esp32 to firebase, where I put mock data to firebase and control the microcontroller.

Here is a brief explanation, I want to create a water quality control, where for example, pH is low, servo will work to put in pH buffer, when oxygen level low, I will make sure the water pump work, and another servo is for feeding fish.

But here is my problem. i want how do I connect all that on my esp32, and make sure it work? here are the list of items that I bought,

  1. esp32
  2. water pump
  3. relay
  4. 2 servos
  5. 6v external power supply (4 AA 1.5v battery)

My plan is to connect 3v3 pin to waterpump and servo, while 5v pin to both my servos. Now I have problem, where my water pump is constantly working, although it should not. and my feeder servo is not working. Please help me.

here is part of my code:

// Pin Definitions #define PH_SERVO_PIN 13 #define FEEDER_SERVO_PIN 12 #define WATER_PUMP_PIN 21

FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;

Servo phServo;
Servo feederServo;

unsigned long sendDataPrevMillis = 0;
bool signupOK = false;
bool hasRotated = false;

// Status Flags
bool isPHNormal = true;
bool isDONormal = true;
bool isTempNormal = true;

// NTP Time Client Setup
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 8 * 3600, 60000); // UTC+8 time

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

// Initialize Water Pump Pin
pinMode(WATER_PUMP_PIN, OUTPUT);
digitalWrite(WATER_PUMP_PIN, LOW); // Ensure pump is off initially

// Wi-Fi Connection
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(300);
}
Serial.println("\nConnected to Wi-Fi!");

// Firebase Initialization
config.api_key = API_KEY;
config.database_url = DATABASE_URL;
if (Firebase.signUp(&config, &auth, "", "")) {
Serial.println("Firebase connected.");
signupOK = true;
} else {
Serial.printf("Firebase signup error: %s\n", config.signer.signupError.message.c_str());
}
config.token_status_callback = tokenStatusCallback;
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);

// Initialize Servos
phServo.attach(PH_SERVO_PIN);
feederServo.attach(FEEDER_SERVO_PIN);

// Start NTP Client
timeClient.begin();
timeClient.update();
}

void loop() {
if (Firebase.ready() && signupOK && (millis() - sendDataPrevMillis > 10000 || sendDataPrevMillis == 0)) {
sendDataPrevMillis = millis();
// Update NTP Time
timeClient.update();
// Read Sensor Data from Firebase
float pH, DO, temperature;
// Read pH Value
if (Firebase.RTDB.getFloat(&fbdo, "sensors/pH", &pH)) {
Serial.print("pH: ");
Serial.println(pH);
handlePH(pH);
} else {
Serial.println("Failed to retrieve pH value");
Serial.println("REASON: " + fbdo.errorReason());
}
// Read Dissolved Oxygen (DO) Value
if (Firebase.RTDB.getFloat(&fbdo, "sensors/DO", &DO)) {
Serial.print("DO: ");
Serial.println(DO);
handleDO(DO);
} else {
Serial.println("Failed to retrieve DO value");
Serial.println("REASON: " + fbdo.errorReason());
}
// Read Temperature Value
if (Firebase.RTDB.getFloat(&fbdo, "sensors/temperature", &temperature)) {
Serial.print("Temperature: ");
Serial.println(temperature);
handleTemperature(temperature);
} else {
Serial.println("Failed to retrieve temperature value");
Serial.println("REASON: " + fbdo.errorReason());
}
// Feeding Fish at Specific Intervals or Conditions
feedFish(); // Implement your feeding logic here
}
}

// Function to handle pH adjustments
void handlePH(float pH) {
if (pH < 6.5 && isPHNormal) {
adjustPH();
isPHNormal = false;
} else if (pH >= 6.5 && pH <= 8.5 && !isPHNormal) {
stopPHAdjustment();
isPHNormal = true;
}
}

// Function to adjust pH using servo
void adjustPH() {
Serial.println("Adjusting pH...");
phServo.write(180); // Adjust pH in one direction
delay(1000); // Run servo for 1 second
phServo.write(90); // Stop the servo
delay(1000); // Wait before next action
logAction("pH adjusted");
}

// Function to stop pH adjustment
void stopPHAdjustment() {
Serial.println("Stopping pH adjustment...");
phServo.write(90); // Set servo to neutral position
logAction("pH adjustment stopped");
}

// Function to handle Dissolved Oxygen (DO) levels
void handleDO(float DO) {
if (DO < 3.0 && isDONormal) {
activateWaterPump("DO level low");
isDONormal = false;
} else if (DO >= 5.0 && !isDONormal) {
deactivateWaterPump("DO level normal");
isDONormal = true;
}
}

// Function to handle Temperature control
void handleTemperature(float temperature) {
if ((temperature < 26.0 || temperature > 32.0) && isTempNormal) {
activateWaterPump("Temperature out of range");
isTempNormal = false;
} else if (temperature >= 26.0 && temperature <= 32.0 && !isTempNormal) {
deactivateWaterPump("Temperature normal");
isTempNormal = true;
}
}

// Function to activate the water pump with logging
void activateWaterPump(const char* reason) {
Serial.println("Activating water pump...");
digitalWrite(WATER_PUMP_PIN, HIGH); // Turn on water pump
logAction(reason);
}

// Function to deactivate the water pump with logging
void deactivateWaterPump(const char* reason) {
Serial.println("Deactivating water pump...");
digitalWrite(WATER_PUMP_PIN, LOW); // Turn off water pump
logAction(reason);
}

// Function to feed fish
void feedFish() {
// Example: Feed fish every hour
static unsigned long lastFeedTime = 0;
const unsigned long feedInterval = 3600000; // 1 hour in milliseconds

if (millis() - lastFeedTime >= feedInterval) {
Serial.println("Feeding fish...");
feederServo.write(180); // Activate feeder servo
delay(1000); // Run servo for 1 second
feederServo.write(90); // Stop the servo
delay(1000); // Wait before next action
logAction("Fish fed");
lastFeedTime = millis();
}
}

// Log actions to Firebase
void logAction(const char* action) {
timeClient.update(); // Ensure the latest time is fetched
unsigned long epochTime = timeClient.getEpochTime(); // Get epoch time

FirebaseJson json;
json.set("action", action);
json.set("epoch", epochTime);
// Push JSON to Firebase
if (Firebase.RTDB.pushJSON(&fbdo, "actionHistory", &json)) {
Serial.println("Action logged successfully.");
} else {
Serial.println("Failed to log action.");
Serial.println("REASON: " + fbdo.errorReason());
}
}

should I do them separately? like using another esp32 to control the water pump. please help me

Please don't do those connections. Your controller will get destroyed.
Please post datasheet links to servo, pump and relay.

It is clear from your post that you are not ready for a big project. You need to learn about electronics. If you do not do this, you will damage the components. You may already have damaged them.

Please read the forum guide in the sticky post, then edit your post above and fix it. It is breaking forum rules.

1 Like

should i just use another esp32 to control the water pump?

strong text

Take heed ....^^^

Is this the ESP32 board you are using?

Please show a datasheet or link to a webpage for the relays?

Thank you for helping me realize my weaknesses.

its my first time in this forum. i will do better.

this is my esp32


and this is the relay



i dont know if this may help. But i am thankful for u being you concern for me

You cannot power the two relays and the pump from the 3V3 pin, you will definitely burnout the ESP32 board.

How are you powering the ESP32? From the USB or 5V?

I recommend using NiMH AA cells, which are around 1.2V and around 5V in total when fully charged, so should be suitable for the ESP, the servos and the pump. Power the pump and servos directly from the batteries, not via the ESP board.

To maximise battery life, I recommend replacing the relay with a transistor of some kind. Otherwise, the relay may draw as much power as the pump itself, when running.

Do not forget to connect a flyback diode across the pump terminals to protect the circuit from damage when the pump is switched off. Any 1N400x diode would be fine.

Thank you kind sir. I may try it and update later

You cannot power the two relays and the pump from the 3V3 pin of that board, you will definitely burnout the ESP32 board.

How are you powering the ESP32?
From the USB or 5V?

right now, i just use power from USB.

I want to note here that, I am not connecting all of the component yet. Right now, I just try connecting 1 servo and the water pump, and i notice the servo move at its own then i unplug my usb.

My relay connected to my water pump, 3v3 pin and external battery.

So in the final version do you want batteries or will a plugin wall adaptor be OK?

And please dont be too harsh on me :slightly_smiling_face:

I am just a teenager that just want to win a mini competition. Its my first time on this forum too.

I also appreciate all the concern and effort u guys give. Thank you.

is the battery sufficient?

A battery can be sufficient.
What I'm asking is if it MUST be battery powered?

yes

Do you have the time/money to buy maybe new relays, batteries and a buck converter module.