An Imperfect Comparison of Arduino Analogue and Digital Inputs: UNO R3, Leonardo, UNO R4 Minima and UNO R4 WiFi

by kevinjwalters in Circuits > Arduino

365 Views, 3 Favorites, 0 Comments

An Imperfect Comparison of Arduino Analogue and Digital Inputs: UNO R3, Leonardo, UNO R4 Minima and UNO R4 WiFi

microcontroller-arduino-fourclassics-inputtesting.jpg

This article presents some testing of the analogue inputs and digital inputs on four similar-looking Arduino boards from the "classic family" shown above and listed below. An Arduino UNO R4 board featuring a digital-to-analogue converter (DAC) output was used as a signal generator and to orchestrate the tests.

  1. Arduino UNO R3 (top left) - ATmega328P microcontroller.
  2. Arduino Leonardo (bottom left) - ATmega32u4 microcontroller.
  3. Arduino R4 Minima (bottom right) - Renesas RA4M1 microcontroller.
  4. Arduino R4 WiFi (top right) - Renesas RA4M1 microcontroller.

These boards are all described as being 5V but the testing will show how the nuances of the R4 boards need to be understood for ADC and DAC use.

For comparison, Playduino's video showing an ADC accuracy comparison shows an external DAC being used for some thorough testing and analysis of analogue inputs. The DAC is powered by a laptop's USB port, its analogue output will still be subject to noise from that source. For this type of testing a low-noise power supply or battery power for the DAC is normally desirable.

The digital input testing presented here is ultimately flawed due to imperfections in the UNO R4 DACs which are likely to be related to the Arduino software. This article was originally intended to be part of Arduino Comparison in Detail: UNO R3, Leonardo, UNO R4 Minima and UNO R4 WiFi to provide an accurate measurement of any hysteresis on the digital inputs. It wasn't included as it failed to show the hysteresis. The reasons for the incorrect results are discussed here and may be of interest. Not publishing these "null results" could be viewed as form of publication bias and contributes to a false impression that everything always works for some people.

An unrelated limitation/risk of the SoftwareSerial library is also briefly covered.

Supplies

  1. At least two Arduino boards, the ones here are from the "classic" range.
  2. A small breadboard, a 10nF capacitor and some male-male jumper wires were used to connect the two boards.
  3. A power supply for the "board under test".
  4. A 9V PP3 battery for the signal generator board and an adapter to a 2.1mm barrel connector.
  5. A host computer for recording the output sent to the serial console over USB.

Testing Setup

arduino-input-comparison-r4minima-testing-r4wifi-oscilloscope-1-65.jpg

The ideal test equipment for testing the GPIO pins in input mode of an Arduino board is a high quality signal generator and a calibrated, data-logging voltage meter. The R4 boards use a Renesas RA4M1 microcontroller which features a 12bit DAC. This can be used to generate a signal between 0.0V and 5.0V which makes it a useful cheap way of testing another 5V microcontroller's analogue input in theory.

The setup can be seen above with the "signal generator" board at the top of the image and the "board under test" at the bottom. In this case the UNO R4 Minima is generating a triangle wave followed by a short duration square wave which shows up as a spike. The board under test is an UNO R4 WiFi. The yellow wire which would normally go to D5 has been temporarily borrowed for the oscilloscope. The oscilloscope's timebase is 5 seconds per division, the triangle wave is very low frequency.

The use of a breadboard and long wires will cause a tiny bit of noise to be introduced to the signal. This is likely to be irrelevant here but is important if the intention is to measure just the performance of the microcontroller's ADC.

Signal Generator (upper board)

The testing program generates 5 triangle waves by stepping through the 12bit DAC values (0 to 4095) then generates 5 short square waves. The photograph with the oscilloscope shows the end of the test with the final 2 triangle waves and the 5 square waves looking like a few spikes. The signal generator board is connected to a laptop to capture the serial console over USB output.

The signal generator changes the DAC value on pin A0 sends a request over a serial connection (D1 and D0) to the board under test asking it to read and respond with the analogue and digital values. A 10nF capacitor sits on the breadboard to act as a small reservoir to reduce the impact of any low impedance ADC sampling and filter high frequency noise to some degree.

