Ultradźwiękowy czujnik odległości Wodoodporny ri o04

User avatar
RAMJET
Posts: 62
Joined: Mon Aug 19, 2024 6:05 pm
Location: Wodzisław Śląski

Post

nebraska wrote: Tue Jan 06, 2026 11:39 pm Czujnik powinieneś podłączyć pod 3.3V zasilanie.
Jeśli masz pod 5V to duży błąd.
Kod ze strony może być lipny, ponieważ przed każdym odczytem powinna być pętla while czyszcząca bufor przed kolejnym odczytem. Może być tak, że po odczycie pierwszej wartośći kolejna już nie wejdzie dopiero jak zresetujesz płytkę to nowa się pojawi.
Trzeba to metodą prób robić modyfikując ten program ze strony.
Czas reakcji czujnika też nie jest prawidłowy na pewno to nie będzie 200 jak na tej stronie.
Powinieneś przerobić jakiś podstawowy kurs i wtedy zabrać się za to. Polecam forbota na początek.
Według instrukcji od 3-5V Podłączyłem pod 3,3V
nebraska
Posts: 204
Joined: Thu Dec 08, 2022 3:00 pm
Been thanked: 1 time

Post

RAMJET wrote: Tue Jan 06, 2026 11:51 pm
nebraska wrote: Tue Jan 06, 2026 11:39 pm Czujnik powinieneś podłączyć pod 3.3V zasilanie.
Jeśli masz pod 5V to duży błąd.
Kod ze strony może być lipny, ponieważ przed każdym odczytem powinna być pętla while czyszcząca bufor przed kolejnym odczytem. Może być tak, że po odczycie pierwszej wartośći kolejna już nie wejdzie dopiero jak zresetujesz płytkę to nowa się pojawi.
Trzeba to metodą prób robić modyfikując ten program ze strony.
Czas reakcji czujnika też nie jest prawidłowy na pewno to nie będzie 200 jak na tej stronie.
Powinieneś przerobić jakiś podstawowy kurs i wtedy zabrać się za to. Polecam forbota na początek.
Według instrukcji od 3-5V Podłączyłem pod 3,3V
To jest zakres napięcia na jakim czujnik może pracować.
Gpioo w ESP32 pracuje na logice 3.3-3.6V.
Podłączając do 5V pin TX może posłać z czujnika do gpioo ESP i wtedy może być problem. Dlatego warto o tym pamiętać. Oczywiście można to podłączyć na 5V, ale wtedy warto dodać jakiś konwerter logiczny lub dzielnik żeby bezpiecznie było.
User avatar
vajera
Posts: 7289
Joined: Wed Oct 31, 2018 7:58 am
Location: Biedrusko
Has thanked: 289 times
Been thanked: 161 times

Post

RAMJET wrote: Tue Jan 06, 2026 11:45 pm Reaguje na zbliżający się obiekt :D
To już dobry początek 😉
Bramka Zigbee <=> SUPLA
Więcej informacji tutaj:
https://forum.supla.org/viewforum.php?f=127
FAQ https://forum.supla.org/viewtopic.php?t=17277
User avatar
RAMJET
Posts: 62
Joined: Mon Aug 19, 2024 6:05 pm
Location: Wodzisław Śląski

Post

nebraska wrote: Wed Jan 07, 2026 12:16 am
RAMJET wrote: Tue Jan 06, 2026 11:51 pm
nebraska wrote: Tue Jan 06, 2026 11:39 pm Czujnik powinieneś podłączyć pod 3.3V zasilanie.
Jeśli masz pod 5V to duży błąd.
Kod ze strony może być lipny, ponieważ przed każdym odczytem powinna być pętla while czyszcząca bufor przed kolejnym odczytem. Może być tak, że po odczycie pierwszej wartośći kolejna już nie wejdzie dopiero jak zresetujesz płytkę to nowa się pojawi.
Trzeba to metodą prób robić modyfikując ten program ze strony.
Czas reakcji czujnika też nie jest prawidłowy na pewno to nie będzie 200 jak na tej stronie.
Powinieneś przerobić jakiś podstawowy kurs i wtedy zabrać się za to. Polecam forbota na początek.
Według instrukcji od 3-5V Podłączyłem pod 3,3V
To jest zakres napięcia na jakim czujnik może pracować.
Gpioo w ESP32 pracuje na logice 3.3-3.6V.
Podłączając do 5V pin TX może posłać z czujnika do gpioo ESP i wtedy może być problem. Dlatego warto o tym pamiętać. Oczywiście można to podłączyć na 5V, ale wtedy warto dodać jakiś konwerter logiczny lub dzielnik żeby bezpiecznie było.
OK Dzięki
User avatar
RAMJET
Posts: 62
Joined: Mon Aug 19, 2024 6:05 pm
Location: Wodzisław Śląski

