HomeSTEM HQ Getting Started with Arduino Arduino Lesson 7


Lesson 7 - Build your own BLE ultrasonic motion detector for under $AUD30

Arduino Reference page

Use your Chrome browser on Mac, Android or ChromeBook, the LightBlue App for iOS or Android or the WebBLE app on iPad to test these pages.


STEM on a Budget

My all time favourite classroom technology tool over many years was the TI CBR (Calculator-Based Ranger) - a sonic motion detector that helped bring deep concepts of mathematics and motion to life for students from the junior years right through to seniors. From my earliest explorations of the new generation of data collection tools, one of my goals has been to recreate the power of this wonderful learning tool.

While the Grove sensors referred to in previous lessons are very good, they tend to be rather pricey. For example, the Grove BLE module sells for around $USD20.

If, however, like me, all you really want is a motion detector that can be readily used in a classroom, and you are conscious of the dollars, then I would like to share my experience, in which I easily built my own BLE Ranger, for well under $AUD30! eBay is a ready source of generic components - Arduino Nano (< $AUD10), HM10 BLE module (as little as $AUD2-3) and HC-SR04 ultrasonic sensor for under $AUD5. Throw in a portable power supply and you will still have change from $AUD30!

While you can readily use jumpers to connect your sensors directly to the pins on your board, there is a very real convenience in the Grove connector system, with each port wired to voltage and ground pins as well as digital or analog signal pins. The Arduino Grove Base Shield makes connecting multiple sensors so easy that I would recommend blowing the $AUD7 or $AUD8 to grab one, and just wiring your inexpensive generic sensors to some grove connector cables. Easy!


Back to Top

First, we will connect the jumpers from our modules to our board.

  • HM-10 BLE Module: This is a six pin module (of which we use four), with the pins clearly labelled on the underside:

    • VCC is the voltage: I connected this to voltage pin (RED Grove connector).

    • GND is the ground: I connected this to a ground (GND) pin (BLACK Grove connector).

    • TXD is TRANSMIT: I connected this to pin 0 (WHITE Grove connector).

    • RXD is RECEIVE: I connected this to pin 1 (YELLOW Grove connector).

    • This is the same combination as used by the Grove BLE module (NOTE that TX on the module connects to RX on the board, and vice versa).

  • HC-SR04 Ultrasonic Module: This is a four pin module like the Grove, but the pins perform different functions here (again the pins are clearly labelled). The Grove module uses just three pins: VCC, GND and SIG (Signal). This module has VCC (5V) and GND, but also TRIG (Trigger) and ECHO.

    • VCC is the voltage: I connected this to a 5V pin.

    • GND is the ground: I connected this to any pin labelled GND.

    • TRIG: I connected this to pin 2 (YELLOW Grove connector).

    • ECHO: I connected this to pin 3 (WHITE Grove connector).

Generic HM-10 BLE module (under $AUD5)

Generic HC-SR04 ultrasonic motion sensor (under $AUD5)

Jumper leads (you can buy a bundle of these for around $AUD5)


Back to Top

Arduino Sketch: Ultrasonic Distance
(Copy and paste into Arduino IDE)

int TRIG_PIN = 2;
int ECHO_PIN = 3;
int soundpin = 4;
int cycle = 0;

void setup() {




void loop() {

if (cycle == 1) {


char recvChar;

if (Serial.available()){

recvChar =;

switch(recvChar) {

case 'd':


case 'D':

cycle = 1;

case 'n':

cycle = 0;;

case 'N':

cycle = 0;;



void getDistance() {

long duration, distance;
digitalWrite(TRIG_PIN, LOW);
digitalWrite(TRIG_PIN, HIGH);

duration = pulseIn(ECHO_PIN,HIGH);
distance = (duration/2)/29.1;

Serial.print(" Distance: ");
Serial.println(" cm ");

tone(soundpin, 10*distance, 1000);



Now we can have a look at the sketch I have used here. It is essentially the same as that we used for distance in the last lesson, but with a few extra features.

As well as the usual Serial (BLE) connection, we define the two pins which will serve as TRIGGER (we use pin D2 and this will function as an OUTPUT, since pulses of ultrasonic sound will be sent from this) and ECHO (pin D3, and this will be INPUT since it will receive the sound bounced back from the nearest object).

The duration is calculated as the time it takes for an echo to be heard. Divide that time by 2 (since it had to travel there and back again) and then divide by a constant (in this case 29.1) which converts that time into the distance of the object in centimetres, using the speed of sound.

This distance in centimetres is sent to the BLE Serial port with the string "D" (eg "D142" for a distance of 142 cm). The loop serves to continuously send HIGH and LOW pulses of sound from the TRIGGER, and listen for the ECHO when these are bounced back to pin D3 (in the Grove module, the SIG pin serves both these functions).

Did you notice that we have defined a function, getDistance() outside both the setup() and loop() functions? This makes it easy to call at different points - for example, by adding it to setup(), the distance will be measured each time the reset button is pressed.

Note, too, the cycle variable - can you explain what purpose this serves?


Back to Top

Congratulations! You now have a functional and elegant BLE motion sensor which is accurate from 0 to around 400 cm.

There are numerous variations on the BLE and ultrasonic modules - I looked for the least expensive to try these out and see if they worked (and they did!) I found eBay to be a great source of such items since you generally have a fairly wide range of choice, and can usually find an option with free shipping and postage. Any modules that use the same chip should work fairly easily (although like me, you may have to do a little Googling to find someone else who has written a sketch for that module!). HM-10 for BLE and HC-SR04 (or HC-SR05 I believe) should work for you.

Obviously, there is nothing to stop you from doing the same thing with other sensors and modules - aim small at first, but soon you should be able to create interesting and useful systems of your own!

If you run into problems, or you try new things, please feel free to drop me a line and I will happily do what I can to help you to get started.


Back to Top

If you are using Chrome browser on Mac, Android or Chromebook, or the WebBLE app on iPad) you might just try this for yourself!


BLE LaunchPad Controls

©Daniel Loginov




Back to Top

Back to Top


Create your own Live Web Page

Back to Top


HomeSTEM HQ Getting Started with Arduino Arduino Lesson 7