PWM Sensor not working with Photon

OK, gentlemen, I followed your guidelines.

I am using the same code on both an Arduino and the Photon (only difference is preceding the pin with a D for the Photon). I am using the SAME sensor circuit for each board.

  1. On the Arduino, everything works.

  2. On the Photon, per Joost, indeed the blue light (D7) does flash when I vary the distance. However, the distance read by the Photon is still zero. My code is below. Could the value of the resistor cause this?

unsigned long pulseWidth;

void setup()
  Serial.begin(115200); // Start serial communications

  pinMode(D4, OUTPUT); // Set pin 4 as trigger pin
  digitalWrite(D4, LOW); // Set trigger LOW for continuous read

  pinMode(D7, INPUT); // Set pin 7 as monitor pin   //Yellow

void loop()
  pulseWidth = pulseIn(D7, HIGH); // Count how long the pulse is high in microseconds
  Serial.print ("pulse= "); 
  // 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

@Jimmie, print the “raw” pulseWidth and don’t do the divide by 10. What do you get? Also, D7 has an LED connected via a current limiting resistor which may affect the circuit. Can you try another pin like D2/

Hello peekay123,

I was printing the “raw” pulseWidth. (code divides it by 10 only when non-zero)

I moved pin 7 to pin 2. Same result, pulseWidth = 0.

@Jimmie, did you change the pin setup in the code as well?

Yes of course.

pinMode(D2, INPUT); // Set pin 7 as monitor pin //Yellow

void loop()
pulseWidth = pulseIn(D2, HIGH); // Count how long the pulse is high in microseconds

@Jimmie, had to check! Do you have a link to the specs for the LIDAR?

Yes, here it is. Thank you.

@Jimmie, my gut tells me to ditch the shield and connect the +5v for the LIDAR to Vin of the Photon with common grounds. Keep the rest connected as you have it (D2, D3). The MODE pin needs to be pulled LOW so 5v is not required from the Photon. Both D2 and D3 are 5v tolerant so no problem there.

I powered the Photon with USB, connected the sensor to VIN and GND and used D2 and D4. pulseWidth still = 0

when you do finally get it right it will start sending data very quickly, i would suggest this.
if(pulseWidth != 0)
pulseWidth = pulseWidth/10; // 10usec = 1 cm of distance
Serial.println(pulseWidth); // Print the distance
digitalWrite(D7, LOW);
delay(200); //Delay so we don’t overload the serial port

on the breadboard have yellow trigger from lidar/resistor/yellow trigger to D3 of photon in one line.
on seperate breadboard line have resistor/line to D2

for power
from 5v power source have 680 cap accross + & - then power to lidar on other side of cap. run a wire from negative of
power to gnd on photon


Did all of that, does not work! Took same circuit again, same code to an UNO and it worked immediately.

I am not an electronics engineer so I have no idea what is going on. It is frustrating though that a modern supposedly “compatible” and superior board is not working. I am happy to try anything else.

I will also borrow an oscilloscope from a friend if you would help me with how to hook it up and what I should see coming out …

Thanks again for your time and effort.

well all i can say is my setup isn’t pretty but it does work.

The only thing I can think of is that the Shield-shield is not handling the bidirectional PWM signal properly. If you look at @dkryder working setup, he uses the Photon without any adapter board.

Can you try with the Photon mounted just on a solderless breadboard instead of on the Shield-shield?

Sure, I will try it without the shield.

However, dkryder says his setup works? Did you hook it up to the same sensor I am using?

I am asking because you are actually switching the wires coming out of the sensor. For it to work (on an Arduino), you need to connect the White wire to D3 and the Yellow wire to D2 (you have them switched). (BTW, I tried them both ways on the Photon and it does not work either way).

Thanks again.



When I placed the Photon on its own breadboard like dkryder showed, everything worked. It turns out the Shield was causing all these problems.

Is there I way I can get Particle to know this so it can be fixed in the future?

Thanks is due to all of you for your time and effort. I learned something new from all of you.

I feel ignored :cry:

1 Like

:slight_smile: :slight_smile:

Sorry peekay123, you were right too. It is my fault as I understood you meant to take to the sensor power directly from the Photon pins (but I left the board in the shield).

Thank you again. As I said, I learned new things from all of you.

1 Like

I’m confused :confused:
In this post you already said you ditched the shield

Leading us on here? :wink:

to answer last question, jimmie, i’m using lidar v3. glad you got it to work.

Hello Scruffr,

My bad! I understood it to mean wire directly to Photon pins but then I left the board in the shield. I am surprised that a simple level shifter caused this. Thanks again.

1 Like