# Basics: Analog to Digital Converters You may hear some construction noise in the background...

# 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.

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.

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

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