Remarks

Adxl345
Status Status badge: working
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet Gallery for Adxl345

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

Adxl345 sensor;

public MeadowApp()
{
    Console.WriteLine("Initializing");

    sensor = new Adxl345(Device.CreateI2cBus());
    sensor.SetPowerState(false, false, true, false, Adxl345.Frequencies.TwoHz);

    // classical .NET events can also be used:
    sensor.Updated += (sender, result) =>
    {
        Console.WriteLine($"Accel: [X:{result.New.X.MetersPerSecondSquared:N2}," +
            $"Y:{result.New.Y.MetersPerSecondSquared:N2}," +
            $"Z:{result.New.Z.MetersPerSecondSquared:N2} (m/s^2)]");
    };

    //==== one-off read
    ReadConditions().Wait();

    // start updating
    sensor.StartUpdating(TimeSpan.FromMilliseconds(500));
}

protected async Task ReadConditions()
{
    var result = await sensor.Read();
    Console.WriteLine("Initial Readings:");
    Console.WriteLine($"Accel: [X:{result.X.MetersPerSecondSquared:N2}," +
        $"Y:{result.Y.MetersPerSecondSquared:N2}," +
        $"Z:{result.Z.MetersPerSecondSquared:N2} (m/s^2)]");
}

Sample project(s) available on GitHub

Wiring Example

Characteristic Locus
Inheritance System.Object ObservableBase<Acceleration3D> SensorBase<Acceleration3D> SamplingSensorBase<Acceleration3D> ByteCommsSensorBase<Acceleration3D> > Adxl345
Implements IDisposable IAccelerometer
Inherited Members ByteCommsSensorBase<Acceleration3D>.Peripheral ByteCommsSensorBase<Acceleration3D>.ReadBuffer ByteCommsSensorBase<Acceleration3D>.WriteBuffer ByteCommsSensorBase<Acceleration3D>.Init(Int32, Int32) ByteCommsSensorBase<Acceleration3D>.Dispose(Boolean) ByteCommsSensorBase<Acceleration3D>.Dispose() SamplingSensorBase<Acceleration3D>.StartUpdating(Nullable<TimeSpan>) SamplingSensorBase<Acceleration3D>.StopUpdating() SensorBase<Acceleration3D>.Updated SensorBase<Acceleration3D>.samplingLock SensorBase<Acceleration3D>.SamplingTokenSource SensorBase<Acceleration3D>.Conditions SensorBase<Acceleration3D>.IsSampling SensorBase<Acceleration3D>.UpdateInterval SensorBase<Acceleration3D>.RaiseEventsAndNotify(IChangeResult<Acceleration3D>) SensorBase<Acceleration3D>.Read() ObservableBase<Acceleration3D>.observers ObservableBase<Acceleration3D>.NotifyObservers(IChangeResult<Acceleration3D>) Meadow.Foundation.ObservableBase<Acceleration3D>.Subscribe(IObserver<>) Meadow.Foundation.ObservableBase<Acceleration3D>.CreateObserver(Action<>, System.Nullable<Predicate<Meadow.IChangeResult<UNIT>>>)
Namespace Meadow.Foundation.Sensors.Motion
Assembly Adxl3xx.dll

Syntax

public class Adxl345 : ByteCommsSensorBase<Acceleration3D>, IDisposable, IAccelerometer

Constructors

Adxl345(II2cBus, Adxl345.Addresses)

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

Declaration
public Adxl345(II2cBus i2cBus, Adxl345.Addresses address = default(Adxl345.Addresses))

Parameters

Type Name Description
II2cBus i2cBus

I2C bus

Adxl345.Addresses address

Address of the I2C sensor

Adxl345(II2cBus, Byte)

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

Declaration
public Adxl345(II2cBus i2cBus, byte address)

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 = null

Field Value

Type Description
System.UInt16

Properties

Acceleration3D

Current acceleration

Declaration
public Acceleration3D? Acceleration3D { get; }

Property Value

Type Description
System.Nullable<Acceleration3D>

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.

Methods

DisplayRegisters()

Dump the registers to the debug output stream.

Declaration
public void DisplayRegisters()

RaiseEventsAndNotify(IChangeResult<Acceleration3D>)

Raise changed event for subscribers

Declaration
protected override void RaiseEventsAndNotify(IChangeResult<Acceleration3D> changeResult)

Parameters

Type Name Description
IChangeResult<Acceleration3D> changeResult

ReadSensor()

Read sensor

Declaration
protected override Task<Acceleration3D> ReadSensor()

Returns

Type Description
Task<Acceleration3D>

Current acceleration

Overrides

Meadow.Foundation.SensorBase<Acceleration3D>.ReadSensor()

SetDataFormat(Boolean, Boolean, Boolean, Boolean, Adxl345.GForceRanges)

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.GForceRanges 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.GForceRanges 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.Frequencies)

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

Declaration
public void SetPowerState(bool linkActivityAndInactivity, bool autoSleep, bool measuring, bool sleep, Adxl345.Frequencies frequency)

Parameters

Type Name Description
System.Boolean linkActivityAndInactivity

Link the activity and inactivity events.

System.Boolean autoSleep

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.Frequencies frequency

Frequency of measurements when the part is in sleep mode.

Events

Acceleration3DUpdated

Event raised when acceleration changes

Declaration
public event EventHandler<IChangeResult<Acceleration3D>> Acceleration3DUpdated

Event Type

Type Description
EventHandler<IChangeResult<Acceleration3D>>