Skip to main content

Meadow.Foundation.Sensors.Motion.Adxl345

Adxl345
StatusStatus badge: working
Source codeGitHub
Datasheet(s)GitHub
NuGet packageNuGet Gallery for Meadow.Foundation.Sensors.Motion.Adxl3xx

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.

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

Class Adxl345

Driver for the ADXL345 triple axis accelerometer +/- 16g

Assembly: Adxl3xx.dll
View Source
Declaration
public class Adxl345 : ByteCommsSensorBase<Acceleration3D>, IObservable<IChangeResult<Acceleration3D>>, IDisposable, IAccelerometer, ISamplingSensor<Acceleration3D>, ISensor<Acceleration3D>, ISensor, ISamplingSensor, II2cPeripheral

Inheritance: System.Object -> Meadow.Foundation.ObservableBase<UNIT>

Implements:
System.IObservable<Meadow.IChangeResult<Meadow.Units.Acceleration3D>>, System.IDisposable, Meadow.Peripherals.Sensors.Motion.IAccelerometer, Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Acceleration3D>, Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Acceleration3D>, Meadow.Peripherals.Sensors.ISensor, Meadow.Peripherals.Sensors.ISamplingSensor, Meadow.Hardware.II2cPeripheral

Properties

DefaultI2cAddress

The default I2C address for the peripheral

View Source
Declaration
public byte DefaultI2cAddress { get; }

Acceleration3D

Current acceleration

View Source
Declaration
public Acceleration3D? Acceleration3D { get; }

OffsetX

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

View Source
Declaration
public sbyte OffsetX { get; set; }

OffsetY

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

View Source
Declaration
public sbyte OffsetY { get; set; }

OffsetZ

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

View Source
Declaration
public sbyte OffsetZ { get; set; }

Fields

MinimumPollingPeriod

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

View Source
Declaration
public const ushort MinimumPollingPeriod = 100

Methods

ReadSensor()

Read sensor

View Source
Declaration
protected override Task<Acceleration3D> ReadSensor()
Returns

System.Threading.Tasks.Task<Meadow.Units.Acceleration3D>: Current acceleration### SetPowerState(bool, bool, bool, bool, Frequencies) Set the PowerControl register (see pages 25 and 26 of the data sheet)

View Source
Declaration
public void SetPowerState(bool linkActivityAndInactivity, bool autoSleep, bool measuring, bool sleep, Adxl345.Frequencies frequency)
Parameters
TypeNameDescription
System.BooleanlinkActivityAndInactivityLink the activity and inactivity events
System.BooleanautoSleepEnable / disable auto sleep when the activity and inactivity are linked
System.BooleanmeasuringEnable or disable measurements (turn on or off)
System.BooleansleepPut the part to sleep (true) or run in normal more (false)
Meadow.Foundation.Sensors.Motion.Adxl345.FrequenciesfrequencyFrequency of measurements when the part is in sleep mode

SetDataFormat(bool, bool, bool, bool, GForceRanges)

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

View Source
Declaration
public void SetDataFormat(bool selfTest, bool spiMode, bool fullResolution, bool justification, Adxl345.GForceRanges range)
Parameters
TypeNameDescription
System.BooleanselfTestPut the device into self test mode when true
System.BooleanspiModeUse 3-wire SPI (true) or 4-wire SPI (false)
System.BooleanfullResolutionSet to full resolution (true) or 10-bit mode using the range determined by the range
parameter (false).
System.BooleanjustificationLeft-justified when true, right justified with sign extension when false
Meadow.Foundation.Sensors.Motion.Adxl345.GForceRangesrangeSet the range of the sensor to 2g, 4g, 8g or 16g

SetDataRate(byte, bool)

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

View Source
Declaration
public void SetDataRate(byte dataRate, bool lowPower)
Parameters
TypeNameDescription
System.BytedataRateData rate for the sensor
System.BooleanlowPowerSetting this to true will enter low power mode (note measurement will encounter more noise in
this mode).

DisplayRegisters()

Dump the registers to the debug output stream.

View Source
Declaration
public void DisplayRegisters()

Implements

  • System.IObservable<Meadow.IChangeResult<Meadow.Units.Acceleration3D>>
  • System.IDisposable
  • Meadow.Peripherals.Sensors.Motion.IAccelerometer
  • Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Acceleration3D>
  • Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Acceleration3D>
  • Meadow.Peripherals.Sensors.ISensor
  • Meadow.Peripherals.Sensors.ISamplingSensor
  • Meadow.Hardware.II2cPeripheral