Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Code to read the Adafruit LSM303 sensor and send the readings to Pure Data
""" Python file to demonstrate reading the LSM303 and pass data to Pure Data
Author: Cameron MacLeod
Date: 2018/03/17
import time
import argparse
import Adafruit_LSM303
from math import atan2, pi
from pythonosc import udp_client
from pythonosc import osc_message_builder
# Parse command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('--ip', type=str, help="IP address of the OSC server")
parser.add_argument('--port', type=int, help="Port OSC server is listening on")
args = parser.parse_args()
# This is the IP and port of your Pure Data instance
PD_IP = ""
if args.ip is not None:
PD_IP = args.ip
PD_PORT = 8000
if args.port is not None:
PD_PORT = args.port
# This can be set to whatever you like and will be the address the OSC message
# is sent with
OSC_ADDR = "/readings"
# Create objects used to read LSM303 and communicate with remote OSC server
lsm303 = Adafruit_LSM303.LSM303()
client = udp_client.SimpleUDPClient(PD_IP, PD_PORT)
def compute_heading(mag_read):
""" Computes the compass heading from the magnetometer X and Y.
Returns a float in degrees between 0 and 360.
return ((atan2((mag_read[1] - -933), (mag_read[0]- 812)) * 180) / pi) + 180
# was (atan2(mag_read[0], mag_read[1]) * 180) / pi + 180 added brackets (unesc.) and swapped inputs to arcTan2.
def send_readings_to_pd(accel, mag, heading):
""" Sends compass, accelerometer and heading readings to PD
In the order acc_x, acc_y, acc_z, mag_x, mag_y, mag_z, heading
msg = osc_message_builder.OscMessageBuilder(address=OSC_ADDR)
msg.add_arg(accel[0], arg_type="i")
msg.add_arg(accel[1], arg_type="i")
msg.add_arg(accel[2], arg_type="i")
msg.add_arg(mag[0], arg_type="i")
msg.add_arg(mag[1], arg_type="i")
msg.add_arg(mag[2], arg_type="i")
msg.add_arg(heading, arg_type="f")
while True:
accel, mag =
heading = compute_heading(mag)
send_readings_to_pd(accel, mag, heading)
# Wait .075 seconds and repeat.

This comment has been minimized.

Copy link
Owner Author

commented Apr 20, 2018

This code assumes you have a PD script that can act as an OSC server. Setup steps below:

  1. Follow the instructions here to configure I2C on the pi.
  2. Install the LSM303 Python library as detailed here. Follow the "build from source" instructions to get the example code.
  3. Install python-osc with pip3 install python-osc
  4. Run your PD script
  5. Run python3


  • Ensure that the LSM303 is soldered correctly, you can test this with a multimeter's connectivity mode.
  • Ensure that I2C is working on the Pi by running sudo i2cdetect -y 1. If the LSM303 is plugged in correctly and working then the addresses 0x19 and 0x1e should show up.
  • The pins that need to be connected (LSM303->Pi) are as follows: Vin->5V, GND->GND, SDA->SDA1, SCL->SCL1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.