Microbit Range Finder - STEM Learning

5
A micro:bit project to build a motion sensor using an ultra-sonic range-finder Adrian Oldknow [email protected] 24 th August 2018 A very useful, robust and cheap sensor for micro:bit applications is the widely available HC-SR04. The example show from Cool Components costs £2.75 including a set of jumper leads. Its two `eyes’ are in fact a transmitter T on the left and a receiver R on the right. The device sends regular ultra-sonic frequency pulses from T which bounce off the nearest object in its `line of sight’ and are captured in the receiver R. It calculates the difference in time between when the pulse is sent and when it is received, and so you can calculate the distance the pulse travelled, and hence the distance to the object it `saw’. More information, including the diagram, can be found here. The HC- SR04 has four pins used to connect to devices like the micro:bit, Arduino and Raspberry Pi. They are clearly marked Vcc, Trig, Echo and Gnd. Vcc is the positive connection to a power source and Gnd is the negative connection. Trig stands for `Trigger’. So we will need to build a circuit where Trig and Echo are attached to 2 of the micro:bit’s pins. If we choose pin P1 for the Trigger and pin P2 for the Echo then we can use crocodile leads like the green and yellow ones shown below. Most of the HC-SR04 units on sale require about 5V to work, and so cannot be powered directly from the micro:bit’s 3V output. An exception is the new version stocked by Kitronik which will work (just) with the 3.3V supplied by the micro:bit. In the first photo the unit is mounted on a red board. Here the Gnd and Vcc pins can be connected to the 3V and Gnd pins of the micro:bit using the red and black leads shown. The next photo shows the 4Tronix version connected to its own 4.5V battery pack with red and black crocodile leads. In order for the micro:bit to communicate with the HC-SR04 via pins P1 and P2, an additional black lead is needed to connect the Gnd pin on the micro:bit to the Gnd pin on the HC-SR04. So that has got the hardware side sorted out in principle.

Transcript of Microbit Range Finder - STEM Learning

Page 1: Microbit Range Finder - STEM Learning

A micro:bit project to build a motion sensor using an ultra-sonic range-finder

Adrian Oldknow [email protected] 24th August 2018

A very useful, robust and cheap sensor

for micro:bit applications is the widely

available HC-SR04. The example show

from Cool Components costs £2.75

including a set of jumper leads. Its two

`eyes’ are in fact a transmitter T on the

left and a receiver R on the right. The

device sends regular ultra-sonic

frequency pulses from T which bounce

off the nearest object in its `line of

sight’ and are captured in the receiver

R. It calculates the difference in time

between when the pulse is sent and

when it is received, and so you can

calculate the distance the pulse travelled,

and hence the distance to the object it

`saw’. More information, including the

diagram, can be found here. The HC-

SR04 has four pins used to connect to

devices like the micro:bit, Arduino and Raspberry Pi. They are

clearly marked Vcc, Trig, Echo and Gnd. Vcc is the positive

connection to a power source and Gnd is the negative

connection. Trig stands for `Trigger’. So we will need to build a

circuit where Trig and Echo are attached to 2 of the micro:bit’s

pins. If we choose pin P1 for the Trigger and pin P2 for the Echo

then we can use crocodile leads like the green and yellow ones

shown below. Most of the HC-SR04 units on sale require about

5V to work, and so cannot be powered directly from the

micro:bit’s 3V output. An exception is the new version stocked

by Kitronik which will work (just) with the 3.3V supplied by the

micro:bit. In the first photo the unit is mounted on a red board.

Here the Gnd and Vcc pins can be connected to the 3V and Gnd

pins of the micro:bit using the red and black leads shown. The

next photo shows the 4Tronix version connected to its own 4.5V

battery pack with red and black crocodile leads. In order for the

micro:bit to communicate with the HC-SR04 via pins P1 and P2,

an additional black lead is needed to connect the Gnd pin on the

micro:bit to the Gnd pin on the HC-SR04. So that has got the hardware side sorted out in principle.

