#include #include "MS5837.h" MS5837 sensor; const int pwmPin = 9; // PWM output pin void setup() { Serial.begin(9600); Wire.begin(); // Returns true if initialization was successful // We can't continue with the rest of the program unless we can initialize the sensor while (!sensor.init()) { Serial.println("Init failed!"); Serial.println("Are SDA/SCL connected correctly?"); Serial.println("Blue Robotics Bar30: White=SDA, Green=SCL"); Serial.println("\n\n\n"); delay(5000); } // .init sets the sensor model for us but we can override it if required. // Uncomment the next line to force the sensor model to the MS5837_30BA. //sensor.setModel(MS5837::MS5837_30BA); sensor.setFluidDensity(997); // kg/m^3 (freshwater, 1029 for seawater) pinMode(pwmPin, OUTPUT); // Set the PWM pin as an output } void loop() { // Update pressure and temperature readings sensor.read(); Serial.print("Depth: "); Serial.print(sensor.depth()); Serial.println(" m"); // Convert depth to a PWM value (0-255) float depth = sensor.depth(); int pwmValue = map(depth, 0, 10, 0, 255); // Assuming depth range is 0 to 10 meters analogWrite(pwmPin, pwmValue); // Output the PWM signal delay(1000); }