Jsn sr04 ultrazonic sensor - time out - no stable measure

I’m playing with this sensor for a few weeks now, but I can’t get it to work properly. I’ve checked the connections and tried various configurations. The best I could achieve is some measurements interspersed with timeouts.

The configuration is in mode 1, powered at 5 volts with an ESP8266 OLED. I’m attaching :slight_smile::

Photo of the schematic
Logs
Configuration
Sensor datasheet in mode

immagine

CODE:

  - platform: ultrasonic
    trigger_pin: GPIO15
    echo_pin: GPIO13
    name: "Livello Tanica"
    id: livello_tanica
 #   pulse_time: 15 us
 #   update_interval: 5s
    unit_of_measurement: "cm"
    accuracy_decimals: 1
    timeout: 30m  # Aumenta il timeout a 20 metri
    filters:
      - filter_out: nan

LOG:


[12:04:45][D][ultrasonic.sensor:040]: 'Livello Tanica' - Got distance: 8.153 m 
[12:04:45][D][sensor:093]: 'Livello Tanica': Sending state 8.15294 cm with 1 decimals of accuracy

…..


[12:05:45][D][ultrasonic.sensor:036]: 'Livello Tanica' - Distance measurement timed out!
[12:05:45][W][component:237]: Component ultrasonic.sensor took a long time for an operation (179 ms).

NOTE:
https://esphome.io/components/sensor/jsn_sr04t.html

write :
Configure the JSN-SR04T for mode 1:
V1.0 and V2.0: Add a 47k resistor to pad R27.

Configure the JSN-SR04T for mode 2:
V1.0 and V2.0: Add a 120k resistor to pad R27.

BUT is no correct versus DATASHEET:



i want add :

on_value:
  then:
    - lambda: |-
        // Conversione manuale da metri a centimetri
        id(livello_tanica).publish_state(x * 100);

incorrect cm VS m as see logger

MODE 1 working datasheet :slight_smile:

You link a component but you are using another

Also, don’t use GPIO15 if you have others available, it has pulldown resistor and it’s strapping pin.
And finally, if you power that sensor at 5V, you need level shifters for esp trigger and echo pins.

Do u suggest that i change supply 5volt to 3.3volt and change gpio15 ?

I try

I suggest to use level shifter, but if you don’t have, try at 3.3V.
Generally, avoid using strapping pins.

I dont have shifter i change supply to 3.3v and try green pin:

immagine

I try to 3.3 volt move gpio15 to gpio5 :slight_smile:

[12:26:09][D][dht:048]: Got Temperature=28.0°C Humidity=31.0%
[12:26:09][D][sensor:093]: 'Temperatura': Sending state 28.00000 °C with 1 decimals of accuracy
[12:26:09][D][sensor:093]: 'Umidità': Sending state 31.00000 % with 0 decimals of accuracy
[12:26:11][D][ultrasonic.sensor:040]: 'Livello Tanica' - Got distance: 3.772 m
[12:26:11][D][sensor:093]: 'Livello Tanica': Sending state 3.77231 m with 2 decimals of accuracy
[12:26:12][D][hx711:031]: 'Peso GPL': Got value 987

real distance 40cm

I solve one part , share for hel someone :slight_smile:

  1. change mode 1 with 47kohm serial mode
  2. chage pin on uart0 swap uart logger on uart1
captive_portal:
uart: #per sensore ultrasuoni
  tx_pin: GPIO1
  rx_pin: GPIO3
  baud_rate: 9600

i2c:
  sda: D6
  scl: D5
  scan: true


display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    address: 0x3C
    update_interval: 5s  # Riduce la frequenza di aggiornamento
    lambda: |-
      // it.fill(0);  // Pulisce il display prima di ogni aggiornamento
      it.print(0, 0, id(font1), "GPL Zanzare");
      it.printf(0, 12, id(font1), "Temp: %.1f C", id(temperatura).state);
      it.printf(0, 24, id(font1), "Umidita: %.1f %%", id(umidita).state);
      it.printf(0, 36, id(font1), "Livello: %.1f cm", id(livello_tanica).state);
      it.printf(0, 48, id(font1), "Peso GPL: %.2f kg", id(peso_gpl).state);




font:
  - file: 'fonts/arial-black.ttf'
    id: font1
    size: 10

sensor:
  - platform: dht
    pin: GPIO16
    temperature:
      name: "Temperatura"
      id: temperatura
    humidity:
      name: "Umidità"
      id: umidita
    update_interval: 10s
    model: DHT11

