Commit 05b32105 authored by Mason Johnson's avatar Mason Johnson

website update

parent 98c9e117
Pipeline #791 passed with stages
in 1 minute and 2 seconds
......@@ -4,30 +4,89 @@ For our last day, we're going to turn the Micro:bit into a plant watering sensor
## Understanding the Sensor
The moisture sensor you were sent in your kit is called a _capacitive_ moisture sensor.
The moisture sensor you have in your kit is called a _capacitive_ moisture sensor. It can sense _relative_ moisture of the environment it is placed in. Let's learn a little bit about how it works!
### How It Works
Our capactive moisture sensor works by measuring the ability of whatever material it is placed in to hold a charge. The ability of a material to hold an electrical charge is called that material's _permittivity_. Water is able to store a charge really well, so it has a high _permittivity_, while soil and air are not able to store a charge very vell and have a low _permittivity_.
If we start with dry soil and low permittivity, and then add water to the soil, the permittivity of the water and soil mixture increases! So, as the proportion of water and soil changes, the values our sensor reports also change.
### Reading Data
Yesterday we talked about digital versus analog signals. This moisture sensor gives us an **analog** signal. Remember that analog signals convey information by increasing and decreasing the amount of power that can flow through a circuit and that the Micro:bit translates the amount of power it is detecting into a number from 0 to 1023, with 0 being no power and 1023 being the maximum power it can detect.
Let's connect the sensor to the Micro:bit and see if we can read a value!
In your kit, you have the capacitive moisture sensor and two cables. We aren't going to use the cable with the yellow wire, so you can set that aside. We're going to attach the cable with three alligator clips (white, red, and black) to the sensor. **The connectors do not match up perfectly**. The connector on the alligator clips is a little bit smaller than the receiving end on the sensor. To attach the cable to the sensor, you can pinch the pins on the sensor together slightly using pliers or just use the connector to push in the end pins slightly. You want the connection to look like this:
![Sensor Connection](pictures/connection.jpg)
Once you have the cable connected, attach the alligator clips to the Micro:bit pins as follows:
- **White** alligator clip to **P0**
- **Black** alligator clip to **GND**
- **Red** alligator clip to **3V**
![Alligator Clips](pictures/alligator.jpg)
Now that we are all connected, let's read values from the sensor to make sure it's working! Start a new project in Makecode called _Moisture Sensor_.
Let's use an `on button A pressed` block to read a value each time we press the `A` button. To read a value, we need an `analog read pin P0` block, which gives us a number between 0 and 1023. Let's put that in a `show number` block! You should have something that looks like this:
![Read value code](pictures/read-value.png)
Download the program to your Micro:bit and then test it out! Each time you push the `A` button, you should see a number between 0 and 1023!
### Calibrating the Sensor
In order to understand what the numbers the sensor is giving us mean, we need to do some _calibration_.
Write down the number your sensor is reporting while it is completely dry in open air. My sensor gave me 874, but your number may be different depending on the sensor!
## Calibrating the Sensor
Put the sensor into a cup of water, being careful to not submerge the metal components at the top of the sensor. If your sensor has a white line going across, don't submerge it past that line. Write down the number your sensor reports with it submerged in water. My sensor gave me 405 but yours may be different!
### Finding the High Value
Now, we have the number that the sensor reports when it is completely dry and when it is completely wet! We could say that when the sensor is reporting our high number, there is 0% moisture. When it is showing the low number, there is 100% moisture. If the number is halfway between our high and low number, it would be sensing 50% moisture!
### Finding the Low Value
We can _scale_ our numbers to be between 0 and 100, giving us the percentage of the soil is water. In the `Pins` tab, there is a very large block called `map`. We can use this block to _scale_ our numbers from 0 to 100. We're going to store the result of the `scale` block in a variable. Here's an example; make sure to use the numbers that you wrote down instead of the ones I got!
### Scaling the Values
![Scaling Sensor Values](pictures/scale.png)
Notice that we mapped our low number to 100 and our high number to 0. This is because the lower our number was, the more water was being sensed. However, we want to know the percent of water the sensor is reporting, meaning 100% should be completely wet!
Also notice that I put the `map` block into a `round` block from the `Math` tab. This just shows us a whole number as the percentage, we don't need the decimal.
Download your program to your Micro:bit and test it! Now, when the sensor is completely dry and you press `A`, you should see `0`, meaning there is 0% moisture. If you put your sensor back into a cup of water and press `A` you should see `100`, meaning there is 100% moisture.
If your numbers aren't exactly 0 and 100, that's okay. If they are more than 3 or 4 off, go through the calibration process again, finding the correct high and low numbers.
## Planting
Now that our sensor can tell us how much water it is sensing, let's get ready to plant our seed!
### Preparing the Pot
Fill your compostable pot with soil. Water the soil with about 1/4 cup of water and mix the soil well. Once the soil is mixed well, put your sensor in the pot and take a reading! My soil was about 55% water according to the sensor. Your reading might be different depending on how much water you used and what type of soil you have.
Now let's plant a seed! Poke the top of the soil with your finger to make a little indent. Place a seed in the hole and cover it with soil.
![Planting the Seed](pictures/planting.jpg)
### Preparing the Planter
### Ideal Moisture Levels
### Finding Ideal Moisture
For our plant, we want to keep the soil moisture between 25% and 65%, so let's program our Micro:bit to tell us when it's time to water our plant!
## Finishing the Program
### Looping While Reading
Instead of having to push the `A` button every time we want the sensor to read the moisture level, let's move our blocks into a `forever` block, like this:
![Reading the Sensor Forever](pictures/forever-read.png)
I put in a `pause` block that pauses the program for 5 seconds after it reads the sensor. This will help the Micro:bit last longer if you are powering it with batteries.
Now, instead of just showing the water percentage on the screen, we want the Micro:bit to tell us when to water the plant. Let's use an `if-else` statement to display "Water me!" when the moisture level goes below 25% and a smiley face when it's above 25%.
Your code should look similar to this:
![Final Sensor Code](pictures/final.png)
### Displaying Results
## Conclusion
Congratulations! You built a program that can tell you when you need to water a plant. You had to use **variables**, **control flow** statements such as an **if/else** statement and the **forever** loop, as well as use what you've learned about **electrical circuits**! You know enough about computer science, programming, Micro:bits, and electricity to go out and build hundreds of other cool things. Have fun with your Micro:bits!
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment