Automatic plant watering system using microcontroller

atmegaatmelavrmicrocontroller

I am trying to build an automatic plant watering system using ATmega16. We are using moisture sensor YL69 for our project. The following is the image of the moisture sensor we have used.

The circuit diagram of the project is as follows:

The following is the code fragment we used:

#include<avr/io.h>
int adc(void);
void pump(void);
int adc_value;
int main(void)
 {
  DDRC=0x01;                          //Defining PC0 as output
  ADCSRA=0x87;                    //Setting the mode of operation
  ADMUX=0x00;                     //Selection of channel and bit alignment
  while(1)
   {
     adc_value=adc();                //reading moisture level
     pump();                               //Pump activator routine
   }
   return 0;
 }

int adc(void)
{
   int lower_bits,higher_bits,result; 
   ADCSRA |= (1 << ADSC)|(1 << ADIF);  //Turn on conversion and clear flag
   while(ADCSRA & (1 << ADIF) == 0);  //wait for flag
   lower_bits=ADCL;
   higher_bits=ADCH;
   result=lower_bits|(higher_bits<<8);         //Accessing converted value by shifting
   return result;
 }

void pump(void)
 {
  if(adc_value>=700)                                //Pump ON trigger point
   {
     PORTC|=(1<<0);
   }
  else if(adc_value<=600)                        //Pump Off trigger point
   {
     PORTC&=~(1<<0);
   }
 }

Is there anything wrong in the code? Because after burning it, i am getting low voltage(0.15) for wet soil and high voltage(4.84) for dry soil from the analog sensor input which is ok … but the problem is, I am always getting voltage like 0.7 (and sometimes like 0.15) at PC0 in both cases(I am using multimeter for measuring this). There in no change in the values for dry and wet soil at PC0.. in such case where is the actual problem? Is there anything wrong in the circuit design or in the code? And one more thing, can anyone please tell me the proper way of measuring the output value I am getting from PC0 which in turn is switching on/off the pump?

Another question is, I am using AVR programmer for 5V DC supply. In the above circuit design I have not made any connection at pin MOSI,MISO,RESET and SCK (of ATmega 16) with AVR programmer. Will I have to make this?

Best Answer

Have you measured this voltage with resistor detached? You should measure it relative to GND and then relative to +5V to make sure. Otherwise it is hard to determine wrongly configured pin.

Try to debug your code. Add 4-5 LEDs since you have many free pins and output highest bits of ADC to these LEDs.

Or output the ADC values to UART and read them via terminal.

BTW, why AVCC is connected to +5 via capacitor? Are you sure ADC is powered properly this way?

In the above circuit design I have not made any connection at pin MOSI,MISO,RESET and SCK (of ATmega 16) with AVR programmer. Will I have to make this?

I do not understand. You need these connections when programming of course, but not when running :)