Enviro Phat need calibrating?


I’m charting the output from the motion sensor on the envirophat but have noticed the X & Y axis appear to be out somewhat (assuming the unit is not moving on the ground). See attached example chart. I would’ve assumed the X & Y would be nearly identical assuming no movement? I have modified the Z axis by -1 to compensate for earth’s gravity.


Could it just be noise, or at least tiny vibrations? They’re very tiny numbers on your y-axis.


Possibly, i’ve attached another PNG from tonight which shows them more ‘aligned’, perhaps accounting for daytime ‘noise’. As I understand it g-force in terms of numbers is minute.

Overall i’m please with it and what i’ve done - I have a graph that shows movements (because if I shake the thing around the graph goes ballistic) and relates the g-force readings from the device into a human readable scale (Mercalli Scale). All this in Python which i’ve never used before (damn those indentations) and hacking about some javascript which I can just about do.

Edit: I’m interested in finding out more about how to get / control readings from this sensor, but a bit confused at this stage by the HEX codes and ‘bits’ from the chip data sheet and the Pimoroni library


Oooh! I applaud you for being interested in how to work closer with the sensor. This might turn into a long topic!

You should familiarise yourself with the concept of bits/bytes etc first if you’ve never encountered low-level programming before, this isn’t a bad resource: http://bob.cs.sonoma.edu/IntroCompOrg-RPi/sec-bits.html

Then we can explore how to interpret a datasheet (for a simple i2c device anyway) and break down the lsm303d datasheet into manageable chunks.


Hello! Going to reopen this thread, and I started a separate thread on filtering digital noise which is related to what I am trying to achieve with the accelerometer. I have the following ideas in mind, but lack the knowledge to integrate them:

  1. According to the enviro lsm303d.py script, the accel sensor is fixed at 50hz:Line 130 - I might need to go lower, or higher, not sure yet.

  2. Can I override this sensor setting? According to the manual:Page 35 I can, from 3.125 Hz to a max of 1600 Hz

  3. How would I override this setting, a two-fold problem

    3.1 I don’t know how to convert 0100 (for example) to HEX

    3.2 Can I override the setting within my script, or do I have to change lsm303d.py?

  4. I’ve read a few articles online that establish a Hz frequency for local traffic. If I can fix the sensor as-close-to 1000hz (for example) in my data logging script do I need to synchronize the same for when the script actually takes a reading and stores it in MySQL? E.g. if I know the sensor is setup to take 1000 readings per second, do I need to do 1000 MySQL INSERTS per second? Imagine I would need to, otherwise there is no point?


Addressing points 1 - 3 above, Using binary <-> hex converter, 57 = 0101 0111 which relates to the 8 registers needed to set settings for CTRL_REG1, so we have 50hz, continious update, all axes enabled.

So if I want to set the sensor to a freq of 800hz I need 1001 0111 which using the online converter = 97. So my value for CTRL_REG1 = 0x97. I’ve managed to locate the enviro setup script and have hardcoded 800hz into the driver setup

pi@raspberrypi:/usr/bin $ pip show envirophat
Name: envirophat
Version: 1.0.0
Summary: Enviro pHAT Driver
Home-page: http://www.pimoroni.com
Author: Philip Howard
Author-email: phil@pimoroni.com
License: MIT
Location: /usr/lib/python2.7/dist-packages
Requires: RPi.GPIO
pi@raspberrypi:/usr/bin $ cd /usr/lib/python2.7/dist-packages
pi@raspberrypi:/usr/lib/python2.7/dist-packages $ ls
enum34-1.1.6.egg-info                       RPi
envirophat                                  RPi.GPIO-0.6.3.egg-info
envirophat-1.0.0.egg-info                   secretstorage
pi@raspberrypi:/usr/lib/python2.7/dist-packages $ cd envirophat
pi@raspberrypi:/usr/lib/python2.7/dist-packages/envirophat $ ls
ads1015.py   bmp280.pyc   __init__.py   leds.pyc     tcs3472.py
ads1015.pyc  i2c_bus.py   __init__.pyc  lsm303d.py   tcs3472.pyc
bmp280.py    i2c_bus.pyc  leds.py       lsm303d.pyc
pi@raspberrypi:/usr/lib/python2.7/dist-packages/envirophat $ sudo nano lsm303d.py
pi@raspberrypi:/usr/lib/python2.7/dist-packages/envirophat $ sudo reboot
Connection to closed by remote host.
Connection to closed.

So, how do I know that has actually changed anything? What does upping the freq actually mean in terms of the behaviour of the sensor and how do I go about logging the data (effectively)


As I can only post two links (cause im a noob member) - new post as I have some graphs.

Magnitude readings, worked out from the following and shows different frequencies using different MySQL insert delays

magnitude = math.pow(axisx, 2) + math.pow(axisy, 2) + math.pow(axisz, 2)
magnitude = math.sqrt(magnitude)

Same thing again, but the raw x, y, z readngs. I don’t know why, but the Z axis (blue) has always read different from the other two axis no matter how level I try and get the pi zero?


A higher frequency isn’t always better when it comes to sensors like this- depending on how they physically read what they’re measuring and integrate that into digital logic, a slower update rate can often yield more accurate results. This isn’t always the case but it’s a useful rule of thumb.

Your Z-axis reading could potentially be an offset due to having the Pi in proximity to the sensor, but it’s difficult to tell.

You can use Python itself as a binary/hex/decimal converter, and Python supports binary notation, examples:

x = 0b1101

>>> hex(0b1101)

>>> bin(123)

>>> "{:04x}".format(94)