Created
October 22, 2023 04:24
-
-
Save kakopappa/a2dffb0f0c46585ed0b35aa902c2447e to your computer and use it in GitHub Desktop.
Detecting floods using water sensor : https://help.sinric.pro/pages/tutorials/custom-device-types/water-sensor/flood-leak-rain-sensor.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 sPin = A0; | |
const int vccPin = D2; | |
#elif defined(ESP32) | |
const int sPin = 34; | |
const int vccPin = 17; | |
#elif defined(ARDUINO_ARCH_RP2040) | |
const int sPin = 26; | |
const int vccPin = 6; | |
#endif | |
const int SENSOR_MAX = 560; | |
const int SENSOR_MIN = 1; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(sPin, INPUT); | |
pinMode(vccPin, OUTPUT); | |
digitalWrite(vccPin, LOW); // turn off the sensor at the begining. | |
} | |
void loop() { | |
digitalWrite(vccPin, HIGH); // turn on the power for sensor. | |
delay(100); // wait 100ms | |
int value = analogRead(sPin); // make a reading from sensor | |
digitalWrite(vccPin, LOW); // turn off the sensor | |
int waterLevelHeightInCm = map(value, SENSOR_MIN, SENSOR_MAX, 0, 5); // Map sensor value between 0..5 levels (1 cm = 1 level) | |
Serial.printf("Water level: %d\n", waterLevelHeightInCm); // print reading | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment