SAMD21 ADC Configuration

What is the best way to configure the ADC to have one pin for positive polarity input and another for negative polarity input, and then read both pin values using DMA, if possible? The code I am using below outputs a single differential value, however, I would like to different values, one from each pin.

Here is the code I have so far:

  // configure the ADC for differential mode
  analogReadResolution(12); // 12-bit resolution

  ADC->CTRLB.bit.DIFFMODE = 0x1;
  ADC->CTRLB.bit.FREERUN = 0x1;
  ADC->INPUTCTRL.bit.MUXPOS = 0x02; 
  ADC->INPUTCTRL.bit.MUXNEG = 0x03;
  ADC->INPUTCTRL.bit.GAIN = 0x0;
  ADC->AVGCTRL.bit.SAMPLENUM = 1; // No averaging

  // disable the internal VREF
  ADC->REFCTRL.bit.REFSEL = 0; // set the reference selection to 0 (internal VREF off)

  ADC->CTRLA.reg = ADC_CTRLA_ENABLE; // enable the ADC

The input voltage cannot be negative with respect to GND. Do you mean a positive voltage on a differential pair, to be subtracted from the reading on the other pin?

I would like to different values, one from each pin.

It is not clear what this means. It sounds like what you really want are individual readings from each pin, not a differential measurement.

According to the SAMD21 data sheet, only one reading at a time results from an ADC conversion cycle. That is either one single ended measurement or one differential measurement.

1 Like

Why do I get negative values as well as positive values from the differential ADC output? Also, the code indicates both positive and negative with the following lines:

ADC->INPUTCTRL.bit.MUXPOS = 0x02; 
 ADC->INPUTCTRL.bit.MUXNEG = 0x03

That is the expected result when the voltage applied to the positive differential input is less then the voltage applied to the negative differential input.

The description of the ADC function in the data sheet is very clear and worth reading.

Hmmm.... I just applied a negative voltage to the negative pin and got back an ADC value that corresponds to the input voltage.

While you can do that the range is very limited as I believe the pins are rated for about -0.5v in reference to ground minimum. The ADC itself will also lose the ability to give you values if you go too far below, and at about -3.3v your board would crash. Do not connect negative voltages to your board pins, you’re risking damaging it. If you really want to try it out don’t use arduino zero with onboard debugger but go for a more barebones and cheaper board.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.