Introduction: Micro:bit Enabled Plant Watering System
Welcome to the InkSmith Advanced Agriculture Instructable! In this lesson, we'll be showing you how to build and code a micro:bit enabled automatic plant watering system.
This lesson requires the InkSmith Land Climate Action Kit and a micro:bit. Additional resources and classroom materials are available online.
Learning Goal:
By the end of this lesson, you will use design and computational thinking to build and code an automated watering system. You will learn how to:
- Build an automatic watering system with a micro:bit and Climate Action Kit so water will be pumped into the plant when a button is pressed.
- Enhance the automatic watering system, so the system will automatically detect when the plan needs watering and turn on the watering system.
At A Glance:
Students will design and physically build an automated watering system with a micro:bit and Climate Action Kit components (sensor, alligator clips, etc.) included in the Climate Action Kit. Through this build, students will develop their digital literacy skills through coding and physical computing.
This lesson is best suited for an Intermediate Skills Level of Coding and Physical Computing.
Supplies
- 1 Climate Action Kit (Includes: Breakout Board, Alligator Clips, DC Water Pump, DC Motor, Moisture Sensor, 360 Servo Motor, Touch Sensor, and Water Tubing)
- 1 micro:bit
- 1 empty cup or glass
- 1 cup or glass filled with water
- 1 towel
- 1 plant with dry soil (optional)
- 1 plant with wet soil (optional)
Step 1: Set Up the Pump & Moisture Sensor
The DC Water Pump should be attached to one of the two sets of positive (+) & negative (-) connectors at the top of the breakout board. Remember which set you connect your pump to, as you'll need to add that to your code later! Details on connect the pump will be in the final step.
The red wire on the pump is positive, and black is negative.
The Water (moisture) Sensor has three connections.
- The + (power) will connect to any of the power (lightning bolt) connectors on the breakout board.
- The - (ground) connects to any of the ground symbol connectors on the breakout board.
- The S (signal) will connect to 0.
Remember which pin you attach the sensor to so you can add that to the code later!
You can find more detailed set-up instructions supplied in your kit.
Step 2: Start a New Project in MakeCode & Install the Climate Action Kit Block "Drawers"
To begin coding open up MakeCode on a computer or tablet, click "New Project" then give it a name! When you're ready, click "Create".
For this project, we'll need to install some custom code blocks (see gif above). To this you'll need to:
- Click "Advanced" in the block menu.
- Scroll down and click "Extensions" at the bottom.
- Search for "Climate Action Kit" and "climate-action-kit" should be the only result.
- Click climate-action-kit to install, or "Learn More" if you want to learn more about the extension.
Now that the Climate Action Kit extension is installed, you should see several extra menu items to select including: servos, soil, motor, pump, and touch.
Step 3: Write the Code for the Water Sensor
The first part of the code instructs the system not to activate if the moisture sensor is wet.
We'll need to start by looking in the logic section for an "if" statement. An if statement will only perform an action "if" the code sees the thing we tell it to look for. We're going to us the "if/else" block, so that we can give a further instruction when the thing we tell the statement to look for isn't happening.
Block code needs to know when to do the action your telling it to do. So if you just drag the "if/else" statement somewhere in the code area, it will grey out. The action can take place:
- On Start - Just once when you start the microbit.
- Forever - Constantly as long as the Microbit is running.
- On Action - when something happens (button pushed, micro:bit tilted, temperature changes, volume increases, etc)
We'll use the forever loop, as we want the micro:bit to always pay attention to the plant and supply it with water when needed.
Finally we need to tell the if statement what it needs to look for. Click into the "Soil" menu, and you'll see 3 different options with different sides (rounded, pointed, and flat). If you look at the if statement, you'll notice that "True" is in a shape that has the pointed sides. Grab the matching shape variable and drag it over the "true" in the if statement.
Set your pin according to where you connected your moisture sensor, and the moisture level to wet or very wet, depending on your plants preference.
Now we need to tell the if statement what to do when the plant is full of water. Go to the "Basic" menu, and drag either the "show leds" or "show icon" under the if command. Draw an image, or set the icon to whatever you would like to show when the plant is watered.
Step 4: Write the Code for the Pump
The second part of the code instructs the pump to activate if the moisture sensor is not wet.
While we're in the "Basic" menu, drag out the "show icon" block, and place it under the "else" portion of the if statement. You can also drag out "show leds" and draw whatever image you would like to show when your plant is being water.
You can also drag out the "clear screen" function from the Basic section, and place it under the block you just placed. This will turn off all the LEDs at the end of the else function.
Finally, go to the "Pump" menu, and dragging out the "run" pump command and place between the "show icon" and "clear screen" blocks. (see image above)
Set left/right accordingly to where you connected your pump to the breakout board.
Set the speed you want to put to run at, and duration. We suggestion starting at speed 60, for 4 seconds, and testing into a cup to see how much water flow you get. Then you can dial in the numbers for your plant.
Once your code is complete, it's time to try it out!
Step 5: Upload Your Code
Now it's time to get the code from your computer to your micro:bit!
- Attach your micro:bit to your computer using a USB cord. It should appear on your computer the same as if you'd plugged in a USB drive.
- In your program on MakeCode, click Download or the save icon to download a .hex file containing your code onto your computer.
- Open the folder on your computer containing the .hex file, and drag it into the micro:bit drive.
- Once it's done copying, your micro:bit is ready to use!
You can find more details on installing your code onto your micro:bit here.
Don't forget to repeat this step any time you edit or update code!
Step 6: Test Your Code and Automated Watering System
Ensure the switch on the Breakout board is in the OFF position until you are ready to test.
Unplug the micro:bit from the computer.
Attach the tubing to the pump and place the pump securely inside a cup full of water. Place the other end of the tubing securely in the empty cup to collect the water that the pump pours out.
Place the micro:bit, LED screen facing forward, into the slot on the Breakout Board, and attach the white USB cable to your computer.
Move the switch on the Breakout board to the ON position.
The umbrella (or whatever image you used in the "else" portion of the if statement) should appear and the pump should turn on repeatedly, since the Water Sensor does not detect water.
Place the water sensor in the cup of water and test again - the micro:bit should now show a happy face.
Note: You may have to adjust the code if the pump is not pumping enough water (remember to upload the code to the micro:bit if you make any change to the code).
Optional: You can also test the system by placing the moisture sensor using a plant with wet soil and a plant with dry soil.
(Important: Please be careful not to spill the water near your computer - be sure to have a towel ready!)


