#include <Arduino.h>
// Define constants
const int analogPin = 34; // GPIO pin connected to the sensor
const float sensorMinVoltage = 0.5; // Minimum voltage from the sensor
const float sensorMaxVoltage = 2.5; // Maximum voltage from the sensor
const float depthMin = 0.0; // Minimum depth in meters
const float depthMax = 60.0; // Maximum depth in meters
void setup() {
Serial.begin(115200); // Initialize Serial communication
analogReadResolution(12); // Set ADC resolution to 12 bits (0-4095)
}
void loop() {
// Read analog value
int analogValue = analogRead(analogPin);
// Convert analog value to voltage
float voltage = analogValue * (3.3 / 4095.0); // Assuming a 3.3V reference
// Map voltage to depth
float depth = mapVoltageToDepth(voltage);
// Print the results
Serial.print("Analog Value: ");
Serial.print(analogValue);
Serial.print(" | Voltage: ");
Serial.print(voltage, 2);
Serial.print(" V | Depth: ");
Serial.print(depth, 2);
Serial.println(" m");
delay(1000); // Wait for 1 second before the next reading
}
float mapVoltageToDepth(float voltage) {
// Ensure voltage is within the expected range
voltage = constrain(voltage, sensorMinVoltage, sensorMaxVoltage);
// Map voltage to depth
return (voltage - sensorMinVoltage) * (depthMax - depthMin) / (sensorMaxVoltage - sensorMinVoltage) + depthMin;
}