banner



How To Setup The System Clock Registers For Freedom Boards

Arduino ADC: If y'all desire to easily find out about your Arduino ADC and then you lot need this folio. Find out how information technology works and how to use it for best results.

The Arduino ADC or Analogue to Digital Converter takes an input voltage and converts information technology into a digital value. With the standard setup yous tin can measure out a voltage between 0V and 5V with a resolution of 4.9mV then you can get a lot of item when measuring counterpart voltages.

Arduino Analog Pins

There are 6 pins on the Arduino Uno (shown below A0 ~ A5) that can be selected for an ADC measurement; A multiplexor feeds one of the vi analogue input pins into the ADC.

To read an analogue voltage from pin A4 you lot utilise the following code and function:

int val;
val = analogRead(A4);

Setting of the multiplexor is washed in that part for you automatically.

arduino uno analogue pins

Arduino ADC Specification

  Parameter
Arduino Uno/Nano
  Voltage Supply (Vs)
 1V8 ~ 5V5
  Interface
Built in
  Resolution
10 bit
Accented Accuracy (Including INL, DNL,
  quantization mistake, gain & offset error)

2 LSB [1]
  Offset mistake (ADC, DAC) [one]
2 LSB
INL [1]
0.50 LSB
DNL [one]
0.25 LSB
Proceeds mistake [1]
2 LSB
  Sampling frequency
ix.6Hz [two]
  Operating temperature
-twoscore°C ~ 85°C

            [1] for 4V functioning and 200kHz ADC sampling clock.
            [2] for 5V performance and 125kHz ADC sampling clock (Encounter below).

Arduino ADC size

The Arduino ADC has a 10 scrap converter, and that ways there are 1024 distinct values that can be returned as a result from the ADC:

since  prisoner of war(2,ten) = 2^x = 1024

Divide by 1023 or 1024?

In that location is always some confusion about whether to divide by 1024 or 1023 to get the voltage value for each bit.

However the ATMega328P datasheet gives the following formula:

ADC= (Vin*1024)/Vref

re arranging that gives:

Vin = (Vref/1024)*ADC

Arduino ADC resolution at 5V

So for Vref=5V, an ADC value of one would result in a Voltage step of iv.88mV - the value of voltage for one LSB - this is the Arduino ADC resolution for a 5V Vref.

Note however that the maximum ADC value is 1023 and then the maximum ADC value that tin ever be reported is:

1023 * (v/1024) = iv.9951V

As information technology states in the datasheet:

"0x000 represents analog basis, and 0x3FF represents the selected reference voltage minus one LSB."

The reason that you lot volition encounter the wrong equation on the web is so that the output "feels" right i.e. 1023*(5/1023) = v.000. This is the wrong equation to utilise and means there is an offset added to all values.

How the Arduino ADC works

This ADC is known equally a successive approximation ADC and requires several clock cycles to zoom in on the right ADC output.

The ADC converter compares the input analogue voltage to a portion of the Vref voltage using a divide past two sequence. The sample and hold capacitor is charged to the input voltage and and then the input asunder and so that the aforementioned voltage is measured throughout the conversion process.

It first checks whether the input voltage is higher or lower than half of the Vref voltage, by using a DAC to generate half the reference voltage. The DAC voltage is the fed into a comparator.

The output of the DAC forms the high bit of the result (stored in a shift register). If the input voltage is higher then the scrap is one, otherwise the chip zero.

If the input is lower than one-half of the Vref voltage and then control logic generates a DAC voltage that is 1/iv the reference voltage. The comparison is made again and this forms the adjacent flake in the ADC output.

The process continues until all the bits are collected.

ADC clock

For the Arudino the conversion process takes 13 cycles of the ADC clock - which you lot set up using a prescaler in the ADC module. The ADC clock must be between 50kHz and 200kHz so you choose the prescaler value to get a valid ADC clock.

