Skip to content

Commit 17fbc33

Browse files
authored
Merge pull request #9 from arduino-libraries/fix-#7
Enable temperature readout of LSM5DSOX sensor.
2 parents 1fdef5b + a7eae16 commit 17fbc33

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Arduino LSM6DSOX - Simple Temperature
3+
4+
This example reads the temperature values from the LSM6DSOX
5+
sensor and continuously prints them to the Serial Monitor
6+
or Serial Plotter.
7+
8+
The circuit:
9+
- Arduino Nano RP2040 Connect
10+
11+
created 30 August 2021
12+
by Alexander Entinger
13+
14+
This example code is in the public domain.
15+
*/
16+
17+
#include <Arduino_LSM6DSOX.h>
18+
19+
void setup()
20+
{
21+
Serial.begin(9600);
22+
while (!Serial);
23+
24+
if (!IMU.begin())
25+
{
26+
Serial.println("Failed to initialize IMU!");
27+
while (1);
28+
}
29+
}
30+
31+
void loop()
32+
{
33+
if (IMU.temperatureAvailable())
34+
{
35+
int temperature_deg = 0;
36+
IMU.readTemperature(temperature_deg);
37+
38+
Serial.print("LSM6DSOX Temperature = ");
39+
Serial.print(temperature_deg);
40+
Serial.println(" °C");
41+
}
42+
}

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ gyroscopeAvailable KEYWORD2
2121
accelerationAvailable KEYWORD2
2222
accelerationSampleRate KEYWORD2
2323
gyroscopeSampleRate KEYWORD2
24+
readTemperature KEYWORD2
25+
temperatureAvailable KEYWORD2
2426

2527
#######################################
2628
# Constants

src/LSM6DSOX.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
#define LSM6DSOX_CTRL7_G 0X16
3232
#define LSM6DSOX_CTRL8_XL 0X17
3333

34+
#define LSM6DSOX_OUT_TEMP_L 0X20
35+
#define LSM6DSOX_OUT_TEMP_H 0X21
36+
3437
#define LSM6DSOX_OUTX_L_G 0X22
3538
#define LSM6DSOX_OUTX_H_G 0X23
3639
#define LSM6DSOX_OUTY_L_G 0X24
@@ -171,6 +174,33 @@ int LSM6DSOXClass::gyroscopeAvailable()
171174
return 0;
172175
}
173176

177+
int LSM6DSOXClass::readTemperature(int & temperature_deg)
178+
{
179+
/* Read the raw temperature from the sensor. */
180+
int16_t temperature_raw = 0;
181+
182+
if (readRegisters(LSM6DSOX_OUT_TEMP_L, reinterpret_cast<uint8_t*>(&temperature_raw), sizeof(temperature_raw)) != 1) {
183+
return 0;
184+
}
185+
186+
/* Convert to °C. */
187+
static int const TEMPERATURE_LSB_per_DEG = 256;
188+
static int const TEMPERATURE_OFFSET_DEG = 25;
189+
190+
temperature_deg = (static_cast<int>(temperature_raw) / TEMPERATURE_LSB_per_DEG) + TEMPERATURE_OFFSET_DEG;
191+
192+
return 1;
193+
}
194+
195+
int LSM6DSOXClass::temperatureAvailable()
196+
{
197+
if (readRegister(LSM6DSOX_STATUS_REG) & 0x04) {
198+
return 1;
199+
}
200+
201+
return 0;
202+
}
203+
174204
float LSM6DSOXClass::gyroscopeSampleRate()
175205
{
176206
return 104.0F;

src/LSM6DSOX.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class LSM6DSOXClass {
4141
virtual float gyroscopeSampleRate(); // Sampling rate of the sensor.
4242
virtual int gyroscopeAvailable(); // Check for available data from gyroscope
4343

44+
// Temperature
45+
int readTemperature(int & temperature_deg);
46+
int temperatureAvailable();
4447

4548
private:
4649
int readRegister(uint8_t address);

0 commit comments

Comments
 (0)