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’ll learn how to use the Raspberry Pi Pico W to measure soil moisture levels using a capacitive sensor and an ADC (Analog to Digital Converter). This beginner-friendly project will introduce you to handling analog signals in MicroPython.

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

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

Raspberry Pi Pico W

BUY

Capacitive Soil Moisture Module

BUY

Breadboard

BUY

Wiring

../_images/Lesson_02_Capacitive_Soil_Moisture_Module_bb.png

Code

from machine import ADC
import time

# Initialize an ADC object on GPIO pin 26.
# This is typically used for reading analog signals.
sensor_AO = ADC(26)

# Continuously read and print sensor data.
while True:
    value = sensor_AO.read_u16()  # Read and convert analog value to 16-bit integer
    print("AO:", value)  # Print the analog value

    time.sleep_ms(200)  # Wait for 200 milliseconds before the next read

Code Analysis

  1. Importing Libraries:

    from machine import ADC
    import time
    
  2. ADC Setup:

    sensor_AO = ADC(26)
    

    This code initializes an ADC object on GPIO pin 26. ADC is used to convert analog signals (from analog sensors) to digital data that the microcontroller can process.

  3. Reading Sensor Data in a Loop:

    while True:
        value = sensor_AO.read_u16()
        print("AO:", value)
        time.sleep_ms(200)
    

    The while True loop runs indefinitely, constantly reading data from the sensor. The read_u16() method reads the analog value and converts it to a 16-bit unsigned integer. The print statement displays this value. The time.sleep_ms(200) causes the loop to wait for 200 milliseconds before reading the sensor value again, preventing excessive data readings and console output.