Chill-Division / M5Stack-ESPHome

Collection of information for getting M5Stack sensors / controllers working with ESPHome
Apache License 2.0
62 stars 9 forks source link

M5Stack Ultrasonic I/O sensor - does work with longer timeout #10

Open Cenedd opened 1 month ago

Cenedd commented 1 month ago

I fell into the same trap as you seem to have with the I/O version of this sensor. Thought it was faulty until I found your comments that only the i2c version works (with modified code). I couldn't work out why it should be different given their code was very similar:

    digitalWrite(_trig, LOW);
    delayMicroseconds(2);
    // Sets the trigPin on HIGH state for 10 micro seconds.
    // 将TrigPin设定为高电平状态,持续10微秒。

    digitalWrite(_trig, HIGH);
    delayMicroseconds(10);
    digitalWrite(_trig, LOW);

    // Reads the echoPin, returns the sound wave travel time in microseconds
    //读取echoPin,返回以微秒为单位的声波移动时间
    float duration = pulseIn(_echo, HIGH);

    return duration;

I managed to get a cheap oscilloscope on it and after much swearing (the software is awful) it looks like there's just a much longer delay (about 42ms) between the trigger pulse and the response pulse. Note the timescale is in milliseconds, not microseconds!

Ultrasonic-io-oscilloscope

Setting a longer timeout for the default ultrasonic component seems to compensate for this. 8m gives a limited max range or about 0.88m and the occasional complaint about the component blocking. 9 or 10m will allow longer range (at least 1.8m....I ran out of space) but will constantly complain about the component blocking.

sensor:
  - platform: ultrasonic
    trigger_pin: GPIO2
    echo_pin: GPIO1
    name: "Distance"
    id: fill_level
    update_interval: 1s
    timeout: 8m     #8m minimum
    unit_of_measurement: "m"

Hope that's of some use to someone. Gareth

Cenedd commented 1 month ago

Managed to get a trace of a 'standard' HC-SR04

Ultrasonic-hc-sr04-oscilloscope

Scale is the same but there are two differences. Firstly the echo is at (nearly) 5V on the HC-SR04 whereas it is 3.3V on the M5Stack. The second is the delay between the trigger and the return. These are on the same scales, by the way. The delay before the response on an HC-SR04 is about 0.5ms - around a tenth of the M5Stack part. Not sure if this could be (clutching at straws here) a delay introduced by sending it via a level shifter to drop it to 3.3V? Anyway, this is the fundamental difference between the two otherwise identical looking parts.

Gareth

ChillingSilence commented 1 month ago

Thanks @Cenedd ! I'll give that a whirl when I can grab my IO model next week. Interesting findings, and thanks for sharing the images alongside too 🙏