0
\$\begingroup\$

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?

\$\endgroup\$
4
  • \$\begingroup\$ Is that code written in Atmel Studio? \$\endgroup\$
    – AHB
    Dec 9, 2015 at 15:59
  • \$\begingroup\$ yes with Atmel Studio @BlueSky \$\endgroup\$ Dec 9, 2015 at 16:02
  • \$\begingroup\$ Are you sure that the code is getting programmed into the chip? \$\endgroup\$
    – Dave Tweed
    Dec 9, 2015 at 16:30
  • \$\begingroup\$ I have used extreme burner for burning the hex file got from atmel studio into atmega16. I haven't got any error @Dave Tweed♦ \$\endgroup\$ Dec 9, 2015 at 16:40

1 Answer 1

1
\$\begingroup\$

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 :)

\$\endgroup\$
16
  • \$\begingroup\$ " 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. "- Will you please clarify this a bit more. I haven't understood it prp\lperly @Rodion Gorkovenko \$\endgroup\$ Dec 9, 2015 at 17:12
  • 1
    \$\begingroup\$ Well, before this could you answer the question about AVCC I outlined in bold? It looks like most strange thing in your schematics. Why haven't you wired it directly to +5? \$\endgroup\$ Dec 9, 2015 at 17:26
  • \$\begingroup\$ well, is there anything wrong if I connect it through capacitor? I am not sure about it @Rodion Gorkovenko \$\endgroup\$ Dec 9, 2015 at 17:37
  • 1
    \$\begingroup\$ The power to ADC simply would not be fed through capacitor. Capacitor do not conduct the current. So I believe ADC is not powered and is not working properly. Though it is a long time since I last read the manual for Atmega16. But you can read it right now to make sure how this pin should be connected. :) \$\endgroup\$ Dec 9, 2015 at 18:00
  • \$\begingroup\$ should I have to connect the AVCC to 5V using a low pass filter? @Rodion Gorkovenko \$\endgroup\$ Dec 9, 2015 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.