The Micro:bit is great for small portable projects like a soil moisture sensor. In this tutorial, we will walk through how to program a Micro:bit to make a reliable and consistent soil moisture sensor that will give consistent readings. The goal of this project is to create a program that will give consistent readings between plants. We will be using MakeCode to program the Micro:bit.
Hi, Steven here from Core Electronics, in this tutorial I'm going to show you how to make a soil moisture sensor using the Micro-Bit and a soil moisture sensor by SparkFun.
When the soil moisture sensor is whenever it's on that it's going to be taking soil moisture readings and displaying a logo of your choice to represent how wet or dry it is. So right now in the air it's very dry and I'll mimic wet soil just by putting my hands on it and somewhere in between there'll be a middle moisture sensor reading, I think we can see it there it's an I've tried to make a pot logo and it would like a pot plant water filled or empty and we've also added another feature where if you press button A, it will give you a number reading that represents the moisture of the sensor between approximately 0 and 100, so let's take a look at the code.
So, as we open up MakeCode we're going to have a forever and an "on start" block we're just going to get rid of the on start and we're not because we're not going to use it and we'll come back to the forever later so I'm just going to drag it out of the way. First, we're going to get the on-button A pressed, so we're going to build in the feature where when button is pressed it will display the number and just the quick word about how this is assembled. The SparkFun sensor that we recommend in the tutorial has terminal block attachments and we just used alligator clips with the pins in the terminal blocks tightened in with a screw, so no soldering required and the ground line as indicated on the sensor will go to ground on the Micro-Bit the VCC is power it will go to 3 volts and the signal will go to pin zero. So, to start out with this code first we want to we want to take a reading so if we look at our completed clip here, we're going to set moisture reading to be an analog rip in zero so the first thing we're going to do is take a moisture reading. So you will need to make a variable so first off hit make a variable and name it moisture reading I've already created one here and once you've created one you'll get these block options so I'm going to set moisture reading to the advanced pin analog read pin 0 and this reads the data coming in from the external sensor and then under basic they'll be a show number, we're going to set it to the variable that we just saved moisture reading and then afterwards we'll clear the screen.
So under basic and more and then clear screen and this is the simplest form of this code is that when button A is pressed it'll take a reading and display it on the screen but this will return a really wide range of readings between like 1 and 1024 and it's kind of hard to imagine those readings is something tangible, so what we want to do is we want to bring it down to just be between 0 and 100 which is a little easier to understand. So, we're going to use math and use the map tool, so I'm going to remove the analog read pin zero and replace this map in our on our variable set block and then put analog read in the first field. So we're going to map the reading from analog read from 0 to 1023 and make it beachfront be between 0 and 100 so what that means is we know the reading is going to be between 0 and 1023 but we want to we want it to appear as being between 0 and 100 so this block shifts all those numbers to be in their relative position between 0 and 100s so, zeros still zero, 1000 will become 100, 512 will become 50 but one of the disadvantages to not calibrating a sensor every time, this will be using an uncalibrated sensor for this project is that we don't know what the true high value reading is because it's different a little bit from sensor to sensor and it changes based on whether you're using 5 volts to power the sensor or 3 volts like the Micro-Bit bit but I know from experience that the high reading from this sensor powered off the Micro-Bit is going to be about 750, so I'm going to just set it to 750 and you can um test this by putting your sets or in water once the codes done and hitting the button and seeing what the raw reading is and then set your code to match.
So now we're going to this will be remapping the moisture reading from 0 to 100 and then we just want to use the round bubble, I put it in place of the show number and then put moisture reading inside it so this will make the moisture reading a nice round number because as we're shifting numbers from the range of 0 to 750 to be 0 and 100 it'll give us a lot of decimal places and we don't need that. So there we have it that's section one is button a pressed to give you a number reading displayed on the Micro-Bit and the second half we're going to make an image that always appears on the face of the Micro-Bit that represents how wet the soil is so if you want something that's always in the soil that you can that you can just glance over and look at without needing to push A button to take a number reading. So again we're going to use the set the moisture reading variable - and we're going to do it in order for this time to map analog read from low zero, too high 750 again but this time we wanted to map it to be between 0 and 3 because we're only going to make 3 symbols this just makes it easy to sort the program later on and then we're going to use some logic.
So, we'll go into an if "true" then "else" block and then we're going to grab the conditional, so if moisture reading is less than one then it's going to be really dry, so we're going to get a show LEDs from the basic block and I'm just going to make my pot plant is empty logo here, so that would be our dry reading and then we're going to hit this plus down on the "if" to give us more fields. So we'll get another comparison in here I will hold off on that we're going to need an and block because this next one will need two conditions, so we'll put an and in there so this needs two things to be true in order for this whole section to be true and we'll grab the comparison again we'll grab the comparison so if moisture reading is will say that if it's greater than or equal to one and we'll grab another comparison if moisture reading is less than two. Then we're going to be middle-level moisture that'll be middle of the range, so I'll grab another Shoei LEDs block and I'll make my pot plant it's half filled with water and then the only other condition since we only have three options the range is going to be one, two or three the only other option will be that it's completely wet says because if we're not returning a value that's dry, we're not returning a value that that's in the middle we must be returning a value that's wet. So, we don't need to make another condition for this we can just say else the plant is completely wet and full.
So that's our next section of the code this will display our readings constantly in a graphic format on the LED screen, so I'm just going to download it to device as we can see I've got an empty pot plant the sensors in the air my sort of wet fingers will make the pop plant appear full and if it's on maybe a drier part of my skin then maybe we'd be in somewhere in the middle it kind of shifts around your hands not really the best way to measure moisture with a moisture sensor but you know it'll it allows you to test to see if your program is working a little bit.
All right so that's our soil moisture sensor all you got to do now is get a battery and carry it around and stick it into the soil and take some readings. If you want to learn more about the Micro-Bit, we've got a lot of other Micro-Bit tutorials out there on the website and we're making new ones every day, so check back in and hopefully, we'll see you there.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.