Post

vajera wrote: Wed Jan 07, 2026 6:19 am
RAMJET wrote: Tue Jan 06, 2026 11:45 pm Reaguje na zbliżający się obiekt :D
To już dobry początek 😉
Też się ucieszyłem, że wreszcie "ożył" :D
User avatar
vajera
Posts: 7289
Joined: Wed Oct 31, 2018 7:58 am
Location: Biedrusko
Has thanked: 289 times
Been thanked: 161 times

Post

RAMJET wrote: Wed Jan 07, 2026 1:06 pm
vajera wrote: Wed Jan 07, 2026 6:19 am
RAMJET wrote: Tue Jan 06, 2026 11:45 pm Reaguje na zbliżający się obiekt :D
To już dobry początek 😉
Też się ucieszyłem, że wreszcie "ożył" :D
Wypróbuj poniższy kod:

Code: Select all


#define RXD2 16
#define TXD2 17

const uint8_t HEADER_BYTE = 0xFF;
const uint16_t READ_TIMEOUT_MS = 200;
const int ERROR_DISTANCE = -1;  // Error return value


uint8_t readN(uint8_t *buf, size_t len) {
  size_t offset = 0, left = len;
  uint8_t *buffer = buf;
  long curr = millis();
  while (left) {
    if (Serial2.available()) {
      buffer[offset] = Serial2.read();
      offset++;
      left--;
    }
    if (millis() - curr > READ_TIMEOUT_MS) {
      break;
    }
  }
  return offset;
}

/**
 * Get distance data from the sensor
 * @return Returns the distance value (non-negative) on success, -1 on failure
 */
int getDistance(void) {
  uint8_t data[4] = { 0 };
  uint8_t receivedByte = 0;
  unsigned long startTime = millis();

  while (millis() - startTime < READ_TIMEOUT_MS) {                      // Check if timeout
    if (readN(&receivedByte, 1) == 1 && receivedByte == HEADER_BYTE) {  // Find the header byte
      data[0] = receivedByte;
      if (readN(&data[1], 3) == 3) {                     // Read the remaining 3 bytes
        uint8_t checksum = data[0] + data[1] + data[2];  // Checksum
        if (checksum == data[3]) {
          uint16_t distance = (data[1] << 8) | data[2];  // Calculate and return the distance
          return distance;
        }
      }
    }
    Serial.println("Error data");
  }
  Serial.println("Error Reading data timeout");
  return ERROR_DISTANCE;
}

void setup() {
  
  Serial.begin(115200);
 
  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
  Serial.println("Serial Txd is on pin: "+String(TX));
  Serial.println("Serial Rxd is on pin: "+String(RX));
}

void loop() {
  Serial.print("distance=");
  Serial.print(getDistance());
  Serial.println("mm");
}

Bramka Zigbee <=> SUPLA
Więcej informacji tutaj:
https://forum.supla.org/viewforum.php?f=127
FAQ https://forum.supla.org/viewtopic.php?t=17277
User avatar
RAMJET
Posts: 62
Joined: Mon Aug 19, 2024 6:05 pm
Location: Wodzisław Śląski

Post

SUPER - pokazuje dokładną odległość w mm :D
Dużo lepiej niż czujnik odległości HC-SR04, który miałem zainstalowany wcześniej i po około roku odmówił posłuszeństwa :D
You do not have the required permissions to view the files attached to this post.
Last edited by RAMJET on Wed Jan 07, 2026 1:56 pm, edited 1 time in total.
User avatar
vajera
Posts: 7289
Joined: Wed Oct 31, 2018 7:58 am
Location: Biedrusko
Has thanked: 289 times
Been thanked: 161 times

Post

RAMJET wrote: Wed Jan 07, 2026 1:35 pm SUPER - pokazuje dokładną odległość w mm :D
Dużo lepiej niż czujnik odległości HC-SR04
Git! To teraz przeniesiemy go do Supla - wrzucę kod dzisiaj/jutro.
Bramka Zigbee <=> SUPLA
Więcej informacji tutaj:
https://forum.supla.org/viewforum.php?f=127
FAQ https://forum.supla.org/viewtopic.php?t=17277
User avatar
RAMJET
Posts: 62
Joined: Mon Aug 19, 2024 6:05 pm
Location: Wodzisław Śląski

Post

vajera wrote: Wed Jan 07, 2026 1:54 pm
RAMJET wrote: Wed Jan 07, 2026 1:35 pm SUPER - pokazuje dokładną odległość w mm :D
Dużo lepiej niż czujnik odległości HC-SR04
Git! To teraz przeniesiemy go do Supla - wrzucę kod dzisiaj/jutro.
SUPER WIELKIE DZIĘKI :D :D :D
User avatar
vajera
Posts: 7289
Joined: Wed Oct 31, 2018 7:58 am
Location: Biedrusko
Has thanked: 289 times
Been thanked: 161 times

