Can't get JSN-SR04T distance sensor to work

Hi,

I don’t seem to get a JSN-SR04T distance sensor to work. I have tried with both a ESP8266 board and a atom lite esp32 and none of them work. I have also bought an extra JSN-SR04T sensor if the first one was faulty but I get the same result.

> [01:06:35][D][ultrasonic.sensor:036]: 'garden_watertank_distance' - Distance measurement timed out!
> [01:06:35][D][sensor:093]: 'garden_watertank_distance': Sending state nan m with 2 decimals of accuracy

If I connect a HC-SR04 sensor it works perfectly

> [00:57:40][D][ultrasonic.sensor:040]: 'garden_watertank_distance' - Got distance: 0.057 m
> [00:57:40][D][sensor:093]: 'garden_watertank_distance': Sending state 0.05677 m with 2 decimals of accuracy

My config file for the esp8266 board is

> > esphome:
> >   name: esphome-web-cfa09a
> >   friendly_name: Vattenniva
> > 
> > esp8266:
> >   board: esp01_1m
> > 
> > # Enable logging
> > logger:
> > 
> > # Enable Home Assistant API
> > api:
> >   encryption:
> >     key: "8pzfT6DRfSi5c/BE/dIQIZD9UFd78PnMOL+g1u9t33A="
> > 
> > ota:
> > 
> > 
> > wifi:
> >   ssid: !secret wifi_ssid
> >   password: !secret wifi_password
> > 
> >   # Enable fallback hotspot (captive portal) in case wifi connection fails
> >   ap:
> >     ssid: "Esphome-Web-Cfa09A"
> >     password: "RSToQSbvHwRY"
> > 
> > captive_portal:
> > 
> > sensor:
> > 
> >  # The actual distance sensor
> >  - platform: ultrasonic
> >    trigger_pin: 3
> >    echo_pin: 1
> >    name: garden_watertank_distance
> >    update_interval: 5s
> >    pulse_time: 20us

Any suggestions on a resolution will be appreciated. I have also tried a pulse time of 50us.

They should be functionally equivalent. Are you sure you’re wiring it correctly?

99,9% sure. I can also here a small click sound from the speaker if I listen carefully.

Two things might be worth checking:

  1. When I ordered my JSN-SR04T sensors from Amazon, I received a very similar looking AJ-SR04M, it said JSN-SR04T on the box but in the box was an AJ-SR04M. Couldn’t get it to work, asked Amazon for a replacement and received the same incorrectly labled boxes again - had to order from a different vendor.

  2. I couldn’t get the code to work just using the pin numbers, it worked fine on my old sensor (forgot what it’s called) but I had to change it to this

sensor:
 - platform: ultrasonic
   trigger_pin: GPIO13
   echo_pin: GPIO12

I struggled also with this sensor. I discovered that I also purchased a AJ-SR04M sensor and that this device ISN’T the same as the JSN-SR04T which is documented in ESPHOME.

When reading the documentation, the modes for the AJ are slightly different. You can read all about this @ probots tutorials : How to Communicate Waterproof Ultrasonic Sensor AJ-SR04M/JSN-SR04T with Arduino/ESP32 – Probots Blog.

In the ESPHOME documentation they give 2 modes as an option. I succeeded in making mode 1 work with my Home Assistant. But instead using a 47K resistor (ESPHOME documentation), I used 120k resistor at R19 as documented by probots for the automatic mode.

I think the mode 2 option isn’t possible with the AJ-SR04M as the trigger signal will be sent as a 0x05 (ESPHOME documentation) while the sensor would expect a 0x01 signal before returning the distance.
This is a real bummer because this mode would allow reducing power consumption.
Therefore, support for this slightly different sensor would be great! :slight_smile:

I really wish documentation on ESPhome and thru retailers were better for this device.

I also bought this device thinking it was a JSN-SR04T. Is there any way to use it in “mode 1”?

Hi,

do you have the possibility to send me the exact yaml you are using for the sensor. I have soldered on a 120k resistor to put it into mode 1 but still can’t get it to work.

Thank you in advance!

i used jsn-sr04m, as it is available when ever you get jsn-sr0t, and i used it with d1 mini and esp32 wroom too, here is the configuration with d1 mini

switch:
  - platform: restart
    name: "water_level_sensor reboot"
text_sensor:
  - platform: wifi_info
    ip_address:
      name: sensor IP Address
    ssid:
      name: ESP Connected SSID
  - platform: version
    name: "ESPHome Version"
    entity_category: diagnostic
uart:
  tx_pin: GPIO5
  rx_pin: GPIO4
  baud_rate: 9600

sensor:
  - platform: "jsn_sr04t"
    name: "water-depth"
    id: water_level
    update_interval: 10s
    unit_of_measurement: "ft"  # Set the unit to feet
    accuracy_decimals: 2       # Ensure the output has 2 decimal places
    filters:
      - lambda: |-
          if (x < 0.2 || x > 4.50) {
            return NAN;  // Return NaN for out-of-range values
          }
          float value_in_feet = x * 3.28084;  // Convert meters to feet
          return int(value_in_feet * 100 + 0.5) / 100.0;  // Round to 2 decimal places

  - platform: template
    name: "%"
    # if value in metters ( as if sensor is placed above your tank full level 0.27 meter, (as minimum range is 0.2 meter, so it should be atleast 0.2 m above))
    # and if full tank depth when it should be considered empty, is 1.22 meter, adjust height as you need it.
    # if converted in feet as above, then % should be calculated from feets 
    unit_of_measurement: "%"
    lambda: !lambda |- 
          if ((id(water_level).state) <= 0.886){ 
            return 100;
          }
          else if ((id(water_level).state) >= 4.0){ 
            return 0;
          } else {
            return (100 - ((id(water_level).state - 0.866) / 3.114) * 100);
          }
    update_interval: 10s

  - platform: wifi_signal
    name: "WiFi Signal Sensor"
    update_interval: 60s

  - platform: uptime
    name: uptime
    filters:
      - lambda: return x / 3600;
    unit_of_measurement: "h"
    update_interval: 60s

   
  - platform: template
    name: "Free Heap Memory"
    lambda: |-
      return ESP.getFreeHeap() / 1024.0;  // Convert bytes to KB
    unit_of_measurement: "KB"
    update_interval: 5h

  - platform: template
    name: "Chip ID"
    lambda: |-
      return (float)ESP.getChipId();
    update_interval: 12h
    accuracy_decimals: 0
    entity_category: diagnostic
3 Likes

HI! I think I have the same problem, how can I discover which type of sensor I really received from Amazon?

Is there any way to use the sensor with a NodeMCU V3? Thanks!

Why not? It doesn’t ask any specific MCU…

Because when I connect the JSN to the pin D5 and D6 (I haven’t added any resistor) I cannot get any distance measure. I have also discovered that I have a AJ-SR04M board. Do I must add the resistor or is there any way to do it without them?

You need to control what you have on R19 on your module.