I2C Setup Guide
STMicroelectronics LSM9DS1 accelerometer
This guide demonstrates how to get accelerometer data from a Raspberry Pi Sense HAT. According to the specification, the Raspberry Pi Sense HAT uses STMicroelectronics LSM9DS1 device to provide accelerometer data. Although LSM9DS1AccelerometerGyroscope or SenseHAT recipe can already get the data from the device, this example demonstrates how to get the data using the general purpose models only.
This example assumes that the device is located at: /dev/i2c-1 and leverages the fact that the LSM9DS1 on Raspberry Pi Sense HAT has device address 0x6A.
- Create a system.
- Add I2CIOServer to the application of the system.
- Set Adapter of the I2CIOServer to /dev/i2c-1.
Note: One can run i2cdetect -l to check if /dev/i2c-1 is available.
- Add I2CDevice to the I2CIOServer.
- Set Address of the I2CDevice to
0x6A
. - Add I2CRegisterWrite to the I2CDevice.
- Set Address of the I2CRegisterWrite to
10
(in hexadecimal notation). - Add eight CDPSignalChannels of type
bool
to the I2CRegisterWrite with the following attributes:
Note: This channel group of 1 byte at address 0x10 corresponds to CTRL_REG1_G of the LSM9DS1 device. It is used to configure Gyroscope output data rate selection at startup (or runtime when changed). This example sets the Gyroscope output data rate to 952 Hz by setting ODR_G0 and ODR_G1 bits to 1.
Name | Nr | Value |
---|---|---|
BW_G0 | 0 | 0 |
BW_G1 | 1 | 0 |
Placeholder | 2 | 0 |
FS_G0 | 3 | 0 |
FS_G1 | 4 | 0 |
ODR_G0 | 5 | 1 |
ODR_G1 | 6 | 1 |
ODR_G2 | 7 | 0 |
- Add I2CRegisterRead to the I2CDevice.
- Set Address of the I2CRegisterRead to
28
(in hexadecimal notation). - Add three CDPSignalChannels of type
short
to the I2CRegisterRead with the following attributes:
Note: This channel group of 6 bytes at address 0x28 corresponds to OUT_X_XL, OUT_Y_XL, OUT_Z_XL of the LSM9DS1 device containing the linear acceleration of X, Y and Z, two bytes each. This channel block will be read out at I2CIOServer fs frequency.
Name |
---|
AccelerationX |
AccelerationY |
AccelerationZ |
As the 16-bit values in two’s complement notation do not correspond to any SI magnitude it is useful to scale the values to known SI units. This is achieved by using ScalingOperator to scale the Max positive value 32767 to represent 2g and Min value -32768 to represent -2g.
- Add ScalingOperator of type
double
to each CDPSignalChannel of typeshort
. - Add two ScalingPoints of type
double
to each ScalingOperator with the following attributes:
InValue | OutValue |
---|---|
-32768 | -2 |
32767 | 2 |
The values of the CDPSignalChannels of type short
should now vary between -2g...2g.
It is possible that more complex devices require more operators to be used in the channels to perform for example output calibration like in the HTS221HumidTemp model.
Note: Some I2C devices may require a custom CDPOperator to be created by the user to perform complex calibration or formatting of output values.
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.