printing sensor analog voltage to terminal window

05 Jan 2014

Using the LPC1768.

Using Sharp Distance Sensor GP2Y0A02YK (output voltage should range from 0 to about 2.8).

Using tera term VT with com port at 9600 baud.

Started by importing a program from the library called SharpSensor that lights up the LEDs on the board, depending on the sensor distance from target. Modified it to also print the pin voltage to a terminal window. It seems to work but I have questions. The LEDs go on at specific pin voltage levels (1 led means low pin voltage thus longer distance, 4 leds mean high pin voltage thus short distance). The output in the terminal indicates a lot of fluctuation in the voltage, which I expect within reason. What seems weird to me is the stability of the LEDs vs. the fluctuation in the printed voltage data. For instance....the board shows 3 solid LEDs while my terminal shows voltages from 2.8v to 3.1v, but then there is an occasional voltage below zero.....I would expect the LEDs to shut off when that low voltage is sensed, but they don't change. Another question is, I don't understand the voltage thresholds in the code, they are all less than 1, why is that? Any advice on a better way to do this is appreciated. Honestly I see very little stability in the data printed in that terminal window, in fact sometimes it is inverse to what I expect. Here is a cut/paste of the terminal data, followed by the code:

terminal output

 SenseVal-f: 3.037289
 SenseVal-f: 2.908352
 SenseVal-f: 2.921245
 SenseVal-f: 2.908352
 SenseVal-f: 2.985714
 SenseVal-f: 2.934139
 SenseVal-f: 2.818095
 SenseVal-f: 3.050183
 SenseVal-f: 3.011502
 SenseVal-f: 3.024396
 SenseVal-f: 2.947033
 SenseVal-f: 2.959927
 SenseVal-f: 0.072527
 SenseVal-f: 2.947033
 SenseVal-f: 2.959927
 SenseVal-f: 2.921245
 SenseVal-f: 2.934139
 SenseVal-f: 2.959927
 SenseVal-f: 3.101758
 SenseVal-f: 2.947033
 SenseVal-f: 3.153333
 SenseVal-f: 2.689158
 SenseVal-f: 2.895458
 SenseVal-f: 2.947033
 SenseVal-f: 0.111209
 SenseVal-f: 3.114652
 SenseVal-f: 2.856777
 SenseVal-f: 2.985714
 SenseVal-f: 2.882564
 SenseVal-f: 3.114652
 SenseVal-f: 3.269377
 SenseVal-f: 2.985714
 SenseVal-f: 2.985714
 SenseVal-f: 2.947033
 SenseVal-f: 2.869670

SharpSensor code

/* Sharp Sensor Test
 * Connections:
 * PIN15          (White) Signal
 * 5.0V USB Out   Sensor Power (+)
 * GND            Sensor Gnd   (-)
 * Place a 10uf cap close the the sensor between VCC nad GND
 */
#include "mbed.h"

/* Standard LED's on the mbed */
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

Serial pc(USBTX, USBRX); // tx, rx

/* Our signal line is PIN15 */
AnalogIn   Sensor(p15);

int main() {
    unsigned short SenseVal; /*I added*/
    while (1) {
        /*
         * Efficient way as the object gets closer to the
         * sensor the LED's will turn on, as the object
         * moves away they count back down. Original by Dan Ros
        */
        led1 = (Sensor > 0.2) ? 1 : 0;
        led2 = (Sensor > 0.4) ? 1 : 0;
        led3 = (Sensor > 0.6) ? 1 : 0;
        led4 = (Sensor > 0.8) ? 1 : 0;
        
        /* 
        An attempt to write the sensor output
        to the terminal screen.  Also chaged wat from .02 to .2
        */
        SenseVal = (Sensor.read_u16()&0xFFF); /*I added*/
        pc.printf("\n SenseVal-f: %f",(SenseVal*3.3/4095));  /*I added*/

        /*
         * A cool effect when the full range of the sensor
         * has been reached, all LED's flash
        */
        if (Sensor  >= 0.9) {
            led1 = led2 = led3 = led4 = 1;
            wait(0.2);
            led1 = led2 = led3 = led4 = 0;
            wait(0.2);
        }

        wait(0.2);

    }
}
05 Jan 2014

If you just read using 'Sensor', it will return a float between 0 and 1 (same as .read()), so the thresholds are also between 0 and 1.

