Jupyter Notebooks are a powerful cross-platform method for analyzing data using Python

You can definitely use Jupyter to plot, analyze and calibrate your sensor data. This method is the most powerful because you can do plotting and calculations. However, we assume you already have Jupyter installed (either desktop or thru Anaconda) and have some familiarity with running 'notebook' style Python!

This example runs on chips with at least 64 KB of flash, and will not fit on an UNO (Atmega328) or Leonardo (Atmega32u4) - try the simple calibration instead!

Step 1 - Download Calibration Notebook

The gyro/magnetometer notebook lives in the SensorLab Arduino library, in the notebooks folder.

Look for this section in the website:

You must open this notebook within Jupyter - you cannot run it direct from github or from the command line as a text file!

Once open, your browser will look like this:

Step 2 - Upload the SensorLab imucal Example

Next we have to tell the microcontroller board to send the magnetometer (and, if there is one, accelerometer and gyroscope) data out over serial in the right format.

Open up the Adafruit_SensorLab->calibration->imucal

Select your desired board & port from the Tools menu then click Upload

Open up the serial console, you'll see SensorLab initialization and detection of whatever magnetometer is available. In this case is a LIS3MDL, but any magnetometer can be calibrated!

You'll then see a stream of data that looks like:
Raw:-58,-815,8362,76,-121,-95,-375,-159,-24
Uni:-0.07,-0.98,10.00,0.0832,-0.1327,-0.1046,-37.50,-15.93,-2.50

The first three numbers are accelerometer data - if you don't have an accelerometer, they will be 0

The middle three numbers are gyroscope data - if you don't have an gyroscope, they will be 0

The last three numbers are magnetometer, they should definitely not be zeros!

Configure the notebook

Close the serial port, and go back to Jupyter. In the first cell, find where we define the PORT and change the port to match your serial/COM port. For windows it'll be something like COM4 for Mac/Linux it'll be like /dev/cu.USBSERIAL or something

Run the first cell so the serial port is set

Then run the second cell, you should see output like this - the serial port is opened and IMU raw data is output as numbers

If you get errors or no numbers, hard-reset the board (click the reset button once) then try re-running the cell again.

At the next cell we will perform the data capture. Move the board away from any strong magnets and run this cell

At the bottom of the cell you should start seeing a live plot of 3 circles - each one represents the X, Y and Z offsets. Keep spinning the board in various directions until you get 3 spheroids

Once you're happy, use the mouse to click on the graph. This will cause the data capture to stop

Run the next cell to perform the analysis. You'll get your X/Y/Z magnetic ranges and the final hard-offset calibration values.

In this case, the calibration is X=-5.21, Y=-7.7 and Z= 20.86

You'll also see the results of removing the offset, this should be 3 nearly-perfectly-superimposed circles with centers at 0,0

This guide was first published on Jan 26, 2020. It was last updated on Jan 26, 2020.

This page (Magnetic Calibration with Jupyter) was last updated on Nov 06, 2020.