The signal generator board is connected to a laptop's USB port and could be powered by that but the addition of a 9V PP3** battery connected to the barrel jack means the battery overrides the USB power. The battery appears superfluous but it has two effects:

  1. it may reduce the noise on the microcontroller's power supply which is likely to get transferred to the DAC output;
  2. it deals with the thorny issue of the diode-related voltage drop on the R4 boards when USB powered - without this there's an unfortunate effect of DAC output ranging from 0.0V to approximately 4.6V rather than the desired 0.0V to 5.0V.

Board Under Test (lower board)

The board under test program waits for a command sent over a serial port using D8 and D9. When instructed it reads the analogue value from A5 and then the digital value from D5 and sends the values as a response over the serial connection.

The board under test is powered by a USB power bank which has been recently charged. This is reasonably low noise power source. Instructables: Exploring and Reducing ADC Noise on Adafruit CLUE (Nordic NRF52840) shows how noise from the power supply can be a significant factor for ADC noise on some microcontroller boards.

** PP3 batteries have low capacity (around 500mAh) which limits their use for powering Arduino projects.

The Software

arduinoide-dac-adc-tester-src-screengrab1.png

The signal generator board was running the dac-adc-tester.ino program. The board under test board was running the ana-dig-reader.ino program. These were both compiled using the Arduino IDE 2.3.4.

The original inter-board communication was at a baud rate of 115200 using the SoftwareSerial library. This worked fine on the R4 boards but the UNO R3 and Leonardo would occasionally fail to transmit characters correctly in some way at this speed. This is a limitation of the software library. The baud rate was lowered to 38400 and the communication was then fine for this setup and short testing. Truly reliable communication can only be achieved with a more sophisticated protocol with error detection and correction and some form of flow control.

The frequency of the generated waves is dependent on the communication baud rate and how long the board under test takes to measure the analogue voltage. The digital input read should be extremely quick and not have any significant effect here.

Four Arduino Combinations

arduino-input-comparison-r4wifi-testing-r3-1-65.jpg
arduino-input-comparison-r4wifi-testing-leonardo-1-65.jpg
arduino-input-comparison-r4wifi-testing-r4minima-1-65.jpg
arduino-input-comparison-r4minima-testing-r4wifi-1-65.jpg

The four combinations are shown above and described below.

  1. R4 WiFi testing R3.
  2. R4 WiFi testing Leonardo.
  3. R4 WiFi testing R4 Minima.
  4. R4 Minima testing R4 WiFi.

A superior setup would be a fifth board as a dedicated, consistent signal generator but lack of resources precluded this.

The board on the left is the signal generator powered by both USB and a 9V PP3 battery. The board on the right is the board under test powered by a USB power bank. For use as a signal generator: the R4 WiFi 5V pin measured 4.994V on a multimeter; the R4 Minima measured 5.014V.

An astute viewer will notice a 330 ohm resistor on the R4 boards between 5V and GND. Without this the low current consumption of the R4 boards triggers the USB power bank's automatic power off.

It's good practice to have either both boards powered or both boards unpowered. Data connections between boards can create a risk of an unpowered board being back powered via GPIO. An asynchronous serial protocol which uses a low quiescent state reduces the risk of this.

Test Results - All the Data

arduino-input-comparison-v1-g1.png

The complete output from the four tests is show in the plot above. Some observations:

  1. The waveforms do not all align. The R4 Minima testing the R4 WiFi is the fastest, followed by the R4 WiFi testing the R4 Minima and the R3 and Leonardo are identical. A difference between the R4 boards and the R3/Leonardo is expected but the difference between the R4 boards when their roles are reversed is mysterious.
  2. Both R4 boards under test do not reach 5V they peak around 4.7V flattening the top of the triangle wave. This is showing an important difference with the R4 boards, a schottky diode is used between the USB power and microcontroller power causing a voltage drop of around 0.3V. This means the ADC range is only 0.0V to 4.7V assuming a voltage reference isn't used.
  3. The triangle waves have a far longer period than the square waves. This was intentional.

The voltages on the 5V pin of the board under test were checked with a multimeter and are shown below. These values were used for the plotting to translate the ADC values to the correct voltage.

  1. UNO R3: 5.023V
  2. Leonardo: 5.040V
  3. R4 Minima: 4.730V
  4. R4 WiFi: 4.690V

