Tag Archives: BerryIMU

Using the BerryIMUv3 on a Raspberry Pi Pico with MicroPython

In this guide we will show how to get the a BerryIMUv3 working with a Raspberry Pi Pico, using MicroPython. This code example supports I2C and SPI.

Hook up Guide

The two images below show how to hook up the BerryIMUv3 via I2C or SPI. (If using SPI, you will need to place a solder "blob" on JP7 on the BerryIMUv3 to complete the SPI connection, as shown here)

 

Raspberry Pi Pico and BerryIMU
Raspberry Pi Pico and BerryIMv3 via SPI

 

Raspberry Pi Pico and BerryIMU
Raspberry Pi Pico and BerryIMv3 via I2C

Thonny and the MicroPython code

The MicroPython code can be downloaded from our GitHub repository. The code for this example can be found under the PicoMicroPython directory.

We will be using Thonny to program the Raspberry Pi Pico.

If you are new to the Raspberry Pi Pico and Thonny, we suggest viewing these excellent tutorials from our friends at Core Electronics;
1. Getting started with Raspberry Pi Pico
2. Pico and Thonny

 

The sample code supports both I2C and SPI communications.  Comment out the protocol which will not be used as shown below. Below we have commented out I2C as we will be using SPI

import utime
import math
from LSM6DSL import *
import machine
#Comment out one of the below lines
import IMU_SPI as IMU
#import IMU_I2C as IMU

 

BerryIMU Pico Thonny
BerryIMU Pico Thonny

 

Connecting BerryIMUv3 via SPI to a Raspberry Pi

The accelerometer and gyroscope on the BerryIMUv3 can output data at a rate of 6,664 times a second!  I2C is too slow to read the output at this rate, this is where SPI comes in.

Buy default, BerryIMUv3 is setup to use I2C.  You can complete the SPI interface by placing a solder blob on jumper 7 (JP7).

BerryIMU SPI
Blob on jumper 7

 

SPI uses 4 pins,  and depending on what device you are using these pins could be named differently, which causes confusion.

