ADS1115 with the NodeMCU ESP8266-12E Plant Monitoring System

Hi! I’m trying to setup my ESP8266 with 3 DHT22s, 3 Soil Moisture Sensors, And 3 Light Sensors. The light sensors and the moisture sensors need more than the 1 analog input on the ESP8266. I have two ADS1115s because I read that they can be wired together. I’ve tried looking at other topic similar to mine but I’m still drawing a blank. I’m quite new to this! This is for a school project.

I have the code for the 3 DHT22 sensors working


#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

//My auth token to connect to the Blynk app.
char auth[] = "***********";

//My Wifi SSID and password.
char ssid[] = "**********";
char pass[] = "************";

 // These 3 define what Digital pin we're connected to. (NodeMCU Esp8266 was a weird pin layout)
#define DHTPIN 14 
#define DHTPIN2 12 
#define DHTPIN3 13 

//Defines which DHT type I have.
#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321

DHT dht(DHTPIN, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);
DHT dht3(DHTPIN3, DHTTYPE);
BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pins.
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
  float h2 = dht2.readHumidity();
  float t2 = dht2.readTemperature();
  float h3 = dht3.readHumidity();
  float t3 = dht3.readTemperature();  
  if (isnan(h) || isnan(t) || isnan(h2) || isnan(t2) || isnan(h3) || isnan(t3)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
  Blynk.virtualWrite(V1, h2);
  Blynk.virtualWrite(V2, t2);  
  Blynk.virtualWrite(V3, h3);
  Blynk.virtualWrite(V4, t3);  
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

void loop()
{
  Blynk.run();
  timer.run();
}


But I’m still clueless as to how to configure the ADS1115 code. What is the gain for?

#include <Wire.h>
#include <Adafruit_ADS1015.h>

// Adafruit_ADS1115 ads;  /* Use this for the 16-bit version */
Adafruit_ADS1015 ads;     /* Use thi for the 12-bit version */

void setup(void) 
{
  Serial.begin(9600);
  Serial.println("Hello!");
  
  Serial.println("Getting single-ended readings from AIN0..3");
  Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)");
  
  // The ADC input range (or gain) can be changed via the following
  // functions, but be careful never to exceed VDD +0.3V max, or to
  // exceed the upper and lower limits if you adjust the input range!
  // Setting these values incorrectly may destroy your ADC!
  //                                                                ADS1015  ADS1115
  //                                                                -------  -------
  // ads.setGain(GAIN_TWOTHIRDS);  // 2/3x gain +/- 6.144V  1 bit = 3mV      0.1875mV (default)
  // ads.setGain(GAIN_ONE);        // 1x gain   +/- 4.096V  1 bit = 2mV      0.125mV
  // ads.setGain(GAIN_TWO);        // 2x gain   +/- 2.048V  1 bit = 1mV      0.0625mV
  // ads.setGain(GAIN_FOUR);       // 4x gain   +/- 1.024V  1 bit = 0.5mV    0.03125mV
  // ads.setGain(GAIN_EIGHT);      // 8x gain   +/- 0.512V  1 bit = 0.25mV   0.015625mV
  // ads.setGain(GAIN_SIXTEEN);    // 16x gain  +/- 0.256V  1 bit = 0.125mV  0.0078125mV
  
  ads.begin();
}

void loop(void) 
{
  int16_t adc0, adc1, adc2, adc3;

  adc0 = ads.readADC_SingleEnded(0);
  adc1 = ads.readADC_SingleEnded(1);
  adc2 = ads.readADC_SingleEnded(2);
  adc3 = ads.readADC_SingleEnded(3);
  Serial.print("AIN0: "); Serial.println(adc0);
  Serial.print("AIN1: "); Serial.println(adc1);
  Serial.print("AIN2: "); Serial.println(adc2);
  Serial.print("AIN3: "); Serial.println(adc3);
  Serial.println(" ");
  
  delay(1000);

Thanks in advance!

The gain is for voltage range it can convert. As you probably noted, this ADC has no settings for reference voltage, it uses gain instead. Just check the datasheet if in doubts. And as a note: good choice for ADC.

But I’ve noted one potential issue in your code:

if (isnan(h) || isnan(t) || isnan(h2) || isnan(t2) || isnan(h3) || isnan(t3)) { Serial.println("Failed to read from DHT sensor!"); return; }

Are you sure you need to discard ALL of the sensors readings in case of ANY failure??? As for me it is not the right way, but …

If this was my project, I’d take a different approach…

For the sensors to be of any practical use, they need to be physically separate from each other. If you use one MCU then you’ll need long runs of wire to connect the sensors.
My solution would be to be use multiple NodeMCUs, each connected to a smaller number of sensors and each uploading data to the same Blynk project.

Pete.