PWM Sensor not working with Photon

Hello:

I have a sensor which can use either i2C or PWM (please see attached).

I tried the PWM approach with an Arduino MEGA and DUE and it worked fine.

On a Particle Photon, I could not get it working with PWM. I only got it to work with I2C but I prefer PWM.

Is there a difference when using a Photon PWM vs Arduino?

Thanks in advance for any help.

Here is the code that works on Arduino:

/*------------------------------------------------------------------------------

  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?

Hi Joost,

I am using the identical Arduino code for the Photon.

When hooked up to the Photon, I am not getting any readings (pulseWidth =0)

@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. :wink:

Thank you Peekay. I just changed to Dx but same result, distance measured = 0

@Jimmie, reading the Particle documentation for pulseIn(), I found this nugget:

Works on pulses from 10 microseconds to 3 seconds in length.

I am assuming your pulse is 10 or more microseconds in length. Can you repost your code?

Thanks. here is the code:

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);
  }
}

How are you connecting the LIDAR to the Photon? Are you using a 5V supply for it since it is a 5V-only device?

Hi Brian:

Yes, I am powering the sensor from the 5VDC of an Arduino (only purpose of Arduino is to supply power to sensor).

I know the Arduino gives it enough power because when the sensor is powered the same way when connected to the Arduino.

Please see attached circuit. Does the value of the resistor I am using (1KOhm) need to be different for the Photon?

BTW, does the Photon support “pulseIn”?

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?

Yes, the Photon supports pulseIn within some limits-the doc is here:

https://docs.particle.io/reference/firmware/photon/#pulsein-

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 :blush:)

Hello:

Thank you for your help.

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
  }
  
}

I have not done that as I do not know how to do it …

Realized mapping of shield so wired sensor directly to Photon. Again, all readings = 0

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.