I received the same sensor (AJ-SR04M) and I can’t get it to work. It shows no distance readings. I have been following this thread but to no avail. Can someone enlighten me where to put the AJ_SR04M_Sensor.h file. I was hoping to use it withou any resistor (to work just like the HC-SR04). Are there any changes to the code?
Hello - I found this post interesting - I’m starting to connect AJ-SR04M to my HA via esphome board : esp32 or wemos D1 mini
Have You considered voltages ?
I assume that You get 5v for AJ-SR04M from the board (esp32 or D1mini) but as far as I know TX/RX pins on these boards are working with 3.3v.
So If You connect TX/RX from SR04M You get 5v signal … and connecting it to 3.3v pins can cause damage or malfunction.
There is an instruction how to connect and there is a film presenting it is working but this example is based on Arduino UNO - there are 5v pin !
instructions
film - arduino UNO working with AJ-SR04M
So … did You noticed this “voltage question” ?
Hello everyone,
I am trying to reproduce this configuration on my Home Assistant via ESPHOME.
However, I have errors, and I’m more than new to Arduino.
My setup is a “Wemos D1 mini” with “JSN-SR04T” (AJ-SR04M) and 47KΩ residence on R19
In /homeassistant/esphome/
Here is my file AJ_SR04M_Sensor.h
#include "esphome.h"
class AJ_SR04M_Sensor : public PollingComponent, public UARTDevice, public Sensor {
public:
AJ_SR04M_Sensor(UARTComponent *parent) : PollingComponent(5000), UARTDevice(parent) {}
// AJ_SR04M_Sensor format:
// Trigger: 0x00
// Response: Byte1 Byte2 Byte3 Byte4 Byte5
// Start Byte=FF MSB LSB Checksum (LSB+MSB) 00
void update() override {
char frame[5];
int pos = 0;
float value = 0.0;
write(0x01);
while (available()) {
frame[pos] = read();
pos++;
if(pos==5) {
if ((frame[0] == 0xFF) && (frame[4] == 0x00) && ((frame[1]+frame[2])==frame[3])) {
value = ((frame[1]<<8) + frame[2]) / 10.0;
publish_state(value);
}
break;
}
}
}
};
and my esp-cuve.yaml file
esphome:
name: esp-cuve
friendly_name: esp-cuve
includes:
- AJ_SR04M_Sensor.h
uart:
id: uart_bus
tx_pin: D1
rx_pin: D2
baud_rate: 9600
stop_bits: 1
rx_buffer_size: 4
debug:
esp8266:
board: d1_mini
# Enable logging
logger:
baud_rate: 0
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
captive_portal:
web_server:
port: 80
# Time: https://esphome.io/components/time.html
time:
- platform: homeassistant
timezone: "Europe/Paris"
id: homeassistant_time
# Status Binary Sensor: https://esphome.io/components/binary_sensor/status.html
binary_sensor:
- platform: status
name: "Status"
# Restart Button: https://esphome.io/components/button/restart.html
button:
- platform: restart
name: "Restart"
sensor:
- platform: custom
lambda: |-
auto my_sensor = new AJ_SR04M_Sensor(id(uart_bus));
App.register_component(my_sensor);
return {my_sensor};
sensors:
unit_of_measurement: cm
accuracy_decimals: 1
name: "Distance"
No error on compilation but here are the logs
[20:21:22][D][uart_debug:114]: >>> 01
[20:21:27][D][uart_debug:114]: >>> 01
[20:21:32][D][uart_debug:114]: >>> 01
[20:21:37][D][uart_debug:114]: >>> 01
[20:21:42][D][uart_debug:114]: >>> 01
Do you have any idea where the problem could come from?
Thank you again for your help
Just curious, did you get it working? I’m in the same situation.
Did you figure this one out? I’m very new to esphome so I’m really just trying anything an everything, but I’ve just got it working by removing the checksum comparison.
Wish I could say why that would be causing errors for me, but its the only way I’m getting any readings, and have been consistent and very accurate.
I’m having issues getting this working - see