WaterSensor with AVR

Interfacing water sensor with AVR

Blog DSS#002

On these days water level indicator provides a great relieve with its simple mechanism. It is as simple as detect and indicate water level in water tank / water reservoir by activating applied features. Some can detect water load and enable/disable pump eclectic supply, or some can blow alarm beyond water level.

” Water level sensor working principle is when the sensor is sensing water or put into certain depth, the pressure on the sensor’s lower layer converted into water level height. The calculated formula is Ρ=ρ.g.H+Po, ” Where P is Pressure on the sensor’s lower layer, ρ is the density of water, Po is the atmospheric pressure of water surface area, g is acceleration to the gravity, H is the depth at which the sensor inside water level.

There are different types of water level sensor which is used based on application to application. Other types of water level sensors can be found in https://google.com or we will update in future if we have demonstrated any other water level sensor.

Water level sensor has 3 pins (VCC 5v [+], GND [-], Source [Analog Input]). You can see below picture for connections. This code is implemented as when water sensor detects water level (Touch to liquid) it activates PORTD PIN0. This PIN0 is connected to relay module (5V). You can feel a delay in response while reading signal and enable/disable relay due delay mechanism. It is required to give some delay of sensing the water to ensure constant water and delay time also helps relay to power ON pump or power OFF pump.

Code :


/*
 * ADCinAVR.c
 *
 * Created: 5/18/2023 9:44:14 PM
 * Author : DSSIAN
   License : STUDENT
   Copyrights : DSSIAN   
 */ 

#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>

#define ANALOG_READTHRESHLD 150u
#define SIGNAL_OFF 0x00
#define SIGNAL_ON 0x01
#define Pump_swt PORTD 

uint16_t WaterSensor1_ReadData = 0u;
uint16_t WaterSensor2_ReadData = 0u; /* Defined for future implementation */

void adc_init()
{
	// AREF = AVcc
	ADMUX = (1<<REFS0);

	// ADC Enable and prescaler of 128
	// 16000000/128 = 125000
	ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
}

uint16_t adc_read(uint8_t ch)
{
	// select the corresponding channel 0~7
	// ANDing with '7' will always keep the value
	// of 'ch' between 0 and 7
	ch &= 0b00000111;  // AND operation with 7
	ADMUX = (ADMUX & 0xF8)|ch;     // clears the bottom 3 bits before ORing

	// start single conversion
	// write '1' to ADSC
	ADCSRA |= (1<<ADSC);

	// wait for conversion to complete
	// ADSC becomes '0' again
	// till then, run loop continuously
	while(ADCSRA & (1<<ADSC));

	return (ADC);
}

void PumpControl_SignalReceiveData(uint16_t WaterSensor_ReadData)
{
    WaterSensor1_ReadData = WaterSensor_ReadData;
	   
	if(WaterSensor_ReadData>ANALOG_READTHRESHLD)
	Pump_swt = SIGNAL_OFF;
	else 
	{
		_delay_ms(500);
		Pump_swt = SIGNAL_ON;
	}
	//Pump_swt = SIGNAL_ON;	
	
	
}
int main(void)
{
	uint16_t adc_result0;
	
	 DDRD = 0x01;           // to connect led to PC0
	  adc_init();
	  _delay_ms(50);
	 
    /* Replace with your application code */
    while(1) 
    {
		adc_result0 = adc_read(0);      // read adc value at PA0
		PumpControl_SignalReceiveData(adc_result0);
	}
}


All these components are available at https://robu.in/. The price and quality of component quite good here with my experience but you can buy where you are comfortable.

Leave a Reply

Your email address will not be published. Required fields are marked *

Leave a Reply

Your email address will not be published. Required fields are marked *