Difference between revisions of "Basics: Analog to Digital Converters"

From Let's Control It
Jump to navigation Jump to search
m (inserted ADC scheme)
 
(16 intermediate revisions by the same user not shown)
Line 41: Line 41:
  
 
Note:
 
Note:
       The 0 ...1 V range is meant for the naked ESP8266!
+
       The 0 ...1 V range is meant for the blank ESP8266!
       If you're using a nodeMCU or a WeMOS D1 please remember those boards have a built in voltage divider that extends
+
       If you're using a nodeMCU or WeMOS D1 please remember those boards have a built in voltage divider that extends
 
       the range to 0 ...3 V
 
       the range to 0 ...3 V
  
Line 54: Line 54:
  
 
For measuring the register (counter) counts up from 0 ... 1023 and  
 
For measuring the register (counter) counts up from 0 ... 1023 and  
rises the voltage source output step by step accordingly.
+
rises the voltage source output step by step accordingly.<br/>
 
At one point the voltage source and the analog input have  
 
At one point the voltage source and the analog input have  
 
equal values.
 
equal values.
Line 63: Line 63:
 
It's called "ticks" in jargon, just a value, how often  
 
It's called "ticks" in jargon, just a value, how often  
 
the counter "ticked" to reach the value.
 
the counter "ticked" to reach the value.
 
  
 
== What do do with these "ticks"? ==
 
== What do do with these "ticks"? ==
Line 103: Line 102:
 
You can set the Gain values:
 
You can set the Gain values:
  
+/- 6,144 V (187,5 µV)
+
    +/- 6,144 V (187,5 µV)
+/- 4,096 V (125,0 µV)
+
    +/- 4,096 V (125,0 µV)
+/- 2,048 V (62,5 µV)
+
    +/- 2,048 V (62,5 µV)
+/- 1,024 V (31,25 µV)
+
    +/- 1,024 V (31,25 µV)
+/- 0,512 V (15,625 µV)
+
    +/- 0,512 V (15,625 µV)
+/- 0,256 V (7,8125 µV)
+
    +/- 0,256 V (7,8125 µV)
  
 
The first number is called FSR, the full scale rating.
 
The first number is called FSR, the full scale rating.
It marks the max. voltage you can measure with this setting.
+
It marks the max. voltage you can measure with this setting. <br/>
Note the "+/-" - this ADC can take negative voltage at the inputs!
+
Note the "+/-" - this ADC might give back negative values if you use differential inputs! <br/>
 +
It can not measure negative voltage!
  
 
WARNING:  
 
WARNING:  
         The input voltage is always limited to Vcc + 0,3V !
+
         <span style="color: red>The input voltage is always limited to Vcc + 0,3V !
         Do not apply higher voltages to the inputs!
+
         Do not apply higher voltages to the inputs!</span>
  
 
This says: If you connect Vcc directly to the ESP running 3,3V,
 
This says: If you connect Vcc directly to the ESP running 3,3V,
Line 122: Line 122:
 
settings are!
 
settings are!
  
If you use higher voltage for the ADS1115's  Vcc, you have to  
+
<span style="color: red>If you use higher voltage for the ADS1115's  Vcc, you have to  
use a level shifter for connecting to the ESP.
+
use a level shifter for connecting to the ESP.</span>
  
  
Line 130: Line 130:
 
given settings. You need this value for calculating the voltage.
 
given settings. You need this value for calculating the voltage.
  
 +
== Differential input? Direct input? What's that? ==
  
== Differential input? What's that? ==
+
The ADS1115 has four single inputs that can be used as two differential inputs.<br/>
 
+
Let's enlighten this technical "chinese" a bit.
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:
+
In the setup you find several settings to select from:
  
ADNx - GND direct input.
+
    AINx - GND         direct input.
ADNX - ADNy differential input.
+
    AINx - AINy         differential input.
  
 
