Ultrasonic aj-sr04m not working

Hi,

I gave the setup of my ultrasonic sensor another go after I found a new platform but I doesn’t seem to get it to work. My config file is

esphome:
  name: brunn-niva
  friendly_name: Brunn nivåvakt

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "2apmcN4EQkZxtoru3FvaEuNC1uQ2sdP5Ty9loScamnE="

ota:
  - platform: esphome
    password: "418d6aef251ab49e9141722247b2ef31"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Brunn-Niva Fallback Hotspot"
    password: "ovcbLTWLTUtd"

captive_portal:

uart:
  id: uart_1
  tx_pin: 3
  rx_pin: 1
  baud_rate: 9600

sensor:
  - platform: "jsn_sr04t"
    name: "Distance"
    model: "aj_sr04m"
    uart_id: uart_1

The sensor is set to mode 1 and I have connected the sensor trigger pin to D3 and the receiver pin to D1 but the only thing I get in my logs are

INFO ESPHome 2024.11.0
INFO Reading configuration /config/esphome/brunn-niva.yaml...
INFO Starting log output from 192.168.1.9 using esphome API
INFO Successfully connected to brunn-niva @ 192.168.1.9 in 0.016s
INFO Successful handshake with brunn-niva @ 192.168.1.9 in 2.898s
[23:52:34][I][app:100]: ESPHome version 2024.11.0 compiled on Nov 21 2024, 23:17:17
[23:52:34][C][wifi:600]: WiFi:
[23:52:34][C][wifi:428]:   Local MAC: 08:3A:8D:CF:A0:9A
[23:52:34][C][wifi:433]:   SSID: [redacted]
[23:52:34][C][wifi:436]:   IP Address: 192.168.1.9
[23:52:34][C][wifi:439]:   BSSID: [redacted]
[23:52:34][C][wifi:441]:   Hostname: 'brunn-niva'
[23:52:34][C][wifi:443]:   Signal strength: -61 dB ▂▄▆█
[23:52:34][C][wifi:447]:   Channel: 3
[23:52:34][C][wifi:448]:   Subnet: 255.255.255.0
[23:52:34][C][wifi:449]:   Gateway: 192.168.1.1
[23:52:34][C][wifi:450]:   DNS1: 192.168.1.1
[23:52:34][C][wifi:451]:   DNS2: 0.0.0.0
[23:52:34][C][logger:185]: Logger:
[23:52:34][C][logger:186]:   Level: DEBUG
[23:52:34][C][logger:188]:   Log Baud Rate: 115200
[23:52:34][C][logger:189]:   Hardware UART: UART0
[23:52:34][C][uart.arduino_esp8266:118]: UART Bus:
[23:52:34][C][uart.arduino_esp8266:119]:   TX Pin: GPIO3
[23:52:34][C][uart.arduino_esp8266:120]:   RX Pin: GPIO1
[23:52:34][C][uart.arduino_esp8266:122]:   RX Buffer Size: 256
[23:52:34][C][uart.arduino_esp8266:124]:   Baud Rate: 9600 baud
[23:52:34][C][uart.arduino_esp8266:125]:   Data Bits: 8
[23:52:34][C][uart.arduino_esp8266:126]:   Parity: NONE
[23:52:34][C][uart.arduino_esp8266:127]:   Stop bits: 1
[23:52:34][C][uart.arduino_esp8266:131]:   Using software serial
[23:52:34][C][jsn_sr04t.sensor:060]: JST_SR04T Sensor 'Distance'
[23:52:34][C][jsn_sr04t.sensor:060]:   State Class: 'measurement'
[23:52:34][C][jsn_sr04t.sensor:060]:   Unit of Measurement: 'm'
[23:52:34][C][jsn_sr04t.sensor:060]:   Accuracy Decimals: 3
[23:52:34][C][jsn_sr04t.sensor:060]:   Icon: 'mdi:arrow-expand-vertical'
[23:52:34][C][jsn_sr04t.sensor:066]:   sensor model: aj_sr04m
[23:52:34][C][jsn_sr04t.sensor:069]:   Update Interval: 60.0s
[23:52:34][C][captive_portal:089]: Captive Portal:
[23:52:34][C][mdns:116]: mDNS:
[23:52:34][C][mdns:117]:   Hostname: brunn-niva
[23:52:34][C][esphome.ota:073]: Over-The-Air updates:
[23:52:34][C][esphome.ota:074]:   Address: brunn-niva.local:8266
[23:52:34][C][esphome.ota:075]:   Version: 2
[23:52:34][C][esphome.ota:078]:   Password configured
[23:52:34][C][safe_mode:018]: Safe Mode:
[23:52:34][C][safe_mode:019]:   Boot considered successful after 60 seconds
[23:52:34][C][safe_mode:021]:   Invoke after 10 boot attempts
[23:52:34][C][safe_mode:022]:   Remain in safe mode for 300 seconds
[23:52:34][C][api:140]: API Server:
[23:52:34][C][api:141]:   Address: brunn-niva.local:6053
[23:52:34][C][api:143]:   Using noise encryption: YES

