1+ /*
2+ @file ISM330DHCX_DataLogTerminal.ino
3+ @author Frederic Pillon <frederic.pillon@st.com>
4+ @brief Example to use the ISM330DHCX 3D accelerometer and 3D gyroscope
5+ *******************************************************************************
6+ Copyright (c) 2021, STMicroelectronics
7+ All rights reserved.
8+
9+ This software component is licensed by ST under BSD 3-Clause license,
10+ the "License"; You may not use this file except in compliance with the
11+ License. You may obtain a copy of the License at:
12+ opensource.org/licenses/BSD-3-Clause
13+
14+ *******************************************************************************
15+ */
16+
17+
18+ // Includes
19+ #include < ISM330DHCXSensor.h>
20+
21+ #define SerialPort Serial
22+
23+ #if defined(ARDUINO_STM32WB5MM_DK) || defined(ARDUINO_B_U585I_IOT02A)
24+ #define USE_I2C_INTERFACE
25+ #else
26+ // Uncomment one communication interface to use:
27+ // X-NUCLEO-IKS02A1 uses I2C interface (set by default)
28+ // #define USE_SPI_INTERFACE
29+ #define USE_I2C_INTERFACE
30+ #endif
31+
32+ #if !defined(USE_SPI_INTERFACE) && !defined(USE_I2C_INTERFACE) || \
33+ defined (USE_SPI_INTERFACE) && defined(USE_I2C_INTERFACE)
34+ #error "Uncomment one communication interface to use!"
35+ #endif
36+
37+ #ifdef USE_SPI_INTERFACE
38+ // Uncomment to set SPI pins to use else default instance will be used
39+ // #define ISM330DHCX_SPI_MOSI PB10
40+ // #define ISM330DHCX_SPI_MISO PB11
41+ // #define ISM330DHCX_SPI_SCLK PB11
42+ // Define Software Chip Select pin to use (default: SS)
43+ #define ISM330DHCX_SPI_SSEL SS
44+
45+ #if defined(ISM330DHCX_SPI_MOSI) && defined(ISM330DHCX_SPI_MISO) && \
46+ defined (ISM330DHCX_SPI_SCLK) && defined(ISM330DHCX_SPI_SSEL)
47+ SPIClass dev_interface(ISM330DHCX_SPI_MOSI, ISM330DHCX_SPI_MISO, ISM330DHCX_SPI_SCLK);
48+ #else
49+ #define dev_interface SPI
50+ #endif
51+
52+ ISM330DHCXSensor AccGyr (&dev_interface, ISM330DHCX_SPI_SSEL);
53+ #else // USE_I2C_INTERFACE
54+ #if defined(ARDUINO_STM32WB5MM_DK)
55+ #define ISM330DHCX_I2C_SCL PB13
56+ #define ISM330DHCX_I2C_SDA PB11
57+ #elif defined(ARDUINO_B_U585I_IOT02A)
58+ #define ISM330DHCX_I2C_SCL PH4
59+ #define ISM330DHCX_I2C_SDA PH5
60+ #else
61+ // Uncomment to set I2C pins to use else default instance will be used
62+ // #define ISM330DHCX_I2C_SCL PYn
63+ // #define ISM330DHCX_I2C_SDA PYn
64+ #endif
65+ #if defined(ISM330DHCX_I2C_SCL) && defined(ISM330DHCX_I2C_SDA)
66+ TwoWire dev_interface (ISM330DHCX_I2C_SDA, ISM330DHCX_I2C_SCL);
67+ #else
68+ // X-NUCLEO-IKS02A1 uses default instance
69+ #define dev_interface Wire
70+ #endif
71+
72+ ISM330DHCXSensor AccGyr (&dev_interface);
73+ #endif
74+
75+ void setup () {
76+ // Led
77+ pinMode (LED_BUILTIN, OUTPUT);
78+ // Initialize serial for output
79+ SerialPort.begin (9600 );
80+
81+ // Initialize bus interface
82+ dev_interface.begin ();
83+
84+ // Initlialize component
85+ AccGyr.begin ();
86+ AccGyr.ACC_Enable ();
87+ AccGyr.GYRO_Enable ();
88+ }
89+
90+ void loop () {
91+ // Led blinking
92+ digitalWrite (LED_BUILTIN, HIGH);
93+ delay (250 );
94+ digitalWrite (LED_BUILTIN, LOW);
95+ delay (250 );
96+
97+ // Read accelerometer and gyroscope
98+ int32_t accelerometer[3 ];
99+ int32_t gyroscope[3 ];
100+ AccGyr.ACC_GetAxes (accelerometer);
101+ AccGyr.GYRO_GetAxes (gyroscope);
102+
103+ // Print compatible with Serial plotter
104+ Serial.print (" Acc[mg]:" );
105+ Serial.print (accelerometer[0 ]);
106+ Serial.print (" , " );
107+ Serial.print (accelerometer[1 ]);
108+ Serial.print (" , " );
109+ Serial.print (accelerometer[2 ]);
110+ Serial.print (" , Gyro[mdps]:" );
111+ Serial.print (gyroscope[0 ]);
112+ Serial.print (" , " );
113+ Serial.print (gyroscope[1 ]);
114+ Serial.print (" , " );
115+ Serial.println (gyroscope[2 ]);
116+ }
0 commit comments