0% found this document useful (0 votes)
3 views2 pages

Servo Control Using Water Sensor

This document contains an Arduino sketch that controls a servo motor based on water sensor readings. When the sensor value exceeds a threshold, the servo moves from 0 to 180 degrees and back, otherwise it remains at 0 degrees. The code includes setup for serial communication and continuous monitoring of the sensor value in the loop function.

Uploaded by

prakharnanda50
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Servo Control Using Water Sensor

This document contains an Arduino sketch that controls a servo motor based on water sensor readings. When the sensor value exceeds a threshold, the servo moves from 0 to 180 degrees and back, otherwise it remains at 0 degrees. The code includes setup for serial communication and continuous monitoring of the sensor value in the loop function.

Uploaded by

prakharnanda50
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

//Project by https://www.youtube.

com/channel/UCbBBiC-2H7LKwFR3pTz2g0w

#include <Servo.h> //include servo library

Servo myservo; //define servo as servo

const int waterSens = A0;//set water sensor to A0

int pos = 0;//define servo position

void setup()

Serial.begin(9600);

myservo.attach(9);//attach servo to pin 9

void loop()

int sensorValue = analogRead(waterSens);//read the water sensor value

sensorValue = map(sensorValue, 0, 1023, 0, 180);

if (sensorValue >= 50) {

for (pos = 0; pos <= 175; pos += 1) { // goes from 0 degrees to 180 degrees

// in steps of 1 degree

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(15); // waits 15ms for the servo to reach the position

for (pos = 175; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(15); // waits 15ms for the servo to reach the position

else

{
myservo.write(0);

Serial.println(sensorValue);

delay(20);

You might also like