Soil Moisture Sensor for micro:bit
This project uses a moisture sensor to create a simple device to warn when soil becomes too dry.
Concepts: displaying images; using digital & analogue pins
- BBC micro:bit
- YL-38 YL-69 moisture sensor
- Connecting wires
- Prototyping breadboard
Step by Step Instructions
- Connect the two pins on the YL-69 probe to the connectors at the top of the YL-38 module board.
It doesn’t matter which way you make these connections.
- Connect GND (0V) on the micro:bit to GND on the YL-38
- Connect PIN 2 on the micro:bit to Vcc on the YL-38
- Connect PIN 3 on the micro:bit to A0 on the YL-38
PIN 2 is used to turn on the circuit so that a reading can be taken.
PIN 3 is used to read the analogue signal from the sensor. This is a voltage that is high when the moisture content is low and lower when the moisture content is high.
- Using your preferred editor, type or copy/paste the code and flash to the micro:bit.
from microbit import * # sensor reading above which the soil needs watering threshold = 500 # time in milliseconds between readings interval = 30000 while True: # turn on sensor (pin 2 connected to Vcc on sensor) pin2.write_digital(1) # wait for sensor to settle sleep(5000) # read analog pin 3 (connected to A0 on sensor) moist = pin3.read_analog() # print reading to console print(str(moist)) # needs watering if moist > threshold: # show sad face display.show(Image.SAD, clear=False) else: # show happy face display.show(Image.HAPPY, clear=False) # turn off sensor and sleep to conserve sensor contacts pin2.write_digital(0) sleep(interval) # end
- In our example we have used both a breakout board for the micro:bit and a prototyping breadboard to make the connections. In theory you could connect the sensor directly to the micro:bit using crocodile clips but this is likely to be unreliable in practice.
- Add code to query the last reading taken when button A on the micro:bit is pressed.
- Store the last 5 readings and present these as a bar chart using the LEDs on the micro:bit when button B is pressed.