Giter Site home page Giter Site logo

alekseyfedorovich / adxl345_spi_micropython Goto Github PK

View Code? Open in Web Editor NEW
15.0 1.0 1.0 37 KB

Library for interacting through the SPI protocol with an 'Analog Devices ADXL345' accelerometer from an MCU flashed with MicroPython. Methods are optimised for trying to reach max available sampling rate for this device

License: MIT License

Python 100.00%
micropython micropython-esp32 esp32 accelerometer vibrational-analysis adxl345 frequency

adxl345_spi_micropython's Introduction

uPy - ADXL345 - SPI

Library for controlling through the SPI protocol an 'Analog Devices ADXL345' accelerometer from an MCU flashed with MicroPython (in particular this was tested with a ESP32-WROVER (4MB RAM)).

Methods are optimised for being as fast as possible, trying to reach max available sampling rate (3.2kHz) for this device.

Wiring

The following wirings refers to the tested setup on an ESP32-WROVER:

ADXL345 Pin name ESP32 Pin name (number)
Vs 3v3
GND GND
CS vspi cs (D5)
SCL/SCLK vspi scl (D18)
SDO/ALT ADDRESS vspi miso (D19)
SDA/SDI/SDO vspi mosi (D23)

RAM and MemoryErrors

Consider that at high sampling rates the MCU collects 3_axes x sampling_rate floats per second. This may result in ending the available RAM of MCUs very quickly: set your acquisition time accordingly and clear data arrays when you are done with them.

Examples

read one x, y, z

from ADXL345_spi import ADXL345 as Accelerometer
accelerometer = Accelerometer(cs_pin=5, scl_pin=18, sda_pin=23, sdo_pin=19, spi_freq=5000000)
accelerometer.init_spi()
accelerometer.set_sampling_rate(1.56)   # Hz
accelerometer.set_g_range(2)            # max measurable acceleration pm 2g
accelerometer.set_fifo_mode('bypass')
accelerometer.set_power_mode('measure')
buf, T = accelerometer.read_many_xyz(n=1)
accelerometer.set_power_mode('standby')
x, y, z = accelerometer.xyzbytes2g(buf)  # convert bytearray in 3 acceleration arrays (x, y, z) in g units
accelerometer.deinit_spi()  # this is necessary, otherwise if another SPI is initialized it won't work

read many x, y, z

from ADXL345_spi import ADXL345 as Accelerometer
accelerometer = Accelerometer()                     # assumes accelerometer is connected to MCU spi default Pins
accelerometer.init_spi()
accelerometer.set_sampling_rate(3200)
accelerometer.set_g_range(2)
accelerometer.set_fifo_mode('bypass')
accelerometer.set_power_mode('measure')
buf, T = accelerometer.read_many_xyz(n=10)  # reads ten samples for each axis at the requested sampling rate
accelerometer.set_power_mode('standby')
x, y, z = accelerometer.xyzbytes2g(buf)     # arrays of length 10, to be associated with the T array that holds the times of the measure of each sample
accelerometer.deinit_spi()

read continuosly when data is ready

from ADXL345_spi import ADXL345 as Accelerometer
accelerometer = Accelerometer()
accelerometer.init_spi()
accelerometer.set_sampling_rate(3200)
accelerometer.set_g_range(2)
accelerometer.set_fifo_mode('bypass')
accelerometer.set_power_mode('measure')
buf, T = accelerometer.read_continuos_xyz(acquisition_time=1.5)  # reads samples for 1.5 seconds from each axis at the requested sampling rate
accelerometer.set_power_mode('standby')
x, y, z = accelerometer.xyzbytes2g(buf)  # arrays (in principle) of length acquisition_time * sampling_rate 
accelerometer.deinit_spi()

read continuosly from fifo

The method for reading from fifo is implemented even though it doesn't read more than one row of the fifo in one transaction, making its performances equal to the method reading measures when they are ready. Trying to read more than one row of the fifo in one transaction always resulted in reading following registers instead of other rows of the fifo.

from ADXL345_spi import ADXL345 as Accelerometer
accelerometer = Accelerometer()
accelerometer.init_spi()
accelerometer.set_sampling_rate(3200)
accelerometer.set_g_range(2)
accelerometer.set_fifo_mode('stream')
accelerometer.set_power_mode('measure')
buf, T = accelerometer.read_continuos_xyz_fromfifo(acquisition_time=1.5)
accelerometer.set_power_mode('standby')
x, y, z = accelerometer.xyzbytes2g(buf)
accelerometer.deinit_spi()

adxl345_spi_micropython's People

Contributors

alekseyfedorovich avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

whatdtech

adxl345_spi_micropython's Issues

not fast enough at 3200Hz

Hi,
I've tried your code on an ESP32 development board. If I run it as is, I get the message that the actual sampling rate is 320Hz, so only 1/10th of the desired 3200Hz. Which board did you use to achieve the 3200Hz?

I also had to reduce the aquisition_time to 0.1s, because otherwise I would ran into MemoryErrors.

Here is my code:

def start_measurement():
    accelerometer = Accelerometer()
    accelerometer.init_spi()
    accelerometer.set_sampling_rate(3200)
    accelerometer.set_g_range(16)
    accelerometer.set_fifo_mode('stream')
    accelerometer.set_power_mode('measure')
    cntr=0
    try:
        while True:
            cntr+=1
            buf, T = accelerometer.read_continuos_xyz_fromfifo(acquisition_time=0.1)
            x, y, z = accelerometer.xyzbytes2g(buf)
            print(len(x))
            if cntr>100:
                break
    except:
        pass
    finally:
        accelerometer.set_power_mode('standby')
        accelerometer.deinit_spi()

I've tried removing stream/standby switches and cs.value() calls to make it faster, but with no success. Only if I remove your "# it is impossible to read a block of measures from fifo"-for-loop, and read all the data inside the fifo at once, I can read ~28 samples at once (so right before the FIFO is full, maybe) and end up with an actual sampling rate of ~7200 or ~4500Hz, depending if I use cs.value calls or not. Which is strange, because I'm checking the size of the FIFO and I am only reading what is there. So I guess I'm not acually reading the whole FIFO, and it just pops one sample... But the size is not increasing to 32, so I'm really not sure what is happening. The data that I get from this also doesn't look plausible, there are a lot of 0's in there, but not everywhere as I would expect.
Why is it that we cannot read the complete FIFO at once?
Should I switch to Adruino IDE/C and back off of python if I need the 3200Hz? Did you manage to get it working?

Thanks

Raspberry pi Pico

Hello, im sorry for this question. I have a raspberry pi Pico with a adxl345. How do i connect to Pico and what library do i need to run? Also once i install the library do i use the example code you provided to run it? Thanks

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.