#  - platform: ultrasonic
#    trigger_pin: GPIO15
#    echo_pin: GPIO13
#    name: "Livello Tanica"
#    id: livello_tanica
#    pulse_time: 10us  # Impulso più breve per migliorare la precisione
#    update_interval: 5s  # Aumenta l'intervallo di aggiornamento per ridurre l'impatto sulle prestazioni
#    unit_of_measurement: "m"  # Assicurati che l'unità sia correttamente impostata su "cm"
#    accuracy_decimals: 2
#    timeout: 1m  # Riduci il timeout a 4 metri
#    filters:
#      - filter_out: nan  # Filtra le letture non valide
#    on_value: # anche se unita è messa in cm il sensore mi da in metri quindi aggiusto con questo codice
#      then:
#        - lambda: |-
#            id(livello_tanica).publish_state(x * 100);

  - platform: "jsn_sr04t"
    name: "Livello Tanica"
    id: livello_tanica
    update_interval: 1s
    unit_of_measurement: "cm"
    accuracy_decimals: 2


  - platform: hx711
    name: "Peso GPL"
    id: peso_gpl
    dout_pin: D2
    clk_pin: D1
#    gain: 128
    update_interval: 5s
    filters:
      - calibrate_linear:  # Aggiusta il moltiplicatore in base alla calibrazione
          - -212990 -> 0  # massa nota 0kg mi da xxx
          - -166431 -> 2  # massa nota 2kg mi da xxx


#binary_sensor:
#  - platform: gpio
#    pin:
#      number: GPIO9
#      mode: INPUT_PULLUP
#    name: "Pioggia Rilevata"
#    device_class: moisture

switch:
  - platform: gpio
    pin: GPIO0
    name: "Pompa Antizanzare"
    id: relay_pompa
#  - platform: gpio
#    pin: GPIO11
#    name: "Scorta"
#    id: relay_scorta

measure true:


accuracy
[16:15:54][D][sensor:093]: 'Livello Tanica': Sending state 0.32700 cm with 2 decimals of accuracy
[16:15:54][D][sensor:093]: 'Livello Tanica': Sending state 0.32700 cm with 2 decimals of accuracy
[16:15:55][D][sensor:093]: 'Livello Tanica': Sending state 0.31200 cm with 2 decimals of accuracy
[16:15:55][D][sensor:093]: 'Livello Tanica': Sending state 0.32700 cm with 2 decimals of accuracy
[16:15:55][D][sensor:093]: 'Livello Tanica': Sending state 0.32700 cm with 2 decimals of accuracy
[16:15:55][D][sensor:093]: 'Livello Tanica': Sending state 0.32700 cm with 2 decimals of accuracy
[16:15:55][D][sensor:093]: 'Livello Tanica': Sending state 0.32700 cm with 2 decimals of accuracy
[16:15:55][D][sensor:093]: 'Livello Tanica': Sending state 0.32700 cm with 2 decimals of accuracy
[16:15:55][D][sensor:093]: 'Livello Tanica': Sending state 0.32700 cm with 2 decimals of accuracy
[16:15:55][D][sensor:093]: 'Livello Tanica': Sending state 0.32700 cm with 2 decimals of accuracy
[16:15:55][D][sensor:093]: 'Livello Tanica': Sending state 0.31200 cm with 2 decimals of accuracy
[16:15:56][D][hx711:031]: 'Peso GPL': Got value 969
[16:15:56][D][sensor:093]: 'Peso GPL': Sending state 9.19088  with 0 decimals of accuracy
[16:15:56][D][sensor:093]: 'Livello Tanica': Sending state 0.31200 cm with 2 decimals of accuracy
[16:15:56][D][sensor:093]: 'Livello Tanica': Sending state 0.32700 cm with 2 decimals of accuracy
[16:15:56][D][sensor:093]: 'Livello Tanica': Sending state 0.31200 cm with 2 decimals of accuracy
[16:15:56][D][sensor:093]: 'Livello Tanica': Sending state 0.32700 cm with 2 decimals of accuracy
[16:15:56][D][sensor:093]: 'Livello Tanica': Sending state 0.32700 cm with 2 decimals of accuracy
[16:15:56][D][sensor:093]: 'Livello Tanica': Sending state 0.32700 cm with 2 decimals of accuracy
[16:15:56][D][sensor:093]: 'Livello Tanica': Sending state 0.32700 cm with 2 decimals of accuracy
[16:15:56][D][sensor:093]: 'Livello Tanica': Sending state 0.32700 cm with 2 decimals of accuracy
[16:15:56][D][sensor:093]: 'Livello Tanica': Sending state 0.32700 cm with 2 decimals of accuracy
[16:15:57][D][sensor:093]: 'Livello Tanica': Sending state 0.32700 cm with 2 decimals of accuracy
[16:15:57][W][component:237]: Component display took a long time for an operation (317 ms).
[16:15:57][W][component:238]: Components should block for at most 30 ms.
[16:15:57][D][sensor:093]: 'Livello Tanica': Sending state 0.32700 cm with 2 decimals of accuracy
[16:15:57][D][sensor:093]: 'Livello Tanica': Sending state 0.32700 cm with 2 decimals of accuracy
[16:15:57][D][sensor:093]: 'Livello Tanica': Sending state 0.32700 cm with 2 decimals of accuracy
[16:15:57][D][sensor:093]: 'Livello Tanica': Sending state 0.32700 cm with 2 decimals of accuracy



