Coder Social home page Coder Social logo

gabrielesartor / melopero_lsm9ds1 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from melopero/melopero_lsm9ds1

0.0 1.0 0.0 716 KB

A Python3 library for interfacing the Melopero LMS9DS1 9-DOF breakout board with a Raspberry Pi.

License: MIT License

Python 100.00%

melopero_lsm9ds1's Introduction

Melopero LSM9DS1 Python3 library

A library for interfacing the Melopero LSM9DS1 9-DOF breakout board with the Rasberry Pi.
If you were looking for the Arduino library click HERE

Melopero LSM9DS1 breakout board

melopero logo

Pinouts

Melopero LSM9DS1 Description
3V3 Input power pin. Apply 3.3V to this pin
SCL I2C or SPI Serial Clock pin
SDA I2C SDA pin or SPI MOSI pin
GND Ground pin
CSAG Accelerometer/Gyroscope SPI Chip Select
CSM Magnetometer SPI Chip Select
SDOAG Accelerometer/Gyroscope SPI MISO pin
SDOM Magnetometer SPI MISO pin
INT1 Accelerometer/Gyroscope Interrupt pin
INTM Magnetometer Interrupt pin
INT2 Another Interrupt pin for the Accelerometer/Gyroscope.
This pin is not supported in our library
DEN Gyroscope data enable pin.
This pin is not supported in our library
DRDY Magnetometer data ready pin.
This pin is not supported in our library

Getting Started

Prerequisites

You will need:

Install the library

You can install the melopero-lsm9ds1 module by typing this line in a terminal window:

sudo pip3 install melopero-lsm9ds1

Connect the sensor to the Raspberry Pi
(use only 3.3V power and logic, do not connect this sensor board directly to 5V)

You can find a description of the GPIO connector of the Raspberry Pi HERE
This sensor communicates over I2C or SPI.
I2C connections:

Melopero LSM9DS1 Raspberry Pi
3V3 3.3V
SCL SCL
SDA SDA
GND GND

SPI connections:
Melopero LSM9DS1 Raspberry Pi
3V3 3.3V
SCL SCLK
SDA MOSI
SDOAG, SDOM MISO
CSAG CE_0 (BOARD pin 24, GPIO 8)
CSM CE_1 (BOARD pin 26, GPIO 7)

Optional interrupt pins:
Melopero LSM9DS1 Raspberry Pi
INT1
(Interrupt pin for Accelerometer and Gyroscope)
Any pin that supports interrupts
INTM
(interrupt pin for Magnetometer)
Any pin that supports interrupts

Usage

Import the melopero_lsm9ds1 and time modules in your Python3 script:

import melopero_lsm9ds1 as mp
import time

Create and initialize the sensor object

sensor = mp.LSM9DS1()

Select I2C or SPI as communication bus.
To select I2C (if no parameters are given the default i2c addresses will be used):

sensor.use_i2c()
#sensor.use_spi(gyro_cs, mag_cs) to use spi

If you want to use SPI bus, first you must initialize gyro_cs (CSAG pin on the sensor) and mag_cs (CSM on the sensor)
Note: the module uses GPIO.BOARD pin numbers:

gyro_cs = 24 #Chip Enable 0 (CE_0) pin on the Raspberry Pi
mag_cs = 26 #Chip Enable 1 (CE_1) pin on the Raspberry Pi  

Now select SPI bus

sensor.use_spi(gyro_cs, mag_cs)

Set the output data rate

sensor.set_gyro_odr(mp.LSM9DS1.ODR_119Hz)
sensor.set_acc_odr(mp.LSM9DS1.ODR_119Hz)
sensor.set_mag_odr(mp.LSM9DS1.MAG_ODR_20Hz)

Print out 100 measurments at a .5s interval

