What is Soil Moisture sensor
The Soil Moisture Sensor module is a simple sensor that measures the soil moisture. When the soil moisture is insufficient, the output value of the sensor will decrease; on the other hand, the value will increase when there’s enough water. The surface of the sensor is gilded to prolong its life. The CM Module consists of a comparator LM393 and extremely simple external circuits. When using the module, you can set a threshold via the blue potentiometer beforehand. When the input analog value reaches the threshold, the digital pin S will output a Low level.
The schematic diagram
Soil moisture sensor Module |
1 | Analog output |
2 | Analog output |
CM Module |
1 | Analog output |
2 | Analog output |
S | Digital output |
A | Analog output |
Operating steps
1. First, we need to prepare the following experimental materials:
1 * microbit
1 * microbit expansion board
1 * USB cable
1 * CM Module
1 * Soil Moisture sensor
- Several Jumper wires
connection diagram:
Experimental code
MU python:
#----------------------------------------------------------- # File name : Waterlevel.py # Description : Waterlevel sensor. # Author : jason # E-mail : jason@adeept.com # Website : www.adeept.com # Date : 2019/01/03 #----------------------------------------------------------- from microbit import * while True: # define potentiometer pin waterlevel = pin0.read_analog() print(waterlevel) sleep(1000)
Click “Download” and download the code onto the micro:bit.
When we insert the moisture sensor into the soil, we can see the changes of the soil moisture on the serial port.
Components
- 1 * Adeept Arduino UNO R3 Board
- 1 * Soil Moisture Sensor Module
- 1 * CM Module
- 1 * USB Cable
- 1 * 4-Pin Wires
- 1 * 2-Pin Female to Female Wires
Adeept UNO R3 Board | CM Module Soil | Moisture Sensor Module |
D8 | S | |
A0 | A | |
5V | + | |
GND | - | |
1 | 2 | |
2 | 1 |
Arduino code:
/*********************************************************** File name: _34_SoilMoistureModule.ino Description: Now, when UNO collect soil moisture module data, you will see the data in the serial monitor Website: www.adeept.com E-mail: support@adeept.com Author: Tom Date: 2019/01/03 ***********************************************************/ int soilmoisturePin = 0; // soil moisture module A pin connected to analog pin 0 void setup() { pinMode(soilmoisturePin, INPUT); //Set Analog 0 port mode, the INPUT for the input Serial.begin(9600); // opens serial port, sets data rate to 9600 bps } void loop() { Serial.print("Soil moisture data: "); //send data to the serial monitor Serial.println(analogRead(0)); //send data to the serial monitor delay(1000); //delay 1 s }
Compile and download the sketch to the UNO R3 board
Components
- 1 * Raspberry Pi
- 1 * GPIO Extension Board
- 1 * 40-Pin GPIO Cable
- 1 * Breadboard
- 1 * Soil Moisture Sensor Module
- 1 * LM393 CM Module
- 1 * ADC0832 Module
- 1 * 2-Pin Wires
- 1 * 3-Pin Wires
- 1 * 4-Pin Wires
- 1 * 5-Pin Wires
c code:
/* * File name : soilMoisture.c * Description : . * Website : www.adeept.com * E-mail : support@adeept.com * Author : Jason * Date : 2019/01/03 */ #include <wiringPi.h> #include <stdio.h> typedef unsigned char uchar; typedef unsigned int uint; #define ADC_CS 0 #define ADC_DIO 1 #define ADC_CLK 2 uchar get_ADC_Result(void) { //10:CH0 //11:CH1 uchar i; uchar dat1=0, dat2=0; digitalWrite(ADC_CS, 0); digitalWrite(ADC_CLK,0); digitalWrite(ADC_DIO,1); delayMicroseconds(2); digitalWrite(ADC_CLK,1); delayMicroseconds(2); digitalWrite(ADC_CLK,0); digitalWrite(ADC_DIO,1); delayMicroseconds(2); //CH0 10 digitalWrite(ADC_CLK,1); delayMicroseconds(2); digitalWrite(ADC_CLK,0); digitalWrite(ADC_DIO,0); delayMicroseconds(2); //CH0 0 digitalWrite(ADC_CLK,1); digitalWrite(ADC_DIO,1); delayMicroseconds(2); digitalWrite(ADC_CLK,0); digitalWrite(ADC_DIO,1); delayMicroseconds(2); for(i=0;i<8;i++) { digitalWrite(ADC_CLK,1); delayMicroseconds(2); digitalWrite(ADC_CLK,0); delayMicroseconds(2); pinMode(ADC_DIO, INPUT); dat1=dat1<<1 | digitalRead(ADC_DIO); } for(i=0;i<8;i++) { dat2 = dat2 | ((uchar)(digitalRead(ADC_DIO))<<i); digitalWrite(ADC_CLK,1); delayMicroseconds(2); digitalWrite(ADC_CLK,0); delayMicroseconds(2); } digitalWrite(ADC_CS,1); pinMode(ADC_DIO, OUTPUT); return(dat1==dat2) ? dat1 : 0; } int main(void) { uchar adcVal; uchar moi; if(wiringPiSetup() == -1){ printf("setup wiringPi failed !"); return 1; } pinMode(ADC_CS, OUTPUT); pinMode(ADC_CLK, OUTPUT); while(1){ pinMode(ADC_DIO, OUTPUT); adcVal = get_ADC_Result(); moi = 255 - adcVal; printf("analog value: %03d moisture: %d\n", adcVal, moi); delay(100); } return 0; }
Python code:
#!/usr/bin/env python import ADC0832 import time def init(): ADC0832.setup() def loop(): while True: res = ADC0832.getResult() moisture = 255 - res print 'analog value: %03d moisture: %d' %(res, moisture) time.sleep(0.1) if __name__ == '__main__': init() try: loop() except KeyboardInterrupt: ADC0832.destroy() print 'The end !'
Step 2: Edit and save the code with vim or nano. (code path: /home/Adeept_Sensor_Kit_for_RPi_C_Code/31_soilMoisture/soilMoisture.c) Step 3: Compile
$ sudo gcc soilMoisture.c -o soilMoisture -lwiringPi
Step 4: Run
$ sudo ./soilMoisture
For Python users: Step 2: Edit and save the code with vim or nano.
(code path: /home/Adeept_Sensor_Kit_for_RPi_Python_Code/31_soilMoisture.py)
Step 3: Run
$ sudo python 31_soilMoisture.py
Plug the sensor into the soil, you will see the value of soil moisture collected by the module displayed on the terminal.
Data link: http://www.adeept.com/learn/
Video: http://www.adeept.com/video/