I’m having a weird problem here with my ultrasonic sensors. I’m reading noise off my one of the 4 sensors in my system.
Attached is the code, output and the environment where the system is being tested. The values came out to be within 72 - 120 range where the actual distance is 2.4 to 2.6m
long ds1 = 0;
long ds2 = 0;
long ds3 = 0;
long ds4 = 0;
long duration1, duration2, duration3, duration4;
#define POWER_PIN_1 D4
#define ECHO_PIN_1 D3
#define TRIG_PIN_1 D2
#define ECHO_PIN_2 D1
#define TRIG_PIN_2 D0
#define ECHO_PIN_3 B5
#define TRIG_PIN_3 B4
#define ECHO_PIN_4 A1
#define TRIG_PIN_4 A0
// particle setup function...
void setup() {
/*********************************************************************************************************************************************************************/
pinMode(POWER_PIN_1, OUTPUT);
pinMode(TRIG_PIN_1, OUTPUT);
pinMode(ECHO_PIN_1, INPUT_PULLDOWN);
pinMode(TRIG_PIN_2, OUTPUT);
pinMode(ECHO_PIN_2, INPUT_PULLDOWN);
pinMode(TRIG_PIN_3, OUTPUT);
pinMode(ECHO_PIN_3, INPUT_PULLDOWN);
pinMode(TRIG_PIN_4, OUTPUT);
pinMode(ECHO_PIN_4, INPUT_PULLDOWN);
Serial.begin(9600);
digitalWrite(POWER_PIN_1, HIGH);
Cellular.off();
}
void loop() {
delay(500);
digitalWrite(TRIG_PIN_1, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(TRIG_PIN_1, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(TRIG_PIN_1, LOW);
ds1 = pulseIn(ECHO_PIN_1, HIGH); // gets duration...
ds1 = microsecondsToCentimeters(ds1); // gets distance...
Serial.println(ds1);
delay(500);
digitalWrite(TRIG_PIN_2, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(TRIG_PIN_2, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(TRIG_PIN_2, LOW);
ds2 = pulseIn(ECHO_PIN_2, HIGH); // gets duration...
ds2 = microsecondsToCentimeters(ds2); // gets distance...
Serial.println(ds2);
delay(500);
digitalWrite(TRIG_PIN_3, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(TRIG_PIN_3, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(TRIG_PIN_3, LOW);
ds3 = pulseIn(ECHO_PIN_3, HIGH); // gets duration...
ds3 = microsecondsToCentimeters(ds3); // gets distance...
Serial.println(ds3);
delay(500);
digitalWrite(TRIG_PIN_4, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(TRIG_PIN_4, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(TRIG_PIN_4, LOW);
ds4 = pulseIn(ECHO_PIN_4, HIGH); // gets duration...
ds4 = microsecondsToCentimeters(ds4); // gets distance...
Serial.println(ds4);
}
long microsecondsToCentimeters(long microseconds)
{
return (microseconds / 58.2);
}
@Rftop I have missed the copy,paste, will update shortly.
@kennethlimcp the sensor works fine when it’s working with a single ultrasonic sensor. I have tried the following ways to troubleshoot.
The sensor with the problem would be working fine if one of the other sensors are unplugged together with the ultrasonic driver.
If the problematic sensor was plugged out leaving the driver on, with the other 3 sensors untouched, it would be reading noise as shown in the output.
If all 4 sensors are plugged out with their drivers on, the other 3 will have a timeout reading while the first sensor will be reading noise
4)If I changed the USB to be connected to my laptop instead of my PC, the all 4 sensors would have the timeout readings with the drivers on only. The readings would be correct if I have all 4 sensors plugged in.
5)When running on batteries, the output would be the same as 4).
Anyone knows what might be the cause of the problem?
It just feels like the ultrasonic sensors are within the range/view of the sound wave sent from one device, ending up being received by another. I would test with two spaced far apart to see what happens first.
@peekay123 either the usb or a battery via the Vin port is the only source to power the device. @kennethlimcp I tried inserting a 2 seconds delay after transmitting and receiving each pulse from individual sensors, I have also put the echo pin to low positions after it has obtained the distance values.
I know this is a long shot, but is there any chance the “faulty” values are always related to the same pair of pins?
For instance: Is it always (ds2) on Pins D0 & D1, no matter where you install the sensors ?
You could add the (“ds1”…2…3…4) to your serial.print and swap the sensors around to test.
@Rftop the faulty values are always the same set of pins. The error would shift over to the next sensor if I connect the driver to a breadboard with jumper wires to the circuit board.
@NST1992 , The reason I asked was because I always try to stay away from D0 & D1 pins if I can, since those are used for the i2c Bus. That probably has nothing to do with your issues, but there is a lot about the MCU that I don’t understand.
I’ve used this sensor in a Wastewater application to measure the flow of water over a V-notch weir. I never got comfortable with it measuring the height in an enclosed space (manhole), but other people have.
Didn’t have time to read all the responses, so somebody may have already caught this, but the sensor has about a 20cm minimum distance. You will get erratic readings at a distance shorter than that. I was just discussing the issue in another thread.
@scraphound As shown in the picture, there was nothing blocking the ultrasound sensors pointing to the ceiling. I’m guessing that there may be built up static within the PCB.
You can use a small ceramic capacitor to drain off any spurrious rf. If that doesn’t work, a ferrite choke, known as an antenna balun, should help. Changing the length of the lead wires might also help. Lead wires act as an antenna and will receive a much stronger RF signal at their resonant frequency. For example, in the distant past, telephone hookup wires were typically 10’ long… pretty close resonant at CB radio frequencies… This caused lots of problems.
Hey, @NST1992 I don’t see a schematic, but the sensor really works with 5V coming in. It needs 5V to work properly. The Particle pins are 3.3. Are you stepping up the voltage somewhere? I tried using a SparkFun 5V boost converter from 3.3 to 5V, but it didn’t work because the 5V wasn’t coming at the right frequency, but I found this 5V charge pump on aliexpress and it seems to work pretty well. link here
I also think it could be potentially a great application in the waste and gas and oil space. I’d love to talk about it more.