Skip to content

JavaScript Sensor API

Overview

In addition to the Core API enabling you to work with sensors via I2C or other means, there is a list of specific sensors made available to you to use with an easy-to-use interface.

This consists of well-known sensors. Feel free to request additional, or changes to existing.

Consider the BMP280 Temperature/Pressure Sensor

You could communicate with it using JavaScript I2C, or, instead, something much easier!

1
2
3
4
5
// Create an instance of the sensor
let sensor = new BMP280(0x77)

// Extract data - no direct I2C required!
sensor.GetPressureMilliBars()

Page Structure

For each sensor, in addition to the API description, useful details are made available. They are:

What Description
Description What the sensor does.
Product Page A page where you can see product info.
Technical Page A page where you can see technical details.
Github repo A link if the JavaScript interface mirrors a known library.
Datasheet Datasheet of the sensor.
Power Any notable power requirement details.
Technical Details Useful technical details.

Weather

SI7021 - Temperature, Humidity

SI7021 - Temperature, Humidity

What Description
Description Temperature and humidity sensor.
Product Page Adafruit Si7021 Temperature & Humidity Sensor Breakout Board - STEMMA QT
Technical Page Adafruit Si7021 Temperature + Humidity Sensor
Github repo Adafruit_Si7021
Datasheet datasheet
Power 1.9 - 3.6 Volt, 150uA
Technical Details Hookup via I2C

API Description

// create an instance of the sensor - addr automatically set to 0x40
// (takes 50ms to return)
let sensor = new SI7021()

// get temperature in celsius
// (takes 20ms to return)
sensor.GetTemperatureCelsius()

// get temperature in fahrenheit
// (takes 20ms to return)
sensor.GetTemperatureFahrenheit()

// get humidity as a percent
// (takes 20ms to return)
sensor.GetHumidityPct()

API Example

1
2
3
4
5
let sensor = new SI7021()

Log(`sensor.GetTemperatureCelsius()    = ${sensor.GetTemperatureCelsius()}`)
Log(`sensor.GetTemperatureFahrenheit() = ${sensor.GetTemperatureFahrenheit()}`)
Log(`sensor.GetHumidityPct()           = ${sensor.GetHumidityPct()}`)

BME280 - Temperature, Pressure, Humidity, Altitude

BME280 - Temperature, Pressure, Humidity, Altitude estimation

What Description
Description Temperature, Pressure, Humidity, Altitude estimation sensor.
Product Page Adafruit BME280 I2C or SPI Temperature Humidity Pressure Sensor - STEMMA QT
Technical Page Adafruit BME280 Humidity + Barometric Pressure + Temperature Sensor Breakout
Github repo Adafruit_BME280_Library
Datasheet datasheet
Power 1.8 - 3.6 Volt, 1mA
Technical Details Hookup via I2C

API Description

// Create an instance of the sensor - addr either 0x76 or 0x77
// (takes 120ms to return)
let sensor = new BME280(addr)

// Get temperature in Celsius
// (takes 1ms to return)
sensor.GetTemperatureCelsius()

// Get temperature in Fahrenheit
// (takes 1ms to return)
sensor.GetTemperatureFahrenheit()

// Get pressure in hPa
// (takes 1ms to return)
sensor.GetPressureHectoPascals()

// Get pressure in millibar
// (takes 1ms to return)
sensor.GetPressureMilliBars()

// Get altitude in Meters (estimated from sea level nominal pressure at 1013.25 hPa)
// (takes 1ms to return)
sensor.GetAltitudeMeters()

// Get altitude in Feet (estimated from sea level nominal pressure at 1013.25 hPa)
// (takes 1ms to return)
sensor.GetAltitudeFeet()

// Get humidity in percent
// (takes 1ms to return)
sensor.GetHumidityPct()

API Example

1
2
3
4
5
6
7
8
9
let sensor = new BME280(0x77)

Log(`sensor.GetTemperatureCelsius()    = ${sensor.GetTemperatureCelsius()}`)
Log(`sensor.GetTemperatureFahrenheit() = ${sensor.GetTemperatureFahrenheit()}`)
Log(`sensor.GetPressureHectoPascals()  = ${sensor.GetPressureHectoPascals()}`)
Log(`sensor.GetPressureMilliBars()     = ${sensor.GetPressureMilliBars()}`)
Log(`sensor.GetAltitudeMeters()        = ${sensor.GetAltitudeMeters()}`)
Log(`sensor.GetAltitudeFeet()          = ${sensor.GetAltitudeFeet()}`)
Log(`sensor.GetHumidityPct()           = ${sensor.GetHumidityPct()}`)

BMP280 - Temperature, Pressure, Altitude

BMP280 - Temperature, Pressure, Altitude estimation

What Description
Description Temperature, Pressure, Altitude estimation sensor.
Product Page Adafruit BMP280 I2C or SPI Barometric Pressure & Altitude Sensor - STEMMA QT
Technical Page Adafruit BMP280 Barometric Pressure + Temperature Sensor Breakout
Github repo Adafruit_BMP280_Library
Datasheet datasheet
Power 1.8 - 3.6 Volt, 2mA
Technical Details Hookup via I2C

