Example: Adding a light sensor to your project

This section contains an example of adding a simple sensor to your project. It contains steps to create a project that uses the Grove* - Light Sensor to output light readings at constant intervals. The JavaScript* code used in this example can be found in the Sensor Library Explorer.

The steps below are written for the Intel® Edison board; steps for your board may vary.

The example code

// Load Grove module
var groveSensor = require('jsupm_grove');

The first line of code, require('jsupm_grove'), loads the Grove sensor module.

// Create the light sensor object using AIO pin 0
var light = new groveSensor.GroveLight(0);

A light sensor object is then created and attached to pin A0 on the board with groveSensor.GroveLight(0).

// Read the input and print both the raw value and a rough lux value,
// waiting one second between readings
function readLightSensorValue() {
console.log(light.name() + " raw value is " + light.raw_value() +
", which is roughly " + light.value() + " lux");
}
setInterval(readLightSensorValue, 1000);

Finally, the various methods of the object, light.name(), light.raw_value(), and light.value(), are called to retrieve the sensor's name and light readings.

Requirements

  • You have installed the Intel® XDK and connected it to your board. For steps, see Getting started with the Intel® XDK.
  • You must have a Grove* - Base Shield, Grove*- Light Sensor, and a Grove* Cable.

Connecting the light sensor to your board

  1. Line up the male header pins on the bottom of your base shield with the female pins on the expansion board, as shown in the image below. Once aligned, gently push the base shield down firmly and evenly towards your board until it is securely attached.

    Attaching the base shield to your board
  2. Check that the voltage toggle switch (which is marked VCC and located next to the A0 socket) on the base shield is set to 5V.
  3. Slide the connector on one end of the cable into the socket on your light sensor until you feel the cable snap into place.
  4. Slide the other end of the cable into the A0 socket on your base shield until you feel the cable snap into place.

    Example of a light sensor attached to a board

Creating your project

  1. In the top left of the Intel XDK, click the Projects tab.
  2. Click Start a New Project.

    Starting a new project

  3. Under Internet of Things Embedded Application, click Templates.

    Creating a project from a template

  4. From the right, click Blank Template. Click Continue.
  5. Type a name for the project in the Project Name field, then click Create. Your new project opens.

    Naming your project

  6. On the left under Sensor Libraries, click Open Sensor Library Explorer. The Sensor Library Explorer opens.
  7. From the list on the left, click Light/Proximity/IR.
  8. In the Device list, click grovelight. Information about the selected device is displayed in the Description pane to the right.
  9. To copy the example code to the clipboard, click Copy snippet to clipboard. The example code is automatically copied to your clipboard for you to paste into your project.
  10. Paste the light sensor code into the main.js file for your project.
            // Load Grove module
    var groveSensor = require('jsupm_grove');
    // Create the light sensor object using AIO pin 0
    var light = new groveSensor.GroveLight(0);
    // Read the input and print both the raw value and a rough lux value,
    // waiting one second between readings
    function readLightSensorValue() {
        console.log(light.name() + " raw value is " + light.raw_value() +
                ", which is roughly " + light.value() + " lux");
    }
    setInterval(readLightSensorValue, 1000);
    In the bottom toolbar, click the Upload icon Upload icon to upload the project to your board.
  11. Click the Run icon Run icon to run your project. When the code is run, the Intel XDK console should start putting out light sensor readings at regular intervals. You can cover or uncover the sensor to affect the readings taken.

    Example of light sensor readings

Next Steps

In addition to the UPM sensor library, you can use third party libraries to interact with your sensors, such as Johnny Five and Cylon.JS.

 

For more complete information about compiler optimizations, see our Optimization Notice.