Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
Operating system
Development tools
Security and connectivity
Important update: Arm Announces End of Life Timeline for Mbed. This site will be archived in July 2026. Read the full announcement.
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
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));
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.
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
This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.
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
SharpSensor code