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

Remarks

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

Purchasing

The ADXL345 is available on a small breakout board:

Examples

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.

Interrupt Example

The application below demonstrates how to connect an interrupt handler to the ADXL345 sensor and display changes only when the acceleration changes in the x, y or z axis by more than the acceleration threshold (default is 5 units):

using System.Threading;
using Meadow;
using Meadow.Foundation.Sensors.Motion;

namespace ADXL345_Sample
{
    public class Program
    {
        static IApp _app; 
        public static void Main()
        {
            _app = new App();
        }
    }

    public class App : AppBase<F7Micro, App>
    {
        public App ()
        {
            Console.WriteLine("\n\n");
            Console.WriteLine("ADXL345 Interrupt Example.");
            Console.WriteLine("--------------------------");

            var adxl345 = new ADXL345();
            Console.WriteLine("Device ID: " + adxl345.DeviceID);

            // Attach an interrupt handler.            
            adxl345.AccelerationChanged += (s, e) =>
            {
                Console.WriteLine("X: " + e.CurrentValue.X.ToString() +
                            ", Y: " + e.CurrentValue.Y.ToString() +
                            ", Z: " + e.CurrentValue.Z.ToString());
            };

            // Interrupts are attached so power on the sensor.
            adxl345.SetPowerState(false, false, true, false, ADXL345.Frequency.EightHz);

            // Put Meadow to sleep as the interrupt handler will deal 
            // with changes in acceleration.
            Thread.Sleep(Timeout.Infinite);
        }
    }
}

Polling Example

The sensor is put into polling mode by setting the updateInterval in the constructor to 0 milliseconds. The following application will read the sensor and output to the debug console every 500 milliseconds:

using System.Threading;
using Meadow;
using Meadow.Foundation.Sensors.Motion;

namespace ADXL345_Sample
{
    public class Program
    {
        static IApp _app; 
        public static void Main()
        {
            _app = new App();
        }
    }

    public class App : AppBase<F7Micro, App>
    {
        public App ()
        {
            Console.WriteLine("\n\n");
            Console.WriteLine("ADXL345 Polling Example.");
            Console.WriteLine("------------------------");

            ADXL345 adxl345 = new ADXL345(updateInterval: 0);
            Console.WriteLine("Device ID: " + adxl345.DeviceID);

            adxl345.SetPowerState(false, false, true, false, ADXL345.Frequency.EightHz);
            while (true)
            {
                adxl345.Update();
                Console.WriteLine("X: " + adxl345.X.ToString() + ", Y: " + adxl345.Y.ToString() + ", Z: " + adxl345.Z.ToString());
                Thread.Sleep(500);
            }
        }
    }
}
Example Circuit

This diagram shows how to connect the ADXL345 to Meadow when interrupts are not required.

The SDA and SCL lines both require pull-up resistors (4k7) when they are not present on the breakout board.

Constructors

View Source

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

View Source

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

View Source

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
View Source

DeviceID

Get the device ID, this should return 0xe5.

Declaration
public byte DeviceID { get; }

Property Value

Type Description
System.Byte
View Source

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.

View Source

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.

View Source

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.

View Source

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.

View Source

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.

View Source

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

View Source

DisplayRegisters()

Dump the registers to the debug output stream.

Declaration
public void DisplayRegisters()
View Source

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

View Source

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

View Source

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.

View Source

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

View Source

AccelerationChanged

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

Declaration
public event SensorVectorEventHandler AccelerationChanged

Event Type

Type Description
SensorVectorEventHandler