NodeMCU + KY-108 + MQ-2 + ADS1115

Hello,

I've two analog sensors connected to the NodeMCU Amica 12F, via ADS1115 analog to digital converter.

I'm using I2C for ADS1115 and I connected the 2 sensors to A0 and A1 of the ADS1115.

The 3 pieces of HW (KY-108, MQ-2 and ADS1115) are connected to VIN (5V).

I'm struggling to understand how to read the values from both the sensors.

For example, with KY-108 I have values from 3/4 (max light) to 1024 (max darkness) when connected to PIN A0 of NodeMCU... while I have 5284 dark, 5125 full light when connected to ADS1115.

How shall I transform them?

How can I then transform those values in "Lux" (light intensity) ?

What about the MQ-2 ? How can I calculate the "ppm" value (part per millions) ?

I'm using this libraries

#include <Adafruit_ADS1015.h>
Adafruit_ADS1115 ads(0x48);

I took the numbers connecting for example KY-108 to both A0 of NodeMCU and A0 of ADS1115... ratio doesn't seem to be linear....

Untitled.png

Same test, but with MQ-2 shows that ratio is linear... that's strange.

Please look at the 2 attachments...

first of all I had a hard time to find the sensor KY-108
its a KY 018. These cheap fotoresistors are non-linear. And even if the sensor would be linear the output-signal is created by a simple voltage-divider which is non-linear in every case.

A KY-018 should be called two-state bright-dark-sensor

If you want to have lux you will have to calibrate your code with a luxmeter

you could use a light to frequency converter like this

pretty cheep too

datasheet https://www.sparkfun.com/datasheets/Sensors/Imaging/TSL235R-LF.pdf

If you do not have a digital multimeter yet. A digital multimeter is the bare minimum measurement equipment if you want to connect electronic parts to an arduino

I recommend this one

great price to capability-ratio. Has even bluetooth for measuring over time on your smartphone or tablet
best regards Stefan

Thanks for the reply, I will have a look. I already have a multimeter.

What about the result of ADS1115 vs NodeMCU A0 pin?

How shall I convert them?
As I understand both A0 NodeMCU and ADS1115 calculates the output pin voltage of the KY-018 (and MQ-2) and converts them in bit.... A= NodeMCU with 10 bits and ADS1115 with 16 bits and sign (+ and -).... but why the ratio between the two measured values is non-linear for the KY-018 if they should measure the same thing?

What is the ratio I shall use?

Hi stefanogiu,

I'm not sure yet if I understand you right.
you wrote

but why the ratio between the two measured values is non-linear for the KY-018 if they should measure the same thing?

can you please name all the details how you calculate the ratio.
I mean it this way
darkness:

  • measured value on A0: xxxxx
  • measured value on ADS1115: yyyyy

ratio = .....
the anlog-to-digital-converters have different resolutions 10 bit and 16 bit
that mus lead to different values
what reference-voltages are you using on the A0?
what reference-voltage are you using on the ADS1115?

What voltages do you measure with your multimeter between ground and analog-input
in both cases?

using an analog-to-digital-converter is a bit more than
"alexa take a look at the wries- tell me the voltage" ;-))

best regards Stefan

I understand it's not like "Hey Google...!" I have Google and not Alexa, that's why I'm here to ask questions :wink:

I started all this in March, during the lockdown... I never had experience of Arduino/NodeMCU before.

As I said, "I took the numbers connecting for example KY-108 to both A0 of NodeMCU and A0 of ADS1115... ratio doesn't seem to be linear....",

Meaning that KY-018 was connected, at the same time (its signal wire), to A0 of NodeMCU and A0 of ADS1115... and I was reading together the two values and making the ratio between them.

In the attached pic I show the ratio between them for a sample of values, from light to dark.

If they both measure the voltage (in bit, different resolution), I was expecting the same ratio of values in bit (10 and 16) from light to dark... instead the ratio changes...

Check the Untitled.png image for reference of values...

I connected KY-018 to VIN and ADS1115 to VIN

Now the different ratio might be through different reference voltages.
Does the ADS1115 have a reference voltage-input? If not what does the datasheet of the ADS1115 say about reference-voltage?
What does the A0-pin of the Arduino have for a reference-voltage?

