Raspberry Pi soil moisture sensor

Welcome back Guys 👋. I hope you are finding my posts on Raspberry PI interesting. Let’s continue with today’s topic: Raspberry Pi Soil-Moisture sensor. Like other sensors based on LM393 it is also same but working and reading are a bit different. To know more about it and to see how it is hooked to Pi. Let’s Begin !!!!!

Soil-Moisture Sensor:

Soil-Moisture sensor is a LM393 based sensor which is an Op-Amp IC. For more information on Op-Amps Google it. For now in simple words it amplifies the current value received by the probes dug into the soil for testing moisture content in soil. I strongly recommend you to first look at it’s arduino tutorial for better and easy understanding. As coding for arduino is much simpler as in Python. If you want its Schematic diagram then click here.

It’s value can be adjusted by using the Potentiometer provided on it. It has both type of output Analog and Digital. To know the difference between Google it. In short words Digital value is either 0 or 1. Whereas analog value ranges from 0 to 1024.

Material Required:

  • Raspberry Pi with screen, keyboard and mouse (Raspberry Pi 4 | 4GB variant used in tutorial)
  • Soil-moisture sensor
  • Red and Green Led

Circuit Diagram:


NOTE: The pins connection are as:

  • soil moisture sensor —> GPIO14 | PIN 8
  • RGB Red —> GPIO12 | PIN 32
  • RGB Green —> GPIO 16 | PIN 36
  • Rest are Power connection VCC —> PIN 2 & 4, GND —> PIN 6

Programming Pi :

First we import RPi.GPIO module. Then we set mode to BOARD numbering.

After that we define 3 pins 1 as input and rest as output with initial state of Pins as low.


Then in While loop we put try and error concept to terminate the program by using Ctrl+C command and gives the condition for output.

As in my previous article on IR Sensor, I have clearly defined about the reverse output we are getting due to non-inverting Op-Amp circuit using LM393. So, we have to reverse the output and input by using ‘not’ as prefix.

print(not gp.input(8))  
 gp.output(32,not gp.input(8))    

Here is the complete code:

import RPi.GPIO as gp  
 while True:  
     print(not gp.input(8))  
     gp.output(32,not gp.input(8))   
   except :  

The working video of this is attached below. Have a look at it and see it’s working for better understanding.

With this, we end our today’s tutorial of Raspberry Pi soil moisture sensor. I hope you like it. For any queries, ask me below.


Want to make a Pi HAT for your Project to make it into professional ones. Then take a look at NextPCB service, they provide awesome PCB manufacturing for 1,2,4 and more layer PCB as a reasonable price. Along with this, they also provide PCB Assembly service for SMD components, which you may find hard to solder on. Under such price they provide multiple PCB continuity testing, AOI & X-Ray testing for PCB in different colors. So If you want a professional looking PCB for your project, then just look up at NextPCB.