I am trying to use the ultrasonic RAK12007 with the RAK19007 base and RAK3172 core. the duration coming in is not reading correct. This is the basic code I am using the duration value is in the rounded 1,000. I am thinking it maybe something with the clock setting.
#include <Wire.h>
#define TRIG WB_IO6
#define ECHO WB_IO4
#define PD WB_IO5 //power done control (=1 power done,=0 power on)
unsigned long duration;
void setup()
{
Serial.begin(115200);
Wire.begin();
Wire.setClock(400000); // The sensor is listed to work up to 1 MHz I2C speed, but the I2C clock speed is global for all sensors on that bus so using 400kHz or 100kHz is recommended
pinMode(ECHO, INPUT); // Echo Pin of Ultrasonic Sensor is an Input
pinMode(TRIG, OUTPUT); // Trigger Pin of Ultrasonic Sensor is an Output
pinMode(PD, OUTPUT); // power done control pin is an Output
pinMode(LED_BLUE, OUTPUT); // The LED is an Output
digitalWrite(PD, LOW); //low = power on
Serial.println("========================");
Serial.println(" RAK12007 test");
Serial.println("========================");
}
void loop()
{
digitalWrite(LED_BLUE, HIGH);
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
duration = pulseIn(ECHO, HIGH); // microseconds
Serial.print("duration - ");
Serial.println(duration);
delay(500);
digitalWrite(LED_BLUE, LOW);
}
This is the Serial.print;
10:28:25.691 → duration - 2000
10:28:26.207 → duration - 2000
10:28:26.675 → duration - 2000
10:28:27.191 → duration - 3000
10:28:27.660 → duration - 2000
10:28:28.160 → duration - 1000
10:28:28.630 → duration - 1000
10:28:29.122 → duration - 1000
10:28:29.639 → duration - 1000
10:28:30.141 → duration - 1000
10:28:30.610 → duration - 2000
10:28:31.126 → duration - 9000
10:28:31.609 → duration - 9000
10:28:32.112 → duration - 9000
10:28:32.583 → duration - 8000