Introduction to Arduino ADC :

logo_YT.jpg

Analog to Digital Converter (ADC) is a very useful feature available with Arduino boards.  The Analog input voltage is converted propotionally to 10 bit Digital form, i.e., 0 to 1023 in decimal format.

ARDUINO_ADC_TN_WIX.jpg

Reference Voltage: The default referernce voltage (analog) is set to Vcc of the board, i.e., 5V or 3.3V.   But, it can be changed to a required value (input voltage, which shall be less thant the Vcc of the MCU) and the sketch has to be set for the external input voltage reference.  The reference voltage may also be set to built-in, fixed, internal reference voltage and the sketch is to be updated accordingly.

Input Voltage: The input voltage is the analog voltage, that is to be measured and converted to Digital form, which shall not be more than Vcc of the Arduino board.  The input voltage shall be positive only, starting from 0V (ground reference of the Arduino board).  The ADC of the Arduino cannot measure negative voltage, i.e., voltage less than ground voltage of the Arduino board.

ADC Register : The ADC register holds the 10 bit digital data, after performing the ADC operation.  The data is available for reading until next ADC operation.  So, the digital data has to be read / saved to the user variable, if required, before performing next ADC operation.

Playing with Arduino ADC :

Measuring Analog Voltage with DEFAULT settings : The Arduino board is set to DEFAULT for Analog Reference voltage, by default (unless changed), which is same as Vcc voltage of the board. i.e., 5V or 3.3V, depending on the Arduino board.  

So, following statement in the sketch may be added to set the Analog reference voltage as Default, which is optional.

analogReference(DEFAULT); // may be used once in the setup()

Then, call the following statement to convert the Analog voltage to 10 bit Digital format, and store the value in ADC Register.

analogRead (ADC_PIN); // read analog voltage from the specific ADC pin, and saves digital value in ADC reg.

To save the digital value, assign it a variable, as shown below.

int var = analogRead (ADC_PIN); // returns digital value

Now, the var value is set in between 0 to 1023, where 0 indicates 0 Volts and 1023 is equal to Vcc voltage.

So, to summarize the above steps, to read an analog voltage (shall be less than Vcc of the Arduino board) from one of the ADC pins ( A0 or A1 or A2 ...)

int adc_val; // to hold adc (digital) value

#define adc_pin A0 // select one of the ADC pins  

void setup ()

{

   Serial.begin(9600); // initiate Serial Communication

   analogReference(DEFAULT); // optional

}

void loop ()

{

   adc_val = analogRead (adc_pin); // or A0 or A1 . . .

   Serial.println( adc_val ); // display the value on serial monitor

      delay ( 500 );  // wait for 500 milli sec

}

However,  discard some initial readings, which may not be accurate.

The ADC pins on Arduino Uno and Arduino Nano are shown below as an example.  The POSITIVE input voltage to be read, shall be connected to one of the Analog pins ( A0 / A1 / A2 / . . .)  and NEGATIVE input voltage shall be connected to any GND pin marked on the Arduino board.

ADC_PINS_SMALL.jpg
ARDUINO_ADC_EXT_WIX_SMALL.jpg

Measuring Analog Voltage with EXTERNAL Reference Voltage : The Input Voltage may be measured in a range required (of user's choice) like 0 to 2.0V for more accuracy, instead of default Vcc voltage reference.  This concept also avoids inaccurate Vcc voltage reference (eg Vcc varies from 4.75 to 5.25 V), and precision ADC conversion is obtained with regulated voltage reference.

The concept of Analog to Digital Covertion (ADC) is shown as schematic above.

So, following statement in the sketch may be added to set the Analog reference voltage as EXTERNAL, which is mandatory.  The external reference voltage shall be connected across AREF pin (positive) and GND pin Inegative), before using the following statement. 

analogReference(EXTERNAL); // may be used once in the setup()

Then, call the following statement, as earlier to convert the Analog voltage to 10 bit Digital format, and store the value in ADC Register.

analogRead (ADC_PIN); // read analog voltage from the specific ADC pin, and saves digital value in ADC reg.

To save the digital value, assign it a variable, as shown below.

int var = analogRead (ADC_PIN); // returns digital value

Now, the var value is set in between 0 to 1023, where 0 indicates 0 Volts and 1023 is equal to voltage at AREF pin.