The ADC clock prescaler can be set every bit a 2n segmentation from 2 to 128. You obviously want the fastest conversion charge per unit for the clock in employ and then for a 16MHz system clock you would summate 16e6/200e3 = 80 so the closest could exist 64.

However 16e6/64 is 250kHz and is likewise large. Therefore choosing a divisor of 128 must exist used and then the ADC clock will be 16e6/128 = 125kHz.

A conversion volition accept thirteen ADC clock cycles :

    xiii * 1.0/(125e3) = 104us

Check that these settings are used in the Arduino Source code! - I have non - they are extremely likely though.

Arduino Uno sampling rate (16MHz crystal)

i.0 / ( 13 * ane.0/125e3) = 9615Hz

Actually, reading the Arduino reference folio it says the sample rate is about 10kHz then this calculation matches that information.

Then the maximum Arduino ADC sampling rate is:

9.615kHz

The above rate is the maximum sampling charge per unit but to reproduce a sinewave requires double the sampling charge per unit (Nyquist theorem). Another style of looking at information technology is that the bandwidth of the signal you tin can safely reproduce is half the sampling rate.

Alarm: 9.615kHz the sample rate, the bandwidth is one-half (four.8kHz).

Notwithstanding this is simply dealing with sinewaves and non "existent signals". You cannot reproduce a foursquare wave at 4.8kHz because the edges of the signal are at a far college frequency (Fourier analysis) - in fact you would stop up with a four.8kHz sine wave if trying to reproduce a 4.8kHz square wave by converting it with and ADC with a nine.615kHz ADC clock. This is why digital oscilloscopes have sample rates almost 10 times higher than the maximum desired operational frequency.

Changing the Arduino Sampling Rate

ADC clock calculations

If you prepare the organisation clock to 20MHz you become 20e6/128 = 156250.0 - for a fleck faster conversion.

Interestingly if you lot go the other way equally a pattern decision you desire the fastest ADC clock rate of 200kHz, then you accept to inquire the question:

"What crystal clock results in a 200kHz rate later ADC prescaling?" i.e.

Xtal = 200e3 * prescale - trying 64 gives 12800000 or 12.8Mhz

12.8e6/64 = 200e3

And so reducing the Xtal clock allows a faster conversion rate of 200kHz!

Giving a max samping charge per unit of:

1.0 / ( 13 * ane.0/200e3) = 15384Hz (THIS IS FOR  A 12.8MHz XTAL)

...and yes you tin can get crystals made to your spec! - but you lot'll probably use a 12MHz crystal, as its easier to get, so the sample rate to a higher place will be a bit lower.

Example operation of 4bit ADC

This is a diagram of the action or the successive approximation ADC using Vref as 5V. Here a 4 bit ADC is shown but the principle is the same nonetheless many bits are used.

iv bit ADC Functioning

Initially the input voltage (Vin) is greater than the starting value of the DAC voltage (set by initial DAC value of B1000) and so this bit is kept. In the next processing catamenia the DAC output is set up to B1100 but in this case the DAC voltage becomes greater than Vin (and that bit is discarded).

The next two DAC values are kept because the DAC voltage is e'er lower than Vin and yous end up with an ADC output value of B1011 - the ADC successively approaches the terminal value.

Example of successive approximation 4bit ADC

The final output DAC voltage is 2.5 + 0.625 + 0.3125 = three.4375V

The advantage of the successive approximation ADC is that it is deterministic i.e. it always takes the aforementioned amount of fourth dimension. Consider using a binary counter as the input to the DAC that e'er started at zero and counted up. For a low Vin it would take a few counts to find the value and for a high Vin it would take lots of counts (possibly 255 cycles for an 8 bit DAC) i.eastward. information technology would take different times depending on the input voltage!

Annotation: As the number of $.25 in the ADC increases so does the conquering time.

Arduino Uno ADC resolution

As nosotros saw earlier the resolution of the ADC, when Vref=5V is four.88mV per stride.

