Hello, I am trying to read data from my Lidar Lite V3 on my NodeMCU ESP8266 V3, with a 3.3v-5v bi-directional logic level shifter between them. The lidar works fine with my Uno, Nano and Nano Every this way, but the example PWM sketch doesn't on the NodeMCU:
unsigned long pulseWidth;
void setup()
{
Serial.begin(115200); // Start serial communications
pinMode(14, OUTPUT); // Set pin 2 as trigger pin
digitalWrite(12, LOW); // Set trigger LOW for continuous read
pinMode(12, INPUT); // Set pin 3 as monitor pin
}
void loop()
{
pulseWidth = pulseIn(12, 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
}
I noticed that a very simple HC-SR04 ultrasonic sensor setup worked quite well with the NodeMCU so I figured maybe modifying a simple HC-SR04 sketch might do the job. A few times some correct data was produced by this, but at the moment it mostly produces zeroes.
const int trigPin = 12;
const int echoPin = 14;
long duration;
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
if(duration != 0)
{
duration = duration / 10; // 10usec = 1 cm of distance
Serial.println(duration);
}
delay(10);
}
