Skip to content

Instantly share code, notes, and snippets.

@kakopappa
Created October 22, 2023 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kakopappa/a2dffb0f0c46585ed0b35aa902c2447e to your computer and use it in GitHub Desktop.
Save kakopappa/a2dffb0f0c46585ed0b35aa902c2447e to your computer and use it in GitHub Desktop.
#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