ESP32 S3 UNO - Automatic Irrigation System

This ESP32 S3 Uno automatic irrigation system tutorial shows you how to water your plants by itself with a soil moisture sensor, a relay and a 12V pump. The ESP32 S3 Uno form board reads the soil all the time. When the soil gets dry, it turns the pump on. When the soil is wet again, it turns the pump off. It is a simple automatic watering system for a smart garden.

In this tutorial, you will:

  1. Wire a capacitive soil moisture sensor, a relay and a pump to the ESP32 S3 Uno
  2. Read the soil moisture level with analogRead()
  3. Set a dry/wet threshold to control the automatic watering
  4. Upload the Arduino code from Arduino IDE and watch the pump switch on and off
ESP32 S3 Uno automatic irrigation system

Hardware Preparation

1×ESP32 S3 Uno-form Board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Capacitive Soil Moisture Sensor
1×Relay
1×12V Pump
1×Vinyl Tube
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Buy Note: Many soil moisture sensors available in the market are unreliable, regardless of their version. We strongly recommend buying the sensor with TLC555I Chip from the DIYables brand using the link provided above. We tested it, and it worked reliably.

Overview of soil moisture sensor and Pump

This project combines two parts. If one of them is new to you, read its own tutorial first. You will learn the pinout, how it works and how to program it.

In short, the soil moisture sensor gives an analog voltage. A higher value means drier soil. The pump needs 12V, which the board cannot give, so a relay works as a switch between the 12V adapter and the pump. The ESP32 S3 Uno only tells the relay when to switch.

ESP32 S3 Uno Pinout

The image below shows the pinout diagram of the ESP32 S3 Uno form board. Use it to find the A0 and D2 headers and their GPIO numbers when you wire the circuit.

ESP32 S3 Uno pinout diagram

Wiring Diagram

The sensor goes to A0 and the relay input goes to D2. The pump runs from its own 12V power adapter through the relay.

The wiring diagram between ESP32 S3 Uno soil moisture sensor Pump

This image is created using Fritzing. Click to enlarge image

Component Pin ESP32 S3 Uno Pin
Soil moisture sensor VCC 3.3V
Soil moisture sensor GND GND
Soil moisture sensor AOUT A0 (GPIO2)
Relay VCC 5V
Relay GND GND
Relay IN D2 (GPIO18)

Connect the pump, the 12V power adapter and the DC power jack to the relay's COM and NO screw terminals. Never power the pump from the ESP32 S3 Uno.

WARNING

Power the soil moisture sensor from 3.3V, not 5V. The ESP32 S3 Uno pins are NOT 5V tolerant, so a 5V signal on A0 (GPIO2) can damage the board. The relay module may use 5V on its VCC pin, because its IN pin only receives a signal from the board.

ESP32 S3 Uno Code

The sketch reads the soil moisture sensor on A0 (GPIO2) every half second. If the value is above the threshold, the soil is dry, so it turns the relay and the pump on. Otherwise it turns them off and prints the state with the raw value to the Serial Monitor.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-automatic-irrigation-system */ #define RELAY_PIN 18 // The ESP32 S3 Uno pin GPIO18 (D2) that connects to relay #define MOISTURE_PIN 2 // The ESP32 S3 Uno pin GPIO2 (A0) that connects to AOUT pin of moisture sensor #define THRESHOLD 2120 // CHANGE YOUR THRESHOLD HERE (12-bit ADC: 0 - 4095) void setup() { Serial.begin(9600); // set the ADC attenuation to 11 dB (up to ~3.3V input) analogSetAttenuation(ADC_11db); pinMode(RELAY_PIN, OUTPUT); } void loop() { int value = analogRead(MOISTURE_PIN); // read the analog value from sensor if (value > THRESHOLD) { Serial.print("The soil is DRY => turn pump ON"); digitalWrite(RELAY_PIN, HIGH); } else { Serial.print("The soil is WET => turn pump OFF"); digitalWrite(RELAY_PIN, LOW); } Serial.print(" ("); Serial.print(value); Serial.println(")"); delay(500); }

In setup(), the code calls analogSetAttenuation(ADC_11db). By default, the ESP32 S3 Uno ADC can only measure a small voltage. With 11 dB attenuation, the analog pin can read the full range, from 0V up to about 3.3V. Without this line, the readings hit 4095 too early and the sensor values look wrong.

The threshold of 2120 is the old 0 to 1023 value of 530 scaled up for the 12-bit ADC (0 to 4095). It is only a starting point. Your sensor, your soil and the 3.3V supply all change the real value, so calibrate it as shown in the steps below.

Detailed Instructions

