Science Knowings: HTML Course For Social Media

HTML5 Magnetometer Sensor API

HTML5 Magnetometer Sensor API

In this session, we'll dive into the HTML5 Magnetometer Sensor API, a powerful tool for detecting and measuring magnetic fields.

What is Magnetometer?

A magnetometer is a device that measures magnetic fields. It can detect the presence, direction, and strength of magnetic fields.

Detecting Magnetic Fields

The Magnetometer API allows you to detect the presence of magnetic fields around your device.

Measuring Magnetic Field Strength

You can use the API to measure the strength of magnetic fields in micro Teslas (µT).

Measuring Magnetic Field Direction

The API also provides information about the direction of the magnetic field, relative to the device's orientation.

Calibrating the Magnetometer

Before using the Magnetometer API, it's important to calibrate the magnetometer to improve its accuracy.

Using the Magnetometer API in Your Apps

The Magnetometer API is easy to use in your web apps. You can access the API through the navigator.magnetometer property.

Event Handling

The Magnetometer API uses events to notify you when the magnetic field changes. You can listen for the magnetometerreading event.

Applications of the Magnetometer API

The Magnetometer API has various applications, including:


You can use the Magnetometer API to create a compass app that indicates the direction of the magnetic north.

Metal Detector

The Magnetometer API can be used to detect metal objects by measuring changes in the magnetic field.

Orientation Tracking

The Magnetometer API can be used to track the orientation of your device, relative to the magnetic north.


The Magnetometer API can be used in navigation apps to provide more accurate location data.


The Magnetometer API can be used in games to create realistic simulations and effects.