Home TI-Nspire Scripting HQ STEM HQ Getting Started with TI LaunchPads TI LaunchPad Lesson 8


Lesson 8 - Build your own BLE ultrasonic motion detector for under $USD30

Energia Reference page

Use your Chrome browser on Mac, Android or ChromeBook or the LightBlue App for iOS or Android to test this sketch.

Texas Instruments TI-Nspire Scripting Support Page

Download the TI-Nspire LaunchPad document for this lesson or the more general TI-SensorTag document.


STEM on a Budget

My all time favourite classroom technology tool over many years was the 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.

The examples used in the last few lessons have all assumed access to the sensors produced by Grove. In particular, the Grove Starter Kit for LaunchPad represents very good value, including the Grove Base Boosterpack, and multiple sensors, including DHT (digital humidity and temperature), moisture, sound, light, ultrasonic distance, PIR motion, relay, potentiometer, buzzer and 4-digit display. The kit does NOT include a BLE module ($USD19.80 for the Grove model), but if you require such a range of sensors, then this may be a cost-effective way to purchase.

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 under $USD30!

Texas Instruments MSP430 LaunchPad ($USD12.99 or ~$AUD21)

Texas Instruments MSP432 LaunchPad ($USD19.99 or ~$AUD31)

Grove Starter Kit for LaunchPad ($USD59)


Hacking the Hub™

In the sketch below, pins 13 and 12 correspond to BB6 and BB5 on the Innovator Hub! Connect the sensor's VCC (voltage) pin to the 5V BB port, and GND to one of the BB GND ports.

Remember: You can always reflash your Innovator Hub to return it to its original state, using the tools that TI has made available (just follow the Resources tab, and then "Keep your Innovator up to date"). So it is possible to explore coding the Hub in new ways!



TI Innovator™ Hub (with MSP432 LaunchPad)

Generic HM-10 BLE module (under $USD5)

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

Jumper leads (you will need female-female) (you can buy a bundle of these for around $USD5)


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 pin 1 (3V3).

    • GND is the ground: I connected this to pin 22 (GND).

    • TXD is TRANSMIT: I connected this to pin 3 (P3.2).

    • RXD is RECEIVE: I connected this to pin 4 (P3.3).

    • 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 pin 20 (5V).

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

    • TRIG: I connected this to pin 13 (hub BB6).

    • ECHO: I connected this to pin 12 (hub BB5).


LaunchPad Sketch: Simple Distance
(Copy and paste into Energia)

int TRIG_PIN = 13;
int ECHO_PIN = 12;

void setup() {


void loop() {

buttonState1 = digitalRead(PUSH1);
buttonState2 = digitalRead(PUSH2);

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 ");


Now we can have a look at the very simple sketch I have used here and see if you can understand what is happening. It is essentially the same as that we used for distance in the last lesson, but simpler, and easier to follow.

As well as the usual Serial and Serial1 (BLE), we define the two pins which will serve as TRIGGER (we use pin 27 and this will function as an OUTPUT, since pulses of ultrasonic sound will be sent from this) and ECHO (pin 26, 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 Serial1 port with the string "D" (eg "D142" for a distance of 142 cm). Our Lua script from the previous lesson will work well to receive and understand this. A more verbose form ("Distance: 142 cm") is sent to the computer Serial port.

This simple sketch will not use buttons or LEDs. Instead, 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 26 (in the Grove module, the SIG pin serves both these functions).

Try extending the sketch to include buttons and LEDs. The buttons could simply serve as remotes - PUSH1 as enterKey to play and pause the data collection, PUSH2 as an escapeKey to reset (these functions are already included in the Lua script for the attached document). The LEDs might be used to show when the ECHO is being received. When you have tried yourself, check one option that I have done:

For free options to test and explore our BLE code, we can use the LightBlue App for iOS or Android, or your Chrome browser on Mac, Android or ChromeBooks.


Congratulations! You now have a functional and elegant BLE motion sensor which is accurate from 0 to around 450 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.


Or (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




Helpful Hints...

Make sure your LaunchPad with BLE module and suitable sketch is powered and close by, and tap on the button above to connect. Then try the following either by typing the given commands into the input box below, or scroll down to use the buttons which will automate this process. A log of the session will be stored on the text box opposite.

  • LED controls: r (red), g (green), b (blue)
  • Numbers such as 110 and 220 will play these tones if a speaker is attached.
  • If an HC-SR04 ultrasonic ranger is connected, then h will display the distance. If Grove Rangers are connected, then use d and/or D.
  • Use l for Light Intensity and s for Sound Intensity.
  • [I2C] Use p for Barometric Pressure ("p") and Temperature ("t"). and use x for 3-axis Accelerometer.
  • [Robot] F (Forward), B (Back), L (Left) and R (Right). You might also use A (Auto drive).
  • Use n to RESET.

Back to Top


Back to Top


Home TI-Nspire Scripting HQ STEM HQ Getting Started with TI LaunchPads TI LaunchPad Lesson 8