Skip to content

ADC Gain

This setup shows how to configure the ADC (AREF & GAIN) and DAC

Sketch

/* This setup shows how to configure the ADC (AREF & GAIN) and DAC
 *
 * The default setup of the Autonomo is: 
 * 1) Internal analog reference of 1.65V, half VCC (INTVCC1 = 1.65V or half VCC)
 * 2) Gain of division of  2
 * The command to get the default settings back is: "analogReference(AR_DEFAULT);"
 * 
 * Read/Write resolution:
 * To change the resolution of the analog read or write you should write: 
 *  "analogWriteResolution(10);"
 *  "analogReadResolution(12);"
 *  The maximum resolution is: 10 for writing and 12 for reading.
 * 
 * DAC:
 * The DAC is Digital to Analog. The DAC is a fix value between 0-3V3.
 * On the Autnomo, the DAC is working just on A0
 * 
 * Written by Itay Dagan and Gabriel Notman
 * Date: 5/1/16
 * 
*/

void setup()
{
  // Set bit resolution explicitly
  analogWriteResolution(10);
  analogReadResolution(12);

  // A reference signal on DAC0 (A0) 255 ~= VCC / 8
  //analogWrite(A0, 128);
}

void loop()
{
  // Set default reference voltage and gain
  analogReference(AR_DEFAULT);

  uint16_t defaultReading = analogRead(A1);


  // Set custom reference and gain
  // Valid values:
  // ADC_INPUTCTRL_GAIN_1X_Val
  // ADC_INPUTCTRL_GAIN_2X_Val
  // ADC_INPUTCTRL_GAIN_4X_Val
  // ADC_INPUTCTRL_GAIN_8X_Val
  // ADC_INPUTCTRL_GAIN_16X_Val
  // ADC_INPUTCTRL_GAIN_DIV2_Val

  ADC->INPUTCTRL.bit.GAIN = ADC_INPUTCTRL_GAIN_DIV2_Val;


  // INTVCC1 = 1.65V or half VCC
  // Valid values:
  // ADC_REFCTRL_REFSEL_INT1V     1.00V
  // ADC_REFCTRL_REFSEL_INTVCC0   2.23V
  // ADC_REFCTRL_REFSEL_INTVCC1   1.65V
  // ADC_REFCTRL_REFSEL_AREFA     Voltage on AREFA (AREF Pin)
  // ADC_REFCTRL_REFSEL_AREFB     Voltage on AREFB (A3 Pin?)

  ADC->REFCTRL.bit.REFSEL = ADC_REFCTRL_REFSEL_INTVCC1;
  delay (1000); //should give the ADC a little bit time to save and apply the settings  

  uint16_t newSetting = analogRead(A1);

  SerialUSB.println(String("Reading with default setting: ") + defaultReading);
  SerialUSB.println(String("Reading with the new setting:: ") + newSetting);

  delay(1000);
}