NOW I HAVE OTHER ISSUE, In the tank 10 lt i have this error, i think wave go arround wall of tank :

[16:14:48][W][jsn_sr04t.sensor:051]: Invalid data read from sensor: FF.00.DF.DE
[16:14:49][W][jsn_sr04t.sensor:051]: Invalid data read from sensor: FF.00.DF.DE
[16:14:49][W][jsn_sr04t.sensor:051]: Invalid data read from sensor: FF.00.DF.DE
[16:14:49][W][jsn_sr04t.sensor:051]: Invalid data read from sensor: FF.00.DF.DE
[16:14:49][W][jsn_sr04t.sensor:051]: Invalid data read from sensor: FF.00.DF.DE
[16:14:49][W][jsn_sr04t.sensor:051]: Invalid data read from sensor: FF.00.DF.DE
[16:14:49][W][jsn_sr04t.sensor:051]: Invalid data read from sensor: FF.00.DF.DE
[16:14:49][W][jsn_sr04t.sensor:051]: Invalid data read from sensor: FF.00.DF.DE
[16:14:49][W][jsn_sr04t.sensor:051]: Invalid data read from sensor: FF.00.DF.DE
[16:14:49][W][jsn_sr04t.sensor:051]: Invalid data read from sensor: FF.00.DF.DE
[16:14:50][W][jsn_sr04t.sensor:051]: Invalid data read from sensor: FF.00.DF.DE




[16:14:48][W][jsn_sr04t.sensor:051]: Invalid data read from sensor: FF.00.DF.DE
[16:14:49][W][jsn_sr04t.sensor:051]: Invalid data read from sensor: FF.00.DF.DE
[16:14:49][W][jsn_sr04t.sensor:051]: Invalid data read from sensor: FF.00.DF.DE
[16:14:49][W][jsn_sr04t.sensor:051]: Invalid data read from sensor: FF.00.DF.DE
[16:14:49][W][jsn_sr04t.sensor:051]: Invalid data read from sensor: FF.00.DF.DE
[16:14:49][W][jsn_sr04t.sensor:051]: Invalid data read from sensor: FF.00.DF.DE
[16:14:49][W][jsn_sr04t.sensor:051]: Invalid data read from sensor: FF.00.DF.DE
[16:14:49][W][jsn_sr04t.sensor:051]: Invalid data read from sensor: FF.00.DF.DE
[16:14:49][W][jsn_sr04t.sensor:051]: Invalid data read from sensor: FF.00.DF.DE
[16:14:49][W][jsn_sr04t.sensor:051]: Invalid data read from sensor: FF.00.DF.DE
[16:14:50][W][jsn_sr04t.sensor:051]: Invalid data read from sensor: FF.00.DF.DE

I made this :

now is working, sometimes sensor finish to work i change 3.3volt to 5 volt is working 1 day

I write some detail for full project :

Complete Summary of the Mosquito Misting Project with Temperature, Humidity Logic, and Alternative Natural Recipes

This project involves creating an automated misting system for mosquito control, combining monthly chemical treatments with daily natural repellent solutions. The system uses a 12V pump, 12 misting nozzles, and is optimized to maximize effectiveness by considering environmental temperature and humidity variables.

Main Components

  1. 12V Diaphragm Pump (5 L/min):
  • Capacity of 5 liters per minute, controlled via a relay connected to an ESP8266 for automatic activation. Ensures adequate pressure for 12 nozzles along a 30-meter pipe.
  1. Brass Misting Nozzles (0.3 mm):
  • 12 nozzles distributed along the pipe to ensure uniform misting of the solution throughout the area.
  1. ESP8266 with ESPHome:
  • Manages the automated scheduling of the system, monitors sensors for tank level, temperature, and humidity, and controls the pump activation.
  1. Integrated Sensors:
  • Ultrasonic Level Sensor (JSN-SR04T): Monitors the liquid level in the tank.
  • DHT11 Sensor: Monitors ambient temperature and humidity.
  • Weight Sensor (HX711): Checks the weight of the LPG cylinder or tank.
  1. Mosquito Treatment Solutions:
  • Monthly Treatment with DEADYNA (Pyrethroid Insecticide).
  • Daily Treatment with Natural Repellent Solution.

