top of page

Arduino Water Sensor and Servo

Dec 11, 2017
1 min read

SOURCE CODE:

#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 <= 180; 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 = 180; 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); }


 
 
 

1 Comment


stivejoy18
Sep 10

This is a really handy breakdown for combining basic sensors with physical actuators. Using the water sensor's analog readings to trigger specific servo sweep positions is a classic approach, but seeing the actual signal thresholds laid out step-by-step makes it so much easier to understand. Getting servo jitter under control or mapping fluctuating analog inputs smoothly can be a bit of a headache when working through electronics coursework, so tapping into Expert Arduino Assignment Help for Students can be quite a lifesaver for ironing out code bugs or power delivery issues. The point about ensuring the servo has a stable power supply is particularly crucial for preventing board resets. Did you test this setup with varying liquid levels, or mostly…

Like
Single post: Blog_Single_Post_Widget

Address

Fanwood, NJ 07023, USA

Contact

©2017 by Robotic Nation. Proudly created with Wix.com

bottom of page