Arduino ADC affected by digital outs.

Hi, Im using a Duemilanove and Im having some problems.
I've connected a 5DOF IMU from sparkfun to analogs 0 to 4. Everything seems to work as expected, except when I turn on a led connected to pins 4 and 5 (or any others), the values read by analogs are off by like +10%. (im trying to determine the sensors bias)
The LED is a three color one, connected to 3,4,5,6 (4 is ground). If I turn 3 on, calculate the bias and then turn 3 off and turn 6 on (the led goes from red to green), the difference in analog inputs is the biggest. If i turn 6 on without turning 3 off, the difference is smaller. If I remove the led or dont turn it on, the values are correct.
Any idea why this is happening?
The IMU is connected to the 3.3v source. Im going to try setting the VREF external (to that 3.3v output) and see what happens. Maybe the led is making the voltage in the avr drop a little, creating bigger values.
Any idea? Thank you in advance!

Maybe the led is making the voltage in the avr drop a little, creating bigger values.

That's would be my first guess also. So either a well regulated external voltage reference applied to AREF or a better voltage regulator for the whole board to hold the Vcc tighter.

Lefty

It's a known issue with the ATMega chip itself.

After writing to any of the digital pins, you need to wait before reading the analog port as the switching noise throws off your values.

You'll have to experiment with actual timing, I think 20ms is what I was using but it's been a while. If you're calling the LED from a function, just insert a little delay(20) at the end, or just before any of the analogReads.

Thanks for your replies. I had left this issue aside but Im back at it again.
I tried adding a delay in between but that doesnt seem to help. The voltage drop is constant while the led is on, so it doesnt matter when you do it.
The analog reference is connected to the 3.3v that powers the IMU, so theoretically even if the voltage drops a little, the comparator should see the real ~3.3v. I don't understand.

On the other hand, apart from the led issues the IMU works great. I haven't tried implementing a Kalman filter yet but my functions mix gyros and accelerometers, keep track of the integrating drift and the gyro bias drift. Now I have a lot of tuning to do, but its promising. Im getting stable 0.1ยบ readings.
My the program right now can deal with the effect of the LEDs, but it needs a couple of seconds to adapt to the new bias, which is not bad if the led turns on only once, but a blinking led would throw my stability away.