The Arduino analogRead resolution which is the same as the resolution of the ADC is governed by two things

  1. The ADC size - 10bits for the Uno.
  2. The ADC reference voltage - You tin feed in your own voltage as well or use the internal reference.

ADC bits

Note: The arduino function analogReadResoution() allows the analogRead() role to return a different number of $.25 (The number of $.25 depends on your Arduino chip).

Some of the Arduinos e.g. DUE have 12 bit ADCs congenital in, and then returning 10bits will go along the code in these boards uniform with other Arduino boards that only have a ten fleck ADC. This is the default operation - to get 12 $.25 you lot will need to use analogReadResoution(12).

Using an ADC with more bits makes the the minimum step size (LSB) smaller to give higher resolution. The Arduino Uno is fixed at ten bits but in that location is a way of increasing the number of credible bits; run across this page: Arduino ADC fleck increasing.

ADC Reference voltage

You can employ 3 sources of ADC reference voltage:

  1. The power supply.
  2. The internal reference diode (nominally ane.1V).
  3. An external voltage reference.

The reference voltage is the full-scale voltage applied to the ADC converter operating every bit described above.

Say you changed the Vref value to 1V and then the minimum LSB you could discover would exist 1/1024 or

0.976mV

TIP: You lot tin select the internal i.1V reference and this will requite a step size of almost 0.1V: Exact calculation is 1.1/1024 = 0.00107V ~0.11mV per step . This does mean the ADC tin't read voltages above i.1V - they will only return 1023.

Warning: Never turn on the internal reference while applying an external voltage to the reference pivot.

Before turning on the internal reference check that there is no external voltage applied. If in that location is information technology is probable you will blow something up (encounter the ADC block diagram below).

Arduino ADC MUX showing internal voltage reference


Reason for the smoke problem

The problem is that yous might create a direct path from AREF to AVCC, or the internal reference, by turning on the FET while there is a voltage at AREF. To avoid this problem don't fiddle with the Analogue reference if there is whatever voltage applied to Aref.

The 'Internal 1V1 reference' voltage and the AVCC voltage are controlled though a MUX, and and so through a MOSFET, and are then connected to AREF. If an external voltage is applied and the reference or AVCC selected then the MOSFET (and MUX) offers a path (its resistance Rds) to the voltage deviation = heat and possible smoke!

The default state of the ADMUX $.25 REFS1 and REFS0 is nil, significant that AREF and "Internal Vref" are turned off (from datasheet). And then from ability upward the reference source is from the AREF pin i.e. it is "safe".

What Reference voltage does Arduino Code use?

If yous examine the code in the example below yous will encounter that the analogue reference AREF is not mentioned at all. For the ADC to measure out voltage it must take a reference!

In fact if you employ the analogread() function then the information technology volition activate the selected reference source. The default reference source is AVCC (the ability supply).

When writing Arduino code this makes information technology piece of cake to use the analogue read function as the default state is to employ the power supply for the reference voltage.

This presents a problem if you want to use the AREF input pin as the reference source. The solution is e'er, at the offset of code - in the setup() function - to select the reference source before any analogue read function is used.

Setting the Arduino reference source

The Arduino function to use the internal 1V1 reference is:

    analogReference(INTERNAL);

The Arduino function to use the voltage at the AREF pin as the reference is:

    analogReference(EXTERNAL);

This part sets the ADC to utilise the Supply (VCC) every bit the reference.

    analogReference(DEFAULT);

NOTE: The reference is updated simply during analogread(<northward>).

Annotation: Some boards have other internal voltage reference levels available. See the Arduino page on analogReference for more information.


You tin find out much more about the internal (1V1) reference here.

Example ADC Use

Connect a 10k potentiometer with wiper (eye) to pin A0 and one terminate to 5V, and the other end to Ground. This instance just uses the arduino analog read office analogRead() to read data from the specified analogue pin.

