Coding for Raindrop Sensor and 2 servo motors

Hai. We are doing a project about a suspension that can be automatically turned on and close when it detects rain. Also, there will also be a heat lamp as a heater so that the laundry still dry even under a closed suspension. (It’ll also be automatically turned on simultaneously with the rooftop.) We are using a raindrop sensor and 2 servo motors. The problem is, we don’t really know how to do the coding that can involve raindrop sensors and 2 servos. We need both of the servos to be turned 180 degree simultaneously. Can someone help us with the coding?

Thank you!

What have you tried and what is your power supply for the project?

We already programmed one servo motor, so it means when the sensor detects water, only one of the servos spinned. Our power source is a solar panel.
Here is the code that spins 1 servo motor


#include <Servo.h>

#include <Servo.h>

Servo tap_servo;

int sensor_pin = 4;
int tap_servo_pin =5;
int val;

void setup(){
  pinMode(sensor_pin,INPUT);
  tap_servo.attach(tap_servo_pin);
  
}

void loop(){
  val = digitalRead(sensor_pin);

  if (val==0)
  {tap_servo.write(0);
  }
  if (val==1)
  {tap_servo.write(180);
    }
}

the solar panel is 5V. Can you help us change the code so it spins both of the servos?

At what current?

Same goes for the servos.

Why twice? You only need it in once.

Add to this the second servo's name:-

Servo tap_servo;
Servo other_servo;

Add stuff for this other servo in the global variable area and also in the setup and loop functions.

Finally test it with just the other servo attached and see that it behaves correctly before trying to run the two at once because I have doubts if your solar panel is capable of driving two servos.

If it does not work then post the new code and say what it does and what you expected it to do.

Hi, @mrssarsya12
Welcome to the forum.

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Can you post some images of your project?
So we can see your component layout.

Do you have a DMM? (Digital MultiMeter)

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

if controlling 2 servos independently isn't synchronized eough, you can wire them so that they are both controlled by the same pin using a Y harness

ok so now we have changed the plan a little bit. We are making one of the servo turn 180 and the other 360. We did the coding, but when we installed it, the 360 servo spins on its own without having any water on the raindrop sensor. Is there anything we’re doing wrong?
Here’s the coding


#include <Servo.h>

Servo servo360;
Servo other_servo;

int sensor_pin = 4;
int servo360_pin = 5;
int other_servo_pin = 9;
int val;

void setup(){
  pinMode(sensor_pin,INPUT);
  servo360.attach(servo360_pin);
  other_servo.attach(other_servo_pin);
  
}

void loop(){
  val = digitalRead(sensor_pin);

  if (val==0)
  {servo360.write(0);
  other_servo.write(0);
  }
  if (val==1)
  {servo360.write(180);
  other_servo.write(180);
  }
}

what does "spins on its own" mean? is it a continuous servo that can rotate clockwise or ccw?

Hi, @mrssarsya12

What are your servos?
Can you please post a link to specs/data?
Normal servos are 0 to 180deg.

There are continuous servos, but the way you originally programmed them, they acted like 0 to 180.

Tom.. :smiley: :+1: :coffee: :australia:

Here something to get you started:

#include <Servo.h>

#define RAIN_SENSOR_PIN A0 // Pin connected to raindrop sensor
#define SERVO_PIN1 9       // Pin connected to first servo
#define SERVO_PIN2 10      // Pin connected to second servo
#define RELAY_PIN 8        // Pin connected to relay for heat lamp

Servo servo1;
Servo servo2;

void setup() {
  Serial.begin(9600);
  pinMode(RAIN_SENSOR_PIN, INPUT);
  pinMode(RELAY_PIN, OUTPUT);
  
  servo1.attach(SERVO_PIN1);
  servo2.attach(SERVO_PIN2);
  
  servo1.write(0); // Initial position
  servo2.write(0); // Initial position
  digitalWrite(RELAY_PIN, LOW); // Heat lamp off
}

void loop() {
  int rainState = analogRead(RAIN_SENSOR_PIN);
  
  if (rainState < 500) { // Adjust threshold as needed
    Serial.println("Rain detected!");
    servo1.write(180);
    servo2.write(180);
    digitalWrite(RELAY_PIN, HIGH); // Turn on heat lamp
  } else {
    Serial.println("No rain.");
    servo1.write(0);
    servo2.write(0);
    digitalWrite(RELAY_PIN, LOW); // Turn off heat lamp
  }
  
  delay(1000); // Check every second
}

how is that different from what the OP presented in post #7?