All posts

Raspberry Pi automatic plant watering with a camera instead of a soil sensor

A Raspberry Pi above the bench takes a picture every hour, asks if the seedlings are wilting, and runs the pump for twenty seconds on a yes. The fifteen-line program and what to add to it.

By Oussama Messai Published 2 December 2026 6 min read Lire cet article en français
Trays of young seedlings on a greenhouse bench, some of them drooping

On Friday evening the trays are watered. Over a warm weekend they dry out, and on Monday morning a few hundred seedlings are lying flat. Nobody did anything wrong. Nobody was there.

The classic fix is a soil moisture probe, and it works for one pot. A bench of trays is another matter: a probe measures the cell it is stuck in, and the tray by the door dries faster than the one in the corner. What a grower actually does is look. A tray that needs water shows it.

A small board with a camera can look too. This post builds the program from our short film “Give your board eyes”: a Raspberry Pi above the bench, one picture an hour, one question, and a relay on the water pump.1

The parts

  • A Raspberry Pi with a camera module, fixed above the bench so that the trays fill the picture.
  • A relay module on GPIO 17, switching the pump.
  • A Camly API key.

One board is both the eye and the hand. There is no server and nothing to train.

The whole program

import os, time, requests
from picamera2 import Picamera2
from gpiozero import OutputDevice

KEY = os.environ["CAMLY_API_KEY"]
cam = Picamera2(); cam.start()
pump = OutputDevice(17)                        # the relay of the water pump

while True:
    cam.capture_file("now.jpg")
    r = requests.post("https://api.camly.ai/v1/runs",
        headers={"Authorization": f"Bearer {KEY}"},
        data={"config": '{"type":"event","question":"Are the seedlings wilting?"}'},
        files={"image": open("now.jpg", "rb")}, timeout=60).json()
    if r["result"]["flagged"]:
        pump.on(); time.sleep(20); pump.off()  # yes: water for twenty seconds
    time.sleep(3600)                           # and look again in an hour

It has four parts, and they are the four parts of any program of this kind.

  1. The picture. capture_file writes what the camera sees now.
  2. The question. One sentence, in plain words, sent with the picture to POST /v1/runs.
  3. The answer. result.flagged is the yes or the no.
  4. Your decision. What a yes does is your code, on your board: here, twenty seconds of pump.

The request is the API’s real one. The camera and relay lines are the usual ones of picamera2 and gpiozero; we have not run this exact program on a board ourselves, so treat it as a starting point and adapt it to your camera and wiring.

Ask about what can be seen

The question is “are the seedlings wilting?”, not “are the plants healthy?”. The difference matters. Wilting is visible: stems bend, leaves droop. Health is a judgement that a picture cannot settle, and a question about disease invites an answer that sounds confident and means little.

The same rule holds for any question you put to a camera: ask about what a person standing there could see. The types of question and what each returns are covered in six ways to ask a camera a question.

Before wiring a pump to the answer, check the question on your own trays. Take a picture of a tray you would water and one you would not, send both with a live key, and read result.description as well as result.flagged: the sentence tells you what the model looked at. Two checks cost one cent. Whether the question works under your light and at your camera angle is the one thing this post cannot tell you.

Before leaving it alone for a weekend

Fifteen lines are enough to show the idea. A program that controls water needs a few more.

Cap the water. A pump that runs on every yes will flood the bench if the answer is wrong three times in a row, or if a tray that is past saving keeps looking wilted. Count the runs and stop after a few a day:

runs_today = 0          # reset when the date changes
if r["result"]["flagged"] and runs_today < 3:
    pump.on(); time.sleep(20); pump.off()
    runs_today += 1

Treat “no answer” as “do nothing”. If the network is down or the API answers with an error, the reply has no result, and the program above stops with an exception. Catch it, skip this hour, and try again at the next. A board that cannot ask must not water by default, and must not stop for good either.

Look only in daylight. A camera sees nothing useful in a dark greenhouse, and seedlings do not need a verdict at three in the morning. Checking from eight to eight halves the cost.

Wait before asking again. After the pump has run, a plant takes time to stand up. One hour between checks is a reasonable gap; a much shorter one would water the same wilted tray again before the first watering had any effect.

Keep the pictures of every yes. Copy now.jpg to a dated file when the pump runs. On Monday you can see what the board saw, which is how you find out whether the question needs rewording.

The general hardening of a board that calls the API (a key that can only run checks, a saved agent so the question can change without touching the board, retries that do not pay twice) is in give a Raspberry Pi eyes.

What it costs

A check costs €0.005 at the time of writing.2

PaceChecks a dayPer dayPer 30 days
Every hour, around the clock24€0.12€3.60
Every hour, twelve hours of daylight12€0.06€1.80

A test key is free and is the right one for wiring up the relay: it always gives the same answer for the same picture, so you can find a picture that returns a yes and use it to see the pump start.

Same code, another question

Nothing in the program is about seedlings except one sentence. Change the sentence and the last line, and the same board does another job.

QuestionA yes does
Is the water trough empty?Fill it
Is the bench clear?Switch on a green light
Is the greenhouse door open?Send a text

That is the point of asking in plain words. The model is not trained for seedlings, so it does not need retraining for a trough.

Sources

  1. Camly AI, “Give your board eyes”, short film on the Camly API for makers and growers, November 2026. The program above is the one shown in the film.
  2. Camly API, all guides as plain text: Quick start, Send from a device, Keys, Limits and prices. platform.camly.ai
  3. Camly API contract (OpenAPI). api.camly.ai