The direct input is your choice if you're not sure what to use.
 
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.
+
It's just measuring a voltage as one would do with a voltmeter. <br/>
 
The voltage between input pin and ground is measured.
 
The voltage between input pin and ground is measured.
 
Simple example would be a photoresistor (light depending resistor).
 
Simple example would be a photoresistor (light depending resistor).
Line 148: Line 147:
  
 
The differential setup is a bit more complex.
 
The differential setup is a bit more complex.
This uses two inputs and subtracts the values.
+
It uses two inputs and subtracts the values.
  
 
''Mhm... and what's that good for?''
 
''Mhm... and what's that good for?''
  
just one example:  
+
Just one example:  
 
Some analog luminosity sensors don't give back zero in the dark.
 
Some analog luminosity sensors don't give back zero in the dark.
 
There is a "zero deviaton". Even analog dust sensors do the same,  
 
There is a "zero deviaton". Even analog dust sensors do the same,  
Line 162: Line 161:
 
This is called "Zero Point Compensation".
 
This is called "Zero Point Compensation".
  
 +
So if you have 0,5V on AIN0 and 0,3V at AIN1 and set the setup to "AIN0 - AIN1 differential input" <br/>
 +
it will give back a value equivalent to 0,2V.
 +
 +
IF you swap the voltages (0,3V on AIN0 and 0,5V on AIN1) it gives back a negative value equivalent to -0,2V.
  
 
== Calculating the voltage from ADS1115's digital value. ==
 
== Calculating the voltage from ADS1115's digital value. ==
  
 
A bit more complex thing too.  
 
A bit more complex thing too.  
The ADS1115 has a 16 bit register, similiar to the 10 bit of the ESP8266.
+
The ADS1115 has a 16 bit register, similiar to the 10 bit of the ESP8266.<br/>
The big difference: With the ADS1115 you may meassure negative voltages too.
+
The big difference: With the ADS1115 you may get negative results too.
 
We have 16 bit = 2^16 = 65536 steps.  
 
We have 16 bit = 2^16 = 65536 steps.  
  
 
Really? Yes and no, not exactly.
 
Really? Yes and no, not exactly.
  
As we have a range from +6.144V to -6.144V we
+
If you set gain to +/- 6,144V you may get a "- 6,144V" too (if using differential input only!)<br/>
have to calculate for this range. To be exact  
+
We have to calculate for this range. To be exact  
 
it should say: The range is from 32767 to -32768.
 
it should say: The range is from 32767 to -32768.
  
Line 190: Line 193:
  
  
[Check: Does ESPEasy 2.0.0DevX still calculate the "minus" values wrong?
+
[Still missing: Links, pictures, schematics]
Bit 16 set says "negative". ESPEasy R1xx gives 65535 for -1 tick!
 
= direct positive translation from binary negative presentation!
 
 
 
Still missing: Links, pictures, schematics]
 

Latest revision as of 09:33, 22 June 2017

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


Basics: Analog to Digital Converters (ADC)

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?


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 blank ESP8266!
      If you're using a nodeMCU or 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.

ADC-Simple.jpg

For measuring the register (counter) 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.

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.


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.


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 might give back negative values if you use differential inputs!
It can not measure negative voltage!

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.

Differential input? Direct 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 to select from:

    AINx - GND	        direct input.
    AINx - AINy	        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. It 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".

So if you have 0,5V on AIN0 and 0,3V at AIN1 and set the setup to "AIN0 - AIN1 differential input"
it will give back a value equivalent to 0,2V.

IF you swap the voltages (0,3V on AIN0 and 0,5V on AIN1) it gives back a negative value equivalent to -0,2V.

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 get negative results too. We have 16 bit = 2^16 = 65536 steps.

Really? Yes and no, not exactly.

If you set gain to +/- 6,144V you may get a "- 6,144V" too (if using differential input only!)
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



[Still missing: Links, pictures, schematics]