Hi,
In my quality lab, there's a small water tub (length 8 cm) fixed on hydrolysis oven (for fabric testing). the water should always be present in tub at a certain level (4 cm from bottom). I need to install ultrasonic sensor within tub so that whenever water will be below 4cm, a 220V AC bulb will be turned on. And upon refilling the water, sensor will detect the water level and will turn off the bulb.
For this, I have used 5V relay, arduino UNO and ultrasonic sensor HC-SR04. Let me know, If any additional component is required.
How I'll use new ping library of HC-SR04 in programming to get better and long lasting results.
// Define the pins for the ultrasonic sensor and relay
#define TRIG_PIN 8 // Pin connected to Trig of the ultrasonic sensor
#define ECHO_PIN 7 // Pin connected to Echo of the ultrasonic sensor
#define RELAY_PIN 9 // Pin connected to the relay module
// Variables for ultrasonic sensor readings
long duration; // To store the time of the echo pulse
float distance; // To store the calculated distance in cm
// Threshold levels in cm
const float GOOD_LEVEL = 4.0; // Normal water level (good level)
const float LOW_LEVEL_THRESHOLD = 3.5; // Below this level, the light will turn on
void setup() {
// Initialize serial communication for debugging
Serial.begin(9600);
// Set the ultrasonic sensor pins
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// Set the relay pin
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Initially turn off the relay
}
void loop() {
// Measure the distance using the ultrasonic sensor
distance = measureDistance();
// Debugging: Print the distance
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Logic to control the relay based on water level
if (distance >= 0 && distance <= LOW_LEVEL_THRESHOLD) {
// If water level is below or equal to 3.5 cm, turn on the light
digitalWrite(RELAY_PIN, HIGH);
Serial.println("Light ON: Water level is LOW.");
} else if (distance >= 0 && distance > GOOD_LEVEL) {
// If water level is at or above 4 cm, turn off the light
digitalWrite(RELAY_PIN, LOW);
Serial.println("Light OFF: Water level is GOOD.");
}
// Delay before the next measurement
delay(1000); // 1 second delay
}
// Function to measure the distance using the ultrasonic sensor
float measureDistance() {
// Send a 10-microsecond pulse to the Trig pin
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Measure the time of the echo pulse
duration = pulseIn(ECHO_PIN, HIGH);
// Calculate the distance in cm
float distance = (duration * 0.0343) / 2; // Speed of sound: 343 m/s
// Return the calculated distance
return distance;
}
As, i'm a beginner in programming so please guide me in achieving my requirement.
Thankyou.