MCP3008 with ESP8266 for Analog Moisture Sensors – SPI

by Dec 7, 2022nodemcu projects

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

MCP3008

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.

Hygrometer Module

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.

ESP8266 Pinout

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("");
  */
}
Arduino IDE Results

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.

Creating a multiplication Skill in Alexa using python

Written By Monisha Macharla

Hi, I'm Monisha. I am a tech blogger and a hobbyist. I am eager to learn and explore tech related stuff! also, I wanted to deliver you the same as much as the simpler way with more informative content. I generally appreciate learning by doing, rather than only learning. Thank you for reading my blog! Happy learning!

RELATED POSTS

Smart Traffic Light System using NodeMCU(ESP32)

Smart Traffic Light System using NodeMCU(ESP32)

In this tutorial, we are going to make an IoT based Smart Traffic Light system. We will connect the lights by using ESP32. Let's begin! Contents: IntroductionMaterials RequiredThe CircuitCode and it's ExplanationThe Final Run Introduction First of all, we will create...

4×4 Keypad Door Lock using NodeMCU with E-Mail Alerts

4×4 Keypad Door Lock using NodeMCU with E-Mail Alerts

This project will show you how to interface a 4x4 Keypad with the NodeMCU to make a simple code-based door lock. Entering the wrong code three times in a row or changing the existing code will send an E-mail alert to the specified recipient or the owner of the...

VIDEOS – FOLLOW US ON YOUTUBE

EXPLORE OUR IOT PROJECTS

IoT Smart Gardening System – ESP8266, MQTT, Adafruit IO

Gardening is always a very calming pastime. However, our gardens' plants may not always receive the care they require due to our active lifestyles. What if we could remotely keep an eye on their health and provide them with the attention they require? In this article,...

How to Simulate IoT projects using Cisco Packet Tracer

In this tutorial, let's learn how to simulate the IoT project using the Cisco packet tracer. As an example, we shall build a simple Home Automation project to control and monitor devices. Introduction Firstly, let's quickly look at the overview of the software. Packet...

All you need to know about integrating NodeMCU with Ubidots over MQTT

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

All you need to know about integrating NodeMCU with Ubidots over Https

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

How to design a Wireless Blind Stick using nRF24L01 Module?

Introduction Let's learn to design a low-cost wireless blind stick using the nRF24L01 transceiver module. So the complete project is divided into the transmitter part and receiver part. Thus, the Transmitter part consists of an Arduino Nano microcontroller, ultrasonic...

Sending Temperature data to ThingSpeak Cloud and Visualize

In this article, we are going to learn “How to send temperature data to ThingSpeak Cloud?”. We can then visualize the temperature data uploaded to ThingSpeak Cloud anywhere in the world. But "What is ThingSpeak?” ThingSpeak is an open-source IoT platform that allows...

Amaze your friend with latest tricks of Raspberry Pi and Firebase

Introduction to our Raspberry Pi and Firebase trick Let me introduce you to the latest trick of Raspberry Pi and Firebase we'll be using to fool them. It begins with a small circuit to connect a temperature sensor and an Infrared sensor with Raspberry Pi. The circuit...

How to implement Machine Learning on IoT based Data?

Introduction The industrial scope for the convergence of the Internet of Things(IoT) and Machine learning(ML) is wide and informative. IoT renders an enormous amount of data from various sensors. On the other hand, ML opens up insight hidden in the acquired data....

Smart Display Board based on IoT and Google Firebase

Introduction In this tutorial, we are going to build a Smart Display Board based on IoT and Google Firebase by using NodeMCU8266 (or you can even use NodeMCU32) and LCD. Generally, in shops, hotels, offices, railway stations, notice/ display boards are used. They are...

Smart Gardening System – GO GREEN Project

Automation of farm activities can transform agricultural domain from being manual into a dynamic field to yield higher production with less human intervention. The project Green is developed to manage farms using modern information and communication technologies....