Follow these steps in order:

  1. New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
  2. Wire it up as shown in the diagram.
  3. Connect the board to your computer with a USB Type-C cable.
  4. Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
  5. Find your threshold. Measure the value where the soil changes from wet to dry, as shown in ESP32 S3 Uno - Calibrates Soil Moisture Sensor.
  6. Update the code. Put your value in the THRESHOLD line.
  7. Open the Serial Monitor in Arduino IDE and set it to 9600 baud.
  8. Upload the code by clicking the Upload button.
  9. Check the output. Put the sensor in dry soil, then in wet soil, and watch the Serial Monitor.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
The soil is DRY => turn pump ON (2870) The soil is DRY => turn pump ON (2855) The soil is DRY => turn pump ON (2610) The soil is DRY => turn pump ON (2240) The soil is WET => turn pump OFF (1720) The soil is WET => turn pump OFF (1685) The soil is WET => turn pump OFF (1690) The soil is WET => turn pump OFF (1702)
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2
  1. Tip: Test with a cup of water before you put the pump in your garden. Push only the sensor's marked area into the soil and keep its electronics dry.

Code Explanation

Every line of the sketch has a comment that tells you what it does. Read the comments in the code above to follow the logic step by step.

Video Tutorial

Watch the video below to see this ESP32 S3 Uno project step by step.

FAQ

Why is the threshold 2120 and not 530 like on the UNO R4?

The ESP32 S3 Uno has a 12-bit ADC, so analogRead() returns 0 to 4095. The UNO R4 sketch used 0 to 1023. The value 530 times 4 is about 2120. Because the sensor also runs at 3.3V here, always calibrate and use your own value.

Why does the code call analogSetAttenuation(ADC_11db)?

It sets the ESP32 S3 Uno ADC input range to about 0 to 3.3V, so the full soil moisture signal maps to 0 to 4095. Without it the range is smaller and readings saturate early. The UNO R4 does not need this line, as it is ESP32-only. Keep the sensor output at 3.3V or less.

Can I power the soil moisture sensor from 5V?

No. With 5V, the AOUT pin can give more than 3.3V, and the ESP32 S3 Uno pins are not 5V tolerant. The capacitive sensor works well at 3.3V, so connect its VCC to the 3.3V pin.

Can I power the relay module from the 3.3V pin of the ESP32 S3 Uno?

Most relay modules have a 5V coil, so power their VCC from the 5V pin. The IN pin can still take the 3.3V signal from the ESP32 S3 Uno on most modules. If your relay does not click, use a 3V relay module or a module with a transistor driver.

What if my garden needs more sensors or pumps than the Uno headers give?

The ESP32 S3 Uno has two extra rows of holes with more GPIOs. GPIO15 and GPIO16 are I/O, PWM and analog. GPIO45 and GPIO35 to GPIO42 are I/O and PWM. GPIO47 and GPIO48 are output only, with PWM. Solder pin headers to use them and write the GPIO number in code. For more soil moisture sensors, use GPIO15 or GPIO16. For more relays, GPIO35 to GPIO42 are a good choice. Be careful with two things. First, GPIO47 and GPIO48 may run at 1.8V instead of 3.3V on some modules (especially ones with "V" in the name, like R8V or R16V), so 3.3V devices can be damaged or act strangely. Second, GPIO0, GPIO3 (D6), GPIO45 and GPIO46 (D9) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code. Use the other extra pins first.

Why does the pump turn on and off very fast near the threshold?

When the value is close to the threshold, small changes make the relay switch often. You can use two thresholds, one to start and one to stop the pump, or add a minimum watering time in the code.

Troubleshooting

Problem Possible Cause Solution
No COM port in Arduino IDE USB driver is missing or the board is not in upload mode Install the CP210x or CH340 driver, or hold BOOT while pressing RESET
Relay clicks but the pump does not run The 12V adapter is not connected or the pump is on the wrong relay terminal Wire the pump and the 12V adapter through the COM and NO terminals
Relay never switches Relay VCC or GND is not connected, or the relay needs 5V Power the relay VCC from 5V and check the GND wire
Pump runs all the time The threshold is too low for your sensor Print the value in dry and wet soil and set the threshold between them
Readings stay at 4095 analogSetAttenuation(ADC_11db) is missing or the sensor gets 5V Keep the attenuation line and power the sensor from 3.3V
Readings stay at 0 or do not change AOUT is not on A0 (GPIO2) or the sensor has no power Check the AOUT wire and the 3.3V and GND wires
Relay works the opposite way The relay module is active LOW Swap HIGH and LOW in the digitalWrite() lines
Serial Monitor shows strange characters Wrong baud rate Set the Serial Monitor to 9600 baud

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!