2

I'm reading two digital inputs and one analog input. For [reasons] I must use A0 and A1 as digital ins, and A2 as an analog in.

I'm uncertain about how the Arduino hardware deals with mixing analog and digital inputs on the same port. For speed, I'm reading like this:

 uint8_t raw = PINC;
 boolean digitalA = (bool)(raw & 1);  // read A0
 boolean digitalB = (bool)(raw & 2);  // read A1
 int analogC = analogRead(A2);

Will the raw reading of the port interfere with the analogRead in any way? I did see a caveat about mixing A & D, but it only had to do with noise introduced in the analog ins when doing digital outs. Would reading analog first minimize that?

  • Ah, I see what you are asking. Instead of using individual pin reads you are simply reading the entire set of Analog pins (0-5) then using an analogRead on pin 2. I don't think this should be a problem. But you may want to try using individual pin reads to see if it makes any difference. – st2000 Oct 7 '17 at 15:51
  • @st2000 - Thanks. The concern is that performing a raw read may disrupt the ADC process. I'm coding in advance of having equipment available, that's why I'm asking about this rather than just experimenting. The actual code is more complicated than the simple example here. – Jim Mack Oct 7 '17 at 16:30
3

What you are trying to do is perfectly safe. In fact, you reading PINC will have no effect whatsoever on the input circuitry. You are just reading an I/O register and, whether you read it or not, this register is updated on every clock cycle from a set of comparators that monitor the pin voltages.

Just for completeness, I should mention that it is possible to disable the comparators that update the register. The Atmel datasheet recommends to disable them if you want the lowest possible ADC noise. If you follow that recommendation, then reading PINC will just return zero. The Arduino core library does not disable them: the input comparators are thus always active, and you can get a meaningful value by reading that digital input register.

Edit: You can selectivly disable the input comparator on the pin A2 by issuing DIDR0 |= _BV(ADC2D);. But that is only useful if you really need to maximize the accuracy.

  • I agree, it is perfectly safe. The AVR microcontrollers have different kind of internal hardware parallel on the same pin. The digital part and the analog part are default both active and can both be used at the same time. They use the same pin, that's all, there is no internal mix. – Jot Oct 7 '17 at 20:54
1

In general, most embedded processors multiplex external pins into specialized internal circuits. Circuits which check standard fixed threshold crossings (digital inputs), circuits which check variable threshold crossings (comparators) and circuit which combine comparators with a variable settable reference (analog to digital converter) to name only three.

Usually a pin is multiplexed to a particular internal circuit only once during initialization. Switching between internal circuits as part of normal operation is tricky and can lead to unexpected results.

Analog reads are often problematic because of noise. However, it is unlikely that reading the state of a digital input will effect the conversion of the ADC circuit.

It would be far more effective to read the actual processor's application note you are using and understand that processor's ADC issues. You may be surprised that the number is significant bits (usable bits) are less than the number found in the ADC register. Or that you can increase the number of significant bits simply by adding averaging to your software.

  • What part of initialization maps an analog pin to a digital input? As far as I can tell, using pinMode() to set A0..A5 as input doesn't specify how the input will be read from that pin -- that's determined by analogRead() vs digitalRead(), not fixed at init time. Yes? And I'm not using digitalRead() but raw port access, hence the question. – Jim Mack Oct 7 '17 at 16:29

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.