Skip to content
Permalink
afc5ef5f4a
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
36 lines (28 sloc) 859 Bytes
#!/usr/bin/env python3
# This code sample reads the current bearing of a CMPS12 Tilt-Compensated
# compass attached to the I2C interface on a Raspberry Pi.
# https://www.robot-electronics.co.uk/files/cmps12.pdf
# http://wiki.erazor-zone.de/wiki:linux:python:smbus:doc
# $ sudo apt-get install python3 python3-pip python-dev python-smbus
# $ sudo python3 -m pip install --upgrade pip setuptools wheel
# $ sudo pip3 install smbus
#!/usr/bin/env python3
#!/usr/bin/env python3
import smbus
import time
import statistics
bus = smbus.SMBus(1)
DEVICE_ADDRESS = 0x60
DEVICE_REG_BEARING = 0x01
values = []
while 1:
try:
bearing = bus.read_byte_data(DEVICE_ADDRESS, DEVICE_REG_BEARING)
bearing = int((bearing / 255) * 360)
values.append(bearing)
if len(values) > 10:
values.pop(0)
print(statistics.mode(values))
time.sleep(0.1)
except:
pass