Any ideas?

I’ve tried same one. Managed to get it work in mode 4 (47k resistor required to solder) with home-made component.

Can get files from AJ-SR04M ultrasonic distance sensor in Mode 4 - Low Power Serial Mode by Masterz69 · Pull Request #6461 · esphome/esphome · GitHub
Put 4 files to esphome/components/aj_sr04m directory and use it so:

external_components:
  - source: components

uart:
  id: uart_bus
  tx_pin: TXPIN
  rx_pin: RXPIN
  baud_rate: 9600
  stop_bits: 1

sensor:
  - platform: aj_sr04m
    id: distance
    name: "Distance"
    update_interval: 2s

serial pins for Esp8266 are:
tx_pin: 1
rx_pin: 3

and they aren’t labeled D1(gpio5) and D3(gpio0) but TX and RX.
Also, is you board really Esp01 ?

Alright, thank you for the awesome feedback. I uppdated the configuration according to your suggestions and now the configuration file looks like this

esphome:
  name: brunn-niva
  friendly_name: Brunn nivåvakt

esp8266:
#  board: esp01_1m
  board: d1_mini
# Enable logging
logger:
  baud_rate: 0

# Enable Home Assistant API
api:
  encryption:
    key: "2apmcN4EQkZxtoru3FvaEuNC1uQ2sdP5Ty9loScamnE="

ota:
  - platform: esphome
    password: "418d6aef251ab49e9141722247b2ef31"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Brunn-Niva Fallback Hotspot"
    password: "ovcbLTWLTUtd"

captive_portal:

uart:
  id: uart_1
  tx_pin: 1
  rx_pin: 3
  baud_rate: 9600

sensor:
  - platform: "jsn_sr04t"
    name: "Distance"
    model: "aj_sr04m"
    uart_id: uart_1
    

and then when I start it up I get the following in the logs.

