2021-11-17

1. ADC on the ATmega328p

Put this on your Arduino Nano:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
   Analog to Digital Converter
   ECE 322
   dan.white@valpo.edu

   Platform: ATmega328P  (Nano, Uno, ...)
*/

// Translate Arduino voltage reference symbols
// to more descriptive names for the platform
#define VREF_VSUPPLY DEFAULT
#define VREF_INTERNAL_1V1 INTERNAL
#define VREF_EXTERNAL_AREF EXTERNAL


int value0 = 0;
int value1 = 0;


void setup() {
  // UART:  115200 baud, 8N1
  Serial.begin(115200);

  // Uncomment ONE only
  analogReference(VREF_VSUPPLY);
  //analogReference(VREF_INTERNAL_1V1);
  //analogReference(VREF_EXTERNAL_AREF);
}

void loop() {
  // Switch the analog mux,
  // trigger a conversion,
  // return the result

  // we do 2 reads to clear the pipeline and get an accurate result
  value0 = analogRead(A0);
  value0 = analogRead(A0);

  value1 = analogRead(A1);
  value1 = analogRead(A1);

  // print the results to the Serial Monitor:
  Serial.print("1023 ");
  Serial.print(value0);
  Serial.print(" ");
  Serial.print(value1);
  Serial.println();

  // Wait to slow down the effective sample rate.
  delay(100);
}

Compile and upload the above code to an Arduino Nano and then look at the data in the Serial Monitor.

Close the Serial Monitor and view the same UART data using MobaXterm.

Close MobaXterm to release the COM port, then start the Arduino Tools -> Serial Plotter. It plots space/tab separated values on a plot. Super convenient to watch real-time values!


AD2 -> Nano connections:

  • W1 -> AREF

  • W2 -> A0

  • Jumper AREF to A1

Use the Waveforms template adc-aref.dwf3work.

W1 slider is the AREF pin.

W2 slider is the A0 analog input.


Open up the ATmega329P-datasheet to section § 24 Analog-to-Digital Converter, page 264. Review the Overview section then turn to Figure 24-1 for the ADC peripheral’s block diagram.

The original code selects VREF_VSUPPLY, labled as AVCC in Figure 24-1. This selects the appropriate multiplexer path and also turns on the MOSFET controlled by internal signal REFS0.

Remember that chip pin AREF was connected to Wavegen output 1, so there is now a path for current to flow between the AVCC and AREF pins through the internals of the ADC peripheral.

Section § 25.5.2 ADC Voltage Reference on page 252 discusses this issue, saying "If the user has a fixed voltage source connected to the AREF pin [Wavegen output 1], the user may not use the other reference voltage options in the application, as they will be shorted to the external voltage."

  1. in other words: your combination of code and hardware connections can get you into potential trouble.

Disconnect W1 from the AREF pin (now you know why). See how things make more sense. But now, the reference is just fixed to the power supply.

Since W2 can output 5V, the power supply from USB is likely to be slightly less than 5V. Therefore the output value saturates at 1023 slightly before W2 reaches 5V. This is as expected!


Select VREF_INTERNAL_1V1.

Now the W2 voltage ranges between 0 and 1.1 V for the ADC value to go 0 to 1023.

This also is as you would expect from an ADC.


Select VREF_EXTERNAL_AREF.

Connect W1 to AREF pin again.

  • W2 -> 1.0 V

  • W1 -> 5.0 V

Expected conversion value is:

(1.0V / 5.0V) × 1023 = 204.6 (round to integer)