In this tutorial. we will read 2 analog sensor data from ESP8266. Remember that there is only 1 ADC in the ESP Modules, so only one analog sensor can be connected to the hardware. Thanks to MCP3008, we can now read eight different analog sensor data from ESP Modules.
MCP3008
The Microchip MCP3008 ADC is a 10-bit Analog to Digital (A/D) converter featuring an onboard sample and holds hardware.
A primary serial interface compatible with the SPI protocol is used for communication with the device. With normal standby currents of under 5 nA and typical active currents of 320 A, the MCP3008 can function over a wide voltage range (2.7V – 5.5V).
Soil Moisture Sensor
The soil moisture sensor used in this project is from AZ-Delivery Hygrometer v1.0 Module.
The volumetric content of water in the soil is measured using the hygrometer module. Both the analog and digital signals are output by the module. But, we will use only the analog pin from this module.
The module is made up of a hygrometer sensor, a control board, and jumper wires to connect the two components.
The volumetric amount of water in the soil is measured by the hygrometer sensor. Current can travel through the soil, the hygrometer sensor, and back to the control board. The resistivity of the soil varies depending on how much water is present. The hygrometer module can gauge this change in resistance. Higher water content in the ground results in lower ground resistance and vice versa.
We will choose Plant A and Plant B with different Moisture content. Gently place the module inside the soil. The module sensitivity can be adjusted with an onboard potentiometer. Moving the potentiometer shaft in the clockwise direction increases sensitivity. Moving the shaft of the potentiometer in the counterclockwise direction decreases the sensitivity of the module.
The Hygrometer module consists of two boards, the sensor board, and the control board. The sensor board has two pins, and the control board has six pins. The pinouts of these two boards are shown in the image:
Wiring to the ESP8266
As we know, MCP3008 has eight analog inputs. In this case, we are using 2 analog inputs. The VCC pins are connected to the 3V, and the ground pins are also connected to the board.
We will use D5, D5, D6 and D7 digital pins from the ESP8266, connect the digital pins to the MCP3008 as below:
- D5 –> CLK
- D6 –> D_OUT
- D7 –> D_IN
- D8 –> CS / CHIP SELECT
I have used two small plants, commonly called a rubber plant and a spider plant, from my office. The analog data varies from 0-1023.
If you connect the Moisture module, and the value is 1023, there is almost no water. The value tends to have the highest amount of water content.
These plants have different moisture levels, as you can see from the results below.
Code
First and foremost, you need to download the library from the GitHub repo, https://github.com/nodesign/MCP3008
You have to import the library into your Arduino IDE. Sketch > Include Library > Add .zip Library
Copy the following code into your Arduino IDE and save it.
#include <MCP3008.h>
// define pin connections
#define CS_PIN 15
#define CLOCK_PIN 14
#define MOSI_PIN 13
#define MISO_PIN 12
// put pins inside MCP3008 constructor
MCP3008 adc(CLOCK_PIN, MOSI_PIN, MISO_PIN, CS_PIN);
void setup() {
// open serial port
Serial.begin(115200);
}
void loop() {
int plant_a = adc.readADC(0); // read Chanel 0 from MCP3008 ADC
Serial.print("Plant A: ");
Serial.println(plant_a);
delay(1000);
Serial.print("Plant B: ");
int plant_b = adc.readADC(1); // read Chanel 0 from MCP3008 ADC
Serial.println(plant_b);
// iterate thru all channels
/*
for (int i=0; i<8; i++) {
int val = adc.readADC(i);
Serial.print(val);
Serial.print("\t");
}
Serial.println("");
*/
}
Plant A has an analog value of approximately 300, and plant B is near 375. Both plants have a similar moisture level.
In the next part of the tutorial, we will implement the messaging protocol MQTT and a dashboard for displaying the current analog values in real-time.