MISSION 17

Moisture Sensor for Your Plants

Plants provide oxygen, they look good, and they smell great. Plants are cool. To keep my plants alive, I’m going to create a sensor that checks whether the soil inside my potted plants is moist.

When soil is wet, it conducts electricity! So we can use the micro:bit and the Circuit Playground Express to keep an eye on the moistness level of our potted plant soil. Try to say this three times: “potted plant soil, potted plant soil, potted plant soil.”

Instead of using the digital read pin, we’re going to use analogue read. Digital read returns either 1 or 0, true or false. I’d like to know how wet/dry my soil is. I want a range of numbers. Analogue read returns a range of numbers. Both the micro:bit and the Circuit Playground Express have pins that can read analogue data.

Algorithm

We have two algorithms, one for getting our range and the other for determining the moisture content. We do this on dry, then wet soil.

Forever

      Display the analogue data on the pin connected to the pot

Once we have our range, we set up the pot forever! When the soil is dry, I want my alarm to go off until I water the plant, so a while loop is better here.

Images

Build

For this mission, you will need:

Images   A plant in a pot

Images   Two metal nails or screws

Images   Two crocodile clips

I found it easier to get a dry plant and measure it, and then water it and measure it again. It takes ages for a plant to dry out!

1.   Stick the nails in the soil on opposite sides of the plant.

2.   Attach a crocodile clip to each nail.

Images

Figure 17.1    Potted plant ready to go.

micro:bit

Build

1.   Attach one crocodile clip to GND on the micro:bit.

2.   Attach the other crocodile clip to pin 1 on the micro:bit.

3.   Attach a speaker to GND and pin 0 on the micro:bit.

Images

Figure 17.2    Finding the range.

Code

Let’s get our range first. The analogue read pin P1 block is in the Pins menu under the Advanced menu.

I used a cactus that hadn’t been watered in months as my dry plant. When I tested the plant, the number was around 170. I then used a plant that had just been watered as my wet plant. I got 1 for that number.

Here’s my code. I set my “is dry” condition to be more than 50, because a cactus is quite an extreme example of a dry plant! If the plant is not dry, I don’t check it again for 15 minutes. I don’t think plants dry out that quickly, so you could make this number longer.

Images

Figure 17.3    Plant code.

Debug

The whole point of using analogue was to know how wet/dry my plant was. I added the read pin to A button pressed and wrote the number 50 on my potted plant. In that way, I can check how close to dry my plant is.

Images

Figure 17.4    Debug.

Expert Level

You could use the micro:bit as a graph to show how dry the plant is. Use the plot code from Mission 15.

Circuit Playground Express

Build

1.   Attach one crocodile clip to GND on the Circuit Playground Express.

2.   Attach the other crocodile clip to pin A2 on the Circuit Playground Express.

3.   Attach a speaker to GND and pin A0 on the Circuit Playground Express.

Code

With no screen from which to read numbers, we’re going to have to run this code several times on the Circuit Playground Express. The analogue range is 0 to 1,023. I created the micro:bit code first and discovered that the dry plant produces larger numbers, so I tested this on the dry plant first.

I started graphing up to 1,023.

Images

Figure 17.5    Graphing the plant.

This turned on only one light, so that’s about 102? I changed the code to graph up to 100, and seven lights stayed on. I moved the nails to the damp plant and graphed up to 100—two lights stayed on.

Let’s say then that 70 is dry and 20 is wet. If my plant is above 50, I would say it’s dry.

Here’s the code to check for dryness and sound the alarm. If the plant is wet, it won’t check again until 15 minutes later. This code has the alarm constantly going off until you water the plant. What will yours do? Sound the alarm and then pause for a minute and sound it again?

Images

Figure 17.6    Plant code.

Debug

I wanted to see how close to dry my plant was, so I put the graph code into a button press. I cleared the light after 5 seconds, so that it doesn’t stay on for up to 15 minutes.

Images

Figure 17.7    Debugged.

Expert Level

How often does your plant need watering? Can you record how often you water the plant in a week?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.112.56