Professional Documents
Culture Documents
Project - Wireless Moisture Detector
Project - Wireless Moisture Detector
Project - Wireless Moisture Detector
2
You will need:
1. Two micro:bit
moistureLeve
l
Assigning variables
We call this “assigning a variable”. We can read it as being that moistureLevel contains
the number 10 in our program.
Using variables
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
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
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.
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
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
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.
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.
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.
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.
Just like moistureLevel, we constantly check for any messages that are
being sent by radio.
If there are no messages being received, then “received” stores the word
“None”.
Checking the messages
While true
If message is
Checking the messages
While true
“None” If message is
Do something
“water me”
While true
This code asks if “received” contains “water me”, and if it does then it
shows a SAD face on the screen.
While true
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?