Preparation of the Solutions

  1. Monthly Treatment with DEADYNA
  • Recommended Dilution: 10 ml of DEADYNA per liter of water.
  • Solution Preparation:
    • Measure 100 ml of DEADYNA.
    • Mix with 10 liters of water in a tank.
    • Ensure thorough mixing for a uniform solution.
  • Application Quantity: The prepared solution (10 liters) is sufficient to cover an area of 150 m² (10 liters x 15 m² per liter).
  1. Daily Treatment with Natural Solution
  • Ingredients for 10 Liters:
    • 10 drops of bergamot essential oil.
    • 120 ml (1/2 cup) of apple cider vinegar.
    • 10 liters of water.
  • Preparation:
    • Mix the ingredients in a container, ensuring they are well blended.
    • Pour the mixture into the tank of the misting system.

Alternative Natural Recipes

  1. Solution Based on Citronella Essential Oil and Apple Cider Vinegar
  • Ingredients:
    • 10 drops of citronella essential oil.
    • 120 ml (1/2 cup) of apple cider vinegar.
    • 10 liters of water.
  • Preparation:
    • Mix citronella oil with apple cider vinegar.
    • Add 10 liters of water and mix well.
    • Pour the solution into the misting system’s tank.
  • Benefits: Citronella is a very effective natural mosquito repellent.
  1. Solution Based on Eucalyptus Oil and Neem Oil
  • Ingredients:
    • 10 drops of eucalyptus essential oil.
    • 5 ml (1 teaspoon) of neem oil.
    • 120 ml (1/2 cup) of apple cider vinegar.
    • 10 liters of water.
  • Preparation:
    • Mix the essential oils with apple cider vinegar.
    • Add 10 liters of water and mix well.
    • Pour the solution into the misting system’s tank.
  • Benefits: Eucalyptus and neem offer a dual repellent and insecticidal action.
  1. Solution Based on Lavender Oil and Peppermint Oil
  • Ingredients:
    • 10 drops of lavender essential oil.
    • 10 drops of peppermint essential oil.
    • 120 ml (1/2 cup) of apple cider vinegar.
    • 10 liters of water.
  • Preparation:
    • Combine the essential oils with apple cider vinegar.
    • Add 10 liters of water and mix well.
    • Pour the solution into the misting system’s tank.
  • Benefits: Pleasant for the human environment but repellent to mosquitoes.

Usage Calendars

  1. Monthly Treatment Calendar with DEADYNA
  • Frequency: Once a month.
  • Recommended Time: Night, around 11 PM.
  • Misting Duration: 1-2 minutes.
    • With 10 liters of solution and a flow rate of 5 liters/min, mist for up to 2 minutes to cover the 30 m² area.
  • Precautions: Ensure the area is clear of people and animals and follow the manufacturer’s safety instructions regarding re-entry time.
  1. Daily Treatment Calendar with Natural Solution
  • Frequency: Twice a day.
  • Recommended Times:
    • Early Morning: Mist at 6-7 AM for 2 minutes.
    • Late Afternoon/Evening: Mist at 6-7 PM for 2 minutes.
  • Misting Duration: Each misting cycle should last 2 minutes, using about 10 liters of solution to cover the area.

Control Logic Based on Temperature and Humidity

  1. Temperature:
  • Activate Misting Between 20°C and 30°C: Mosquitoes are most active within this temperature range. Avoid misting below 15°C or above 35°C, when mosquitoes are less active or absent.
  1. Humidity:
  • Keep Humidity at a Moderate Range: If humidity is too low (below 40%), evaporation is rapid, and misting effectiveness decreases. If humidity is too high (above 70-80%), the dispersion of mist particles is less effective.
  1. Combined Automation:
  • Set up automations in Home Assistant to activate misting only when the temperature is between 20°C and 30°C and humidity is between 40% and 70%, during daytime hours (6 AM - 9 PM).

Additional Tips

  • Maintenance: Regularly clean the nozzles and tank to avoid clogging.
  • Safety: Wear protective gear when preparing and using DEADYNA.
  • Monitoring: Periodically assess the effectiveness of the strategies and adjust misting parameters based on environmental conditions and mosquito density.

