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
Implements IAccelerometer
Namespace Meadow.Foundation.Sensors.Motion
Assembly Adxl345.dll

Syntax

public class Adxl345 : FilterableChangeObservableBase<AccelerationConditionChangeResult, AccelerationConditions>, IAccelerometer

Constructors

Adxl345(II2cBus, Byte)

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

Declaration
public Adxl345(II2cBus i2cBus, byte address = 83)

Parameters

Type Name Description
II2cBus i2cBus

I2C bus

System.Byte address

Address of the I2C sensor

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

Conditions

Declaration
public AccelerationConditions Conditions { get; protected set; }

Property Value

Type Description
AccelerationConditions

IsSampling

Gets a value indicating whether the analog input port is currently sampling the ADC. Call StartSampling() to spin up the sampling process.

Declaration
public bool IsSampling { get; protected set; }

Property Value

Type Description
System.Boolean

true if sampling; otherwise, false.

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.

XAcceleration

Acceleration along the X-axis.

Declaration
public float XAcceleration { get; }

Property Value

Type Description
System.Single

Remarks

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

YAcceleration

Acceleration along the Y-axis.

Declaration
public float YAcceleration { get; }

Property Value

Type Description
System.Single

Remarks

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

ZAcceleration

Acceleration along the Z-axis.

Declaration
public float ZAcceleration { get; }

Property Value

Type Description
System.Single

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()

RaiseChangedAndNotify(AccelerationConditionChangeResult)

Declaration
protected void RaiseChangedAndNotify(AccelerationConditionChangeResult changeResult)

Parameters

Type Name Description
AccelerationConditionChangeResult changeResult

Read()

Declaration
public Task<AccelerationConditions> Read()

Returns

Type Description
System.Threading.Tasks.Task<AccelerationConditions>

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.

StartUpdating(Int32)

Declaration
public void StartUpdating(int standbyDuration = 1000)

Parameters

Type Name Description
System.Int32 standbyDuration

StopUpdating()

Declaration
public void StopUpdating()

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

Updated

Declaration
public event EventHandler<AccelerationConditionChangeResult> Updated

Event Type

Type Description
System.EventHandler<AccelerationConditionChangeResult>