Ultrasonic Sensor for Water Level

Image.png
Image.png
Image.png
COMPONENTS:
-Arduino Uno
-Buzzer
-Relay
-Ultrasonic SR04

CODE:
#include <NewPing.h>
int Buzzer = 13;
int Relay = 10;
int val=0;
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
  Serial.begin(9600); // Open serial monitor at 9600 baud to see ping results
pinMode(Buzzer,OUTPUT); //Setup Buzzer pin as output pin
pinMode(Relay,OUTPUT); //Setup Relay pin as output pin
digitalWrite(Relay,LOW);
}
void loop() {
  delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  int sensorValue = uS / US_ROUNDTRIP_CM;
  Serial.print(“Ping: “);
  Serial.print(sensorValue); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println(“cm”);
  if (sensorValue < 15 and sensorValue !=0) {
digitalWrite(Buzzer,LOW);
delay(1000);
digitalWrite(Relay,LOW); // Pump OFF
  }
  if (sensorValue > 200 and sensorValue !=0) {
digitalWrite(Buzzer,HIGH);
delay(1000);
digitalWrite(Buzzer,LOW);
delay(1000);
digitalWrite(Relay,HIGH); // Pump ON
  }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s