Then you read using the .read_u16, which returns a 16-bit value (yes, despite it being a 12-bit ADC, it is shifted 4 positions), so your AND you put after it removes the 4 MSBs. Which gives you overflows, which makes it go from 3.3 to 0. Easiest is to just use

pc.printf("\n SenseVal-f: %f",(Sensor*3.3));
05 Jan 2014

Hi Erik, thank you for your reply. I tried what you said, so modified the print statement to be

pc.printf("\n SenseVal: %f   Sensor: %f   SensRaw: %f",(SenseVal*3.3/4095),(Sensor*3.3),Sensor.read());

Here I added the Sensor: and SensRaw: outputs. These give much more stable data. Will go ahead and get rid of the bad code producing the overflows. Thanks again for your help.

Terminal Output

 SenseVal: 0.329597   Sensor: 1.876044   SensRaw: 0.569231
 SenseVal: 0.664835   Sensor: 1.892161   SensRaw: 0.573382
 SenseVal: 0.600366   Sensor: 1.895385   SensRaw: 0.572650
 SenseVal: 0.677729   Sensor: 1.901026   SensRaw: 0.574359
 SenseVal: 0.523004   Sensor: 1.888938   SensRaw: 0.572894
 SenseVal: 0.755092   Sensor: 1.892967   SensRaw: 0.574359
 SenseVal: 0.626154   Sensor: 1.898608   SensRaw: 0.572650
 SenseVal: 0.548791   Sensor: 1.891355   SensRaw: 0.573626
 SenseVal: 0.703516   Sensor: 1.896996   SensRaw: 0.573626
 SenseVal: 0.523004   Sensor: 1.892967   SensRaw: 0.571673
 SenseVal: 0.510110   Sensor: 1.894579   SensRaw: 0.575092
 SenseVal: 0.497216   Sensor: 1.888938   SensRaw: 0.573626
 SenseVal: 0.497216   Sensor: 1.890550   SensRaw: 0.571917
 SenseVal: 0.561685   Sensor: 1.888132   SensRaw: 0.574115
 SenseVal: 0.458535   Sensor: 1.891355   SensRaw: 0.573626
 SenseVal: 0.613260   Sensor: 1.892967   SensRaw: 0.574359
 SenseVal: 0.510110   Sensor: 1.885714   SensRaw: 0.572405
 SenseVal: 0.471429   Sensor: 1.888938   SensRaw: 0.570940
 SenseVal: 0.497216   Sensor: 1.919561   SensRaw: 0.574115
 SenseVal: 0.445641   Sensor: 1.883297   SensRaw: 0.571429
 SenseVal: 0.290916   Sensor: 1.874432   SensRaw: 0.568010
 SenseVal: 0.832454   Sensor: 1.871209   SensRaw: 0.550427
 SenseVal: 0.974286   Sensor: 1.888938   SensRaw: 0.581197
 SenseVal: 0.948498   Sensor: 1.903443   SensRaw: 0.576557
 SenseVal: 1.025861   Sensor: 1.921172   SensRaw: 0.582173
 SenseVal: 0.342491   Sensor: 1.878462   SensRaw: 0.567277
 SenseVal: 0.329597   Sensor: 1.896996   SensRaw: 0.574359
 SenseVal: 0.278022   Sensor: 1.895385   SensRaw: 0.568010
 SenseVal: 0.419853   Sensor: 1.879267   SensRaw: 0.572161
 SenseVal: 0.265128   Sensor: 1.876044   SensRaw: 0.568987
 SenseVal: 0.458535   Sensor: 1.876044   SensRaw: 0.566300
 SenseVal: 0.355385   Sensor: 1.876850   SensRaw: 0.568254
 SenseVal: 0.316703   Sensor: 1.874432   SensRaw: 0.568498
 SenseVal: 0.355385   Sensor: 1.882491   SensRaw: 0.569231
 SenseVal: 0.278022   Sensor: 1.876044   SensRaw: 0.568254
 SenseVal: 0.213553   Sensor: 1.875238   SensRaw: 0.569719
 SenseVal: 0.316703   Sensor: 1.875238   SensRaw: 0.568010
 SenseVal: 0.368278   Sensor: 1.882491   SensRaw: 0.568987
 SenseVal: 0.342491   Sensor: 1.882491   SensRaw: 0.571429