/*------------------------------------------------------------------------------
LIDARLite Arduino Library
GetDistancePwm
This example shows how to read distance from a LIDAR-Lite connected over the
PWM interface.
Connections:
LIDAR-Lite 5 Vdc (red) to Arduino 5v
LIDAR-Lite Ground (black) to Arduino GND
LIDAR-Lite Mode control (yellow) to Arduino digital input (pin 3)
LIDAR-Lite Mode control (yellow) to 1 kOhm resistor lead 1
1 kOhm resistor lead 2 to Arduino digital output (pin 2)
(Capacitor recommended to mitigate inrush current when device is enabled)
680uF capacitor (+) to Arduino 5v
680uF capacitor (-) to Arduino GND
See the Operation Manual for wiring diagrams and more information:
http://static.garmin.com/pumac/LIDAR_Lite_v3_Operation_Manual_and_Technical_Specifications.pdf
------------------------------------------------------------------------------*/
unsigned long pulseWidth;
void setup()
{
Serial.begin(115200); // Start serial communications
pinMode(2, OUTPUT); // Set pin 2 as trigger pin
digitalWrite(2, LOW); // Set trigger LOW for continuous read
pinMode(3, INPUT); // Set pin 3 as monitor pin
}
void loop()
{
pulseWidth = pulseIn(3, HIGH); // Count how long the pulse is high in microseconds
// If we get a reading that isn't zero, let's print it
if(pulseWidth != 0)
{
pulseWidth = pulseWidth / 10; // 10usec = 1 cm of distance
Serial.println(pulseWidth); // Print the distance
}
}
How have you hooked it up to the Photon and what does your photon code look like if different from Arduino? Have you tried with a variety of distances? Does it work with short but not long distances?
@Jimmie, first, you need to use Particle designations for pins like D2 or D3 (unlike Arduino) throughout your code. After doing that, let me know how it went.
unsigned long pulseWidth;
double tempC = 0;
void setup()
{
Serial1.begin(9600); // Start serial communications
Particle.variable("tempC", &tempC, DOUBLE);
pinMode(D2, OUTPUT); // Set pin 2 as trigger pin
digitalWrite(D2, LOW); // Set trigger LOW for continuous read
pinMode(D3, INPUT); // Set pin 3 as monitor pin
pinMode(D7, OUTPUT);
}
void loop()
{
pulseWidth = pulseIn(D3, HIGH); // Count how long the pulse is high in microseconds
tempC=pulseWidth;
digitalWrite(D7, HIGH);
Serial1.print("pulseWidth = ");
Serial1.println(pulseWidth);
// If we get a reading that isn't zero, let's print it
if(pulseWidth != 0)
{
pulseWidth = pulseWidth / 10; // 10usec = 1 cm of distance
Serial.println(pulseWidth); // Print the distance
digitalWrite(D7, LOW);
}
}
Yes or else you wouldnt be able to compile but i believe pulseIn() has a limit of a max 10 second length - shouldnt be an issue in your app unless you are measuring very large distances.
So have you actually measured the incoming pulse on the input pin?
Do you have a common ground connection between the Photon and the Arduino you are using for power so that the entire system has a common ground reference?
It would be best if you could upload a picture of wiring so we can check everything.
i think you might want to check if the serial should be set to 115200 across all settings. i think they suggest to use 115200 on the lidar. if you decide to go with 115200, check the com port setting as well.
If you are powering the sensor from the Arduino, have you got common ground between the Arduino, the Photon and the sensor? (as I just realised @bko already pointed out )
I changed and simplified the wiring and my code is below. Still no measurements. I have attached a picture of my circuit.
unsigned long pulseWidth;
double tempC = 0;
void setup()
{
Serial.begin(115200); // Start serial communications
pinMode(D2, OUTPUT); // Set pin 2 as trigger pin
digitalWrite(D2, LOW); // Set trigger LOW for continuous read
pinMode(D3, INPUT); // Set pin 3 as monitor pin
}
void loop()
{
pulseWidth = pulseIn(D3, HIGH); // Count how long the pulse is high in microseconds
Serial.println(pulseWidth);
// If we get a reading that isn't zero, let's print it
if(pulseWidth != 0)
{
pulseWidth = pulseWidth / 10; // 10usec = 1 cm of distance
Serial.println(pulseWidth); // Print the distance
}
}
Well the best thing would be to get a scope… But you could hook up a voltmeter on the LIDAR output signal and vary the distance it is reading. Since that is supposed to produce a different pulse width, you should see some change in the DC signal. You may have to play with your voltmeter range settings a bit to see anything. Of course this would be nothing more than a ‘something isn’t broke yet’ test; no prove of much more.
Next, instead of reading the pulseWidth etc, try to copy the status of D3 to D7 (the LED). The LIDAR gives very quick pulses so the average light you will get from the LED won’t be much but with varying distances you should be able to get some varying LED light (might be tough to see). If that works, your hardware is working and your input code is working.
be positive that the resistor is very tight in that breadboard and place wires in opening directly next to the resistor for all wires. plug a 5v power supply into protoboard via barrel jack.