Introduction
Use a micro:bit with a Bit Board to check if your plant needs water, and then water it using a pump.
Video Overview
Featured Document
-
-
A Moisture Sensor is capable of reading the conductivity between two legs of a probe inserted into soil or other material that contains moisture.
-
Basically, a small electrical charge travels from one leg of the probe to the other and the resistance is what is measured.
-
The control board that the probe plugs into can output a voltage which can then be read by an analog pin on a microcontroller.
-
After reading the sensor the micro:bit will show a value between 0 and 1023. In the case of this sensor, 1023 is maximum resistance (or no current flowing) and 0 is no resistance (or maximum current flowing).
-
For our use, 1023 would mean the probe is inserted into dry soil. Not good for a plant! If we get a reading less than 400 it means our soil is pretty wet.
-
-
-
The Moisture Sensor consists of two parts, the YL-38 control board, and the YL-69 probe. The probe connects to the control board with two F/F Jumper Wires, and then the control board connects to the Bit Board with three F/F Jumper wires.
-
We use a + (positive) and - (negative) pin on the Bit Board to power the control board, and then connect the AO pin on the control board to Pin 1 on the Bit Board so we can read the analog value coming from the sensor.
-
-
-
Connect a USB cable to the micro:bit and then plug it into your computer.
-
We'll be using makecode.microbit.org to program our board. It uses a simple drag and drop block interface.
-
We're going to load the following code for our Moisture Sensor Reading program: https://makecode.microbit.org/_CCpgM4Eat...
-
The code is very simple. It's going to turn on the LED matrix on the micro:bit, and then read and show the value it gets from the Moisture Sensor by scrolling it across the LEDs on the micro:bit
-
-
-
Once you've got your Moisture Sensor connected and you load the code you should see a number scroll across the LED matrix. It will probably be “1023” if you haven’t inserted the probe into anything.
-
-
-
We can provide power to the micro:bit and Bit Board by using a USB cable (which we used for programming the micro:bit in a previous step) or by using a 2 AAA Battery Pack.
-
The USB cable provides 5 volt power from a computer or USB wall adapter or battery pack, and the voltage regulator on the micro;bit drops the 5 volts down to 3.3 volts to provide power.
-
A battery pack with two AAA batteries provides just over 3 volts when using two fresh alkaline batteries. This also helps if you want your project to be a bit more portable.
-
-
-
Now that we are seeing readings from the Moisture Sensor, we should insert the probe into some soil!
-
Grab your favorite houseplant and push the probe into the soil. You should start to see a new value scroll across the LED matrix on the micro:bit (unless the soil is too dry!)
-
-
-
For this step it may take a bit of trial and error to get it just right. If your soil is too dry (based on what your plant needs) add some water. You should see the value displayed on the LED matrix get lower.
-
Once you have your plant soil to a state that you consider wet enough, make note of the number. For our plant it was around 700.
-
-
-
Now that we've seen the Moisture Sensor in action, let's try some new code!
-
We're going to load the following code for our Moisture Sensor Alarm program: https://makecode.microbit.org/_RXkYgP7L6...
-
For this version we have two variables to consider: dryPlant and reminderTime
-
The dryPlant variable will be set higher than the number we determined in our calibration step. Since we found 700 to be a good amount of moisture for the soil, we'll use 800 for our threshold.
-
The reminderTime variable will set how often we see the sad face on the LED matrix and hear a beep.
-
And yes! We are going to add an alarm as well. See the next step!
-
-
-
We're going to add a Crazy Circuits Piezo Speaker to our circuit, using some 1/8" Maker Tape.
-
Connect the Piezo Speaker between Pin 8 and GND. (Make note of the polarity!)
-
Now when the soil is too dry (based on the number you assigned to dryPlant you will see a sad face, and hear a beep. This will happen every 10 seconds (or whatever interval you set for the reminderTime variable.)
-
-
-
While these Moisture Sensors are cheap (we got a 5 pack for less than $8) they use resistive measurement, which means that eventually one of the probe legs will corrode to the point that it will no longer work. We've got a good workaround though, using Maker Tape!
-
We used 1/8" Maker Tape and just wrapped a piece around each of the probe legs. Since Maker Tape is conductive on both sides and all the way through, it will conduct and protect at the same time.
-
If you see the Maker Tape start to wear out or corrode, it's a simple process to peel it off, make sure the probe is nice and dry, and then put a new piece in place.
-
-
-
We found a DC powered water pump that was meant to run on 12 volts, but it worked fine on 9 volts so we could easily use a 9 volt battery.
-
Make sure your pump can run using your desired power source, or get a power source that will properly run your pump.
-
Test the pump by putting both hoses into a cup of water and pull one up, and then the other, until you can confirm it is pumping water.
-
You may want to make note of which hose is the input and which is the output. (Though you can easily swap them if you get it wrong .)
-
Also, a bit of good luck... With some standard LEGO bricks we made simple mounts to hold the motor and the 9 volt battery in place on our baseplate.
-
-
-
Since we've already got a functional system using the Moisture Sensor that can check when the soil is too dry all we need to do is add in our pump and change the code.
-
-
-
You'll need something to store and draw water from. We had a red plastic cup on hand and a bit more good luck - easily built a LEGO holder on our baseplate that fit it perfectly.
-
-
-
Since we can't control a DC motor directly from the micro:bit we're going to use the Bit Board's built-in relay, which is basically a "smart switch" we can turn on and off using code.
-
The relay is turned on (and completes the circuit) by turning on Pin 16 of the micro:bit - Basically the same way you would turn on an LED is how you turn on the relay.
-
We've got an entire guide on the Bit Board Relay and how it works. It's worth a read if you've never used a relay before.
-
-
-
Now that we've got our water pump added let's load some new code that will use it!
-
We're going to load the following code for our Plant Watering System program: https://makecode.microbit.org/_LqCDaFXRq...
-
We have a few new variables. The first is waterTime which will set how long the pump runs for. This will affect how much water is added to the soil. You'll need to adjust this for your specific needs.
-
The next one is restTime which will set how long the system waits before checking for water again. This gives the system a break so it doesn't add too much water.
-
Remember, the pause command works with seconds, so if you wanted 1 hour it would be 3600 seconds, 12 hours would be 43200 seconds, and one day (24 hours) would be 86400.
-
If you set the restTime to 86400 they you would make sure your plant never gets watered more than once per day.
-
-
-
Hopefully you've already tested the Moisture Sensor in the first part of this guide, and you've also tested your water pump. Now that you've got it all together, test it again!
-
If you want to "trick" your probe into thinking the soil is dry just pull it out of the soil and the value should go high enough to cause the pump to run.
-
-
-
Like many of our projects, we are working with a cause and effect, and we're using a sensor to collect data and do something based on that input.
-
Feel free to change the code to do other things or alter the behavior of the system. Maybe you want to use two Moisture Sensors for a larger pot so you can average the readings.
-
Maybe the numbers don't matter to you and you just want to see a happy face when the soil is wet enough. Make changes and make this project your own!
-