Post

RAMJET wrote: Wed Jan 07, 2026 1:57 pm
vajera wrote: Wed Jan 07, 2026 1:54 pm
RAMJET wrote: Wed Jan 07, 2026 1:35 pm SUPER - pokazuje dokładną odległość w mm :D
Dużo lepiej niż czujnik odległości HC-SR04
Git! To teraz przeniesiemy go do Supla - wrzucę kod dzisiaj/jutro.
SUPER WIELKIE DZIĘKI :D :D :D
Wypróbuj ten kod - z góry zaznaczam, że nie kompilowałem go, więc mogą być drobne błędy - pisz, to będziemy poprawiać.
Oczywiście musisz mieć zainstalowaną bibliotekę SuplaDevice w ArduinoIDE.

Code: Select all

/*
  Copyright (C) AC SOFTWARE SP. Z O.O.

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include <SuplaDevice.h>
#include <supla/network/esp_wifi.h>
#include <supla/device/status_led.h>
#include <supla/storage/littlefs_config.h>
#include <supla/network/esp_web_server.h>
#include <supla/network/html/device_info.h>
#include <supla/network/html/protocol_parameters.h>
#include <supla/network/html/status_led_parameters.h>
#include <supla/network/html/wifi_parameters.h>
#include <supla/sensor/general_purpose_measurement.h>

#include <supla/storage/eeprom.h>

#define RXD2 16
#define TXD2 17

//#define STATUS_LED_GPIO 2

Supla::Eeprom eeprom;
Supla::ESPWifi wifi;
Supla::LittleFsConfig configSupla;
//Supla::Device::StatusLed statusLed(STATUS_LED_GPIO, true); 
Supla::EspWebServer suplaServer;

const uint8_t HEADER_BYTE = 0xFF;
const uint16_t READ_TIMEOUT_MS = 200;
const int ERROR_DISTANCE = -1;  // Error return value


uint8_t readN(uint8_t *buf, size_t len) {
  size_t offset = 0, left = len;
  uint8_t *buffer = buf;
  long curr = millis();
  while (left) {
    if (Serial2.available()) {
      buffer[offset] = Serial2.read();
      offset++;
      left--;
    }
    if (millis() - curr > READ_TIMEOUT_MS) {
      break;
    }
  }
  return offset;
}

/**
 * Get distance data from the sensor
 * @return Returns the distance value (non-negative) on success, -1 on failure
 */
int getDistance(void) {
  uint8_t data[4] = { 0 };
  uint8_t receivedByte = 0;
  unsigned long startTime = millis();

  while (millis() - startTime < READ_TIMEOUT_MS) {                      // Check if timeout
    if (readN(&receivedByte, 1) == 1 && receivedByte == HEADER_BYTE) {  // Find the header byte
      data[0] = receivedByte;
      if (readN(&data[1], 3) == 3) {                     // Read the remaining 3 bytes
        uint8_t checksum = data[0] + data[1] + data[2];  // Checksum
        if (checksum == data[3]) {
          uint16_t distance = (data[1] << 8) | data[2];  // Calculate and return the distance
          return distance;
        }
      }
    }
    Serial.println("Error data");
  }
  Serial.println("Error Reading data timeout");
  return ERROR_DISTANCE;
}

Supla::Sensor::GeneralPurposeMeasurement *distanceGPM = nullptr;

void setup() {
  
  Serial.begin(115200);
 
  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
  Serial.println("Serial Txd is on pin: "+String(TX));
  Serial.println("Serial Rxd is on pin: "+String(RX));
  
  new Supla::Html::DeviceInfo(&SuplaDevice);
  new Supla::Html::WifiParameters;
  new Supla::Html::ProtocolParameters;
  new Supla::Html::StatusLedParameters;
  
  distanceGPM = new Supla::Sensor::GeneralPurposeMeasurement();
  distanceGPM->setInitialCaption("DISTANCE");
  distanceGPM->setDefaultUnitAfterValue("mm");
  
  SuplaDevice.setInitialMode(Supla::InitialMode::StartInCfgMode);
  SuplaDevice.begin();
}

void loop() {

  SuplaDevice.iterate();
  
  int distance = getDistance();
  
  Serial.printf("distance = %d mm\n\r", distance);
  if (distanceGPM)
    distanceGPM->setValue(distance);
}

Bramka Zigbee <=> SUPLA
Więcej informacji tutaj:
https://forum.supla.org/viewforum.php?f=127
FAQ https://forum.supla.org/viewtopic.php?t=17277

Return to “Pomoc”