Page 2: Microbit Range Finder - STEM Learning

The next photos show my own model data-logger using an edge

connector and breakout board. The 3xAAA and 2xAAA battery packs

are held under the board with elastic bands. In addition to wiring in

the HC-SR04 I have added a simple buzzer to the board connected to

the micro:bit’s

Gnd and P0

pins. The

connections

are made using

male to female

jumper leads.

In order to program the micro:bit we need to find where the necessary

commands can be found. There is some technical information about doing

this in Microsoft’s MakeCode JavaScript editor here. This uses a `pulse’

command from the `Pins’ menu. But life is much easier if we use the pre-

written `pxt-sonar’ package. From

`Advanced’ select `Add package’ and type

in `pxt-sonar’. Select the first package

shown and you will now have a `Sonar’

menu which just

contains one block

called `ping’. This

returns a value read

from the sonar sensor.

In our project we are

using P1 as `trig’ and P2

as `echo’. We need also

to change the unit from

microseconds to cm. The

simulator returns a

random reading of 86.

The range of the sensor is

up to about 4m, and we’ll

usually be measuring distance less than 1m. So if we

divide `dist’ by 10 the simulator now shows just the

single digit 8. When you divide in MakeCode, any

remainder is discarded and the result is an integer.

In order to round to the nearest whole number we

just have to add 5 to `dist’ before dividing by 10.

Page 3: Microbit Range Finder - STEM Learning

So we have now built a simple measuring instrument which

displays distances to the nearest tenth of a metre. Now it is

easy to use the sensor as a trigger to detect if an object is too

close for comfort. We just need to use an `if’ command from

the `Logic’ menu. Here we give a simple text warning if

something is closer than 20cm.

My instrument also contains a buzzer connected to

the Gnd and P0 pins of the micro:bit. So with a little

adjustment we can make an audible warning, as well

as a visible one, using one of the `Music’ commands.

As a variation we can use button A to start the

instrument sensing continuously, making both visible and audible displays. First we can try using

the `plot bar graph’ block from the `Led’ menu. This turns on all the 25 leds if `dist’ is 100. The

simulator made `dist’ read 86 cm. Dividing this by 4 means the bar graph uses 21 of the leds.

We can add sound by making the function `Beep’. This uses `dist’ to modify the frequency.

Page 4: Microbit Range Finder - STEM Learning

So now we have turned the instrument into one of the parking sensors used in modern cars to tell

you how close you are to an object like a parked car.

You might like to mount your motion sensor onto a

micro:bit controlled buggy and use the sensor to

stop the motors, or to reverse or to turn when a

close object is detected. An example is the 4Tronix

Robo:bit Mk2 with ultra-sonic sensor. (My advice is

not to use this with a line-following kit.)

Finally I’ll take a look at how we can send captured

distance data to the PC for graphing and saving as a data file. I am using the free Windows 10

MakeCode beta App rather than the online editor. This allows data to be displayed from either the

simulator or from a micro:bit connected with a USB cable.

We just need to add

a `serial write value’

command from the

`Serial’ menu in

`Advanced’. This

sends the name `d’

and the value of

`dist’ down the USB

cable and into one

of the PC’s serial

ports. When

button A is pressed, the program will start sending data to the MakeCode App and a `Show data

device’ message is displayed. Clicking on this plots the graph of `d` against time. Use the `pause’

button at the top right to pause the graph. If you select the `upload’ button at the top right, the

data will be stored in a CSV (comma separated variable) file and opened in the Excel spreadsheet.

Page 5: Microbit Range Finder - STEM Learning

The data in columns A and B can be

displayed as a scatterplot, as shown.

So now we have built a piece of

genuine lab equipment – an ultra-sonic

range-finder. Instead of keeping it

hooked up to the PC with a USB cable,

you could amend the program to

transmit the `dist’ data by radio to a second micro:bit attached to the PC with a USB cable. That

micro:bit could then send the received radio data to the PC using the `serial write value’ command.

Happy sensing!