Raindrop Detection

🎮 Display the Raw Sensor Reading
Goal
Read the raindrop sensor and display its raw analog value on the PC.
Concepts
Analog Input · ADC · Water Detection · Loop · Serial Output
import machine # Controls the board hardware
import time # Controls pauses and timing
# Sets up GPIO2 as an analog input for the rain 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
water = adc2.read_u16() # Reads the sensor value
print(water) # Sends the raw value to the PC
time.sleep(0.5) # Waits before reading again 🎮 Dry or Wet
Goal
Interpret the sensor reading and display Dry or Wet on the PC.
Concepts
Analog Input · Threshold · If/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 rain 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
threshold = 32768 # Sets the wet/dry threshold
while True: # Repeats forever
water = adc2.read_u16() # Reads the sensor value
if water < threshold: # Checks if water is detected
print('Wet') # Reports a wet sensor
else: # Runs when the sensor is dry
print('Dry') # Reports a dry sensor
time.sleep(0.5) # Waits before reading again 🎮 Detect Dry to Wet
Goal
Detect when the sensor changes from dry to wet and report Water detected only when that transition happens.
Concepts
Analog Input · Threshold · Variables · State Change · Serial
import machine # Controls the board hardware
import time # Controls pauses and timing
# Sets up GPIO2 as an analog input for the rain 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
threshold = 32768 # Sets the wet/dry threshold
last_state = 'Dry' # Stores the previous sensor state
while True: # Repeats forever
water = adc2.read_u16() # Reads the sensor value
if water < threshold: # Checks if the sensor is wet
state = 'Wet' # Saves the wet state
else: # Runs when the sensor is dry
state = 'Dry' # Saves the dry state
if state == 'Wet' and last_state == 'Dry': # Checks for a new wet event
print('Water detected') # Reports the new detection
last_state = state # Saves the current state
time.sleep(0.1) # Waits before reading again 🎮 Measure How Long Water Is Detected
Goal Measure how long the sensor remains wet and display the duration on the PC.
Concepts
Analog Input · Threshold · Timing · Variables · Serial Output
import machine # Controls the board hardware
import time # Controls pauses and timing
# Sets up GPIO2 as an analog input for the rain 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
threshold = 32768 # Sets the wet/dry threshold
while True: # Repeats forever
water = adc2.read_u16() # Reads the sensor value
if water < threshold: # Checks if water is detected
start = time.ticks_ms() # Saves the wet start time
print('Water detected') # Reports the wet condition
while adc2.read_u16() < threshold: # Waits while it stays wet
time.sleep(0.1) # Adds a short pause
end = time.ticks_ms() # Saves the dry time
duration = time.ticks_diff(end, start) # Calculates wet duration
print('Water cleared') # Reports that the sensor is dry
print('Wet time:', duration, 'ms') # Shows the wet duration
time.sleep(0.1) # Waits before checking again 🎮 Count and Measure Wet Events
Goal Count separate wet events and report the duration of each event to the PC. Concepts Analog Input · Threshold · Counter · Timing · Variables · Serial Output
import machine # Controls the board hardware
import time # Controls pauses and timing
# Sets up GPIO2 as an analog input for the rain 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
threshold = 32768 # Sets the wet/dry threshold
count = 0 # Stores the number of wet events
while True: # Repeats forever
water = adc2.read_u16() # Reads the sensor value
if water < threshold: # Checks if a wet event begins
count += 1 # Adds one to the event counter
start = time.ticks_ms() # Saves the wet start time
print('Wet event:', count) # Shows the event number
print('Water detected') # Reports the wet condition
while adc2.read_u16() < threshold: # Waits while it stays wet
time.sleep(0.1) # Adds a short pause
end = time.ticks_ms() # Saves the event end time
duration = time.ticks_diff(end, start) # Calculates wet duration
print('Duration:', duration, 'ms') # Shows the event duration
print('Water cleared') # Reports that the sensor is dry
print('---') # Separates each event
time.sleep(0.1) # Waits before checking again 