# Attiny not working after ADC conversion

I am trying to get a simple IR sensor to work on my an attiny85. To do this I first wrote some code to setup the ADC and the perform an operation based on the outcome. Because the code didn't function as expected I decided to test if some code where I simply do one ADC conversion and then turn on an LED works. This did however not work. The cause of this seems to be that the attiny stops executing code after the ADC conversion. I am running the attiny at internal 8Mhz clock and the ADC is hooked up to ground through a 200 ohm resistor.

The following code does not turn on the pin at PB0:

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

int main(void) {

DDRB |= _BV(DDB0); //set PB0 to output

while(1) {

PORTB |= _BV(PORTB0); //set PB0 to high

}

return(0);
}


When I, however, put the "PORTB |= _BV(PORTB0);" line at the start of the loop, so before the ADC conversion, the line does execute and the LED at PB0 does turn on.

Also commenting out the line "ADCSRA |= _BV(ADSC);" makes the code run fine and makes PB0 go high. So I am pretty certain that the attiny somehow crashes when executing that line.

Disabling interrupts using "cli()" does not solve the problem either.

• When you initialize the ADC before your loop you're using "ADCSRA =", but you should probably use "ADCSRA |=" instead. I'm not familiar with the ATtiny though so ... Mar 29 '17 at 15:36
• @brhans You're right, I was just moving around lines to try to get it to work. It's not the cause of the problem though, I just tested it and no result. I'll edit the post right away. Mar 29 '17 at 15:41
• Your code seems correct. The only thing I can recommend is moving definition of adc_result variable to either the head of main() or outside of main --to make it global so that it can be accessed from everywhere. Or maybe remove that line and test again if LED turns on. Some C compilers don't allow variable definiton at the middle of a scope though, they force you to define at the begining of a scope instead. Mar 29 '17 at 15:55

I do not have any Attiny85 but I used your code and I write it to Attiny13.

The code I used:

#include <avr/io.h>

int main( void ) {

DDRB |= ( 1 << DDB0 ); //set PB0 to output

while ( 1 ) {

while ( ADCSRA & ( 1 << ADSC ) );//wait for conversion to end

PORTB ^= ( 1 << PB0 ); //Toggle LED at PB0

}

return( 0 );
}


As you can see I slightly modified the code because My C compilers do not recognize _BV macro so, I replace it with (1 << x) instead. And I also Toggle the led (PB0).

And the signal at PB0 pin looks like this:

Notice the signal frequency is around 5.2MHz.

So it's is very interesting why this code don't work at attiny85.

• I tried the code and it didn't work as well however when I changed my fuse settings as described in my answer your code executed properly on the attiny85 as well. thanks for helping though! Mar 29 '17 at 19:14

So I solved the problem.

For some reason the code started working when I changed the fuse settings to divide the internal clock by 8 so that I have an internal clock of 1 Mhz instead of 8 Mhz. I do not know why but for some reason this caused the ADC to not crash.

New fuse settings: -U lfuse:w:0x62:m -U hfuse:w:0xdf:m -U efuse:w:0xff:m

Old fuse settings: -U lfuse:w:0xe2:m -U hfuse:w:0xdf:m -U efuse:w:0xff:m