Hello all,
I have the following problem with the sensor mentioned above. I have hooked it to an ESP32-S3, trigger to pin 21, echo to pin 47. When trying to measure the distance I have the following error message:
“Distance measurement timed out”. I have tried all the suggestions, increasing the distance, adjusting the timeout, nothing works. I have also tried other pins with the same result.
I power the sensor from a 3.3 volts power supply, the same used for ESP32-S3 to run.
The code I’m using in yaml file is the following:
The sensor itself is functional, I have tried it with Arduino code (see below) and it works like a charm. Only when trying to hook it up with ESPHome, it stops working.
Please help me with this, in case you have suggestions. Really appreciate your help and support.
const int trigPin = GPIO21;
const int echoPin = GPIO47;
//define sound velocity in cm/uS
#define SOUND_VELOCITY 0.034
#define CM_TO_INCH 0.393701
long duration;
float distanceCm;
float distanceInch;
void setup() {
Serial.begin(115200); // Starts the serial communication
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distanceCm = duration * SOUND_VELOCITY/2;
// Convert to inches
distanceInch = distanceCm * CM_TO_INCH;
// Prints the distance on the Serial Monitor
Serial.print("Distance (cm): ");
Serial.println(distanceCm);
Serial.print("Distance (inch): ");
Serial.println(distanceInch);
delay(1000);
}
Hi Karosm,
thank you for your reply. I haven’t, until now, but now I did. I added the uart as well, but I do not know what port should I specify in the uart descriptor and without it I cannot compile. Any idea how can I get the port?
Default uart1 pins for your esp are 18rx and 17tx.
But afaik you need to physically change mode-resistor on your sensor to put it in UART mode.
I would just try it with default settings without uart first
I did that. I have soldered 120k ohm resistor, before writing on the forum. Didn’t work.
I have connected then the sensor to the pins GIPO43 and GPIO44 (the serial port from ESP). From the serial of the esp 32-s3. Now, I have managed to have the sensor LED blinking with high frequency, I do not have the “Distance measurement timed out” error but the distance it measures remains constant to 0.018m.
I will try to connect it to the pins you’ve mention but I do not think it is from there…
There is no answer from the sensor now. At least, when it was connected to the default UART, when the LED was flashing fast, now, there is no blinking. There is no output in the console either.
I have uploaded another picture where you can see the 120k resistor. Don’t mind the connector, it has 5 pins because I have reused it, the green cable is not connected.
Uart0 is hardwired to 43/44, so if you want to try with uart1 on 17/18, you need to enable serial logger. I see from your log that Esphome tries to assign 17/18 to Uart0.
I’ve managed to fix it. I had to comment the
model: aj_sr04m
line and now it started to work like a charm.
Hey, I THANK YOU big time for your help and support, really appreciate, you made my night!!!
Thank you, wherever you are in this world, you did something amazing for today, helping me with this topic.
No how, it’s in continuous mode. But you could use filters to output some averaging value, it would move fluctuation and give you better readings as well.
Or you change the resistor and put it in triggered uart mode.
The problem is that, I don’t understand if your board really is AJ since Esphome thinks it’s JSN.
Changing the resistor to 47k will put the board on triggered UART mode, right?
Anyway I am not the only one having this issue I have found here someone who points to the same comment on removing the model from the yaml to make it work.
I might try tomorrow to see if it works on triggered UART mode. For now I am happy it works like it does.
Thank you once again.
Chinese manufacturers might use any pcb that they find in stock…
Did you try with filtering? You can set higher numbers to have more averaging and longer update interval. I don’t know if they have some max though.