Start the serial monitor, and discover the led menstruation. The on-off fourth dimension is twice the value of the analogue value and so information technology varies from 2s to ~0.

                                          /*   Analog Input    Demonstrates analog input by reading an analog sensor on analog pin 0 and   turning on and off a light emitting diode(LED) continued to digital pin 13.   The amount of time the LED will be on and off depends on the value obtained   by analogRead().    The circuit:   - potentiometer     center pin of the potentiometer to the analog input 0     one side pin (either one) to footing     the other side pin to +5V   - LED     anode (long leg) attached to digital output xiii     cathode (brusk leg) attached to ground    - Note: because most Arduinos accept a built-in LED attached to pivot 13 on the     board, the LED is optional.    created past David Cuartielles   modified 30 Aug 2011   Past Tom Igoe    This example code is in the public domain.    http://www.arduino.cc/en/Tutorial/AnalogInput */                                int                                                sensorPin                                                =                                                A0                ;                                                // select the input pin for the potentiometer                                int                                                ledPin                                                =                                                13                ;                                                // select the pin for the LED                                int                                                sensorValue                                                =                                                0                ;                                                // variable to shop the value coming from the sensor                                void                                                setup                ()                                                {                                                                // declare the ledPin every bit an OUTPUT:                                                                pinMode                (                ledPin                ,                                                OUTPUT                ); }                                void                                                loop                ()                                                {                                                                // read the value from the sensor:                                                                sensorValue                                                =                                                analogRead                (                sensorPin                );                                                                // turn the ledPin on                                                                digitalWrite                (                ledPin                ,                                                HIGH                );                                                                // end the plan for <sensorValue> milliseconds:                                                                delay                (                sensorValue                );                                                                // turn the ledPin off:                                                                digitalWrite                (                ledPin                ,                                                LOW                );                                                                // terminate the program for for <sensorValue> milliseconds:                                                                delay                (                sensorValue                ); }                                                    

Other Uses for Arduino ADC pins

You can employ an ADC pivot as an:

  • Analogue input,
  • A digital input,
  • A digital output.

Note: Other functions can be shared past a pins east.g. A5 is as well SCL.

The internal structure of an ADC input pin allows the pin to exist multi functional. Some people get confused using an ADC pin as a digital input or output but it is perfectly valid to do and so (equally long as information technology has digital logic driving the input signal).

The just caution on using ADC pin equally a digital input is that if y'all have designed the external circuit for measuring an counterpart voltage, that voltage could cause the digital input to overload.

In CMOS circuits there are two FETS, a high fet and a low fet,  connected to the a mutual output - their inputs are too connected. These human action as current source and sinks - to allow a large fan out. In normal utilize the logic driving these inputs is logic high or low. If you lot drive them at the mid range voltage (think analogue input at Vcc/s) then you are turning both on at the same time. Therefore lots of current flows!

To avert this situation for a "digital merely" input you tie the input to the loftier voltage using a pull-upward, and of course the Arduino has internal pull-ups provided for simply this operation.

The reason for the pull-up is that bias voltages in the CMOS circuit (FET circuit) will set the input to some floating value which could plough on both FETs. So to exist sure this does not happen, enable the pull-up on each digital input pin.

You can come across that if external counterpart circuitry drives the inputs to mid range, there will exist a trouble if you set the pin to digital input. Then avoid doing this!

You might also be interested in:

  • How to increment ADC Accuracy past ADC Oversampling.

  • Using the Internal Bandgap Voltage Reference for accurate measurement.

  • How to brand a Capacitive Touch Sensor using the Arduino ADC.
  • How to Generate Analogue Output using an Arduino.


Privacy Policy | Contact | About Me

Site Map | Terms of Use

Ezoic

Source: https://www.best-microcontroller-projects.com/arduino-adc.html

Posted by: hatchelltionot61.blogspot.com

0 Response to "How To Setup The System Clock Registers For Freedom Boards"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel