In this guide, we will learn how to interface a water level sensor with a P89V51RD2 microcontroller. The water level sensor is used to detect the water level in a container or tank. By measuring the analog output of the water level sensor using the external 0804 ADC, we can monitor the water level and use it for various automation purposes.
The goal is to interface the water level sensor with a P89V51RD2 microcontroller, to accurately measure the water level in a container or tank using the analog output of the water level sensor and display the digital output using eight LEDs. The measured water level will be used for monitoring purposes.
To successfully complete this project, you will need the following components:
P89V51RD2 development board
Water level sensor
Jumper wires
ADC0804
Instead of connecting the potentiometer to the 8051 microcontroller, connect Signal(S) pin of water level sensor to VIN+ of ADC.
Copy and paste the following code into main.c or download it from here:
#include<reg51.h>
#define input P1 //Input port (read values of ADC)
#define output P2 // Output port (connected to LED's)
void delay(unsigned intmsec );
void adc();
sbit wr= P3^5; // Write pin.
sbit rd= P3^6; // Read pin.
sbit intr= P3^4; // Interrupt pin.
void main()
{
input=0xff; // Declare port 1 as input port.
output=0x00; // Declare port 2 as output port.
while(1)
{
adc();
}
}
void delay(unsigned int msec ) // Delay function
{
int i,j ;
for(i=0;i< msec;i++)
for(j=0; j<1275; j++);
}
void adc() // Reading values from ADC and display on the LED's
{
rd=1;
wr=0;
delay(10);
wr=1;
while(intr==1);
rd=0;
output=input;
delay(10);
intr=1;
}
After uploading the code to the P89V51RD2 microcontroller, the program will continuously monitor the water level. As the water level varies the digital output will be displayed by using eight LEDs. When the water level is full all the LEDs will glow.
In this guide, you have learned how to interface a water level sensor with a P89V51RD2 microcontroller. By reading the analog output of the water level sensor using the 0804 ADC, you can monitor the water level in a container or tank. This information can be used for automating tasks, such as controlling water pumps, sending notifications, or displaying the water level on a user interface. Experiment with different water levels and observe the corresponding analog readings to understand the behavior of the sensor and its applications in your projects.