API Description

// Create an instance of the sensor - addr either 0x76 or 0x77
// (takes 105ms to return)
let sensor = new BMP280(addr)

// Get temperature in Celsius
// (takes 1ms to return)
sensor.GetTemperatureCelsius()

// Get temperature in Fahrenheit
// (takes 1ms to return)
sensor.GetTemperatureFahrenheit()

// Get pressure in hPa
// (takes 1ms to return)
sensor.GetPressureHectoPascals()

// Get pressure in millibar
// (takes 1ms to return)
sensor.GetPressureMilliBars()

// Get altitude in Meters (estimated from sea level nominal pressure at 1013.25 hPa)
// (takes 1ms to return)
sensor.GetAltitudeMeters()

// Get altitude in Feet (estimated from sea level nominal pressure at 1013.25 hPa)
// (takes 1ms to return)
sensor.GetAltitudeFeet()

API Example

1
2
3
4
5
6
7
8
let sensor = new BMP280(0x77)

Log(`sensor.GetTemperatureCelsius()    = ${sensor.GetTemperatureCelsius()}`)
Log(`sensor.GetTemperatureFahrenheit() = ${sensor.GetTemperatureFahrenheit()}`)
Log(`sensor.GetPressureHectoPascals()  = ${sensor.GetPressureHectoPascals()}`)
Log(`sensor.GetPressureMilliBars()     = ${sensor.GetPressureMilliBars()}`)
Log(`sensor.GetAltitudeMeters()        = ${sensor.GetAltitudeMeters()}`)
Log(`sensor.GetAltitudeFeet()          = ${sensor.GetAltitudeFeet()}`)

Light

BH1750 - Lux

BH1750 - Lux sensor

What Description
Description Light (lux) sensor.
Product Page Adafruit BH1750 Light Sensor - STEMMA QT / Qwiic
Technical Page Adafruit BH1750 Ambient Light Sensor
Github repo BH1750FVI_RT
Datasheet datasheet
Power 2.4 - 3.6 Volt, 1mA
Technical Details Hookup via I2C

API Description

// Create an instance of the sensor - addr either 0x23 or 0x5C
// (takes 1ms to return)
let sensor = new BH1750(addr)

// Tell the sensor the current temperature so it can account for the effect on its measurement.
// Default sensor temperature is 20C / 68F.
// (takes 0ms to return)
sensor.SetTemperatureCelsius(temp)

// Tell the sensor the current temperature so it can account for the effect on its measurement.
// Default sensor temperature is 20C / 68F.
// (takes 0ms to return)
sensor.SetTemperatureFahrenheit(temp)

// Take a low-resolution lux measurement (resolution 4 lux)
// (takes 40ms to return)
sensor.GetLuxLowRes()

// Take a high-resolution lux measurement (resolution 1 lux)
// (takes 200ms to return)
sensor.GetLuxHighRes()

// Take a high2-resolution lux measurement (resolution 0.5 lux)
// (takes 200ms to return)
sensor.GetLuxHigh2Res()

API Example

1
2
3
4
5
let sensor = new BH1750(0x23)

Log(`sensor.GetLuxLowRes()   = ${sensor.GetLuxLowRes()}`)
Log(`sensor.GetLuxHighRes()  = ${sensor.GetLuxHighRes()}`)
Log(`sensor.GetLuxHigh2Res() = ${sensor.GetLuxHigh2Res()}`)

Magnetic

MMC56x3 - 3-Axis Magnetometer

MMC56x3 - 3-Axis Magnetometer

What Description
Description 3-Axis Magnetometer sensor.
Product Page Adafruit Triple-axis Magnetometer - MMC5603 - STEMMA QT / Qwiic
Technical Page Adafruit MMC5603 - Triple-axis Magnetometer
Github repo Adafruit_MMC56x3
Datasheet datasheet
Power 1.7 - 3.6 Volt, 4mA
Technical Details Hookup via I2C

API Description

// Create an instance of the sensor - addr automatically set to 0x30
// (takes 25ms to return)
let sensor = new MMC56x3()

// Get field strength along X-Axis in MicroTesla (uT)
// (takes 15ms to return)
sensor.GetMagXMicroTeslas()

// Get field strength along Y-Axis in MicroTesla (uT)
// (takes 15ms to return)
sensor.GetMagYMicroTeslas()

// Get field strength along Z-Axis in MicroTesla (uT)
// (takes 15ms to return)
sensor.GetMagZMicroTeslas()

API Example

1
2
3
4
5
let sensor = new MMC56x3()

Log(`sensor.GetMagXMicroTeslas() = ${sensor.GetMagXMicroTeslas()}`)
Log(`sensor.GetMagYMicroTeslas() = ${sensor.GetMagYMicroTeslas()}`)
Log(`sensor.GetMagZMicroTeslas() = ${sensor.GetMagZMicroTeslas()}`)