Created
October 16, 2023 10:02
-
-
Save kakopappa/83febb46592b067c79a0dd55136405a6 to your computer and use it in GitHub Desktop.
Sinric Pro Water Tank (Water Level Monitor) using ultrasonic sensor : https://help.sinric.pro/pages/tutorials/custom-device-types/ultrasonic-sensor/HC-SR04.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #if defined(ESP8266) | |
| const int trigPin = 12; | |
| const int echoPin = 14; | |
| #elif defined(ESP32) | |
| const int trigPin = 5; | |
| const int echoPin = 18; | |
| #elif defined(ARDUINO_ARCH_RP2040) | |
| const int trigPin = 15; | |
| const int echoPin = 14; | |
| #endif | |
| long duration; | |
| float distanceInCm; | |
| void setup() { | |
| Serial.begin(115200); | |
| pinMode(trigPin, OUTPUT); | |
| pinMode(echoPin, INPUT); | |
| } | |
| void loop() { | |
| // Clears the trigPin | |
| digitalWrite(trigPin, LOW); | |
| delayMicroseconds(2); | |
| digitalWrite(trigPin, HIGH); | |
| delayMicroseconds(10); | |
| digitalWrite(trigPin, LOW); | |
| duration = pulseIn(echoPin, HIGH); | |
| // convert duration to CM | |
| distanceInCm = duration/29 / 2; | |
| // inches | |
| // distanceInInches = duration / 74 / 2; | |
| Serial.print("Distance (cm): "); | |
| Serial.println(distanceInCm); | |
| delay(1000); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment