Brain Coral Tutorial: Reading analog input using galileo-io

Version 9

    Connect distance sensor

    We will be using a distance sensor to control different LED animation sequences. To begin, we’ll start by hooking up jumper cables from 5V and GND into the breadboard strips. We’ll then connect the sensor to the board and write a program that will log incoming sensor values.

    1. Prepare the distance sensor

      The distance sensor has 3 female inputs. Connect a jumper cable to each input. Then connect the GND and PWR cables into the breadboard. The GND wire on the sensor is black. The PWR wire is red. Most sensors typically follow the same color scheme.

    2. Power the distance sensor

      Plug in the GND wire into the breadboard along the GND (blue) track. Then, connect the PWR cable along the other (red) track.

    3. Connect to the A0 pin

      Connect the remaining cable into the Galileo’s ‘A0’ pin.

    IMG_6758.png

    We’re now ready to receive data from the distance sensor. We’ll write a simple script that logs the values to the console.

     

    Reading analog data

    Just as we did in the "Hello Galileo" example, we start by referencing the galileo-io Node.js library, and instantiating it in a variable called "board". From here, we'll use the method "analogRead" of our "board" instance to read our sensor's data.

     

    Create a new file called "1-2-analog-input.js". You can duplicate the “1-1-hello-galileo.js” file as a starting point.

     

    Use one of the methods discussed earlier for getting the code onto the Galileo. You can do this through ssh/telnet using vi or on your host using scp. The following code demonstrates how to log the sensor’s data to the terminal:

     

    var Galileo = require('galileo-io');
    var board = new Galileo();
    board.on('ready', function() {
        this.analogRead('A0', function(data) {
            console.log(data);
        });
    });
    
    
    
    
    
    

     

    Test out the new code by executing node 1-2-analog-input. Once the board is ready, you should see a range of numbers being displayed on your console. The values will update as you interact with the distance sensor. The closer an object is to the sensor, the higher the analog input value. Close the Node server by pressing "ctrl" + "c".

     

    .
    < Hello GalileoConnect the LPD8806 strip >