Skip to Content
Raspberry Pi 3 Cookbook for Python Programmers - Third Edition
book

Raspberry Pi 3 Cookbook for Python Programmers - Third Edition

by Steven Lawrence Fernandes, Tim Cox
April 2018
Beginner content levelBeginner
552 pages
13h 58m
English
Packt Publishing
Content preview from Raspberry Pi 3 Cookbook for Python Programmers - Third Edition

Calibrating the compass

The compass needs to be calibrated in order to report values that are centered and equalized. This is needed because there are magnetic fields all around; by calibrating the sensor, we can cancel out the effect of any localized fields.

By measuring the readings of the compass on all axes, we can determine the minimum and maximum values for each axis. This will allow us to calculate the mid-point of the readings, and also the scaling, so that each axis will read the same value whenever it is facing the same way.

Add the following code at the top of the file (after the import statements):

CAL=100 #take CAL samples 

Add the following code to __init__(self) of the compass class:

 self.offset,self.scaling=self.calibrateCompass() ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Raspberry Pi for Python Programmers Cookbook - Second Edition

Raspberry Pi for Python Programmers Cookbook - Second Edition

Tim Cox

Publisher Resources

ISBN: 9781788629874Supplemental Content