Here is one of the easiest sensors to work with, at least with respect to collecting data, the Sharp GP2Y0A21YK infrared sensor (see eg http://www.coolcomponents.co.uk/catalog/product_info.php?cPath=36_57&products_id=236). Stricktly speaking it is a 5V device but will accept input voltages from 0.5 to 7V or so. So I just used the mbed's 3.3V output and the following short program:
#include "mbed.h"
AnalogIn IrSensor(p20);
Serial serial(USBTX, USBRX);
int main()
{
float IrVoltage = 0.0;
serial.baud(9600);
while(1) {
IrVoltage=IrSensor.read();
serial.printf("Voltage: %f\r\n", IrVoltage);
wait(1);
}
}
Wire up the sensor: 1) yellow wire = analog output voltage to mbed p20, 2) black wire to mbed GND, and 3) red wire to mbed 3.3V Vout. Compile the program and load the bin, open a terminal window at 9600 baud and restart the mbed and you should see the measured analog output value, with values ranging from 0.1245 to 0.7089. I haven't yet converted to actual volts and converting to distance is a bit tricky as the curve has a steep upslope from 0-10cm and then a downward curve flattining with distance as also shown in the manual. But it should be easy enough to either figure out a formula which fits the data, or just measure a series of distances and base the estimates of other measurements on that series.
With 5V input the analog output voltage should range up to about 3.1 V, so even that shouldn't be big problem.
And for those wanting ideas of what to do with this type of sensor, check out the video of two guys "ping-ponging" a hovering multikopter equiped with four of these sensors and programmed to keep a minimum horizontal distance from obstacles: http://www.diydrones.com/profiles/blogs/announcing-arducopter-the (first video a little bit down from the top of the page)
Here is one of the easiest sensors to work with, at least with respect to collecting data, the Sharp GP2Y0A21YK infrared sensor (see eg http://www.coolcomponents.co.uk/catalog/product_info.php?cPath=36_57&products_id=236). Stricktly speaking it is a 5V device but will accept input voltages from 0.5 to 7V or so. So I just used the mbed's 3.3V output and the following short program:
#include "mbed.h"
AnalogIn IrSensor(p20);
Serial serial(USBTX, USBRX);
int main()
{
float IrVoltage = 0.0;
serial.baud(9600);
while(1) {
IrVoltage=IrSensor.read();
serial.printf("Voltage: %f\r\n", IrVoltage);
wait(1);
}
}
Wire up the sensor: 1) yellow wire = analog output voltage to mbed p20, 2) black wire to mbed GND, and 3) red wire to mbed 3.3V Vout. Compile the program and load the bin, open a terminal window at 9600 baud and restart the mbed and you should see the measured analog output value, with values ranging from 0.1245 to 0.7089. I haven't yet converted to actual volts and converting to distance is a bit tricky as the curve has a steep upslope from 0-10cm and then a downward curve flattining with distance as also shown in the manual. But it should be easy enough to either figure out a formula which fits the data, or just measure a series of distances and base the estimates of other measurements on that series.
With 5V input the analog output voltage should range up to about 3.1 V, so even that shouldn't be big problem.
And for those wanting ideas of what to do with this type of sensor, check out the video of two guys "ping-ponging" a hovering multikopter equiped with four of these sensors and programmed to keep a minimum horizontal distance from obstacles: http://www.diydrones.com/profiles/blogs/announcing-arducopter-the (first video a little bit down from the top of the page)