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

 

Lesson 7 - Real world data at your fingertips: Light, Ultrasonic Motion and more...

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

     
           
     

Reading Data from Connected Sensors

The last lesson was a good example of writing from a TI Nspire Lua document to the LaunchPad, and having it then perform some response (in that case, sending different tones to a connected speaker).

In this lesson, we see how to read data from various sensors (again using the Grove modules, but the same principles should apply in other examples). We will begin with the Grove Light sensor and the Grove Sound Sensor (both relatively simple examples). We will finish with my favourite, the ultrasonic motion sensor.


Grove Light Sensor


Grove Sound Sensor


Grove Ultrasonic Motion Sensor

  

Hacking the Hub™

Note, in the sketch below the brightness pin used is pin 23 - which is the pin allocated to the Innovator Hub brightness sensor, and pin 30 (used for the ultrasonic ranger) corresponds to the IN1 port (IN2 corresponds to pin 28, and IN3 to pin 26)!

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)
  
  

LaunchPad Sketch: Light (and Sound)
(Copy and paste into Energia)

#define LIGHT_SENSOR = 23; /* pin connected to the Light Sensor */
int light = 0;

void setup()
{

Serial.begin(9600);
Serial1.begin(9600);
}

void loop()

{

light = analogRead(LIGHT_SENSOR); /* read the value from the sensor */
Serial1.print("L");
Serial1.println(light);

Serial.print("Light Intensity: ");
Serial.print(light);
Serial.println(" LUX ");
delay(500);

}
}

Study the LaunchPad sketch carefully and see how simple this actually is!

Values are read from pin number 23 (which is where the light sensor is plugged in) and these are stored in the variable "light". This is printed via BLE with "L" attached to the front, so that it can be recognised at the other end (it is also printed to the Serial monitor in a more readable form).

Now just swap "light" for "sound", plug in your sound sensor, and you can collect sound data just as easily!

Lua Script (copy and paste full script into TI-Nspire Script Editor)

If you are interested in learning more about Lua, then feel free to study this script. It has been written so as to be as generic as possible: whatever is entered into the text box is checked against the string coming in from the BLE/Serial port. The sensor is activated upon pressing the enterKey after the identifying letter ("D", "L" or "S"). The script has been amended to also recognise the text commands "RANGER", "LIGHT" and "SOUND".

 
 

platform.apilevel = '2.5'

screen = platform.window
w = screen:width()
h = screen:height()
local date = "110216"

pcall(function () require 'bleCentral' end)

require "color"
local nameList = {'HMSoft'}
local bleState = ''
local bleStatus = 'Stand by'
local peripheralName = ''
local myPeripheral = nil

local groveBLE = 'FFE1'
local keyPress = 0
local alert = nil
local myChar = nil
local sensor = nil

local textBox = D2Editor.newRichText()
local boxX, boxY, boxWidth, boxHeight
local fontSize = 12
local button1 = "Scan for BLE"
local button2 = "Reset"

 
 

 
 

LaunchPad Sketch: Distance
(Copy and paste into Energia)

int ULTRASONIC_PIN = 30; /* pin of the Ultrasonic Ranger */
int distance = 0;
int buttonState1 = 0;

void setup() {

pinMode(PUSH1, INPUT_PULLUP);
Serial.begin(9600);
Serial1.begin(9600);
}

void loop() {

buttonState1 = digitalRead(PUSH1);
char recvChar;

if(Serial1.available()){

recvChar = Serial1.read();
}
if(Serial.available()){
recvChar = Serial.read();
}

switch(recvChar) {

case '1':
Serial1.println("P1");
buttonState1 = LOW;
break;

case 'D':
buttonState1 = LOW;
break;

case '0':
buttonState1 = HIGH;
break;
}

if (buttonState1 == LOW) {

pinMode(ULTRASONIC_PIN, OUTPUT);
digitalWrite(ULTRASONIC_PIN, LOW);
delayMicroseconds(2);
digitalWrite(ULTRASONIC_PIN, HIGH);
delayMicroseconds(5);
digitalWrite(ULTRASONIC_PIN,LOW);
pinMode(ULTRASONIC_PIN,INPUT);
long duration;
duration = pulseIn(ULTRASONIC_PIN,HIGH);
long RangeInCentimeters;
RangeInCentimeters = duration/29/2;

distance = RangeInCentimeters; /* read the value from the sensor */
Serial1.print("D");
Serial1.println(distance);
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm ");
}

}

Some interesting differences in this script from the previous approach.

You will notice that the PUSH1 button is used here: instead of constantly collecting data from the connected sensor, this only occurs when that sensor is triggered. On the LaunchPad, pressing the PUSH1 button will activate data collection - open the Serial Monitor to see this.

From the TI-Nspire iPad App, this occurs when "D" is entered in the textbox.

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.

Now look closely at what happens when PUSH1 is pressed. While the light and sound sensors simply take whatever is read from that pin, the ultrasonic motion sensor requires a bit more work to turn the variations in voltage at that pin into a measurement in centimetres (reasonably accurate from 0 to 400 cm).

Note that these distance readings are digital, not analog like the previous sensors. As you might expect, digital tends to be wither ON or OFF, HIGH or LOW, while analog supports the variations that lead directly to values, like those for sound and light.

It is worth noting at this point that, while we used digitalWrite in earlier lessons for switching the various LEDs on and off, we could also use analogWrite and get variations in these colours and their intensities.

So in the case of the ultrasonic sensor, you will see that the sensor is actually turned on and off very quickly, with very short delays between each of these switches. A burst of ultrasonic vibration is sent out, and returns to the sensor, from which the distance is calculated.

If you browse some of the many excellent example sketches provided in Energia, you will find some that refer to the ultrasonic sensor, and you should play with these. You may notice that these sketches begin with a line such as #include "Ultrasonic.h". This calls another sketch (which in turn actually calls other sketches) which is automtically loaded to perform just the calculations that have been made explicit here.

  
  

Challenges?

So where to from here?

I would certainly suggest that you try merging the three sketches described here into one, so that you have a nice general sketch for sound, light and distance. I have used PUSH! as the trigger for ultrasonic distance; you choose what you would like to do with the other button.

Then go back and continue building and enhancing your sketch. While there are certainly performance benefits in keeping things simple, there is also much to be gained from having a functional and versatile sketch that can be used with sensors, and LEDs as indicators, and perhaps even varying the tone played as one of your sensors varies - the sound as you walk back and forth would be an interesting experience!

  
  

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.

  
  

Back to Top

   
 

Back to Top

  

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