Skip to content

Commit 3084918

Browse files
committed
add src files
1 parent 3c0371b commit 3084918

15 files changed

Lines changed: 17831 additions & 0 deletions

File tree

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,66 @@
11
# LSM6DSO32
22
Arduino library to support the LSM6DSO32 3D accelerometer and 3D gyroscope
3+
4+
## API
5+
6+
This sensor uses I2C or SPI to communicate.
7+
For I2C it is then required to create a TwoWire interface before accessing to the sensors:
8+
9+
TwoWire dev_i2c(I2C_SDA, I2C_SCL);
10+
dev_i2c.begin();
11+
12+
For SPI it is then required to create a SPI interface before accessing to the sensors:
13+
14+
SPIClass dev_spi(SPI_MOSI, SPI_MISO, SPI_SCK);
15+
dev_spi.begin();
16+
17+
An instance can be created and enabled when the I2C bus is used following the procedure below:
18+
19+
LSM6DSO32Sensor AccGyr(&dev_i2c);
20+
AccGyr.begin();
21+
AccGyr.Enable_X();
22+
AccGyr.Enable_G();
23+
24+
An instance can be created and enabled when the SPI bus is used following the procedure below:
25+
26+
LSM6DSO32Sensor AccGyr(&dev_spi, CS_PIN);
27+
AccGyr.begin();
28+
AccGyr.Enable_X();
29+
AccGyr.Enable_G();
30+
31+
The access to the sensor values is done as explained below:
32+
33+
Read accelerometer and gyroscope.
34+
35+
int32_t accelerometer[3];
36+
int32_t gyroscope[3];
37+
AccGyr.Get_X_Axes(accelerometer);
38+
AccGyr.Get_G_Axes(gyroscope);
39+
40+
# Examples
41+
42+
There are several examples with the LSM6DSO32 library.
43+
* LSM6DSO32_HelloWorld: This application provides a simple example of usage of the LSM6DSO32
44+
IMU 6-axis. It shows how to display on a hyperterminal the values of the sensor.
45+
* LSM6DSO32_6DOrientation: This application shows how to use the LSM6DSO32 accelerometer
46+
to find out the 6D orientation and display data on a hyperterminal.
47+
* LSM6DSO32_FreeFallDetection: This application shows how to detect the free fall event using the
48+
LSM6DSO32 accelerometer.
49+
* LSM6DSO32_Pedometer: This application shows how to use the LSM6DSO32 accelerometer
50+
to count steps.
51+
* LSM6DSO32_SingleTap: This application shows how to detect the single tap event using the
52+
LSM6DSO32 accelerometer.
53+
* LSM6DSO32_DoubleTap: This application shows how to detect the double tap event using the
54+
LSM6DSO32 accelerometer.
55+
* LSM6DSO32_TiltDetection: This application shows how to detect the tilt event using the
56+
LSM6DSO32 accelerometer.
57+
* LSM6DSO32_WakeUpDetection: This application shows how to detect the wake-up event using the
58+
LSM6DSO32 accelerometer.
59+
60+
## Documentation
61+
62+
You can find the source files at
63+
https://github.com/stm32duino/LSM6DSO32
64+
65+
The LSM6DSO32 datasheet is available at
66+
https://www.st.com/en/mems-and-sensors/lsm6dso32.html
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/**
2+
******************************************************************************
3+
* @file LSM6DSO32_6DOrientation.ino
4+
* @author Nathan Garnier, based on LSM6DSO lib.
5+
* @version V1.0.0
6+
* @date March 2026
7+
* @brief Arduino test application for the STMicrolectronics
8+
* LSM6DSO32 MEMS IMU 6-axis sensor.
9+
* This application makes use of C++ classes obtained from the C
10+
* components' drivers.
11+
******************************************************************************
12+
*/
13+
14+
#include <LSM6DSO32Sensor.h>
15+
16+
#ifdef ARDUINO_SAM_DUE
17+
#define DEV_I2C Wire1
18+
#elif defined(ARDUINO_ARCH_STM32)
19+
#define DEV_I2C Wire
20+
#elif defined(ARDUINO_ARCH_AVR)
21+
#define DEV_I2C Wire
22+
#else
23+
#define DEV_I2C Wire
24+
#endif
25+
#define SerialPort Serial
26+
27+
#define IMU_INT_1 4
28+
29+
LSM6DSO32Sensor accGyr(&DEV_I2C);
30+
31+
//Interrupts.
32+
volatile int mems_event = 0;
33+
34+
char report[256];
35+
36+
void INT1Event_cb();
37+
void sendOrientation();
38+
39+
void setup() {
40+
// Led.
41+
pinMode(LED_BUILTIN, OUTPUT);
42+
// Initialize serial for output.
43+
SerialPort.begin(115200);
44+
45+
// Initialize I2C bus.
46+
DEV_I2C.begin();
47+
48+
//Int1 pin input
49+
pinMode(IMU_INT_1, INPUT);
50+
51+
accGyr.begin();
52+
accGyr.Enable_X();
53+
accGyr.Enable_6D_Orientation(LSM6DSO32_INT1_PIN);
54+
}
55+
56+
void loop() {
57+
58+
int int_val = digitalRead(IMU_INT_1);
59+
if(int_val && !mems_event)
60+
{
61+
mems_event = 1;
62+
}
63+
64+
if (mems_event)
65+
{
66+
mems_event=0;
67+
LSM6DSO32_Event_Status_t status;
68+
accGyr.Get_X_Event_Status(&status);
69+
if (status.D6DOrientationStatus)
70+
{
71+
sendOrientation();
72+
// Led blinking.
73+
digitalWrite(LED_BUILTIN, HIGH);
74+
delay(100);
75+
digitalWrite(LED_BUILTIN, LOW);
76+
}
77+
}
78+
}
79+
80+
void sendOrientation()
81+
{
82+
uint8_t xl = 0;
83+
uint8_t xh = 0;
84+
uint8_t yl = 0;
85+
uint8_t yh = 0;
86+
uint8_t zl = 0;
87+
uint8_t zh = 0;
88+
89+
accGyr.Get_6D_Orientation_XL(&xl);
90+
accGyr.Get_6D_Orientation_XH(&xh);
91+
accGyr.Get_6D_Orientation_YL(&yl);
92+
accGyr.Get_6D_Orientation_YH(&yh);
93+
accGyr.Get_6D_Orientation_ZL(&zl);
94+
accGyr.Get_6D_Orientation_ZH(&zh);
95+
96+
if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 1 && zh == 0 )
97+
{
98+
sprintf( report, "\r\n ________________ " \
99+
"\r\n | | " \
100+
"\r\n | * | " \
101+
"\r\n | | " \
102+
"\r\n | | " \
103+
"\r\n | | " \
104+
"\r\n | | " \
105+
"\r\n |________________| \r\n" );
106+
}
107+
108+
else if ( xl == 1 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 0 )
109+
{
110+
sprintf( report, "\r\n ________________ " \
111+
"\r\n | | " \
112+
"\r\n | * | " \
113+
"\r\n | | " \
114+
"\r\n | | " \
115+
"\r\n | | " \
116+
"\r\n | | " \
117+
"\r\n |________________| \r\n" );
118+
}
119+
120+
else if ( xl == 0 && yl == 0 && zl == 0 && xh == 1 && yh == 0 && zh == 0 )
121+
{
122+
sprintf( report, "\r\n ________________ " \
123+
"\r\n | | " \
124+
"\r\n | | " \
125+
"\r\n | | " \
126+
"\r\n | | " \
127+
"\r\n | | " \
128+
"\r\n | * | " \
129+
"\r\n |________________| \r\n" );
130+
}
131+
132+
else if ( xl == 0 && yl == 1 && zl == 0 && xh == 0 && yh == 0 && zh == 0 )
133+
{
134+
sprintf( report, "\r\n ________________ " \
135+
"\r\n | | " \
136+
"\r\n | | " \
137+
"\r\n | | " \
138+
"\r\n | | " \
139+
"\r\n | | " \
140+
"\r\n | * | " \
141+
"\r\n |________________| \r\n" );
142+
}
143+
144+
else if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 1 )
145+
{
146+
sprintf( report, "\r\n __*_____________ " \
147+
"\r\n |________________| \r\n" );
148+
}
149+
150+
else if ( xl == 0 && yl == 0 && zl == 1 && xh == 0 && yh == 0 && zh == 0 )
151+
{
152+
sprintf( report, "\r\n ________________ " \
153+
"\r\n |________________| " \
154+
"\r\n * \r\n" );
155+
}
156+
157+
else
158+
{
159+
sprintf( report, "None of the 6D orientation axes is set in LSM6DSO - accelerometer.\r\n" );
160+
}
161+
162+
SerialPort.print(report);
163+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
******************************************************************************
3+
* @file LSM6DSO32_DoubleTap.ino
4+
* @author Nathan Garnier, based on LSM6DSO lib.
5+
* @version V1.0.0
6+
* @date March 2026
7+
* @brief Arduino test application for the STMicrolectronics
8+
* LSM6DSO32 MEMS IMU 6-axis sensor.
9+
* This application makes use of C++ classes obtained from the C
10+
* components' drivers.
11+
******************************************************************************
12+
*/
13+
14+
#include <LSM6DSO32Sensor.h>
15+
16+
#ifdef ARDUINO_SAM_DUE
17+
#define DEV_I2C Wire1
18+
#elif defined(ARDUINO_ARCH_STM32)
19+
#define DEV_I2C Wire
20+
#elif defined(ARDUINO_ARCH_AVR)
21+
#define DEV_I2C Wire
22+
#else
23+
#define DEV_I2C Wire
24+
#endif
25+
#define SerialPort Serial
26+
27+
#define IMU_INT_1 4
28+
29+
LSM6DSO32Sensor accGyr(&DEV_I2C);
30+
31+
//Interrupts.
32+
volatile int mems_event = 0;
33+
34+
void setup() {
35+
// Led.
36+
pinMode(LED_BUILTIN, OUTPUT);
37+
// Initialize serial for output.
38+
SerialPort.begin(115200);
39+
40+
// Initialize I2C bus.
41+
DEV_I2C.begin();
42+
43+
//Int1 pin input
44+
pinMode(IMU_INT_1, INPUT);
45+
46+
accGyr.begin();
47+
accGyr.Enable_X();
48+
accGyr.Enable_Double_Tap_Detection(LSM6DSO32_INT1_PIN);
49+
}
50+
51+
void loop() {
52+
53+
int int_val = digitalRead(IMU_INT_1);
54+
if(int_val && !mems_event)
55+
{
56+
mems_event = 1;
57+
}
58+
59+
if (mems_event)
60+
{
61+
mems_event=0;
62+
LSM6DSO32_Event_Status_t status;
63+
accGyr.Get_X_Event_Status(&status);
64+
if (status.DoubleTapStatus)
65+
{
66+
// Output data.
67+
SerialPort.println("Double Tap Detected!");
68+
69+
// Led blinking.
70+
digitalWrite(LED_BUILTIN, HIGH);
71+
delay(100);
72+
digitalWrite(LED_BUILTIN, LOW);
73+
}
74+
}
75+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
******************************************************************************
3+
* @file LSM6DSO32_FreeFallDetection.ino
4+
* @author Nathan Garnier, based on LSM6DSO lib.
5+
* @version V1.0.0
6+
* @date March 2026
7+
* @brief Arduino test application for the STMicrolectronics
8+
* LSM6DSO32 MEMS IMU 6-axis sensor.
9+
* This application makes use of C++ classes obtained from the C
10+
* components' drivers.
11+
******************************************************************************
12+
*/
13+
14+
#include <LSM6DSO32Sensor.h>
15+
16+
17+
#ifdef ARDUINO_SAM_DUE
18+
#define DEV_I2C Wire1
19+
#elif defined(ARDUINO_ARCH_STM32)
20+
#define DEV_I2C Wire
21+
#elif defined(ARDUINO_ARCH_AVR)
22+
#define DEV_I2C Wire
23+
#else
24+
#define DEV_I2C Wire
25+
#endif
26+
#define SerialPort Serial
27+
28+
#define IMU_INT_1 4
29+
30+
LSM6DSO32Sensor accGyr(&DEV_I2C);
31+
32+
//Interrupts.
33+
volatile int mems_event = 0;
34+
35+
void setup() {
36+
// Led.
37+
pinMode(LED_BUILTIN, OUTPUT);
38+
// Initialize serial for output.
39+
SerialPort.begin(115200);
40+
41+
// Initialize I2C bus.
42+
DEV_I2C.begin();
43+
44+
//Int1 pin input
45+
pinMode(IMU_INT_1, INPUT);
46+
47+
accGyr.begin();
48+
accGyr.Enable_X();
49+
accGyr.Enable_Free_Fall_Detection(LSM6DSO32_INT1_PIN);
50+
}
51+
52+
void loop() {
53+
54+
int int_val = digitalRead(IMU_INT_1);
55+
if(int_val && !mems_event)
56+
{
57+
mems_event = 1;
58+
}
59+
60+
if (mems_event)
61+
{
62+
mems_event=0;
63+
LSM6DSO32_Event_Status_t status;
64+
accGyr.Get_X_Event_Status(&status);
65+
if (status.FreeFallStatus)
66+
{
67+
// Led blinking.
68+
digitalWrite(LED_BUILTIN, HIGH);
69+
delay(200);
70+
digitalWrite(LED_BUILTIN, LOW);
71+
// Output data.
72+
SerialPort.println("Free Fall Detected!");
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)