Basics: Analog to Digital Converters

From Let's Control It
Revision as of 19:11, 14 June 2017 by Shardan (talk | contribs)
Jump to navigation Jump to search

WIP.gifYou may hear some construction noise in the background...


Basics: Analog to Digital Converters (ADC)

1. Basics

An ESP can't read analog values from a sensor easily. The inputs are "logic inputs" using low (voltage below 0,8V) and high (Voltage above 2,4V). The area between 0,8 and 2,4 V is undefined.

But the ESP has an analog input?

Right. More exact the ESP has an ADC (anaolg to digital converter) built in. This ADC converts the analog value to.... oh? A strange value, let's say this way.

The values out of external ADC's like ADS1115 gives similiar strange values.

What is going on here?


1.1. How does an ADC work?

Let's have a (very simplified) view of how an ADC works.

A simple ADC consists of two parts. First is a voltage source. The voltage source can step voltage up from 0 to a specific maximum level in defined steps.

Example: The ADC inside the ESP8266 can measure voltages between 0 ... 1V. Internally this is controlled by a 10-bit register. 10 Bits can count from 0 .. 2^10 - 1 (1024 - 1). So the voltage is stepped up from 0 to 1 Volt in steps of 1/1024 V.

Note:

      The 0 ...1 V range is meant for the naked ESP8266!
      If you're using a nodeMCU or a WeMOS D1 please remember those boards have a built in voltage divider that extends
      the range to 0 ...3 V

The second part in a simple ADC is a comparator.

The comparator compares the voltage from the analog input and the voltage source. If both values are equal, the comparator signals this.


For measuring the register counts up from 0 ... 1023 and rises the voltage source output step by step accordingly. At one point the voltage source and the analog input have equal values. The comparator gives signal and the CPU reads the value from the counting register.

This value is not a voltage value, not directly. It's called "ticks" in jargon, just a value, how often the counter "ticked" to reach the value.


1.2. What do do with these "ticks"?

Every ADC has some (more or less) fixed parameters.

As said the ADC inside the ESP has 10 bit long register so it steps up in 1024 Steps from 0 ... 1 V.

Lets say you get a reading of 476 from the analog input of the ESP. Which voltage is that?

A maximum of 1V in 1024 steps gives a voltage of 0,000977 V per single step.

476 steps * 0,000977V per step = 0,465 V

Voilá! Your voltage.


2. The ADS1115

This ADC is more complex then the internal ADC from the ESP chip. Let's have a look on some differences between them.


2.1. Input Amplifier

The ADS1115 has a PGA (programmable gain amplifier) integrated on the chip.

Sounds good, but...ehm?

Well, for us it says that the user of ESP8266 can change the sensivity of the ADC via the ESPEasy setup. No hardware changes, just setup.

Let's have a look on what the setup offers for this.

You can set the Gain values:

+/- 6,144 V (187,5 µV) +/- 4,096 V (125,0 µV) +/- 2,048 V (62,5 µV) +/- 1,024 V (31,25 µV) +/- 0,512 V (15,625 µV) +/- 0,256 V (7,8125 µV)

The first number is called FSR, the full scale rating. It marks the max. voltage you can measure with this setting. Note the "+/-" - this ADC can take negative voltage at the inputs!

WARNING:

        The input voltage is always limited to Vcc + 0,3V !
        Do not apply higher voltages to the inputs!

This says: If you connect Vcc directly to the ESP running 3,3V, the maximum allowed input voltage is 3,6V, no matter what your settings are!

If you use higher voltage for the ADS1115's Vcc, you have to use a level shifter for connecting to the ESP.


The second number is the step voltage. This is the value the ADS1115 counts up step by step at the given settings. You need this value for calculating the voltage.


2.2 Differential input? What's that?

The ADS1115 has four single inputs that can be used as two differential inputs. let's enlighten this technical "chinese" a bit.

In the setup you find several settings as your choice:

ADNx - GND direct input. ADNX - ADNy differential input.

The direct input is your choice if you're not sure what to use. It's just measuring a voltage as one would do with a voltmeter. The voltage between input pin and ground is measured. Simple example would be a photoresistor (light depending resistor).


The differential setup is a bit more complex. This uses two inputs and subtracts the values.

Mhm... and what's that good for?

just one example: Some analog luminosity sensors don't give back zero in the dark. There is a "zero deviaton". Even analog dust sensors do the same, the GP2Y10 used for ESPEasy has a zero deviation of around 0,5V.

So if you use that dust sensor, you can connect it to one input of the ADS1115 and use a second input with a potentiometer to subtract a variable voltage for an exact "zero" point. This is called "Zero Point Compensation".


2.3. Calculating the voltage from ADS1115's digital value.

A bit more complex thing too. The ADS1115 has a 16 bit register, similiar to the 10 bit of the ESP8266. The big difference: With the ADS1115 you may meassure negative voltages too. We have 16 bit = 2^16 = 65536 steps.

Really? Yes and no, not exactly.

As we have a range from +6.144V to -6.144V we have to calculate for this range. To be exact it should say: The range is from 32767 to -32768.

The voltage you want to measure is calculated then from the number of ticks and the voltage steps you set with the programmable gain in setup.

Example: you get a reading of 17535 from the ADC1115 in ESPEasy.

Your gain setting is "+/- 2,048 V (62,5 µV)".

Then your input voltage is 17535 * 62,5µV = 1,096V



[Check: Does ESPEasy 2.0.0DevX still calculate the "minus" values wrong? Bit 16 set says "negative". ESPEasy R1xx gives 65535 for -1 tick! = direct positive translation from binary negative presentation!

Still missing: Links, pictures, schematics]