secrets of arduino atmega328p adc

In this tutorials i'm gonna tell you all about the adc present in arduino famous boards like UNO,NANO,MINI....the atmega328p chips consist of the "10-bit resolution adc" inside the uC.

The type of adc is used in is Successive approximation ADC.these are kind are slow but gives good resolution though.let's dig in registers of adc in atmega328p.

To read analog input we need The reg "ADMUX". The adc reg have a Pin called AREF (adc reference voltage). The gives 10-bit number i.e 0-1023, 0-0V & 1023-5V.

it measures using this formula   

 Vin = (Analog Ref * analogRead() ) / 1024

we need stable AREF voltage we can use internal 1.1V reference or we need to provide a stable voltage anywhere between 0.3V-5V externally.

[NOTE: if ur are planning to provide external voltage to aref pin present on arduino uno,nano,mini, the recommended value is less than 5v to be safe on safe side cause the voltage of external 5v source may not be 5v exactly so if it is even few decimals more many times it can damage the uc so provide a source less than 5v and less current possible]


depending on the adc ref voltage ur Vin values can change i recommend the internal reference voltage cause no matter what if u powering the arduino with the internal voltage provides stable 1.1V. To enable it, we need two registers REFS1, REFS0



if REFS0 is "1" then we can select vcc and internal reference, if REFS1 is "1" then internal reference 1.1V.
we need to write to ADMUX register in order to enable it  we need to write these following.

REFS0        REFS1        functioning

0                        0                AREF is enabled from external source

0                        1                VCC is taken as AREF

1                        1                   internal 1.1V is AREF 

if we use the "01" option we get vcc as reference if ur are powering the Arduino with battery  then the voltage changes and voltage regulator also changes it's values  even with wall power adapter it happens so use internal reference voltage.

"i made a post on making a +-0.1 accurate voltmeter with these technique check it out". The next is power hack....to controll it we need to access the "ADCSRA" register.
the ADCSRA contains ADEN & ADSC registers if they are "11" then the adc conversion starts. if the conversion is completed then ADSC is written "0" again itself. The values of conversion are stored in ADCH and ADCL registers.

by controlling the ADEN we can save power i made a low power consumption arduino in this post check it out. 

Next is the sample rate hack.......we can the sample rate value of adc .the max accuracy is achievable at sample rate clock of 50KHz - 200KHz in atmega328p. The change can be made using the ADCSRA register using a prescaler division.

The ADC clock of Atmega328P is 16 MHz divided by a ‘prescale factor’. The prescale is set by default to 128 which leads to 16MHz/128 = 125 KHz ADC clock. Since a single conversion takes 13 ADC clocks, the default sampling rate is ~ 9600 Hz.


in order to check the default sampling time use the following code.check out these awesome additional info on many more. and this one

code:
byte PS_16 = (1 << ADPS2);
byte PS_32 = (1 << ADPS2) | (1 << ADPS0);
byte PS_64 = (1 << ADPS2) | (1 << ADPS1);
byte PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);

long starttime, interval;

void setup() {
 // ADCSRA &= ~PS_128;

 // ADCSRA |= PS_64;

  Serial.begin(9600);

  pinMode(A0, INPUT);

}

void loop() {

  starttime = micros();
  for (int i = 0; i < 100 ; i++) {
    analogRead(A0);
  }
  interval = micros() - starttime;
  Serial.println(interval);
  // put your main code here, to run repeatedly:

}

Comments

Popular posts from this blog

"Kernel Panic” error After upgrading Parrot OS

HACK HC-05(BLUETOOTH MODULE) FRIMWARE INTO HID FRIMWARE

1 Best way to add members to Telegram group(2021 using python)