Problem while controlling soil moisture sensor+servo motors

I am making a project of a smart bridge in which the hight of the bridge will increase with the help of servo moters when the sensor detects moisture more than a limit.But my servo motors are not working.i think something is wrong with the code.can please help me correct it?or send me the correct code.

Screenshot_2023-08-09-18-29-39


#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(90);
    }
}

Just 1 include.

At first your code works correctly.

Are you using the digital output of the moisture sensor?
Or does it only have analog outputs?

The arduino cannot provide enough current to power the servos.

Use an external source to power them.

If you call servo.write() at a very high speed, then the servo motor might not work (depending on the used Servo library). Adding a delay() at the end of the loop() should make it work.

The soil sensor of Tinkercad has only a analog output, so I would use a analog input to measure it.

This is what I made of it:

// Forum: https://forum.arduino.cc/t/problem-while-controlling-soil-moisture-sensor-servo-motors/1156635
// This Wokwi project: https://wokwi.com/projects/372589470373021697
// Wokwi custom soil_sensor code by Maverick ?

#include <Servo.h>

Servo tap_servo;

const int sensor_pin = A0;
const int tap_servo_pin = 5;

void setup()
{
  tap_servo.attach(tap_servo_pin);
}

void loop()
{
  int val = analogRead(sensor_pin);

  if (val < 600)
  {
    tap_servo.write(0);
  }
  else
  {
    tap_servo.write(90);
  }

  delay(100);  // slow down the sketch
}

Try it in Wokwi simulation (Wokwi is more fun than Tinkercad, but Wokwi does not simulate an analog circuit):

Click on the green soil sensor (when the simulation is running) to change the soil humidity.

Nice. THX.

wokwi has some analog capabilities.

Here, if I did not have an analog moisture sensor part, I would have used a slide fader as a proxy, which works OK with analogRead().

Curiously your custom part reports 1680 to 3620 for its range, and when analogRead() the value is 420 to 905, the setting divided by four.

Time to learn more about custom parts.

I cannot help but see an opportunity to add hysteresis to the control statements, like

  if (val < 550)
  {
    tap_servo.write(0);
  }

  if (val > 750)  
  {
    tap_servo.write(90);
  }

Don't turn on the water until it is dry enough, don't turn it off until it is wet enough.

a7

Really strange project. Moving a
image
Up an down depending on moisture = soil beeing wet or dry

1 Like

you can use the sensor to just detect the presence of water having the sensor just up in the air.

(of course there are better sensors for this - but sometimes you do with what you have)

1 Like

Sorry, my bad. The custom chip was for a 12-bit ADC. I have changed it now to a percentage and a output voltage from the sensor from 0.5 to 4.5V.

@hetu022 as @ruilviana pointed out you cannot connect the servos to the UNO 5V for power.
You need a separate 5V 2A supply for the servos.

Missed the smart bridge thing totally. We don't have them in my corner of the world, yet.

That should read

Don't raise the bridge until the water is really high. Don't lower it later until the water is really low.

The OP now needs two sensors of a more digital nature to detect the levels of interest.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.