Soil Humidity

🎮 Display the Raw Moisture Reading
Goal
Read the soil moisture sensor and display its raw analog value on the PC.
Concepts
Analog Input · ADC · Moisture · Loop · Serial Output
import machine # Controls the board hardware
import time # Controls pauses and timing
# Sets up GPIO2 as an analog input for the moisture sensor
# GPIO2 corresponds to A0 on the Sensor Shield V5.0
adc2 = machine.ADC(machine.Pin(2))
adc2.atten(machine.ADC.ATTN_11DB) # Sets the analog input range
while True: # Repeats forever
moisture = adc2.read_u16() # Reads the moisture value
print(moisture) # Sends the raw value to the PC
time.sleep(0.5) # Waits before reading again 🎮 Dry, Moist or Wet
Goal
Classify the soil reading as Dry, Moist, or Wet.
Concepts
Analog Input · Thresholds · If/Elif/Else · Classification · Serial Output
import machine # Controls the board hardware
import time # Controls pauses and timing
# Sets up GPIO2 as an analog input for the moisture sensor
# GPIO2 corresponds to A0 on the Sensor Shield V5.0
adc2 = machine.ADC(machine.Pin(2))
adc2.atten(machine.ADC.ATTN_11DB) # Sets the analog input range
while True: # Repeats forever
moisture = adc2.read_u16() # Reads the moisture value
if moisture > 45000: # Checks the dry range
print('Dry') # Reports dry soil
elif moisture > 25000: # Checks the moist range
print('Moist') # Reports moist soil
else: # Runs for the wet range
print('Wet') # Reports wet soil
time.sleep(0.5) # Waits before reading again 🎮 Detect Changes Between Moisture Ranges
Goal
Report when the soil changes between Dry, Moist, and Wet.
Concepts
Analog Input · Thresholds · Variables · State Change · Serial Output
import machine # Controls the board hardware
import time # Controls pauses and timing
# Sets up GPIO2 as an analog input for the moisture sensor
# GPIO2 corresponds to A0 on the Sensor Shield V5.0
adc2 = machine.ADC(machine.Pin(2))
adc2.atten(machine.ADC.ATTN_11DB) # Sets the analog input range
last_state = '' # Stores the previous moisture state
while True: # Repeats forever
moisture = adc2.read_u16() # Reads the moisture value
if moisture > 45000: # Checks the dry range
state = 'Dry' # Saves the dry state
elif moisture > 25000: # Checks the moist range
state = 'Moist' # Saves the moist state
else: # Runs for the wet range
state = 'Wet' # Saves the wet state
if state != last_state: # Checks if the state changed
print('Changed to:', state) # Reports the new state
last_state = state # Saves the new state
time.sleep(0.5) # Waits before reading again 🎮 Track Moisture Changes Over Time
Goal
Compare each moisture reading with the previous reading and show whether the value increased, decreased, or stayed nearly the same.
Concepts
Analog Input · Variables · Comparison · Change Over Time · Serial Output
import machine # Controls the board hardware
import time # Controls pauses and timing
# Sets up GPIO2 as an analog input for the moisture sensor
# GPIO2 corresponds to A0 on the Sensor Shield V5.0
adc2 = machine.ADC(machine.Pin(2))
adc2.atten(machine.ADC.ATTN_11DB) # Sets the analog input range
last_value = adc2.read_u16() # Saves the first reading
while True: # Repeats forever
moisture = adc2.read_u16() # Reads the current value
change = moisture - last_value # Calculates the change
print('Reading:', moisture) # Shows the current reading
print('Change:', change) # Shows the amount of change
if change > 500: # Checks for a clear increase
print('Value increased') # Reports the increase
elif change < -500: # Checks for a clear decrease
print('Value decreased') # Reports the decrease
else: # Runs when the change is small
print('Almost no change') # Reports a stable reading
last_value = moisture # Saves the current reading
time.sleep(1) # Waits before reading again 🎮 Soil Monitoring Report
Goal
Create a soil monitor that displays the current reading, moisture classification, change from the previous reading, and time spent in the current condition.
Concepts
Analog Input · Thresholds · Variables · Timing · State Change · Serial Output
import machine # Controls the board hardware
import time # Controls pauses and timing
# Sets up GPIO2 as an analog input for the moisture sensor
# GPIO2 corresponds to A0 on the Sensor Shield V5.0
adc2 = machine.ADC(machine.Pin(2))
adc2.atten(machine.ADC.ATTN_11DB) # Sets the analog input range
last_value = adc2.read_u16() # Saves the first reading
last_state = '' # Stores the previous moisture state
start_time = time.ticks_ms() # Saves the starting time
while True: # Repeats forever
moisture = adc2.read_u16() # Reads the moisture value
change = moisture - last_value # Calculates the reading change
if moisture > 45000: # Checks the dry range
state = 'Dry' # Saves the dry state
elif moisture > 25000: # Checks the moist range
state = 'Moist' # Saves the moist state
else: # Runs for the wet range
state = 'Wet' # Saves the wet state
if state != last_state: # Checks if the condition changed
start_time = time.ticks_ms() # Starts timing the new condition
last_state = state # Saves the new condition
time_in_state = time.ticks_diff(time.ticks_ms(), start_time) # Gets time in this condition
print('Reading:', moisture) # Shows the current reading
print('Condition:', state) # Shows Dry, Moist or Wet
print('Change:', change) # Shows the reading change
print('Time:', time_in_state, 'ms') # Shows time in this condition
print('---') # Separates each report
last_value = moisture # Saves the current reading
time.sleep(1) # Waits before reading again 