Barometer Sensor Guide
chevron down
 

Barometer Sensor Guide

Overview

A Barometer is used in meteorology to measure atmospheric pressure and forecast short term changes in the weather. Because atmospheric pressure also varies with elevation, a barometer can also be used as a basic altimeter.

Note: The Fitbit Versa Lite does not contain a barometer sensor.

Fitbit uses data from the barometer sensor within the algorithm that calculates the Floors health metric.

The Fitbit Activity Time Series API is the easiest way to determine user's actual Floor count.

Units of Pressure

The Barometer sensor returns atmospheric pressure readings in Pascal (Pa) units.

  • 100 Pa = 1 hPa (hectopascal) which is equal to 1 millibar
  • 1000 Pa = 1 kPa (kilopascal) which is equal to 1 centibar.

Peeking the Current Barometer Reading

If you need to check the latest reading from the barometer, you can access the properties directly from the barometer object using the reading event.

The barometer only supports a specific set of sampling rates between 1Hz and 40Hz, but the API will automatically adjust your requested frequency to match the closest supported rate.

import { Barometer } from "barometer";

if (Barometer) {
  const barometer = new Barometer({ frequency: 1 });
  barometer.addEventListener("reading", () => {
    console.log(`Pressure: ${barometer.pressure} Pa`);
  });
  barometer.start();
}

Calculating a Change in Altitude

Let's take a look at a more advanced example which simulates a floor counter using the barometer sensor as an altimeter.

import { Barometer } from "barometer";

if (Barometer) {
  const barometer = new Barometer({ frequency: 1 });

  // 10 foot of air [15°C, dry] = 36.62293100066991 pascal
  const approxFloorPa = 36.62293100066991;

  // The current floor in pascal
  let currentFloorPa = 0;

  // How many floors has the user completed since we started
  let floorCounter = 0;

  // Event occurs when the batched readings are available
  barometer.addEventListener("reading", () => {

    if (currentFloorPa !== 0) {

      // Calculate the difference between the current floor, and the new pressure
      let diffPa = currentFloorPa - barometer.pressure;
      console.log("Difference: " + diffPa + " Pa");

      // Did the pressure change more than 10 feet?
      if (Math.abs(diffPa) > approxFloorPa) {
        console.log("Floor change detected");

        // Detect up or down direction
        if (barometer.pressure > currentFloorPa) {
          console.log("User went downwards");
        } else {
          console.log("User went upwards");
        }

        // This is now our current floor
        currentFloorPa = barometer.pressure;

        // Increment floor counter
        floorCounter++;
        console.log("Floors completed: " + floorCounter);

      }

    } else {
      // The initial starting floor
      currentFloorPa = barometer.pressure;
    }

  });

  // Begin monitoring the sensor
  barometer.start();
}

Batched Readings

The Barometer API can also generate batches of data at a specified sample rate. This allows a developer to control the frequency that their application receives and processes sensor data.

The onreading event is emitted when a batch of readings is available, and the .readings property contains the sensor readings, with each data channel (pressure, and timestamp) as its own array.

By reducing the sample rate (frequency), or by increasing the batch size, developers will benefit from reduced CPU usage, and minimize their application's impact on battery life.

In order to use batched readings, the batch property must be specified during the initialization of the sensor.

import { Barometer } from "barometer";

if (Barometer) {
  // 1 reading per second, 60 readings per batch
  const barometer = new Barometer({ frequency: 1, batch: 60 });
  barometer.addEventListener("reading", () => {
      for (let index = 0; index < barometer.readings.timestamp.length; index++) {
      console.log(
        `Barometer Reading: \
          timestamp=${barometer.readings.timestamp[index]}, \
          [${barometer.readings.pressure[index]}]`
      );
    }
  });
  barometer.start();
}

Automatically Stopping and Starting

One of the best ways to conserve battery life is to stop the sensor when the display is off. You can use the Display API to respond to changes in the screen's power state.

import { Barometer } from "barometer";
import { display } from "display";

if (Barometer) {
  const barometer = new Barometer({ frequency: 1 });
  barometer.addEventListener("reading", () => {
    console.log(`Pressure: ${barometer.pressure} Pa`);
  });
  display.addEventListener("change", () => {
    // Automatically stop the sensor when the screen is off to conserve battery
    display.on ? barometer.start() : barometer.stop();
  });
  barometer.start();
}

Barometer Best Practices

Here's a simple list of best practices to follow when using the Barometer API:

  1. Always use the most optimal frequency for your specific needs.
  2. Don't forget to call bar.stop(); when you've finished using it.
  3. Check if the sensor exists before using it.

The Barometer in Action

If you're interested in using the Barometer API within your application, why not check out the "Altimeter" example app or review the Barometer API reference documentation.