INFO ESPHome 2024.11.1
INFO Reading configuration /config/esphome/brunn-niva.yaml...
INFO Starting log output from 192.168.1.9 using esphome API
INFO Successfully connected to brunn-niva @ 192.168.1.9 in 0.004s
INFO Successful handshake with brunn-niva @ 192.168.1.9 in 2.487s
[10:36:26][I][app:100]: ESPHome version 2024.11.1 compiled on Nov 22 2024, 17:21:58
[10:36:26][C][wifi:600]: WiFi:
[10:36:26][C][wifi:428]:   Local MAC: 08:3A:8D:CF:A0:9A
[10:36:26][C][wifi:433]:   SSID: [redacted]
[10:36:26][C][wifi:436]:   IP Address: 192.168.1.9
[10:36:26][C][wifi:439]:   BSSID: [redacted]
[10:36:26][C][wifi:441]:   Hostname: 'brunn-niva'
[10:36:26][C][wifi:443]:   Signal strength: -69 dB ▂▄▆█
[10:36:26][C][wifi:447]:   Channel: 3
[10:36:26][C][wifi:448]:   Subnet: 255.255.255.0
[10:36:26][C][wifi:449]:   Gateway: 192.168.1.1
[10:36:26][C][wifi:450]:   DNS1: 192.168.1.1
[10:36:26][C][wifi:451]:   DNS2: 0.0.0.0
[10:36:26][C][logger:185]: Logger:
[10:36:26][C][logger:186]:   Level: DEBUG
[10:36:26][C][logger:188]:   Log Baud Rate: 0
[10:36:26][C][logger:189]:   Hardware UART: UART0
[10:36:26][C][uart.arduino_esp8266:118]: UART Bus:
[10:36:26][C][uart.arduino_esp8266:119]:   TX Pin: GPIO1
[10:36:26][C][uart.arduino_esp8266:120]:   RX Pin: GPIO3
[10:36:26][C][uart.arduino_esp8266:122]:   RX Buffer Size: 256
[10:36:26][C][uart.arduino_esp8266:124]:   Baud Rate: 9600 baud
[10:36:26][C][uart.arduino_esp8266:125]:   Data Bits: 8
[10:36:26][C][uart.arduino_esp8266:126]:   Parity: NONE
[10:36:26][C][uart.arduino_esp8266:127]:   Stop bits: 1
[10:36:26][C][uart.arduino_esp8266:129]:   Using hardware serial interface.
[10:36:26][C][jsn_sr04t.sensor:060]: JST_SR04T Sensor 'Distance'
[10:36:26][C][jsn_sr04t.sensor:060]:   State Class: 'measurement'
[10:36:26][C][jsn_sr04t.sensor:060]:   Unit of Measurement: 'm'
[10:36:26][C][jsn_sr04t.sensor:060]:   Accuracy Decimals: 3
[10:36:26][C][jsn_sr04t.sensor:060]:   Icon: 'mdi:arrow-expand-vertical'
[10:36:26][C][jsn_sr04t.sensor:066]:   sensor model: aj_sr04m
[10:36:26][C][jsn_sr04t.sensor:069]:   Update Interval: 60.0s
[10:36:26][C][captive_portal:089]: Captive Portal:
[10:36:26][C][mdns:116]: mDNS:
[10:36:26][C][mdns:117]:   Hostname: brunn-niva
[10:36:26][C][esphome.ota:073]: Over-The-Air updates:
[10:36:26][C][esphome.ota:074]:   Address: brunn-niva.local:8266
[10:36:26][C][esphome.ota:075]:   Version: 2
[10:36:26][C][esphome.ota:078]:   Password configured
[10:36:26][C][safe_mode:018]: Safe Mode:
[10:36:26][C][safe_mode:019]:   Boot considered successful after 60 seconds
[10:36:26][C][safe_mode:021]:   Invoke after 10 boot attempts
[10:36:26][C][safe_mode:022]:   Remain in safe mode for 300 seconds
[10:36:26][C][api:140]: API Server:
[10:36:26][C][api:141]:   Address: brunn-niva.local:6053
[10:36:26][C][api:143]:   Using noise encryption: YES
[10:36:28][I][safe_mode:041]: Boot seems successful; resetting boot loop counter
[10:36:55][W][jsn_sr04t.sensor:054]: checksum failed: d2 != d1
[10:36:56][W][jsn_sr04t.sensor:054]: checksum failed: d2 != d1
[10:36:56][W][jsn_sr04t.sensor:054]: checksum failed: d6 != d5
[10:36:56][W][jsn_sr04t.sensor:054]: checksum failed: 97 != 96
[10:36:56][W][jsn_sr04t.sensor:054]: checksum failed: d2 != d1
[10:36:56][W][jsn_sr04t.sensor:054]: checksum failed: 97 != 96
[10:36:56][W][jsn_sr04t.sensor:054]: checksum failed: 97 != 96
[10:36:56][W][jsn_sr04t.sensor:054]: checksum failed: 97 != 96
[10:36:56][W][jsn_sr04t.sensor:054]: checksum failed: 97 != 96
[10:36:56][W][jsn_sr04t.sensor:054]: checksum failed: 97 != 96

It feels like I am close but not sure what I am missing…

Some D1 boards have problems with hardware serial pins because of wrong resistors between rx and usb-converter.
Try with software serial, use pins 4 and 5 for rx /tx

same error :frowning:

Could it be that I am missing something in my UART configuration? I found this for this sensor but not sure what to change in the configuration

For that baudrate both hardware serial and software serial should be fine. And default 8N1 parameters should be ok as well.
I don’t know/have the sensor you have, but the log displays checksum that is off by one bit.

Add debug to your uart component to see what the sensor output really is.

Did you try with mode 2 (polling)?