ESP32 JSN-SR04T Waterproof Ultrasonic Distance Sensor
Overview
The JSN-SR04T is a waterproof ultrasonic distance sensor ideal for outdoor and industrial applications. With a measuring range of up to 6 meters and a durable build, it is perfect for detecting objects in harsh conditions. It operates using Trigger and Echo signals and is compatible with microcontrollers like Arduino and ESP32.
About JSN-SR04T Waterproof Ultrasonic Distance Sensor
The JSN-SR04T is a waterproof ultrasonic sensor designed for long-range distance measurement in outdoor and industrial environments. With a measurement range of 25 cm to 600 cm, it is ideal for liquid level sensing, parking sensors, and proximity detection.
⚡ Key Features
- Extended Measurement Range – Detects distances from 25 cm to 600 cm.
- Waterproof & Dustproof Design – Ideal for harsh outdoor conditions.
- Trigger & Echo Mechanism – Operates similarly to the HC-SR04 for easy integration.
- ESP32 & Arduino Compatible – Works seamlessly with microcontrollers for automation projects.
With its rugged build and reliable performance, the JSN-SR04T is perfect for applications requiring water-resistant distance sensing. 🚀
Where to Buy
Prices are subject to change. We earn from qualifying purchases as an Amazon Associate.
Technical Specifications
Pinout Configuration
The VCC
pin is used to supply power to the sensor, and it typically requires 3.3V or 5V (refer to the datasheet for specific voltage requirements). The GND
pin is the ground connection and must be connected to the ground of your ESP32.
The JSN-SR04T pinout is as follows:
- VCC: Connect to a 5V power supply.
- GND: Ground connection.
- Trigger (TRIG): Input pin to initiate distance measurement by sending a short 10 µs pulse.
- Echo: Output pin that provides a pulse width corresponding to the measured distance.
Troubleshooting Guide
Common Issues
⚠️ Sensor Returns Constant or Erroneous Readings
Issue: The JSN-SR04T sensor provides constant or incorrect distance measurements, regardless of the actual distance to the target.
Possible causes include incorrect wiring, insufficient power supply, or improper sensor configuration.
Solution: Ensure that the sensor is connected to a stable 5V power source, as it requires 5V for proper operation. Verify that the TRIG and ECHO pins are correctly connected to the appropriate GPIO pins on the microcontroller. Confirm that the sensor is properly initialized in your code, and that the trigger pulse duration is set correctly; some users have found that a 15-microsecond trigger pulse can improve reliability.
🔄 Inconsistent or Fluctuating Distance Measurements
Issue: The sensor outputs distance readings that vary significantly, even when the target distance remains constant.
Possible causes include environmental factors such as temperature variations, soft or angled target surfaces, or electrical noise.
Solution: Position the sensor perpendicular to a hard, flat target surface to ensure accurate reflections. Be aware that temperature changes can affect the speed of sound; consider implementing temperature compensation if precise measurements are required. Implement averaging of multiple readings in your code to mitigate occasional erroneous data.
❌ Sensor Not Detected or Unresponsive
Issue: The microcontroller fails to detect the JSN-SR04T sensor, or the sensor does not respond to trigger signals.
Possible causes include incorrect pin assignments in the code, lack of proper initialization, or defective sensor module.
Solution: Double-check the pin assignments in your code to ensure they match the physical connections. Confirm that the sensor is properly initialized in the setup section of your code. If the issue persists, test the sensor with a known working setup or replace it to rule out hardware failure.
🌍 Interference from Environmental Factors
Issue: External factors cause the sensor to produce unreliable readings.
Possible causes include high ambient noise levels, temperature variations, or obstacles in the sensor's field of view.
Solution: Operate the sensor in a controlled environment to minimize acoustic and electrical noise. Ensure that there are no unintended obstacles within the sensor's detection range that could cause false readings.
Debugging Tips
🔍 Serial Monitor
Use the Serial Monitor to check for error messages and verify the sensor's output. Add debug prints in your code to track the sensor's state.
⚡ Voltage Checks
Use a multimeter to verify voltage levels and check for continuity in your connections. Ensure the power supply is stable and within the sensor's requirements.
Additional Resources
Code Examples
Arduino Example
#define TRIG_PIN 5
#define ECHO_PIN 18
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
Serial.println("JSN-SR04T Distance Sensor Example");
}
void loop() {
long duration;
float distance;
// Trigger the sensor
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read the Echo pin
duration = pulseIn(ECHO_PIN, HIGH);
// Calculate distance in cm
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
TRIG_PIN
and ECHO_PIN
are defined to connect the sensor to GPIO pins 9 and 10, respectively. The setup()
function initializes these pins and configures the Serial Monitor. In the loop()
, a 10 µs pulse is sent to the Trigger pin to start measurement, and the duration of the Echo pin's HIGH state is measured using the pulseIn()
function. The distance is calculated using the formula duration * 0.034 / 2
, which converts the time into distance in centimeters.ESP-IDF Example
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_timer.h"
#define TRIG_PIN GPIO_NUM_5
#define ECHO_PIN GPIO_NUM_18
void app_main() {
gpio_set_direction(TRIG_PIN, GPIO_MODE_OUTPUT);
gpio_set_direction(ECHO_PIN, GPIO_MODE_INPUT);
while (1) {
// Send trigger pulse
gpio_set_level(TRIG_PIN, 0);
ets_delay_us(2);
gpio_set_level(TRIG_PIN, 1);
ets_delay_us(10);
gpio_set_level(TRIG_PIN, 0);
// Measure echo pulse width
uint64_t start_time = esp_timer_get_time();
while (!gpio_get_level(ECHO_PIN)); // Wait for HIGH
uint64_t echo_start = esp_timer_get_time();
while (gpio_get_level(ECHO_PIN)); // Wait for LOW
uint64_t echo_end = esp_timer_get_time();
uint64_t duration = echo_end - echo_start;
float distance = (duration * 0.034) / 2;
printf("Distance: %.2f cm\n", distance);
vTaskDelay(pdMS_TO_TICKS(500));
}
}
gpio_set_level()
function sends a 10 µs pulse to the Trigger pin. The time taken for the Echo pin to go HIGH and then LOW is measured using esp_timer_get_time()
, which provides timestamps in microseconds. The distance is calculated based on the formula (duration * 0.034) / 2
. The program continuously measures and prints the distance in centimeters every 500 ms.ESPHome Example
sensor:
- platform: ultrasonic
trigger_pin: GPIO5
echo_pin: GPIO18
name: "JSN-SR04T Distance"
update_interval: 500ms
accuracy_decimals: 1
timeout: 2.0m
ultrasonic
platform to interface with the JSN-SR04T sensor. The trigger_pin
and echo_pin
specify the GPIO pins connected to the sensor. The name
assigns a user-friendly identifier ('JSN-SR04T Distance') for use in platforms like Home Assistant. The update_interval
of 500 ms specifies how often distance measurements are taken, while accuracy_decimals
ensures measurements are displayed to one decimal place. The timeout
prevents errors in case of no response within the specified time.PlatformIO Example
platformio.ini
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
PlatformIO Example Code
#define TRIG_PIN 5
#define ECHO_PIN 18
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
long duration;
float distance;
// Trigger the sensor
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read the Echo pin
duration = pulseIn(ECHO_PIN, HIGH);
// Calculate distance
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
TRIG_PIN
and ECHO_PIN
for sensor operation. A short 10 µs pulse triggers the measurement, and the pulse duration is read on the Echo pin using the pulseIn()
function. The calculated distance is printed to the Serial Monitor every 500 ms.MicroPython Example
from machine import Pin, time_pulse_us
from time import sleep
# Define pins for Trigger and Echo
TRIG_PIN = 5
ECHO_PIN = 18
# Initialize Trigger and Echo pins
trig = Pin(TRIG_PIN, Pin.OUT)
echo = Pin(ECHO_PIN, Pin.IN)
def measure_distance():
# Send a 10 µs pulse to the Trigger pin
trig.low()
sleep(0.000002) # 2 µs
trig.high()
sleep(0.00001) # 10 µs
trig.low()
# Measure the duration of the Echo pulse
duration = time_pulse_us(echo, 1, 30000) # Timeout after 30 ms (no response)
# Calculate distance in cm (speed of sound = 343 m/s)
distance = (duration * 0.0343) / 2
return distance
print("JSN-SR04T Distance Sensor Example")
while True:
distance = measure_distance()
if distance > 0:
print("Distance: {:.2f} cm".format(distance))
else:
print("Out of range or no object detected.")
sleep(0.5)
time_pulse_us()
function measures the duration of the Echo pulse in microseconds, with a timeout of 30 ms to prevent infinite waiting. The distance is calculated using the formula (duration * 0.0343) / 2
, where 0.0343 cm/µs is the speed of sound. The script continuously measures and prints the distance to the console every 500 ms. If no response is detected, it prints an 'Out of range' message.Conclusion
The ESP32 JSN-SR04T Waterproof Ultrasonic Distance Sensor is a powerful distance sensor that offers excellent performance and reliability. With support for multiple development platforms including Arduino, ESP-IDF, ESPHome, PlatformIO, and MicroPython, it's a versatile choice for your IoT projects.
For optimal performance, ensure proper wiring and follow the recommended configuration for your chosen development platform.
Always verify power supply requirements and pin connections before powering up your project to avoid potential damage.