-
Notifications
You must be signed in to change notification settings - Fork 1
1.8 Pressure Sensor Code Introduction
Congratulations on reaching this stage in your embedded systems tutorial! In this section, we'll discuss the pressure sensor code that helps us gather crucial data about depth underwater. The pressure sensor works by measuring the pressure of the surrounding water, which changes based on the depth we are at. This information is vital for our underwater robotics competition, allowing us to navigate and operate effectively.
Here's the code for initializing and reading data from the pressure sensor. This code is not simplified and is actually the exact code we use with our current pressure sensor:
#include <Wire.h>
#include "MS5837.h"
MS5837 sensor;
void setup() {
Serial.begin(9600);
Serial.println("Starting");
Wire.begin();
// Initialize pressure sensor
// 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)
}
void loop() {
// Update pressure and temperature readings
sensor.read();
Serial.print("Pressure: ");
Serial.print(sensor.pressure());
Serial.println(" mbar");
Serial.print("Temperature: ");
Serial.print(sensor.temperature());
Serial.println(" deg C");
Serial.print("Depth: ");
Serial.print(sensor.depth());
Serial.println(" m");
Serial.print("Altitude: ");
Serial.print(sensor.altitude());
Serial.println(" m above mean sea level");
delay(1000);
}
Before working with any sensor, it is crucial to read its datasheet and understand how it is supposed to work. Fortunately, in our case, we are lucky enough to have libraries that already contain pre-written code for interfacing with the pressure sensor. All we need to do is import these libraries and call the functions provided within them.
While this tutorial focuses on using existing libraries to interact with the pressure sensor, it's essential to note that as you progress to more advanced projects, you might need to write your own libraries or find more sophisticated methods to rewrite the code for specific sensors. For now, calling functions from the library will help us efficiently obtain data from the pressure sensor without getting into the complexities of low-level programming.
We include the Wire.h library and the MS5837.h library to facilitate communication with the pressure sensor. The Wire library is essential for I2C communication, allowing us to interface with the sensor easily. Libraries provide pre-written code that helps streamline your projects. For more details on using libraries, check out this section.
The setup() function initializes serial communication for debugging and sets up the I2C interface. It also initializes the pressure sensor, checking if the connection is established. If the initialization fails, the code will repeatedly attempt to initialize the sensor, providing feedback through the serial monitor.
The loop() function continuously reads the sensor data and prints the pressure, temperature, depth, and altitude to the serial monitor. This function runs every second, providing real-time data updates that are essential for monitoring underwater conditions.
The sensor.read() function updates the pressure and temperature readings from the sensor. The sensor.pressure(), sensor.temperature(), sensor.depth(), and sensor.altitude() functions retrieve the respective measurements. These functions abstract away the complexities of directly interfacing with the sensor, allowing us to focus on processing the data.
In this project, we are using the I2C (Inter-Integrated Circuit) communication protocol to connect the pressure sensor to our microcontroller. I2C allows multiple devices to communicate with each other using only two wires: the data line (SDA) and the clock line (SCL). This is particularly useful in our application, as we can connect multiple sensors and devices on the same bus without requiring additional pins.
I2C is a versatile and straightforward protocol that simplifies wiring and communication in embedded systems. It is essential to ensure that SDA and SCL connections are correctly made to avoid initialization issues with the sensor.
Understanding how to interface with sensors like the MS5837 is a crucial skill for any embedded engineer. While this code serves as a basic introduction, mastering these concepts will pave the way for more complex projects in the future. If you have any questions or need assistance, please reach out to a lead or senior member of the team.
Happy coding!