So, to summarize the above steps, to read an analog voltage (shall be less than Voltage at AREF pin of the Arduino board) from one of the ADC pins ( A0 or A1 or A2 ...)

int adc_val; // to hold adc (digital) value

#define adc_pin A0 // select one of the ADC pins  

void setup ()

{

   Serial.begin(9600); // initiate Serial Communication

   analogReference(EXTERNAL); // mandatory and connect reference voltage across VREF pin (+ve) and GND pin (-ve) before switch on.

}

void loop ()

{

   adc_val = analogRead (adc_pin); // or A0 or A1 . . .

   Serial.println( adc_val ); // display the value on serial monitor

      delay ( 500 );  // wait for 500 milli sec

}

Here also,  discard some initial readings, which may not be accurate.

ARDUINO_ADC_INT_WIX_SMALL.jpg

Measuring Analog Voltage with INTERNAL Reference Voltage : The Input Voltage may be measured with an internal fixed voltage available in the micro controller.  This is also a good choice for accurate measurement, but some fixed voltage only possible to select.  This concept avoids connecting any external power supply and reduces external wiring also.

The concept of Analog to Digital Covertion (ADC) using internal voltage reference, is shown as schematic above.

So, the following statement(s) in the sketch may be added to set the Analog reference voltage as INTERNAL, which is mandatory.  The various internal reference voltage are available based on the selected Arduino board.  Use one of the statements matching to the selected Arduino board

// mandatory, may be used once in the setup()

analogReference(INTERNAL); // = 1.1V on the ATmega168 or ATmega328P and 2.56V on the ATmega32U4 micro-controllers

 

analogReference(INTERNAL1V1); //1.1V for ArduinoMega

analogReference(INTERNAL2V26); // = 2.56V for ArduinoMega

(Please refer Arduino Data sheets, to select internal reference voltages of other Arduino boards or micro-controllers)

So, to read an analog voltage (shall be less than the set Internal Voltage, from one of the ADC pins ( A0 or A1 or A2 ...), the Arduino sketch shall be,

int adc_val; // to hold adc (digital) value

#define adc_pin A0 // select one of the ADC pins  

void setup ()

{

   Serial.begin(9600); // initiate Serial Communication

   analogReference(INTERNAL); // mandatory and select one of the statements as explained above

}

void loop ()

{

   adc_val = analogRead (adc_pin); // or A0 or A1 . . .

   Serial.println( adc_val ); // display the value on serial monitor

      delay ( 500 );  // wait for 500 milli sec

}

Here also,  discard some initial readings, which may not be accurate.

Making a simple Voltmeter :

Making a Voltmeter using Arduino:  Any one of the ADC input may be used to make a voltmeter, and the result may be viewed on the screen using Serial Monitor of Arduino IDE.  Here, Arduino Uno board is used for making Voltmeter ranging from 0 to 5V (maximum).  Follow the required circuit and sketch as shown  below 

int adc_val; // to hold adc (digital) value

double adc_volts; // to hold adc (digital) value

#define adc_pin A0 // select one of the ADC pins#   

#define max_volts 5.0 // set max volts  

void setup ()

{

   Serial.begin(9600); // initiate Serial Communication

   analogReference(DEFAULT); // upto 5V

   adc_val = analogRead (adc_pin); // discard reading(s)

}

void loop ()

{

   adc_val = analogRead (adc_pin); // read from A0

   adc_volts = (max_volts*adc_val)/1023; // calculate volts

   // display the value(s) on serial monitor

   Serial.print( "ADC VALUE : ");

   Serial.print( adc_val );

   Serial.print( "  ->  VOLTS : ");

   Serial.println( adc_volts );

      delay ( 1000 );  // wait for 1 sec

}

SimpleVoltmeter_Circuit_SMALL.jpg

Making a Voltmeter using LCD :

Making a Voltmeter using Arduino and a LCD display:  Here also one of the ADC input is used, but the values are displayed on a LCD,  so that, the circuit is independent of your laptop or PC, after uploading the sketch to the Arduino.  The circuit is shown below for 10V / 25V Voltmeter and use contact form for complete sketch / soruce code

Voltmeter_Circuit.jpg

Select the Series Resistor, RS, as per the Voltage Range to be measured.  Then, connect a known DC power source (eg. 5V or 9V battery), and adjust the trimpot, until correct voltage value is displayed on the LCD. Now, your Arduino Voltmeter is ready to use.