Soil Moisture Alarm Using CircuitPython on Seeeduino XIAO

Soil Moisture Alarm with Coffin Dance melody.

Introduction

Continuing from last week’s tutorial – Program Seeeduino XIAO Using CircuitPython, this time we will interface the analog capacitive soil moisture sensor and produce a tone melody using CircuitPython on Seeeduino XIAO. The application is simple, when the soil is dry, the alarm will be activated.

Video

This video shows how to program capacitive soil moisture sensor and speaker using CircuitPython on Seeeduino XIAO.

Hardware Preparation

This is the list of items used in the video.

Sample Program

This is CircuitPython sample program for Seeeduino XIAO to read an analog data at pin A2 and play melody at pin A3.

import time
import board
import simpleio
import digitalio
from analogio import AnalogIn
led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT
piezo = board.A3
sensor = AnalogIn(board.A2)
NOTE_C4 = 262
NOTE_D4 = 294
NOTE_E4 = 330
NOTE_F4 = 349
NOTE_G4 = 392
NOTE_A4 = 440
NOTE_E5 = 659
NOTE_F5 = 698
# Define a list of tones/music notes to play.
COFFIN_DANCE = [
NOTE_D4, 1, NOTE_D4, NOTE_A4, NOTE_G4, 1, NOTE_F4, 1, NOTE_E4, 1,
NOTE_E4, NOTE_F4, NOTE_G4, 1, NOTE_F4, NOTE_E4, NOTE_D4, 1,
NOTE_D4, NOTE_F5, NOTE_E5, NOTE_F5, NOTE_E5, NOTE_F5, NOTE_D4, 1,
NOTE_D4, NOTE_F5, NOTE_E5, NOTE_F5, NOTE_E5, NOTE_F5
]
NOTE_DURATIONS = [
4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4,
]
# Play start melody
simpleio.tone(piezo, NOTE_C4, duration=0.1)
simpleio.tone(piezo, NOTE_G4, duration=0.1)
prev_time = time.monotonic()
alarm = False
# Main loop will go through each tone in order up and down.
while True:
if time.monotonic() prev_time > 5:
adc = sensor.value
led_count = adc/6553
if led_count > 6:
alarm = True
else:
alarm = False
for i in range(led_count):
led.value = False
time.sleep(0.05)
led.value = True
time.sleep(0.5)
prev_time = time.monotonic()
if alarm == True:
for i in range(len(COFFIN_DANCE)):
simpleio.tone(piezo, COFFIN_DANCE[i], duration=1/NOTE_DURATIONS[i])

Thank You

References:

Thanks for reading this tutorial. If you have any technical inquiries, please post at Cytron Technical Forum.

Please be reminded, this tutorial is prepared for you to try and learn.
You are encouraged to improve the code for better application.

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

Simple Alarm System Using Maker Nano
Program Cucumber ESP32-S2 Using CircuitPython
Face Recognition Using OpenCV on Raspberry Pi 400
Turned Your Tablet as a Display for the Raspberry Pi 400
OpenCV Template Matching on Raspberry Pi 400
Scroll to Top