best regards Stefan

The numbers came from a single session of test, where voltage reference was not changing.... so I don't understand why the ratio between ADS1115 and A0 of NodeMCU is not the same...

Yes but what WAS the reference-voltage ? Was it the same for Arduino A0 and the ADS1115.
voltage reference means the measured voltage is compared to a second voltage the reference voltage.

On your signal-pin this is the voltage to measure but there is a second voltage. The reference voltage.

Depending on the input-impedance of the ADC it might be that the voltage-divider has a too high impedance itself so that measuring it will have a noticeable influence.

an Arduinoboard can use an internal created reference-voltage of you can feed in your own on a special pin.
How the ADS1115 does its reference-voltage and if he can be feeded with an external reference-voltage

How about a test where you use 1 5 kOhm or 10 kOhm potentiometer and measure the voltage on the wiper
doing the same comarison as with the sensor?

best regards Stefan

I didn't understand your reply... I'm using NodeMCU and not Arduino and neither NodeMCU nor ADS1115 have a pin for a reference voltage...

i Stefanogiou,

I forgot that you were using a nodeMCU-ESP8266 board.
The ESP8266-module itself can only measure voltages between 0V and 1,0V
Most nodeMCU-boards have a voltage-divider on the PCB. This means voltages
between 0V and 3,3V can be measured.

The input-impedance is 330 kOhm. For an ADC this is a pretty low value

The ADS1115 is different
here is the interesting part of the datasheet

The ADS1115 devices are precision, low-power, 16-bit,

I2Ccompatible, analog-to-digital converters (ADCs)

offered in an ultra-small, leadless, X2QFN-10

package, and a VSSOP-10 package. The ADS111x

devices incorporate a low-drift voltage reference and

an oscillator. The ADS1115 also

incorporate a programmable gain amplifier (PGA) and

a digital comparator. These features, along with a

wide operating supply range, make the ADS111x well

suited for power- and space-constrained, sensor

measurement applications.

The ADS111x perform conversions at data rates up

to 860 samples per second (SPS). The PGA offers

input ranges from ±256 mV to ±6.144 V, allowing

precise large- and small-signal measurements.

this means the ADS1115 is configurable in a wide range.
The input-impedance is minimum 3 MegaOhm (depending on the programmable voltage amplification.
That's ten times more than the onboard A0

The voltage-divider on the nodeMCU-board changes the electrical characteristic of the KY-018-sensor.
How big this change is depends on the resistance of the KY-018.

can you measure the resistance range of the KTY-018.
DISconnect the KY-018 sensor completely from any thing else then measure the resistance between KY-018 signal and GND
a.) when in bright light
b.) when in darkness

This KY-018 seems to be only suitable for digital detecting "darkness"and "not completely darkness"
Of course there are some solutions to improve the measured signal. This will require some additional electronic parts like an OP-amplifier configured as a voltage-follower which is pretty easy to build.

So which way do you want to go?
best regards Stefan

The resistor on the module is 10Kohm (the one in series with the LDR). I measured it with a multimeter.

Then I tested 2 LDR of the same pack I received and they seem to have different values.

Full darkness... one is around 33Kohm and one is around 40kohm.

Full light... one is around 120ohm and one is around 150ohm.

I connected two KY-018 together to the same NodeMUC... one with A0 pin and another with ADS1115.

This is the result I got with this code:

#include <Adafruit_ADS1015.h>
Adafruit_ADS1115 ads(0x48);

#define sensorPin A0

void setup() {
Serial.begin(115200);
ads.begin();

}

void loop() {
float AR0 = ads.readADC_SingleEnded(0);
float AR1 = analogRead(sensorPin);
Serial.println(String(AR0) + "-" + String(AR1) + ":" + String(AR0/AR1));
}

4 percent deviation between max ratio 22.8 and min ratio 21.82

(22.8-21.82)/ ( (21.82+22.8) / 2 )
Ans = 0.04392649

I guess with that low-cost equipement this is pretty good. So from here what is your question now?#

Did you see all the numbers?

480.00-91.00:5.27

This is not 4% deviation :wink: ratio of 5.27 is completely different...