ADXL345 with Pyboard

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Posts: 84
Joined: Fri Dec 18, 2015 7:36 pm

Re: Interfacing Sparkfun ADXL345 with Pyboard.

Post by Jim.S » Wed Apr 13, 2016 6:45 pm

You are thinking too complicated at the moment. You need to break the problem down into simple parts
1. understand how to edit so that it does not mount the /flash or /sd card on the host PC (i.e uncomment the line pyb.usb_mode('CDC+HID')). If you don’t do this you will have problems logging data to a text file
2. Learn how to use the program. Rshell is your friend. It will let you transfer your files to and from the host PC to your pyboard when the flash drive and sd card are not mounted as storage devices.
3. Write a very simple program that just writes numbers to a text file. This will teach you about the python string formatting, stuff like print('{},{},{}'.format(1,2,3)).
4. Write a simple program that creates a ADXL345Accel object using the library/driver you have posted earlier in this thread. This program should just
a) create an ADXL345Accel object
b) get the three acceleration values
c) print them to the screen (see 3 above) and exit

5. Modify the program so that it runs continuously, gets three acceleration values once a second and prints them to the screen. This will teach you how to control the speed of the program loop
6. Now put it all together. Modify the program so that it writes the values to a file but only once a second!
7. Gradually increase the speed of the program so it writes faster and faster. This will reveal all sorts of issues with your program and teach you loads.

This is exactly what I have done over the last few weeks........

Posts: 288
Joined: Sun May 04, 2014 8:54 am

Re: Interfacing Sparkfun ADXL345 with Pyboard.

Post by Turbinenreiter » Wed Apr 13, 2016 8:30 pm

There is a datalogger in the examples folder of the micropython repo.

User avatar
Posts: 3587
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada

Re: Interfacing Sparkfun ADXL345 with Pyboard.

Post by dhylands » Wed Apr 13, 2016 11:27 pm

Open a file and write the data (it's the same in micropython as regular python).

User avatar
Posts: 1
Joined: Tue May 10, 2016 9:58 pm

Re: Interfacing Sparkfun ADXL345 with Pyboard.

Post by markph » Sun May 15, 2016 1:33 pm

[quote="Turbinenreiter"]There is a datalogger in the examples folder of the micropython repo.[/quote]

Also here:

Posts: 21
Joined: Mon Aug 27, 2018 4:43 pm

Re: Interfacing Sparkfun ADXL345 with Pyboard.

Post by aleskeyfedorovich » Thu Oct 22, 2020 6:58 am

Abhinay_1 wrote:
Wed Apr 13, 2016 9:59 am

Code: Select all

from pyb import Pin
from pyb import SPI

READWRITE_CMD = const(0x80)
MULTIPLEBYTE_CMD = const(0x40)
DEVID_ADDR = const(0x00)
DATAX1_ADDR = const(0x33)
DATAY1_ADDR = const(0x35)
DATAZ1_ADDR = const(0x37)

ADXL345_DEVID_VAL = const(0xE5)
ADXL345_POWER_CTL_ADDR = const(0x2D)
ADXL345_BW_RATE_ADDR = const(0x2C)
ADXL345_DATA_FORMAT_ADDR = const(0x31)
ADXL345_ENABLE_MSRM_ADDR = const(0x2D)

ADXL345_POWER_CTL_CONF = const(0x00)
ADXL345_BW_RATE_CONF = const(0x0F)
ADXL345_DATA_FORMAT_CONF = const(0x10)
ADXL345_ENABLE_MSRM_CONF = const(0x08)

class ADXL345Accel:
        def _init_(self):
                self.cs_pin = Pin('X5', Pin.OUT_PP, Pin.PULL_NONE)
                self.spi = SPI(1, SPI.MASTER, baudrate=2000000, polarity=0, phase=1, bits=8)
                self.devid = self.read_id()
                if self.devid == ADXL345_DEVID_VAL:
                      self.write_bytes(ADXL345_POWER_CTL_ADDR, bytearray([ADXL345_POWER_CTL_CONF]))
                      self.write_bytes(ADXL345_BW_RATE_ADDR, bytearray([ADXL345_BW_RATE_CONF]))
                      self.write_bytes(ADXL345_DATA_FORMAT_ADDR, bytearray([ADXL345_DATA_FORMAT_CONF]))
                      self.write_bytes(ADXL345_ENABLE_MSRM_ADDR, bytearray([ADXL345_ENABLE_MSRM_CONF]))
                      self.sensitivity = 32
                      raise Exception('ADXL345 accelerometer not present')
     def convert_raw_to_g(self, x):
             if x & 0x80:
                   x = x - 256
             return x * self.sensitivity / 1000
  def read_bytes(self, addr, nbytes):
          if len(buf) > 1:
         addr |= MULTIPLEBYTE_CMD
    for b in buff:
        def read_id(self):
                return self.read_bytes(DEVID_ADDR, 1)[0]
        def x(self):
                return self.convert_raw_to_g(self.read_bytes(DATAX1_ADDR, 1)[0])
        def y(self):                                                                                                                                
                return self.convert_raw_to_g(self.read_bytes(DATAY1_ADDR, 1)[0])
        def z(self):
                return self.convert_raw_to_g(self.read_bytes(DATAZ1_ADDR, 1)[0])
        def xyz(self):
                return (self.x(), self.y(), self.z())
This code misses the write_bytes method, could you please provide the complete working library, please?

Post Reply