Automatic Plant Watering
A stuck sensor does not mean "unsure" — it settles at one extreme and stays there. This sketch is built to survive that.
- Arduino
- C++
- Sensors
About this project
A soil moisture sensor decides whether a plant needs water, a relay or transistor drives a small pump, and the board's own LED shows what state the system is in — idle, watering, or resting.
The naive version of this project is three lines: read the sensor, and if it says dry, turn the pump on. That says nothing about what happens if the sensor is lying — and a cheap resistive soil probe lies in exactly one direction that matters. Continuous exposure to wet soil corrodes its bare metal prongs, a well-documented failure of this sensor type, and a corroded probe does not report "unsure." It settles at one electrical extreme and stays there — which, on this project's default wiring, reads as "bone dry," forever, whether or not the soil is actually drowning. Feed that into the naive loop and the pump simply never turns off. This project exists to stop that: a hard per-cycle ceiling on watering time, and an independent cooldown that ignores the sensor entirely afterward — two safeguards, both required, and the README traces through exactly why one alone still floods the pot.
What you will learn
- Design a sensor fault to fail safe, not fail wet, and prove the difference with real numbers
- Understand why one safeguard alone can re-arm itself on the same bad input and stop meaning anything
- Combine a hard ceiling with an independent cooldown so the two together are actually bounded
- Smooth a noisy analog reading with a rolling average instead of trusting a single sample
- Schedule two independent millis() timers on every loop pass with no delay() anywhere
Before you start
- Arduino basics — setup, loop, and reading a pin
- The Arduino IDE, or Priodemy Labs if you have no board
- Optional hardware: an UNO, a soil moisture sensor, and a relay or transistor to drive a pump
New to Arduino? The free Arduino course covers everything this project assumes.
Academic integrity notice
These projects are provided for learning, experimentation and reference. Understand the code, modify it for your own requirements, and acknowledge external work according to your institution’s academic policies. Priodemy does not supply work to be submitted as your own.
Features
- Two independent safeguards: a hard per-cycle ceiling, plus a cooldown that ignores the sensor entirely
- Rolling average over several soil readings, so one stray glitch reading cannot flip the pump
- SIMULATE mode manufactures wet, dry, and permanently-stuck readings with nothing wired up
- Works with either a 5V relay module or an NPN transistor pump driver — both wired and documented
- RELAY_ACTIVE_LOW and SENSOR_HIGHER_IS_DRIER constants for what your specific board actually does, not a guess
- millis() scheduling throughout — no delay() anywhere, including the safety ceiling itself
Technology stack
Track: IoT & Hardware · Level: Mini project
What is included
- plant_watering.ino — commented throughout, including a full section on why one safeguard alone fails
- A README on the corrosion failure mode, the fail-safe design, and both pump-driver wiring options
- Wiring diagram, parts list for the relay and transistor options, and five suggested extensions
Delivered as a 17 KB ZIP. SHA-256 published under Changelog so you can verify it.
System requirements
- Operating system
- Windows for the Priodemy Labs simulator; any OS for the Arduino IDE
- Runtime
- Arduino IDE 2.x, or Priodemy Labs
- Hardware
- Optional — Arduino UNO, a soil moisture sensor module, a 5V relay or NPN transistor, and a small DC pump
Licence
Learn from it, change it, and use it in your own coursework and personal projects. Do not resell it or pass it off as your own original work.
- Personal and educational use
- Modifying the code however you like
- Using it as the basis of your own coursework, with honest acknowledgement
- Putting your modified version in a portfolio or on your own GitHub
- Reselling or redistributing the project, modified or not
- Presenting it as your own original work
- Commercial deployment — see the Commercial licence
Support window: 6 months from download.
Frequently asked
Why does a stuck sensor matter this much? Isn't that a rare failure?
It is a documented, common failure of exactly this sensor type. A resistive probe sitting in wet soil with a voltage across it is an electrolytic cell whether anyone intended that or not, and continuous exposure corrodes it. When it fails, it does not report "unsure" — it settles at one extreme and reads as bone dry forever, which is the one direction that can flood a pot if nothing is watching for it.
Do I need the hardware to run this?
No. Set SIMULATE to 1 and the sketch plays out a wet phase, a genuine dry spell that watering fixes, and a probe that gets stuck and never recovers — with real, reproducible numbers on the serial monitor. It also opens in Priodemy Labs, which gives you a virtual board.
Relay or transistor — which should I use?
A relay if your pump needs its own 12V supply, since the relay only switches that circuit rather than powering it, or if you simply want the option ready for a bigger pump later. A transistor is enough for a small 5V pump and needs no separate module — just remember the flyback diode across the pump leads.
Changelog
- First release
SHA-256470dae7ccba395b39ff6df6ad8a31274c8d1456587c70133df72fd17a5d21c4b