Back to Tutorial

ESP32 with a capacitive soil moisture sensor

ESP32 with a capacitive soil moisture sensor:

Hardware Setup:

  1. Gather the necessary components:

  2. Connect the soil moisture sensor to the ESP32:

    • Connect the VCC pin of the sensor to the 3.3V or 5V output of the ESP32.
    • Connect the GND pin of the sensor to the ground pin of the ESP32.
    • Connect the analog output pin of the sensor (usually labeled “A0”) to an analog input pin on the ESP32 (e.g., A0).

Software Setup(first time users) :Ignore if already setup done 

  1. Install the Arduino IDE: Download and install the Arduino IDE from https://support.arduino.cc/hc/en-us/articles/360019833020-Download-and-install-Arduino-IDE.  
  2. Install the ESP32 board support package: Open the Arduino IDE, go to File > Preferences, and add the following URL to the Additional Boards Manager URLs field: https://dl.espressif.com/package/esp32/index.json. Then, go to Tools > Board > Boards Manager and install the “ESP32” board.  

 

Code:

const int sensorPin = 4; //change pin as per your board pin out

void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(sensorPin);

  Serial.println(sensorValue);

  delay(1000);   
}

Explanation:

  • The code defines the analog pin connected to the soil moisture sensor.
  • In the setup() function, the serial monitor is initialized to print the sensor readings.
  • In the loop() function, the analogRead() function is used to read the analog value from the sensor pin. The value is then printed to the serial monitor.

Uploading the Code:

  1. Connect the ESP32 to your computer.
  2. Select the correct board and port in the Arduino IDE.
  3. Compile and upload the code to the ESP32.

Testing:

  1. Open the serial monitor in the Arduino IDE.
  2. Insert the soil moisture sensor into the soil. You should see the sensor values change as the soil moisture changes.

Additional Notes:

  • The analog values from the soil moisture sensor can vary depending on the specific model and the type of soil. You might need to calibrate the sensor to get accurate readings.
  • You can customize the code to perform different actions based on the sensor readings (e.g., triggering irrigation systems, sending notifications).
  • Consider using a library or framework like ThingSpeak or Blynk to visualize and analyze the sensor data.

By following these steps, you can effectively use an ESP32 to measure soil moisture using a capacitive soil moisture sensor.

Share this post

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Tutorial