Meadow.Foundation.Sensors.Motion.Adxl345
Adxl345 | |
---|---|
Status | |
Source code | GitHub |
Datasheet(s) | 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.
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
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
public byte DefaultI2cAddress { get; }
Acceleration3D
Current acceleration
View Source
public Acceleration3D? Acceleration3D { get; }
OffsetX
Values stored in this register are automatically added to the X reading
View Source
public sbyte OffsetX { get; set; }
OffsetY
Values stored in this register are automatically added to the Y reading
View Source
public sbyte OffsetY { get; set; }
OffsetZ
Values stored in this register are automatically added to the Z reading
View Source
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
public const ushort MinimumPollingPeriod = 100
Methods
ReadSensor()
Read sensor
View Source
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
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) |
Meadow.Foundation.Sensors.Motion.Adxl345.Frequencies | frequency | Frequency 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
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 |
Meadow.Foundation.Sensors.Motion.Adxl345.GForceRanges | range | Set 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
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). |
DisplayRegisters()
Dump the registers to the debug output stream.
View Source
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