Project - Wireless Moisture Detector

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 46

The School Observatory Challenge

Wireless Moisture Sensor


Building a wireless moisture sensor

1. Check the level of moisture with Microbit A

2. Send a message from Microbit A to Microbit B when the moisture


level is below a certain point

3. Receive the message at Microbit B

2
You will need:

1. Two micro:bit

2. One Tinker Kit breakout board

3. A Tinker Kit moisture sensor attached to pin 0

4. A red, yellow and black connecting wire


1. Check the level of moisture with
Microbit A
Variables

A variable is a way of storing one piece of information somewhere


in the computer’s memory whilst the program is running and
getting that information back later. It is a simple data structure.
Variables

We can imagine variables to be like a storage box, which can


hold exactly one thing.
Naming variables

They have a name (like a label), which gives the programmers an


idea of what is inside the box. The name is always one word long
(but can be multiple words attached together), and has to start with
a lowercase letter.

moistureLeve
l
Assigning variables

How do you store data in a variable in your code? We use “=“

We call this “assigning a variable”. We can read it as being that moistureLevel contains
the number 10 in our program.
Using variables

We can then use these stored bits of information in our code

Take a look at this code, what do you think it does?


Using variables

We can then use these stored bits of information in our code

Take a look at this code, what do you think it does?

Answer: It will constantly scroll the number 10, using the number we stored inside
our moistureLevel variable on Line 6!
Setting up the Tinker Kit

Since we don’t want moistureLevel to be 10 forever, let’s put together


the Tinker Kit.

Step 1: Put the Micro:bit into the top slot of the Tinker Kit board
Setting up the Tinker Kit

Step 2: Take a red, black and yellow wire. Find the smooth end (without
the clip) and match the colour of the wires up to the pin marked 0~.
Setting up the Tinker Kit

Step 3: Attach the moisture sensor to the clip end of the red, yellow and
black wire.
Setting up the Tinker Kit

We use the moisture sensor by giving the micro:bit instructions in our


code
We asked the micro:bit to read the
information sent by the moisture
sensor

It does this by sending a small pulse of


electricity down the yellow wire and down
the right-hand fork.
Setting up the Tinker Kit

We use the moisture sensor by giving the micro:bit instructions in our


code
Depending on how much electricity gets to the
left-hand fork, that’s how much moisture is in
the soil – water makes it easier for the
electricity to travel between the two forks.

If any electricity reached the left-hand fork, it


uses the black wire to get back to the
micro:bit

The more moisture in the soil, the higher the


amount of electricity comes back.
while True:

Anything inside “while True” will run forever and ever and ever.
Since we want our moisture sensor to keep getting the moisture
levels forever and ever, the next step is to add “while True:”
while True:

IMPORTANT

All the code we want to run inside our “while True” needs to be
indented.

Indented means that the code is either pushed in by pressing


the space bar 4 times, or the Tab button 1 time
The value of the moisture sensor

We’ve only changed one thing since our last


bit of code, what do you think it does?
The value of the moisture sensor

pin0.read_analog() is the line of code that


sends out the small pulse of electricity

pin0 is the bit that tells the micro:bit where to send the small pulse of
electricity, and where it should expect to receive some information in return.

(If we sent it to pin1, but there was no moisture sensor attached, we would
get some random, useless numbers back)
The value of the moisture sensor

Read_analog() tells us what kind of


information we should be expecting
back.

Analog is a number that can be between 0 and 1023.

Have a go at downloading this to your micro:bit and seeing what number scrolls
across the screen.
Then try holding a prong in each of your hands (only one person at once) and seeing if
that number changes!
1. Send a message from Microbit A to
Microbit B when the moisture level is
below a certain point
Setting up the radio

To be able to send messages between two micro:bits, we need to set up the


“radio”.

This isn’t radio like you might hear in the car, but is how the micro:bits use high
frequencies to send information between each other.

Imagine them to be a little bit like walkie talkies.


Importing Radio

At the very top of your code, after “from microbit import *” you need
to include “import radio”

This means the micro:bit knows we want to use code for the radio
Turning on the radio

After importing the radio, the next thing you need to do is turn the
“radio.on()”.
The radio is usually turned off, because it uses more power. This line of code
makes sure it’s on, to be able to send and receive messages
Setting your channel
Then you need to set a
channel.

Both of your micro:bits will


need to be set to the same
number (anywhere between 0
and 83).

This means your messages


won’t get mixed up with
anybody else’s!
If moisture is below a certain point

We only want to send a message to a second micro:bit if moistureLevel drops below


a certain level (less electricity can travel between the prongs because there’s less
moisture)
If moisture is below a certain point

Inside our while loop, we check if the variable moistureLevel is less than 300.
• If moistureLevel is less than 300, then the If statement on Line 19 is True
• If moistureLevel is more than 300, then the If statement on Line 20 is False
If moistureLevel is 600

While true
True

Get moistureLevel

If
False moistureLevel
is less than
300
True
Send a radio message

If the moistureLevel is 600 (which is more than 300), then nothing happens and we
keep going back to the beginning of the while loop
If moistureLevel is 600

While true
True

Get moistureLevel

If
False moistureLevel True Send a radio message
is less than
300

If the moistureLevel is 200 (which is less than 300), then we trigger the code that
sends the radio message
Sending the message

To be able to send a radio message, it only takes one line of code inside our if
statement.
Remember anything we want to trigger inside our If statement needs to be indented!
Have a go at coding what we’ve done
3. Receive the message at Microbit B
Setting up Micro:bit B

Now you have your moisture sensor microbit coded, we need to create another
micro:bit to receive the radio message.

The first step is to create a new python file.


Setting up the radio
Just like Microbit A, we need to
set up Microbit B for radio. This
includes:
1. Importing the radio code 2.
Turning the radio on
3. Setting up the radio
channel (make this the
same number as the
channel you chose for
Microbit A)
while True:

Anything inside “while True” will run forever and ever and ever. Since
we want Microbit B to keep receiving radio messages forever and ever,
the next step is to add “while True:”
while True:

IMPORTANT

All the code we want to run inside our “while True” needs to be
indented.

Indented means that the code is either pushed in by pressing


the space bar 4 times, or the Tab button 1 time
Receive messages

Just like moistureLevel, we constantly check for any messages that are
being sent by radio.

If a message is received then we store that message in “received”

If there are no messages being received, then “received” stores the word
“None”.
Checking the messages

While true

Get received message

If message is
Checking the messages

While true

Do something Get received message

“None” If message is
Do something

“water me”

If we receive no messages, then the message is empty. So if message is


“None”, then we do one action. Then we go back to the while loop and
repeat the check again.
Checking the messages

While true

Do something Get received message Do something

“None” If message is “water me”

If we do receive a message, and that message is “water me” (the message


sent by microbit A), then we should trigger the micro:bit to do something.
Make a choice

We use If statements to check the contents of our received variable.

This code asks if “received” contains “water me”, and if it does then it
shows a SAD face on the screen.

If “received” doesn’t contain “water me”, then it shows a HAPPY face


on the screen
The full flow diagram

While true

Get received message

If message is
Microbit B
Microbit A
Instead of showing a SAD face on
Microbit B, can you make it play
How can you music?
improve this
code? Could you program Microbit A to send
the radio message “water me”
when you press Button A?

You might also like