# Access the magnetometer sensor

**URL:** <https://discourse.processing.org/t/access-the-magnetometer-sensor/33605>\
**Category:** Project Guidance\
**Created:** [November 19, 2021, 9:45am UTC](https://discourse.processing.org/t/access-the-magnetometer-sensor/33605 "2021-11-19T09:45:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sasharvrn](https://avatars.discourse-cdn.com/v4/letter/s/df788c/32.png) [@sasharvrn](https://discourse.processing.org/u/sasharvrn)\
**Post date:** [November 19, 2021, 9:45am UTC](https://discourse.processing.org/t/access-the-magnetometer-sensor/33605/1 "2021-11-19T09:45:34Z")

</div>

Hello, I am working on a project to display and test the phone’s sensors (accelerometer, gyroscope etc) by using p5.js. I can’t find any information to access the magnetometer sensor, is there something implemented in P5js ?

Thanks in advance 🙂

---

<div class="post-metadata">

**Author:** ![yaniki](https://avatars.discourse-cdn.com/v4/letter/y/c77e96/32.png) [@yaniki](https://discourse.processing.org/u/yaniki)\
**Post date:** [November 19, 2021, 4:44pm UTC](https://discourse.processing.org/t/access-the-magnetometer-sensor/33605/2 "2021-11-19T16:44:56Z")

</div>

Some browsers on some OSes are able to access magnetometer (via Javascript) and some not. It also depends on web server configuration. Here are some useful notes: [https://developer.mozilla.org/en-US/docs/Web/API/Magnetometer](https://developer.mozilla.org/en-US/docs/Web/API/Magnetometer)

---

<div class="post-metadata">

**Author:** ![sasharvrn](https://avatars.discourse-cdn.com/v4/letter/s/df788c/32.png) [@sasharvrn](https://discourse.processing.org/u/sasharvrn)\
**Post date:** [November 22, 2021, 9:43am UTC](https://discourse.processing.org/t/access-the-magnetometer-sensor/33605/3 "2021-11-22T09:43:55Z")

</div>

Hi, thank you for the response I will try with that method 🙂

---

<div class="post-metadata">

**Author:** ![fbouquet](https://avatars.discourse-cdn.com/v4/letter/f/45deac/32.png) [@fbouquet](https://discourse.processing.org/u/fbouquet)\
**Post date:** [January 3, 2022, 4:06pm UTC](https://discourse.processing.org/t/access-the-magnetometer-sensor/33605/4 "2022-01-03T16:06:41Z")

</div>

Hello,

I am working with sasharvrn, and we have not been successfull in accessing the magnetometer. We understand that this is something that is not supported by all browsers, but that it should be possible to access it using chrome. I made some tests on an Android smartphone + chrome browser, and could run the demo webpage here ([Sensor info](https://intel.github.io/generic-sensor-demos/sensor-info/build/bundled/)). So I know that my magnometer is alive and can talk to my browser when asked properly.

I am trying to replicate this using p5.js, following yaniki’s suggestions, tried different ways but none that worked. Here is a link to one of my tries 🙂

> **[p5.js Web Editor](https://editor.p5js.org/fbouquet/sketches/5aQanQ_e1)**
>
> A web editor for p5.js, a JavaScript library with the goal of making coding accessible to artists, designers, educators, and beginners.

(I will paste the code at the end of this message if you prefer not follow the link)

I am sure that I am missing something obvious, but I do not know my way well enough in javascript and p5 to see it. If anyone has a clue, I would appreciate it.

– Code Below –

```auto
let magX = 0;
let magY = 0;
let magZ = 0;
let magSensor;

function setup() {
  createCanvas(400, 400);
  magSensor = new Magnetometer();

  magSensor.addEventListener("reading", (e) => {
    magX = magSensor.x;
    magY = magSensor.y;
    magZ = magSensor.z;
  });

  magSensor.start();
}

function draw() {
  //frameRate(2);
  background(220);

  textSize(42);
  fill(255);
  noStroke();
  text("x : " + magX, 25, 50);
  text("y : " + magY, 25, 100);
  text("z : " + magZ, 25, 150);
}

```
