0
\$\begingroup\$

I have connected a photo-resistors terminal to atmega320'S ADC0 (PC0) PIN, the other to 5V and the third to ground via resistor enter image description here

I'm powering my atmega with 3.7v. My goal is to output the value of ADC0 on DC6 pin using PWM. I have the following code:

#define F_CPU 8000000UL

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
    initPWM();
    initADC();

    while(1)
    {
        startConversion();

        OCR0A = ADCH;

        _delay_ms(20);  
    }
}

void initPWM()
{
    DDRD   = 0b01100000;   
    TCCR0A = 0b10100011; 
    TCCR0B = 0b00000001; 
}


void initADC()
{
    // Internal 2.56V voltage reference
    // set ADC0 as the ADC input channel
    ADMUX  = 0b11000000;

    // enable ADC
    // set prescaler to 128
    ADCSRA = 0b10000111;
}


void startConversion()
{
    // enable ADC
    // start conversion
    // set prescaler to 128
    ADCSRA = 0b11000111;
}

I tried to output both ADCH and ADCL via OCR0A, but the light intensity of the PD6 doesn't change, but if I set 0CR0A to 255, it glows super bright so PWM is working. I tested photoresistor with multimeter and I'm getting 0.6v to 1v between the ground and ADC0. Any suggestions?

\$\endgroup\$
1
\$\begingroup\$

Measure the voltage of the pin with a high impedance meter (the voltage between the phototransistor and the resistor) and see if its matching up with what you have in software. If it is then you need to fix your bias on your photo transistor or the transistor could be broken.
If the voltage doesn't match up with your software value then you have got some debugging to do.

\$\endgroup\$
  • \$\begingroup\$ Thanks! Voltage between the resistor and photoresistor is 0.6v to 1v so it seems to be working. I set OCR0A value to 0 and to 255 and the led was off and on full brightness, so PWM is working, something is wrong with ADC, but I can't understand what.. \$\endgroup\$ – 0x29a Nov 15 '16 at 22:57
0
\$\begingroup\$

Turns out the problem was loose pin on the ADC0, the code and setup is correct. Sorry everybody.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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