Remarks

ADXL345
Status
Source code GitHub
NuGet package

The ADXL345 is a small, low power, triple axis acceleration sensor capable of measuring up to +/-16g with a resolution of 13-bits.

The ADXL345 is controlled via I2C.

The ADXL345 can operating in interrupt and polling mode. Polling applications are responsible for determining when a sensor is read. Interrupt applications will be notified when the sensor reading changes by + / - a threshold value.

Purchasing

The ADXL345 is available on a small breakout board:

Code Example

public class MeadowApp : App<F7Micro, MeadowApp>
{
    Adxl345 sensor;

    public MeadowApp()
    {
        sensor = new Adxl345(Device.CreateI2cBus(), 83, 0);

        sensor.SetPowerState(false, false, true, false, Adxl345.Frequency.EightHz);

        while (true)
        {
            sensor.Update();

            Console.WriteLine($"{sensor.X}, {sensor.Y}, {sensor.Z}");

            Thread.Sleep(500);
        } 
    }
}

Sample projects available on GitHub

Wiring Example

Characteristic Locus
Inheritance System.Object > Adxl345
Inherited Members System.Object.ToString() System.Object.Equals(System.Object) System.Object.Equals(System.Object, System.Object) System.Object.ReferenceEquals(System.Object, System.Object) System.Object.GetHashCode() System.Object.GetType() System.Object.MemberwiseClone()
Namespace Meadow.Foundation.Sensors.Motion
Assembly Adxl345.dll

Syntax

public class Adxl345

Constructors

Adxl345(II2cBus, Byte, UInt16, Double)

Create a new instance of the ADXL345 communicating over the I2C interface.

Declaration
public Adxl345(II2cBus i2cBus, byte address = 83, ushort updateInterval = 100, double accelerationChangeNotificationThreshold = 5)

Parameters

Type Name Description
II2cBus i2cBus

I2C bus

System.Byte address

Address of the I2C sensor

System.UInt16 updateInterval

How frequently this sensor should be updated.

System.Double accelerationChangeNotificationThreshold

Notification threshold, changes greater than +/- this value will generate and interrupt.

Fields

MinimumPollingPeriod

Minimum value that can be used for the update interval when the sensor is being configured to generate interrupts.

Declaration
public const ushort MinimumPollingPeriod = 100

Field Value

Type Description
System.UInt16

Properties

AccelerationChangeNotificationThreshold

Any changes in the acceleration that are greater than the acceleration threshold will cause an event to be raised when the instance is set to update automatically.

Declaration
public double AccelerationChangeNotificationThreshold { get; set; }

Property Value

Type Description
System.Double

OffsetX

Values stored in this register are automatically added to the X reading.

Declaration
public sbyte OffsetX { get; set; }

Property Value

Type Description
System.SByte

Remarks

Scale factor is 15.6 mg/LSB so 0x7f represents an offset of 2g.

OffsetY

Values stored in this register are automatically added to the Y reading.

Declaration
public sbyte OffsetY { get; set; }

Property Value

Type Description
System.SByte

Remarks

Scale factor is 15.6 mg/LSB so 0x7f represents an offset of 2g.

OffsetZ

Values stored in this register are automatically added to the Z reading.

Declaration
public sbyte OffsetZ { get; set; }

Property Value

Type Description
System.SByte

Remarks

Scale factor is 15.6 mg/LSB so 0x7f represents an offset of 2g.

X

Acceleration along the X-axis.

Declaration
public short X { get; }

Property Value

Type Description
System.Int16

Remarks

This property will only contain valid data after a call to Read or after an interrupt has been generated.

Y

Acceleration along the Y-axis.

Declaration
public short Y { get; }

Property Value

Type Description
System.Int16

Remarks

This property will only contain valid data after a call to Read or after an interrupt has been generated.

Z

Acceleration along the Z-axis.

Declaration
public short Z { get; }

Property Value

Type Description
System.Int16

Remarks

This property will only contain valid data after a call to Read or after an interrupt has been generated.

Methods

DisplayRegisters()

Dump the registers to the debug output stream.

Declaration
public void DisplayRegisters()

SetDataFormat(Boolean, Boolean, Boolean, Boolean, Adxl345.Range)

Configure the data format (see pages 26 and 27 of the data sheet).

Declaration
public void SetDataFormat(bool selfTest, bool spiMode, bool fullResolution, bool justification, Adxl345.Range range)

Parameters

Type Name Description
System.Boolean selfTest

Put the device into self test mode when true.

System.Boolean spiMode

Use 3-wire SPI (true) or 4-wire SPI (false).

System.Boolean fullResolution

Set to full resolution (true) or 10-bit mode using the range determined by the range parameter (false).

System.Boolean justification

Left-justified when true, right justified with sign extension when false.

Adxl345.Range range

Set the range of the sensor to 2g, 4g, 8g or 16g

Remarks

The range of the sensor is determined by the following table: 0: +/- 2g 1: +/- 4g 2: +/- 8g 3: +/ 16g

SetDataRate(Byte, Boolean)

Set the data rate and low power mode for the sensor.

Declaration
public void SetDataRate(byte dataRate, bool lowPower)

Parameters

Type Name Description
System.Byte dataRate

Data rate for the sensor.

System.Boolean lowPower

Setting this to true will enter low power mode (note measurement will encounter more noise in this mode).

SetPowerState(Boolean, Boolean, Boolean, Boolean, Adxl345.Frequency)

Set the PowerControl register (see pages 25 and 26 of the data sheet)

Declaration
public void SetPowerState(bool linkActivityAndInactivity, bool autoASleep, bool measuring, bool sleep, Adxl345.Frequency frequency)

Parameters

Type Name Description
System.Boolean linkActivityAndInactivity

Link the activity and inactivity events.

System.Boolean autoASleep

Enable / disable auto sleep when the activity and inactivity are linked.

System.Boolean measuring

Enable or disable measurements (turn on or off).

System.Boolean sleep

Put the part to sleep (true) or run in normal more (false).

Adxl345.Frequency frequency

Frequency of measurements when the part is in sleep mode.

Update()

Read the six sensor bytes and set the values for the X, Y and Z acceleration.

Declaration
public void Update()

Remarks

All six acceleration registers should be read at the same time to ensure consistency of the measurements.

Events

AccelerationChanged

Event to be raised when the acceleration is greater than +/- AccelerationChangeNotificationThreshold.

Declaration
public event SensorVectorEventHandler AccelerationChanged

Event Type

Type Description
SensorVectorEventHandler