for _ in range(100):
    #read the data from the sensor
    gyro_measurements = sensor.get_gyro()
    acc_measurements = sensor.get_acc()
    mag_measurements = sensor.get_mag()

    print("*" * 20)
    print("[Gyro]: {:.2f} dps {:.2f} dps {:.2f} dps".format(*gyro_measurements))
    print("[Accelerometer]: {:.2f} g {:.2f} g {:.2f} g".format(*acc_measurements))
    print("[Magnetometer]: {:.2f} G {:.2f} G {:.2f} G".format(*mag_measurements))
    print("*" * 20)

    time.sleep(.5)
    

Close the connection after using the sensor

sensor.close()

Example using I2C bus

The following example will Print out 100 measurments of the accelerometer, gyroscope and magnetometer at a .5s interval.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: Leonardo La Rocca
"""

#First you need to import the module
import melopero_lsm9ds1 as mp
import time

#Then you create and initialize the sensor object
sensor = mp.LSM9DS1()

#You can set the sensor to use i2c or spi
sensor.use_i2c() #if no parameters are given the default i2c addresses will be used
#sensor.use_spi(gyro_cs, mag_cs) to use spi

#next you can set the output data rate you like
sensor.set_gyro_odr(mp.LSM9DS1.ODR_119Hz)
sensor.set_acc_odr(mp.LSM9DS1.ODR_119Hz)
sensor.set_mag_odr(mp.LSM9DS1.MAG_ODR_20Hz)

#now you can read the measurements!
#Print out 100 measurments at a .5s interval

for _ in range(100):
    #read the data from the sensor
    gyro_measurements = sensor.get_gyro()
    acc_measurements = sensor.get_acc()
    mag_measurements = sensor.get_mag()

    print("*" * 20)
    print("[Gyro]: {:.2f} dps {:.2f} dps {:.2f} dps".format(*gyro_measurements))
    print("[Accelerometer]: {:.2f} g {:.2f} g {:.2f} g".format(*acc_measurements))
    print("[Magnetometer]: {:.2f} G {:.2f} G {:.2f} G".format(*mag_measurements))
    print("*" * 20)

    time.sleep(.5)
    
#Close the device correctly!
sensor.close()

Example using SPI bus

The following example will Print out 100 measurments of the accelerometer, gyroscope and magnetometer at a .5s interval.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: Leonardo La Rocca
"""

#First you need to import the module
import melopero_lsm9ds1 as mp
import time

#Then you create and initialize the sensor object
sensor = mp.LSM9DS1()

#Gyro and Mag chip select pins
#Note: the module uses GPIO.BOARD pin numbers
gyro_cs = 24 #Chip enable 0 pin on raspberry pi
mag_cs = 26 #Chip enable 1 pin on raspberry pi  

#Setup the device to use SPI communication protocol
sensor.use_spi(gyro_cs, mag_cs) 

#next you can set the output data rate you like
sensor.set_gyro_odr(mp.LSM9DS1.ODR_119Hz)
sensor.set_acc_odr(mp.LSM9DS1.ODR_119Hz)
sensor.set_mag_odr(mp.LSM9DS1.MAG_ODR_20Hz)

#now you can read the measurements!
#Print out 100 measurments at a .5s interval

for _ in range(100):
    #read the data from the sensor
    gyro_measurements = sensor.get_gyro()
    acc_measurements = sensor.get_acc()
    mag_measurements = sensor.get_mag()

    print("*" * 20)
    print("[Gyro]: {:.2f} dps {:.2f} dps {:.2f} dps".format(*gyro_measurements))
    print("[Accelerometer]: {:.2f} g {:.2f} g {:.2f} g".format(*acc_measurements))
    print("[Magnetometer]: {:.2f} G {:.2f} G {:.2f} G".format(*mag_measurements))
    print("*" * 20)

    time.sleep(.5)
    
#Close the device correctly!
sensor.close()

Attention:

The module is written in python3 and by now supports only python3, remember to use always pip3 when you install the module and python3 when you run the code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.