I am using an RCWL-1601 Ultrasonic sensor, ran off of a Raspberry Pi Pico. According to Adafruit, the sensor is equivalent to the more popular HC-SR04 Ultrasonic sensor. I am trying to use the sensor to measure distance. The output is currently showing 0.The sensor is wired to 3v3 & GND (datasheet for sensor says 3v3 or 5v compatible, I have tried both 3v3 & 5v), with Trig on GPIO3 and Echo on GPIO2. The code compiles and provides the output of "Distance = Out of range" on the serial monitor. I am running the following code through Arduino IDE:
#define trigPin 3
#define echoPin 2
float duration, distance;
void setup() {
Serial.begin(9600);
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);
// convert the time into a distance
distance = (duration/2) * 0.0343;
Serial.print("Distance = ");
if(distance >= 400 || distance <= 2){
Serial.println("Out of range ");
}
else{
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
delay(500);
}
Output when uploading to Pi:
Sketch uses 53548 bytes (2%) of program storage space. Maximum is 2093056 bytes.
Global variables use 10232 bytes (3%) of dynamic memory, leaving 251912 bytes for local variables. Maximum is 262144 bytes.
Resetting COM8
Converting to uf2, output size: 142848, start address: 0x2000
Scanning for RP2040 devices
Flashing D: (RPI-RP2)
Wrote 142848 bytes to D:/NEW.UF2
Serial monitor output (Repeating):
13:35:08.795 -> Distance = Out of range
13:35:10.294 -> Distance = Out of range
13:35:11.795 -> Distance = Out of range
13:35:13.289 -> Distance = Out of range
13:35:14.791 -> Distance = Out of range
13:35:16.295 -> Distance = Out of range
I have verified that the sensor is getting power through measuring the VCC and GND pins on a scope. I have tried multiple of the same sensor. I tried rewiring to different pins on the Pico. On the back of the sensor is a jumper spot for either UART or I2C, with the default being I2C connected. I have tried soldering the UART jumper and disconnecting the I2C jumper.
I believe the issue may be that the echo pin is not going low when it receives the pulse, but I do not know how/why. I have tried multiple different versions of code from Youtube tutorials & forums.
I have tried the Blink example to verify that I can flash to the Pico from the Arduino IDE and that worked fine.
I am on Arduino IDE version 1.8.18. I am using the boards manager package called Raspberry Pi Pico/RP2040 version 3.3.0 by Earle F. Philhower.