Test Results - Normalised Time

arduino-input-comparison-v1-g2a.png
arduino-input-comparison-v1-g2b.png
arduino-input-comparison-v1-g2c.png

The plots here have time as a percentage of the test output to normalise the time and allow the values to be more easily visually compared. The calculated DAC voltage is shown as a thin black line. The top of the last triangle wave is shown zoomed-in and without the DAC voltage line to reveal the noise. The values appear to be similar spaced between the boards suggesting the noise is similar. There are no distant outliers which is a good thing.

One surprise here for the uninitiated is the 14bit ADC on the R4 boards has similar noise performance to the 10bit ADC on the other two boards despite having 4 extra bits. The sample rate is far higher on the Renesas RA4M1 which may permit some averaging to get a more accurate result in the same or less time than the ATmega328p or ATmega32u4. There will also be some noise on the DAC output which may or may not be significant.

Test Results - Voltage Difference Distribution

arduino-input-comparison-v1-g3.png

This shows the distribution as a violin plot with a boxplot and the arithmetic mean shown as a grey triangle. This appears to confirm the ADCs have similar performance. The standard deviations are very similar which is suspicious. Some or most of this noise could be coming from the DAC on the signal generator board or the long wires connecting the boards. It would be useful to compare these results with those from using an external DAC with a low-noise power supply. Measurements of the constant voltage from some batteries would be great too.

Test Results - Digital Inputs

arduino-input-comparison-v1-g4a.png
arduino-input-comparison-v1-g4b.png
arduino-input-comparison-v1-g4c.png

The digital inputs are shown alongside the analogue values here. This allows the voltage to be seen where the digital value changes from low to high as the voltage rises and high to low as it falls. There are two extra plots zooming in on the transitions with identical y scales.

  1. The voltage for digital values is clearly different based on the microcontroller, the two R4 boards are near identical as they both use the Renesas RA4M1.
  2. The voltage appears to be identical for rising and falling, this suggests there is no hysteresis or a very small voltage difference that the plotting doesn't clearly show.

The approximate values for the digital threshold is shown below.

  1. R3: 2.45V
  2. Leonardo: 1.58V
  3. R4 Minima & R4 WiFi: 3.03V

The ATmega Hysteresis Mystery

arduino-forum-johnlincoln-r4dacsiglentscope-1.png
atmega328p-digital0.png
atmega328p-digital1.png

The ATmega328P datasheet has plots (shown above) of the rising and falling threshold voltages for its digital inputs for varying supply voltage and temperatures. The manual testing in Step 7: GPIO - Digital Input Threshold Voltage of Arduino Comparison in Detail: UNO R3, Leonardo, UNO R4 Minima and UNO R4 WiFi using a potentiometer confirmed the ATmega chips do have hysteresis on the digital inputs, particularly the ATmega328P on the Arduino UNO R3. This leaves a mystery of why this automated testing using the UNO R4 DAC does not show the hysteresis.

Creating a waveform using analogWrite() on the Arduino UNO R4 DACs produces very noisy output. There are spikes when the DAC output is set which appear to be roughly towards the midpoint voltage. These spikes can defeat a schmitt trigger which provides the hysteresis. These brief spikes may not be very apparent on all oscilloscopes without zooming in by setting a shorter timebase. John Lincoln kindly provided a clear view of these spikes from a 10Hz sinewave from an R4 DAC on a Siglent oscilloscope in the Arduino forums, shown above.

Going Further

6c1ac882-ef5b-4dcf-ac64-60e0e613d0e8.jpg

