Somtimes, we need to measure the level of water in a recipient, or if you want to see the level of water in a tank, it is a requirement to measure the levels of water that it has; so in this section, we will explain how to do this.
The sensor is Normally Open. When the water is over the limit, the contact opens, and it sends a signal to the Arduino board. We use pin number 2
, which is a digital input:
We declare the variables and const
in the program:
const int buttonPin = 2; // the number of the input sensor pin const int ledPin = 13; // the number of the LED pin
We also define the states of the digital signals:
// variables will change: intbuttonState = 0; // variable for reading the pushbutton status
We configure the signals of the program, inputs, and outputs:
void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); Serial.begin(9600); }
We read the state of the digital input:
void loop() { // read the state of the pushbutton value: buttonState = digitalRead(buttonPin);
We make the comparisons for the sensor:
if (buttonState == HIGH) { Serial.println(buttonState); Serial.println("The recipient is fulled"); digitalWrite(ledPin, HIGH); delay(1000); }
If the sensor detects a LOW level, the recipient is empty:
else { digitalWrite(ledPin, LOW); Serial.println(buttonState); Serial.println("The recipient is empty"); delay(1000); } }
The following screenshot shows the result when the recipient is empty:
The water is over the limit:
18.119.125.120