Home TI-Nspire Scripting HQ TI-Nspire 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

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

  
  

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 = 27; /* 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.

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!

  
  

Back to Top

  

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