1
\$\begingroup\$

I am using the ADC0 (PA0) pin of an Attiny84 to read analog values from an audio circuit. This part is working fine. Since I need to connect the Attiny84 to 8 LEDs, I'd like to use the ADC0 pin as an output also.

Here is the schematic:

schematic

simulate this circuit – Schematic created using CircuitLab

But this doesn't work. The LEDs don't light up. (They are turned on based on ADC0 values read in.)

Does this have something to do with enabling pull-ups?

Or is it not possible to switch ADC0 between input and output?

Here is the code:

// set pin as input
DDRA &= ~(1 << PA0);

// unset DIDR0
DIDR0 &= (1 << ADC0D);

// enable ADC
ADCSRA |= (1 << ADEN);

// start conversion
ADCSRA |= (1 << ADSC);
// loop till done
while(ADCSRA & (1 << ADSC));

// use h/l regs
unsigned char adcl = ADCL;
unsigned char adch = ADCH;
int result = (adch << 8) | adcl; 

// set DIDR0
DIDR0 |= ~(1 << ADC0D);

// set pin as output
DDRA |= (1 << PA0);

// switch LED on
PORTA |= (1 << PA0);

// switch other LEDs on which have been set as output
//...
PORTA |= (1 << PA1); 

The above code is being called from the 8-bit timer overflow interrupt handler. The timer is setup from main() as follows:

// set 8-bit timer
  cli();

  // set prescaler to 256
  TCCR0B |= (1 << CS02);
  // set overflow interrupt enable
  TIMSK0 |= (1 << TOIE0);

  sei(); 
\$\endgroup\$
  • 2
    \$\begingroup\$ Can you add a schematic of what you connected to the ADC0 pin. It sounds like you have connected two things to it (ADC input, LED output). This might be the problem. \$\endgroup\$ – Tom L. May 28 '14 at 5:22
  • \$\begingroup\$ I've added the schematic, Tom. Yes, I've connected the LED. But how else would I use the pin as input and output? \$\endgroup\$ – M-V May 28 '14 at 6:25
  • \$\begingroup\$ I can see that connecting the LM358 output directly to the LED is problematic. But that doesn't explain why the uC output to LEDs on other pins also stop working. Thanks \$\endgroup\$ – M-V May 28 '14 at 6:41
  • \$\begingroup\$ I know this is kind of old, but take a look at my question and the associated answer, as I am successfully using a pin as an output to drive LEDs and an input using the ADC. electronics.stackexchange.com/questions/66431/… \$\endgroup\$ – Kurt E. Clothier May 12 '15 at 4:59
1
\$\begingroup\$

There is a battle between the LM358 and the MCU pin so if the LM358 wins and it wants to output a fairly low voltage then the LED won't light up no matter how hard you try. I think a possible solution is to put a resistor (maybe 470 ohms) in series with the LM358 output. This will allow PA0 to win every time but the problem arises when the port is being used as an analogue input - will the LED and resistor affect the analogue voltage?

I'm not sure what voltage range you are using for your analogue input but if it's 1V maximum then you should be OK. Maybe OK even at 2V but above two volts the LM358 will be trying to drive the LED and losing voltage thru the 470 ohms I recommended you put in series originally.

If the Vmax for the ADC is less than 2V it should be OK and also add a 10nF cap down to ground on the input to reduce impedance increases from the 470 ohm resistor.

\$\endgroup\$
  • \$\begingroup\$ Thanks, Andy. Do you know why this is affecting other PORTA pins though, where I have LEDs? They don't work when I select AD0 as input, read ADC values, and then switch back the pin AD0 to output. For example, PA1 doesn't work anymore. \$\endgroup\$ – M-V May 28 '14 at 9:27
  • \$\begingroup\$ I don't know - possibly bad code! \$\endgroup\$ – Andy aka May 28 '14 at 10:16
  • \$\begingroup\$ Or maybe the current bottleneck that makes your pin go low when the op-amp drinks everything out can get its situated between port A and vcc pin in the attiny? Did you check vcc, by chance? \$\endgroup\$ – Nicolas D May 29 '14 at 8:08
  • \$\begingroup\$ @NicolasD I didn't understand what you said - was it meant for me? \$\endgroup\$ – Andy aka May 29 '14 at 8:28

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.