Conclusion

This summary integrates both chemical and natural misting strategies, optimized for effectiveness based on temperature and humidity. The use of different natural recipes offers alternatives for mosquito control, while automation based on environmental conditions ensures a sustainable and targeted approach to pest control.

1 Like
captive_portal:
uart: #per sensore ultrasuoni
  tx_pin: GPIO1
  rx_pin: GPIO3
  baud_rate: 9600

i2c:
  sda: D6
  scl: D5
  scan: true


display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    address: 0x3C
    update_interval: 5s  # Riduce la frequenza di aggiornamento
    lambda: |-
      // it.fill(0);  // Pulisce il display prima di ogni aggiornamento
      it.print(0, 0, id(font1), "GPL Zanzare");
      it.printf(0, 12, id(font1), "Temp: %.1f C", id(temperatura).state);
      it.printf(0, 24, id(font1), "Umidita: %.1f %%", id(umidita).state);
      it.printf(0, 36, id(font1), "Livello: %.1f m", id(livello_tanica).state);
      it.printf(0, 48, id(font1), "Peso GPL: %.2f kg", id(peso_gpl).state);




font:
  - file: 'fonts/arial-black.ttf'
    id: font1
    size: 10

sensor:
  - platform: dht
    pin: GPIO16
    temperature:
      name: "Temperatura"
      id: temperatura
    humidity:
      name: "Umidità"
      id: umidita
    update_interval: 10s
    model: DHT11

#  - platform: ultrasonic
#    trigger_pin: GPIO15
#    echo_pin: GPIO13
#    name: "Livello Tanica"
#    id: livello_tanica
#    pulse_time: 10us  # Impulso più breve per migliorare la precisione
#    update_interval: 5s  # Aumenta l'intervallo di aggiornamento per ridurre l'impatto sulle prestazioni
#    unit_of_measurement: "m"  # Assicurati che l'unità sia correttamente impostata su "cm"
#    accuracy_decimals: 2
#    timeout: 1m  # Riduci il timeout a 4 metri
#    filters:
#      - filter_out: nan  # Filtra le letture non valide
#    on_value: # anche se unita è messa in cm il sensore mi da in metri quindi aggiusto con questo codice
#      then:
#        - lambda: |-
#            id(livello_tanica).publish_state(x * 100);

  - platform: "jsn_sr04t"
    name: "Livello Tanica"
    id: livello_tanica
    update_interval: 1s
    unit_of_measurement: "m"
    accuracy_decimals: 2
    filters:
      - filter_out: nan  # Filtra le letture non valide
      - sliding_window_moving_average:
          window_size: 10
          send_every: 10
      - lambda: |-
          float sum = 0;
          int count = 0;
          for (int i = 0; i < 10; i++) {
            if (x < 0.28 || x > 0.70) {  // Ignora le letture fuori intervallo (tra 0.28 m e 0.70 m)
              continue;  // Ignora i valori fuori media
            }
            sum += x;
            count++;
          }
          float avg_distance = sum / count;

          // Calcolo del livello in percentuale, considerando le misure in metri
          float livello_percentuale = 100.0 * (1 - ((avg_distance - 0.28) / (0.70 - 0.28)));
          if (livello_percentuale > 100.0) livello_percentuale = 100.0;
          if (livello_percentuale < 0.0) livello_percentuale = 0.0;

          // Pubblica lo stato per il livello percentuale
          id(livello_percentuale_tanica).publish_state(livello_percentuale);

          return avg_distance;

  - platform: "template"
    name: "Percentuale Livello Tanica"
    id: livello_percentuale_tanica
    unit_of_measurement: "%"
    accuracy_decimals: 1


  - platform: hx711
    name: "Peso GPL"
    id: peso_gpl
    dout_pin: D2
    clk_pin: D1
#    gain: 128
    update_interval: 5s
    filters:
      - calibrate_linear:  # Aggiusta il moltiplicatore in base alla calibrazione
          - -212990 -> 0  # massa nota 0kg mi da xxx
          - -166431 -> 2  # massa nota 2kg mi da xxx


#binary_sensor:
#  - platform: gpio
#    pin:
#      number: GPIO9
#      mode: INPUT_PULLUP
#    name: "Pioggia Rilevata"
#    device_class: moisture

switch:
  - platform: gpio
    pin: GPIO0
    name: "Pompa Antizanzare"
    id: relay_pompa
#  - platform: gpio
#    pin: GPIO11
#    name: "Scorta"
#    id: relay_scorta

immagine