Water Level Indicator

Water level indicator indicates the level of water in a source. Let’s build a simple program that indicate the level of water of a reserve tank and auto full when the level of water is less than 25%. We use ULN2003A driver IC that we already introduce in stepper motor. Let’s look at the connection diagram

In this connection if water reaches that level the corresponding pin will low otherwise high. So we need to enable the internal pull-up for the input pin. The main control logic as follow-

float sensor(void) -> return the level of water & also turn on/off the motor.

float sensor(void)
{
     float water_level=0;     //variable declear
	if(!(PINB&(1<<PINB0)))
	{
	 water_level=100;           //dipaly 100%
	 PORTC&=~(1<<PORTC0);       //load off
	}
	else if(!(PINB&(1<<PINB1))) water_level=87.5;        //dipaly 87.5%
	else if(!(PINB&(1<<PINB2))) water_level=75;          //dipaly 75%
	else if(!(PINB&(1<<PINB3))) water_level=62.5;        //dipaly 62.5%
	else if(!(PINB&(1<<PINB4))) water_level=50;          //dipaly 50%
	else if(!(PINB&(1<<PINB5))) water_level=37.5;        //dipaly 37.5%
	else if(!(PINB&(1<<PINB6)))
	{
	 water_level=25;          //dipaly 25%
	 PORTC|=(1<<PORTC0);       //relay on
	}
	return water_level;
}

The function returns a floating value type and On/Off the Motor but in LCD there is a problem to display floating point variable. So we need to modify in MFile that-

We can use sprintf function or can use standard library function under

format: dtrostrf(float_value, minimum_width, num_digits_after_decimal, string);

i.e dtostrf(float, 6, 3, string); -> xxxxxx.xxx -> [6 digits].[3 decimal]

The main program as follow-

#include<avr/io.h>
#include<util/delay.h>
#include<stdlib.h>
#define INPUT_DDR     DDRB
#define INPUT_PORT    PORTB
#include"lcd.h"        // PORTD as LCD port
#include"input.h"      // PORTB as input with internal pull-up
char level[6];
int main(void)
{
DDRC|=(1<<DDC0);      //output pin for relay control
LCD_INIT();
LCD_Clear();
INPUT_DDR=0x00;       // All port are input
INPUT_PORT=0xff;      // Internal pull-up activate
float Value=0.00;
while(1)
	{
      Value=sensor();
	  dtostrf(Value,3,2,level);
	  LCD_write_string(1,1," Resorver Water ");
	  LCD_write_string(1,2,"water level=");
	  LCD_write_string(13,2,level);
	}
return 0;
}