getting wrong distance value while using HC SR04 Ultrasonic Sensor with ESP323

Ritesh
Posts: 1365
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

getting wrong distance value while using HC SR04 Ultrasonic Sensor with ESP323

Postby Ritesh » Sun Jun 14, 2020 10:33 am

Hello,

Hope you guys are doing well into COVID-19.

We are using Ultrasonic Sensor HC SR04 with ESP32-WROVER-I module to measure object distance. We have ported with ESP32 IDF 3.2 Stable Release and it works fine into normal condition.

But when we run test continuously at every 2[*]0 msec interval to measure distance then sometimes we are getting distance value as 10 or 15 or something like lower values even there is no any object placed.

Code: Select all

#define ULTRASONIC_OUTPUT_GPIO GPIO02
#define ULTRASONIC_INPUT_GPIO GPIO36
#define PORT_ENTER_CRITICAL portENTER_CRITICAL(&mux)
#define PORT_EXIT_CRITICAL portEXIT_CRITICAL(&mux)
#define timeout_expired(start, len) ((esp_timer_get_time() - (start)) >= (len))
#define PING_TIMEOUT 6000
#define ROUNDTRIP 58
#define ULTRASONIC_POLL_DELAY	5

	float cm = 0;
	PORT_ENTER_CRITICAL;
	gw_gpio_level_set(ULTRASONIC_OUTPUT_GPIO, LOW);
	ets_delay_us(2);
	gw_gpio_level_set(ULTRASONIC_OUTPUT_GPIO, HIGH);
	ets_delay_us(10);
	gw_gpio_level_set(ULTRASONIC_OUTPUT_GPIO, LOW);
	// Wait for echo
	int64_t start = esp_timer_get_time();
	while (!gpio_get_level(ULTRASONIC_INPUT_GPIO)) {
		if (timeout_expired(start, PING_TIMEOUT)) {
			PORT_EXIT_CRITICAL;
			return cm;
		}
	}

	// got echo, measuring
	int64_t echo_start = esp_timer_get_time();
	int64_t time = echo_start;
	int64_t meas_timeout = echo_start + 500 * ROUNDTRIP;
	while (gpio_get_level(ULTRASONIC_INPUT_GPIO)) {
		time = esp_timer_get_time();
		if (timeout_expired(echo_start, meas_timeout)) {
			PORT_EXIT_CRITICAL;
			return cm;
		}
	}
	PORT_EXIT_CRITICAL;

	cm = (time - echo_start) / ROUNDTRIP;
	DEBUG_LOGI(UART, REST_TAG, "Distance %f cm", cm); 
So does anyone face this type of issue or not? if yes then any resolution for same?

Let me know if need anything else from my end
Regards,
Ritesh Prajapati

Ritesh
Posts: 1365
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: getting wrong distance value while using HC SR04 Ultrasonic Sensor with ESP323

Postby Ritesh » Sun Aug 16, 2020 12:18 pm

Hello,

Anyone has faced this type of issue while using it with ESP32?
Regards,
Ritesh Prajapati

ESP_Sprite
Posts: 9051
Joined: Thu Nov 26, 2015 4:08 am

Re: getting wrong distance value while using HC SR04 Ultrasonic Sensor with ESP323

Postby ESP_Sprite » Mon Aug 17, 2020 7:53 am

Did you make sure this is not a hardware issue, by eg. connecting an oscilloscope to the output lines?

Ritesh
Posts: 1365
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: getting wrong distance value while using HC SR04 Ultrasonic Sensor with ESP323

Postby Ritesh » Tue Aug 25, 2020 4:49 am

ESP_Sprite wrote:
Mon Aug 17, 2020 7:53 am
Did you make sure this is not a hardware issue, by eg. connecting an oscilloscope to the output lines?
Yes. We have verified like there is no any issue on module.

But when we tested long run at that time we found one thing that C6 Capacitor of Ultrasonic Module is going to damage as per its required resistance which should be 4.2K Ohm while we are getting 200 Ohm

So Do you have any idea like why it is causing that type of issue?
Regards,
Ritesh Prajapati

Ritesh
Posts: 1365
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: getting wrong distance value while using HC SR04 Ultrasonic Sensor with ESP323

Postby Ritesh » Sat Aug 29, 2020 6:55 pm

ESP_Sprite

Any update for last comment which I have given for this issue?
Regards,
Ritesh Prajapati

Who is online

Users browsing this forum: No registered users and 258 guests