Ideas for areas to explore:

  1. Some similar testing using an external DAC board powered by a low-noise power supply.
  2. Carefully compare the 14bit ADC on the UNO R4 boards to the 10bit ADC on the UNO R3 and Leonardo to see if the extra bits provide a useful increase in accuracy (cf ENOB) or are just useless noise.
  3. Add an external ADC for a "second opinion" on the output voltage. The ADS1115 is commonly used by hobbyists and has good library support. Be careful to purchase a genuine Texas Instruments ADS1115 one.
  4. Check the contribution of noise to DAC and ADC from
  5. USB power,
  6. USB power with additional battery power to 2.1mm barrel connector,
  7. power bank power,
  8. battery power to 2.1mm barrel connector,
  9. the spikes currently produced using analogWrite() on the R4 DAC outputs.
  10. Check any other ADC modes or settings available on the R4 boards (Renesas RA4M1 microcontroller). Explore any hardware approaches for improving accuracy of ADC values. Look at performance of mean and interquartile mean in software in terms of cpu time and accuracy improvement.
  11. Explore the input impedance on the ADC and how this can affect measurements. The Renesas RA4M1 appears to have a much lower input impedance than the AVR ADCs. The input impedance can vary on some microcontrollers based on the configurable duration (window) for sampling.
  12. Investigate the curious difference in duration when the roles are swapped for the R4 WiFi and R4 Minima. They have the same microcontroller and the duration would be expected to be the same. This could either be a difference in performance on the signal generator board or the board under test or both. That could be due to hardware or software libraries - latter is more likely.

Further reading:

  1. Digital inputs and hysteresis
  2. Electronics Weekly: Not all Arduino inputs are equal - hunt the Schmidt trigger
  3. Inacio, Pedro & Guerra, Rui & Stallinga, P.. (2022). An Ultra-Low-Cost RCL-Meter (Sensors 2022) - in-depth look at making an LCR meter with an Arduino UNO R3 and Arduino Leonardo - this relies on the voltage transition levels on the digital inputs.
  4. Analogue inputs
  5. Element 14: How Do ADCs Work? - The Learning Circuit (YouTube)
  6. Instructables: Exploring and Reducing ADC Noise on Adafruit CLUE (Nordic NRF52840) and ADC Analysis page of Adafruit Learn: CLUE Metal Detector in CircuitPython
  7. Andreas Spiess: #340 How good are the ADCs inside Arduinos, ESP8266, and ESP32? And external ADCs (ADS1115) (YouTube)
  8. Playduino: ADC accuracy comparison (YouTube) - a thorough comparison and analysis of the ADCs on Arduino UNO R2 (not a typo, very similar to R3), UNO R4 Minima, Pi Pico and ESP32 using an external Analog Devices AD5693R DAC and a TI ADS1115 ADC for comparison. The ADS1115 oddities appear to be due to an issue with the serial communication used to transmit the values, perhaps buffer size issues or lack of flow control.
  9. Adafruit Forums: Feather ADC comparison including 2.6V limited ESP32-S2 - a basic comparison of the ADCs in the RP2040, ESP32-S2, ESP32, ATMega328p, nRF52840, SAMD21.
  10. Instructables: Measuring Small Voltages With Pi Pico ADC and Comparison With Microchip MCP3208 Using Cytron EDU PICO
  11. Microchip: Techniques that Reduce System Noise in ADC Circuits (Analog Design Note ADN007) - a look at selecting external components (pdf).
  12. Open Music Labs: ATmega ADC | ATmega ADC in-depth analysis
  13. Stargirl Flowers: Getting the most out of the SAM D21's ADC
  14. STMicroelectronics: How to get the best ADC accuracy in STM32 microcontrollers (Application Note AN2834) (pdf)
  15. Stack Exchange: Use Cases for an External ADC
  16. GitHub: arduino / ArduinoCore-renesas
  17. Calls to digitalWrite() don't work after a previous analogWrite() #58
  18. analogWrite on R4 boards generates spikes #460
  19. DAC use
  20. Arduino Project Hub: Arduino Due Arbitrary Waveform Generator by Bruce Evans
  21. Adafruit Learn: Making oscilloscope images with DACs
  22. Arduino Forum
  23. Hairy output on R4 DAC
  24. Spikes on UNO R4 DAC output using analogWrite()
  25. Arduino Music and Audio Projects book by Mike Cook
  26. GitHub: TriodeGirl / Arduino-UNO-R4-Minima-Fast-DAC - an example of UNO "R4 Minima barebones direct-register code to use the DAC12 module in the RA4M1 processor"
  27. Serial connections
  28. Microcom Networking Protocol - example of error-correcting serial protocols on dial-up modems.