Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Lesson 02: Capacitive Soil Moisture Module
In this lesson, you will learn how to connect a capacitive soil moisture sensor to an Arduino and interpret its readings. The project includes reading the sensor’s analog output with the Arduino and understanding that lower readings indicate higher soil moisture levels. You’ll gain practical experience in handling analog input and serial communication with the Arduino by using the provided code as a hands-on example.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Universal Maker Sensor Kit |
94 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
Arduino UNO R3 or R4 |
|
Wiring
Code
Code Analysis
Defining the sensor pin:
This line of code declares a constant integer
sensorPinand assigns it the value ofA0, which is the analog input pin the sensor is connected to.const int sensorPin = A0;
Setup function:
The
setup()function is executed once when the program starts. It initializes serial communication at 9600 baud rate. This setup is necessary for sending data to the serial monitor.void setup() { Serial.begin(9600); }
Loop function:
The
loop()function runs continuously aftersetup(). It reads the sensor value from pin A0 usinganalogRead()and prints this value to the serial monitor. Thedelay(500)statement pauses the loop for 500 milliseconds before the next reading, thus controlling the rate of data acquisition.void loop() { Serial.println(analogRead(A0)); delay(500); }