Skip to content
This repository has been archived by the owner on Jun 14, 2024. It is now read-only.

Latest commit

 

History

History

Microsoft.PowerShell.IoT.ADXL345

Example module Microsoft.PowerShell.IoT.ADXL345

This PowerShell module is to interact with ADXL345 accelerometer for reading acceleration on 3 axis.

Hardware setup

Several vendors have breakout boards with ADXL345 sensor. In this example we'll use SparkFun Triple Axis Accelerometer Breakout.

ADXL345 sensor supports both I2C and SPI interfaces; here we'll use I2C.

Wiring diagram with Raspberry Pi 3 looks like this:

ADXL345_Wiring

Software setup

Install PowerShell Core on Raspberry Pi

Installation instructions can be found here.

Enable I2C interface on Raspberry Pi

  1. sudo raspi-config
  2. 5 Interfacing options
  3. P5 I2C
  4. Would you like ARM I2C interface to be enabled -> Yes

Start Powershell and install modules

Don't forget to start PowerShell with sudo or you'll be unable to access I2C bus.

sudo pwsh
Install-Module -Name Microsoft.PowerShell.IoT
git clone https://github.com/PowerShell/PowerShell-IoT.git
Import-Module ./PowerShell-IoT/Examples/Microsoft.PowerShell.IoT.ADXL345

Collect Data

To simply collect acceleration values in g:

PS /home/pi> $accelerometer = Get-ADXL345Device
PS /home/pi> Get-ADXL345Data -Device $accelerometer

Name                           Value
----                           -----
y                              -0.03008072
x                              0.0828058
z                              0.86966985

To represent current acceleration on the 3 axis with bargraphs:

PS /home/pi> $accelerometer = Get-ADXL345Device
PS /home/pi> while ($true) {
    $data = Get-ADXL345Data -Device $accelerometer -Limit 1
    Write-Progress -id 1 -Activity 'X axis' -Status 'Acceleration' -PercentComplete ($data.x * 50 + 50)
    Write-Progress -id 2 -Activity 'Y axis' -Status 'Acceleration' -PercentComplete ($data.y * 50 + 50)
    Write-Progress -id 3 -Activity 'Z axis' -Status 'Acceleration' -PercentComplete ($data.z * 50 + 50)
}

X axis
Acceleration
[ooooooooooooooooooooooooooooooooooooooooooooooooooo                                                 ]
Y axis
Acceleration
[ooooooooooooooooooooooooooooooooooooooooooooooooo                                                   ]
Z axis
Acceleration
[ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo     ]