The most common pin names are;

  • MOSI  (Master out Slave In)
  • MISO  (Master In Slave Out
  • SCLK (Serial Clock)
  • CS (chip select)  This is CE0 or CE1 on the Raspberry PI.

These pins have been highlighted below

Raspberry Pi SPI pins
Raspberry Pi 40 pin header

 

On the BerryIMUv3, they are called;

  • SDI (Slave Data In)
  • SDO (Slave Data Out)
  • SPC (Serial Port Clock)
  • CS (Chip Select)

When we connect a BerryIMUv3 to a Raspberry Pi using SPI, the Raspberry Pi will be acting as a master and the BerryIMUv3 will be acting as a slave. This is how they are connected logically.

BerryIMU SPI
BerryIMU and Raspberry Pi SPI

Here is the physical wiring

Raspberry Pi BerryIMU SPI
Raspberry Pi SPI and BerryIMUv3

 

You can enable SPI on the Raspberry Pi using raspi-config

pi@raspberrypi ~ $ sudo raspi-config

Raspberry Pi SPIGo into "Interfacing Options"

Raspberry Pi SPI
Then select "SPI"

When asked if you want to enable SPI, select "yes"

 

The code for SPI can be found here https://github.com/ozzmaker/BerryIMU/tree/master/python-BerryIMUv3-SPI

Double tap detection with BerryIMUv3

The accelerometer(LSM6DSL) on the BerryIMUv3 has built in double tap detection, which makes it very easy to detect double taps without the need for any fancy code.

double tap IMU

When the LSM6DSL detects a double tap, it can fire an interrupt pin on the BerryIMUv3.  We will use a Raspberry Pi to monitor the interrupt pin and turn a LED off and on when a double-tap is detected.

 

Double-Tap event recognition has special registers which control tap recognition functionality, these are the tap threshold and the Shock, Quiet and Duration time windows

Double-tap event recognition
Double-tap event recognition

 

The Raspberry Pi will configure the BerryIMUv3 for double tap recognition. It will also monitor for double taps, which will be used to turn a LED on and off.

 

INT1 On the BerryIMUv3 will go high when a double tap is detected.


GPIO18
(physical pin 12) on the Raspberry Pi will be used to monitor INT1 , using an interrupt.

GPIO20 (physical pin 28) will be used to drive the LED.

The resister below is 330 Ohms

Here is the hock up diagrams

BerryIMU double-tap using QWIIC cable
BerryIMU double-tap using QWIIC cable
BerryIMU double-tap
BerryIMU double-tap
import signal
from LSM6DSL import *
import sys
import RPi.GPIO as GPIO 
import smbus
bus = smbus.SMBus(1)
LED_ON = 0                                                           #Used to track of the current state of the LED
INTERRUPT_PIN = 12                                                   #The interrupt pin which will be connected to the IMU
LED_PIN = 38                                                         #The pin which will be driving the LED
#Used to clean up when Ctrl-c is pressed
def signal_handler(sig, frame):
    GPIO.cleanup()
    sys.exit(0)
    
#Used to write to the IMU
def writeByte(device_address,register,value):
    bus.write_byte_data(device_address, register, value)


def LEDnotification(channel):
	global LED_ON
	if LED_ON:
		GPIO.output(LED_PIN,0)
		LED_ON = 0
	else:
		GPIO.output(LED_PIN,1)
		LED_ON = 1

writeByte(LSM6DSL_ADDRESS,LSM6DSL_CTRL1_XL,0b01100000)                 #ODR_XL = 416 Hz, FS_XL = +/- 2 g
writeByte(LSM6DSL_ADDRESS,LSM6DSL_TAP_CFG,0b10001110)                  #Enable interrupts and tap detection on X, Y, Z-axis
writeByte(LSM6DSL_ADDRESS,LSM6DSL_TAP_THS_6D,0b10001100)               #Set tap threshold
writeByte(LSM6DSL_ADDRESS,LSM6DSL_INT_DUR2,0b01111111)                 #Set Duration, Quiet and Shock time windows
writeByte(LSM6DSL_ADDRESS,LSM6DSL_WAKE_UP_THS,0b10000000)              #Double-tap enabled 
writeByte(LSM6DSL_ADDRESS,LSM6DSL_MD1_CFG,0b00001000)                  #Double-tap interrupt driven to INT1 pin
        
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(INTERRUPT_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.output(LED_PIN, 0)
GPIO.add_event_detect(INTERRUPT_PIN, GPIO.RISING, callback=LEDnotification, bouncetime=300)

while True:
    signal.signal(signal.SIGINT, signal_handler)
    signal.pause()

We will cover specific code which relates to double-tap recognition.

Line 37 , LSM6DSL_TAP_CFG is used to enable tap recognition on the X, Y, Z directions.  It is also used to enable the interrupt function for double-tap recognition.

Line 38 , LSM6DSL_TAP_THS_6D is used to set the tap thresholds. a lower value will result in softer taps being detected.

Line 39 , LSM6DS_INT_DUR2 is used to set the duration, quiet and shock time window.  A larger duration will result in a longer time between 1st and 2nd tap.

Line 40, LSM6DSL_WAKE_UP_THS. Set the left most bit to enable double tap recognition.

Line 41,  LSM6DSL_MD1_CFG is used to set which interrupt pin ont he BerryIMUv3 is used. In this instance, it is set to INT1.

 

 

 

 

 

 

 

New BerryIMU version

We are now selling version 3 of the BerryIMU.

BerryIMU Raspberry Pi

 

BerryIMUv3 has been updated with the latest sensors, resulting in lower noise and a faster output rate (up to 6,664 times a second!).

We have also included level shifters for 5V MCUs. Which means you can safely connect the BerryIMUv3 directly to an Arduino.

 

BerryIMU Arduino

 

BerryIMUv3 is compatible with the SparkFun QWIIC echo system.

 

We sell a QWIIC connector and cable for the Raspberry here.  This does away with the need to solder headers onto the BerryIMUv3 when connecting to a Raspberry Pi.

QWIIC connector Raspberry Pi

Some of the features.

  • Gyroscope and accelerometer output rates of 6.7KHz (6,664 times a second!)
  • Detect tilt, tap and double tap
  • Pedometer, step detector and step counter
  • Interrupt pins
  • Read temperature
  • supports both 3.3V and 5V
  • I2C and SPI
  • “Always-on” experience with low power
    consumption for both accelerometer and gyroscope

 

BerryIMU

 

 

Raspberry Pi Embedded Cap With GPS & 10DOF

In this post we will show you how to geotag and capture the "attitude"  of photos taken with the Raspberry Pi camera and record these values within the photo itself using EXIF metadata

We used a modified (hacked?) cap to take the images in this post. The cap took photos, geo-tagged and recorded attitude as we walked around Sydney Harbour.

Components used were;

The BerryGPS-IMU was used to capture the GPS coordinates as well as "attitude".   No external antenna was needed as the BerryGPS-IMU includes an internal antenna.

The "attitude" would include values such as pitch, roll, direction. Some of this data you can see annotate in the image below.


raspberry pi camera gps

Other programs can use some of this data to plot the image on a map and even show the direction of the camera at the time the image was taken.  A good example of this is seen in  GeoSetter

Camera attitude

 

The Cap

The cap has the BerryGPS-IMU sitting on top of the visor, with the Raspberry Pi sitting under the viso.  Some holes where made in the visor to allow connectivity between the BerryGPS-IMU and Raspberry Pi.
We also created a basic camera mount out of 3mm laser cut acrylic. M2.5 Nylon screws were used to hold everything in place.
Raspberry Pi GPS

 

Continue reading Raspberry Pi Embedded Cap With GPS & 10DOF

BerryIMU running on Raspberry Pi running Windows IoT

BerryIMU also works great with Windows IoT Core on the Raspberry Pi.

Our Git repository contains the source files needed to get the BerryIMU up and running on Windows IoT.

The code will print out the following values to the screen;

  • Raw values from the gyroscope, accelerometer and magnetometer.
  • Accelerometer calculated angles.
  • Gyro tracked angles.
  • Fused X and Y angles.
  • Heading.
  • Tilt compensated heading.

Connecting BerryIMU to a Raspberry Pi

BrryIMU can connect via the jumper cables to the Raspberry Pi as shown below;
Raspberry Pi BerryIMU

IMU Raspberry Pi Accelerometer gyro

Or BerryIMU can sit right on top of the GPIO pins on a Raspberry Pi. The first 6 GPIOs are used as shown below.

IMU Raspberry Pi Accelerometer gyro

Get the Code

Download the BerryIMU code for Windows IoT from our GIT repository. The files you need are under the WindowsIoT-BerryIMU folder.

You will need to download the entire git repository as GIT doesn't allow downloading individual folders.

Once downloaded, double-click  the file WindowsIoT-BerryIMU.sln to open up the project in Visual Studio.

 

About the code

The project code outputs all of the needed values to the screen and a complementary filter is used to fuse the accelerometer and gyroscope angles.

We have a number of guides already documented on how to get the BerryIMU working with the Raspberry Pi.
https://ozzmaker.com/berryimu/
These are based on Raspbian, however the principals and math are the same for Windows Iot.

The final values which should be used are the fused X &Y angles and the tilt compensated heading.

The sensor on the BerryIMU is the LSM9DS0 and all the I2C registers for this sensor can be found in LSM9DS0.cs

The main code can be found in MainPage.xaml.cs

 

BerryIMU Raspberry Pi Gyroscope Accelerometer

Complementary Filter

A complementary filter is used to fuse the angles. Is summary, the complementary filter trusts the gyroscope for short periods and trusts the accelerometer for longer periods;

CFangleX = AA * (CFangleX + (rate_gyr_x * DT / 1000)) + (1.0f - AA) * AccXangle;
CFangleY = AA * (CFangleY + (rate_gyr_y * DT / 1000)) + (1.0f - AA) * AccYangle;

Changing how much trust is given for each of the sensors can be changed by modify the complementary filter constant at the start of the code.

const float AA = 0.03f;     // Complementary filter constant

Loop Speed

The loop speed is important as we need to know how much time has past to calculate the rotational degrees per second on the gyroscope.
A time delta is set at the start of the code.

const int DT = 100;         //DT is the loop delta in milliseconds.

This is then used to specify a new timer method.

periodicTimer = new Timer(this.TimerCallback, null, 0,DT);

Here you can see where DT is used to keep track of the gyroscope angle. You can also see it in the above calculation for the complementary filter.

//Calculate the angles from the gyro
gyroXangle += rate_gyr_x * DT / 1000;
gyroYangle += rate_gyr_y * DT / 1000;
gyroZangle += rate_gyr_z * DT / 1000;

BerryIMU orientation

The calculations in the code are based on how the BerryIMU is orientated. If BerryIMU is upside down, then some of the angles need to be reversed. It is upside down when the skull logo is facing up(or to the sky).
If it is upside down, set the below value to true. Otherwise, set it to false.

bool IMU_upside_down = true;

 

 

 

[wp_ad_camp_1]

.

BerryIMU Code for Teensy

Our BerryIMU GIT repository has been updated with code for the Teensy, specifically Teensy 3.6.
teensy accelerometer gyroscopeNow you can have access to an accelerometer, gyroscope, compass, temperature and pressure sensor on your Teensy.

Here you can see the angles displayed using the Serial Plotter in the Arduino IDE which is connected to a Teensy 3.6.

 

 

Teensy 3.6  Hookup

teensy accelerometer gyroscope

Connect SCL on the BerryIMU to PIN 19
Connect SDA on the BerryIMU to PIN 18
Use 3.3v pin on the Teensy to power the BerryIMU.

 

 

Code

Download the BerryIMU code from our GIT repository. The files you need are under the Teensy-BerryIMU folder.

TeenysDuino
We use the Arduino IDE to program the Teensy.  You will need to install Teensduino so that your Arduino IDE supports Teensy.

You can download it from this link;
https://www.pjrc.com/teensy/td_download.html

 

BerryIMU Raspberry Pi Gyroscope Accelerometer

 

Arduino Serial plotter

The latest version of Arduino IDE includes a Serial Plotter. This is great to show angles in a sliding graph.

The below image shows how to access the Serial Plotter.

Arduino Serial Plotter

The Serial Monitor will have to be closed as both cannot be opened at the same time.

The Serial Plotter expects the values to be separated with a space. To show the X and Y angles on the plotter, comment out the print statements in the existing code and insert the code below.

  Serial.print(AccXangle);
  Serial.print("\t");
  Serial.print(AccYangle);

Continue reading BerryIMU Code for Teensy

Record Temperature and Pressure with ESP8266 and BerryIMU

In this guide, we will be using the ESP8266 and BerryIMU to record temperature and pressure and display the output onto a web page.

Google charts is used to display the data in an easy to read format.

ESP8266 Record temperature
ESP8266 Record temperature

Adafruit Feather Hazzuh! and BerryIMU
Adafruit Feather Hazzuh! and BerryIMU

Components used are;

Summary

  • NTP is used to get the correct time of day.
  • The free available RAM is calculated to work out how much data we can store,
  • Google charts is used to display chart data.
  • 4 web pages are created:
    "/" - home page which is used to display the gauges.
    "/chart.html" - Is used to display chart.
    "/table.html" - Is used to display the data in a table.
    "/data.json"- Is used by the home page to update the the gauge values.

Hook Up

The below diagram shows how to connect a BerryIMU to an ESP8266 microcontroller, in this case it is the  Adafruit Feather Huzzah .

Adafruit Huzzah IMU
Adafruit Huzzah and BerryIMU

 

Prepare Arduino IDE

The Arduino IDE needs to be updated with the board packages for the ESP8266.  This is very easy to do and both Sparkfun & Adafruit have detailed guides on how to do this;
Sparkfun Arduino IDE and ESP8266
Adafruit Arduino IDE and ESP8266

Continue reading Record Temperature and Pressure with ESP8266 and BerryIMU

ESP8266 and a BerryIMU – simple web server

The ESP8266 is another good microcontroller which can be used with the BerryIMU.

The ESP8266 is small and includes Wifi.

 

In this guide we will setup of the ESP8266 to provide a web page which we can then use to read the  accelerometer, gyroscope and compass values from the BerryIMU. We will also force this webpage to refresh every 1 seconds.

Accessing the ESP8266 from an iPhone

 

 

The ESP8266 Arduino core to program our ESP8266. This allows you to use the Arduino IDE to program and upload to the ESP8266.

We have used the Adafruit Feather Huzzah and the Sparkfun Thing Dev board in this guide. Both are excellent boards with included USB to serial converters. Just plug in and upload. This guide can also be used with other ESP8266 boards, just take note of the pins used.

Sparkfun Thing and BerryIMU
Sparkfun Thing and BerryIMU
Adafruit Feather Hazzuh! and BerryIMU
Adafruit Feather Hazzuh! and BerryIMU

Hook Up

The below diagrams show how to connect a BerryIMU to an ESP8266 microcontroller, in this case the  Adafruit Feather Huzzah and the Sparkfun Thing Dev board.

 

Adafruit Huzzah IMU
Adafruit Huzzah and BerryIMU
Sparkfun Thing IMU
Sparkfun Thing Dev and BerryIMU

 

Prepare Arduino IDE

The Arduino IDE needs to be updated with the board packages for the ESP8266.  This is very easy to do and both Sparkfun & Adafruit have detailed guides on how to do this;
Sparkfun Arduino IDE and ESP8266
Adafruit Arduino IDE and ESP8266

BerryIMU Raspberry Pi Gyroscope Accelerometer

The Code

The code can be found here . Download the entire Git repo. The code for this guide can be found under the directory
ESP8266-BerryIMU/BerryIMU_ESP8266_simple_web/
The file you load into the Arduino IDE is BerryIMU_ESP8266_simple_web.ino.

This guide will only cover the specific to the ESP8266. There is another guide here https://ozzmaker.com/berryimu/ which covers the code used to calculate the angles and heading from the BerryIMU.

The first thing to do is update the code with your wireless network settings.

const char* ssid = "******";
const char* password = "***************";

Further down you can see where we define the web server and what port to listen on

ESP8266WebServer server(80);

There is then a function called handleroot(). This is what builds the web page and sends it to the client when the client requests it E.g. When a web browser requests for a page.
Looking at the line which contains the meta tag, you can see where the refresh timer is set to 1 seconds.

I have also hilighted the variables which store the angles and heading from the BerryIMU.

void handleroot()
{
  //Create webpage with BerryIMU data which is updated every 1 seconds
  server.sendContent("HTTP/1.1 200 OK\r\n"); //send new p\r\nage
  server.sendContent("Content-Type: text/html\r\n");
  server.sendContent("\r\n");
  server.sendContent
  ("<html><head><meta http-equiv='refresh' content='1'</meta>"
  "<h3 style=text-align:center;font-size:200%;color:RED;>BerryIMU and ESP8266</h3>"
  "<h3 style=text-align:center;font-size:100%;>accelerometer, gyroscope, magnetometer</h3>"
  "<h3 style=text-align:center;font-family:courier new;><a href=https://ozzmaker.com/ target=_blank>https://ozzmaker.com</a></h3><hr>");
  server.sendContent
  ("<h2 style=text-align:center;> Filtered X angle= " + String(<strong><span style="color: #ff0000;">CFangleX</span></strong>));
  server.sendContent
  ("<h2 style=text-align:center;> Filtered Y angle= " + String(<span style="color: #ff0000;"><strong>CFangleY</strong></span>));
  server.sendContent
  ("</h2><h2 style=text-align:center;> Heading = " + String(<span style="color: #ff0000;"><strong>heading</strong></span>));
  server.sendContent
  ("</h2><h2 style=text-align:center;> Tilt compensated heading =  " + String(<strong><span style="color: #ff0000;">headingComp</span></strong>));
}

Within setup(), we define what pins are used for I2C to communicated with the BerryIMU.

Wire.begin(4,5);

The first value is the SDA pin and the second specifies the SCL pin. Any pin on the ESP8266 can be used for I2C.

Wireless is then enabled  and then we try and connect to the wireless network. The IP address is then printed to the serial console.

  WiFi.begin(ssid, password);
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  //Print IP to console
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  delay(3);

And finally, the web server is started and match on root of the web server and then run handleroot().

server.on("/", handleroot);

You need to add the below line in the main loop to handle web requests.

server.handleClient(); //Handler for client connections

[wp_ad_camp_3]

Converting values from an Accelerometer to G

In this post I will show how to convert the raw values read from an accelerometer to 'Gs'.

Git repository here
The code can be pulled down to your Raspberry Pi with;

pi@raspberrypi ~ $ git clone https://github.com/ozzmaker/BerryIMU.git

The code for this guide can be found under the python-BerryIMU-measure-G directory.

 

An accelerometer measures proper acceleration, which is the acceleration it experiences relative to freefall. This is most commonly called "G-Force" (G)

For example, an accelerometer at resting on a table would measure 1G ( 9.81 m/s2) straight upwards. By contrast, accelerometers in free fall and accelerating due to the gravity of Earth will measure zero.

The accelerometer used by the BerryIMU is a MEMS sensors(LSM9DS0), which outputs the raw readings as mg/LSB.
Most MEMS accelerometers use this output format.

mg = milli-G's (just like milliliters)
1mG = 0.001 G's of acceleration, so 1000mG = 1G.
LSB = Least Significant bit, which is the last bit on the right.

BerryIMU Raspberry Pi Gyroscope Accelerometer

The LSM9DS0 outputs a 16 bit value for the accelerometer readings.

If you look at the characteristics of the LSM9DS0 in the datasheet, you can see the sensitivity levels for the accelerometer highlighted in red below and the corresponding values for the LSB, which are highlighted in blue. You can download the datasheet here;

The raw values from the accelerometer are  multiplied by the sensitive level to get the value in G.

LSM9DS1 BerryIMU

Let's use FS ±2 g as an example sensitivity level.  As the range is -2 to +2, this would be a total of 4g.  Or 4,000 Milli-Gs.
The output is 16 bits. 16 bits equals 65,535.   This means we can get 65,535 different readings for the range  between -2 and +2. (or -2,000 MilliGs and +2,000 MilliGs)

 4,000 MilliGs / 65,535 = 0.061

Each time the LSB changes by one, the value changes by 0.061, which is the value highlighted in blue in the table above.

For FS ±8 g, the range would be -8 to +8, which is a total of 16,000 MilliGs.
 16,000 MilliGs / 65,535 = 0.244

Example when using ±2g sensitivity
In the table below, every time the raw values increments by one, the final calculated value(which is MilliG) increments by 0.061

RAW  	BINARY	LSB value for +/-2G	Calc MilliG
16	10000		0.061		0.976
17	10001		0.061		1.037
18	10010		0.061		1.098

The above values of 16,17 and 18 above a very low and only used for illustration.
If your accelerometer is horizontal and resting and at rest when using a sensitive level of ±2g, the raw value for Z should hover  around 16,500.
16,500 X 0.061 = 1006 MilliGs or 1G

Example when using ±8g sensitivity
In the table below, every time the raw values increments by one, the final calculated value(which is MilliG) increments by 0.244

RAW  	BINARY	LSB value for +/-2G	Calc MilliG
16	10000		0.244		3.904
17	10001		0.244		4.148
18	10010		0.244		4.392

If you accelerometer is horizontal and at rest, when using a sensitive level of ±8g, the raw value for Z should hover  around 4,475.

4,175
 X 0.244 = 1018.7 MilliGs or 1G

 

The Code

The two above examples are easy to implement in python;
±8g Sensitivity

writeACC(CTRL_REG2_XM, 0b00010000) #+/- 8G full scale
print("G Value for Z axis %f G" % ((ACCz * 0.244)/1000))

The first line above is used to initialise the accelerometer with a sensitivity level of ±2g.
The second line prints the calculated value as G using the raw values from the accelerometer.

±2g Sensitivity

writeACC(CTRL_REG2_XM, 0b00000000) #+/- 2G full scale
print("G Value for Z axis %f G" % ((ACCz * 0.061)/1000))

The first line above is used to initialise the accelerometer with a sensitivity level of ±2g.
The second line prints the calculated value as G uses using raw values from the accelerometer.
Below is a snippet from the main program;

import smbus
import time
import math
from LSM9DS0 import *
import datetime
bus = smbus.SMBus(1)


def writeACC(register,value):
        bus.write_byte_data(ACC_ADDRESS , register, value)
        return -1


def readACCx():
        acc_l = bus.read_byte_data(ACC_ADDRESS, OUT_X_L_A)
        acc_h = bus.read_byte_data(ACC_ADDRESS, OUT_X_H_A)
	acc_combined = (acc_l | acc_h <<8)
	return acc_combined  if acc_combined < 32768 else acc_combined - 65536

def readACCy():
        acc_l = bus.read_byte_data(ACC_ADDRESS, OUT_Y_L_A)
        acc_h = bus.read_byte_data(ACC_ADDRESS, OUT_Y_H_A)
	acc_combined = (acc_l | acc_h <<8)
	return acc_combined  if acc_combined < 32768 else acc_combined - 65536

def readACCz():
        acc_l = bus.read_byte_data(ACC_ADDRESS, OUT_Z_L_A)
        acc_h = bus.read_byte_data(ACC_ADDRESS, OUT_Z_H_A)
	acc_combined = (acc_l | acc_h <<8)
	return acc_combined  if acc_combined < 32768 else acc_combined - 65536


	
#initialise the accelerometer
writeACC(CTRL_REG1_XM, 0b01100111) #z,y,x axis enabled, continuos update,  100Hz data rate
writeACC(CTRL_REG2_XM, 0b00011000) #+/- 8G full scale

while True:
	
	
	#Read the accelerometer,gyroscope and magnetometer values
	ACCx = readACCx()
	ACCy = readACCy()
	ACCz = readACCz()
	print("##### X = %f G  #####" % ((ACCx * 0.244)/1000)),
	print(" Y =   %fG  #####" % ((ACCy * 0.244)/1000)),
	print(" Z =  %fG  #####" % ((ACCz * 0.244)/1000))
	
	
	#slow program down a bit, makes the output more readable
	time.sleep(0.03)

 

Guides and Tutorials