Skip to content

Commit 7aa9165

Browse files
author
Wolfgang
authored
Support Temperature Sensor (#12)
1 parent 79e2976 commit 7aa9165

File tree

6 files changed

+93
-6
lines changed

6 files changed

+93
-6
lines changed

CHANGELOG

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
Arduino_LSM6DSM ?.?.? - ????.??.??
22

3+
* Add functions to use temperature sensor.
4+
35
Arduino_LSM6DSM 1.0.0 - 2019.07.19
46

57
* Initial release
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Arduino LSM6DS3 - Simple Gyroscope
3+
4+
This example reads the temperature values from the LSM6DS3
5+
sensor and continuously prints them to the Serial Monitor
6+
or Serial Plotter.
7+
8+
The circuit:
9+
- Arduino Uno WiFi Rev 2 or Arduino Nano 33 IoT
10+
11+
created 15 Mar 2020
12+
by Alex Hoppe
13+
14+
This example code is in the public domain.
15+
*/
16+
17+
#include <Arduino_LSM6DS3.h>
18+
19+
void setup() {
20+
Serial.begin(9600);
21+
while (!Serial);
22+
23+
if (!IMU.begin()) {
24+
Serial.println("Failed to initialize IMU!");
25+
26+
while (1);
27+
}
28+
29+
Serial.print("Temperature sensor sample rate = ");
30+
Serial.print(IMU.temperatureSampleRate());
31+
Serial.println(" Hz");
32+
Serial.println();
33+
Serial.println("Temperature reading in degrees C");
34+
Serial.println("T");
35+
}
36+
37+
void loop() {
38+
float t;
39+
40+
if (IMU.temperatureAvailable()) {
41+
// after IMU.readTemperature() returns, t will contain the temperature reading
42+
IMU.readTemperature(t);
43+
44+
Serial.println(t);
45+
}
46+
}

keywords.txt

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#######################################
22
# Syntax Coloring Map For Arduino_LSM6DS3
3-
#######################################
3+
#######################################
44
# Class
55
#######################################
66

@@ -9,18 +9,21 @@ LSM6DS3 KEYWORD1
99
IMU KEYWORD1
1010

1111
#######################################
12-
# Methods and Functions
13-
#######################################
12+
# Methods and Functions
13+
#######################################
1414

1515
begin KEYWORD2
1616
end KEYWORD2
1717

1818
readAcceleration KEYWORD2
1919
readGyroscope KEYWORD2
20+
readTemperature KEYWORD2
2021
gyroscopeAvailable KEYWORD2
2122
accelerationAvailable KEYWORD2
23+
temperatureAvailable KEYWORD2
2224
accelerationSampleRate KEYWORD2
2325
gyroscopeSampleRate KEYWORD2
26+
temperatureSampleRate KEYWORD2
2427

2528
#######################################
2629
# Constants

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name=Arduino_LSM6DS3
22
version=1.0.2
33
author=Arduino
44
maintainer=Arduino <[email protected]>
5-
sentence=Allows you to read the accelerometer and gyroscope values from the LSM6DS3 IMU on your Arduino Nano 33 IoT or Arduino Uno WiFi Rev2 boards.
5+
sentence=Allows you to read the accelerometer, gyroscope, and temperature values from the LSM6DS3 IMU on your Arduino Nano 33 IoT or Arduino Uno WiFi Rev2 boards.
66
paragraph=
77
category=Sensors
88
url=https://www.arduino.cc/reference/en/libraries/arduino_lsm6ds3/

src/LSM6DS3.cpp

+31-2
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,43 @@ float LSM6DS3Class::gyroscopeSampleRate()
149149
return 104.0F;
150150
}
151151

152+
int LSM6DS3Class::readTemperature(float& t)
153+
{
154+
int16_t data[1];
155+
156+
if (!readRegisters(LSM6DS3_OUT_TEMP_L, (uint8_t*)data, sizeof(data))) {
157+
t = NAN;
158+
159+
return 0;
160+
}
161+
162+
t = data[0] / 16.0 + 25;
163+
164+
return 1;
165+
}
166+
167+
int LSM6DS3Class::temperatureAvailable()
168+
{
169+
if (readRegister(LSM6DS3_STATUS_REG) & 0x04) {
170+
return 1;
171+
}
172+
173+
return 0;
174+
}
175+
176+
float LSM6DS3Class::temperatureSampleRate()
177+
{
178+
return 52.0F;
179+
}
180+
152181
int LSM6DS3Class::readRegister(uint8_t address)
153182
{
154183
uint8_t value;
155-
184+
156185
if (readRegisters(address, &value, sizeof(value)) != 1) {
157186
return -1;
158187
}
159-
188+
160189
return value;
161190
}
162191

src/LSM6DS3.h

+7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#define LSM6DS3_CTRL7_G 0X16
3434
#define LSM6DS3_CTRL8_XL 0X17
3535

36+
#define LSM6DS3_OUT_TEMP_L 0X20
37+
3638
#define LSM6DS3_OUTX_L_G 0X22
3739
#define LSM6DS3_OUTX_H_G 0X23
3840
#define LSM6DS3_OUTY_L_G 0X24
@@ -68,6 +70,11 @@ class LSM6DS3Class {
6870
virtual float gyroscopeSampleRate(); // Sampling rate of the sensor.
6971
virtual int gyroscopeAvailable(); // Check for available data from gyroscope
7072

73+
// Temperature Sensor
74+
virtual int readTemperature(float& t); // Results are in deg. C
75+
virtual float temperatureSampleRate(); // Sampling rate of the sensor.
76+
virtual int temperatureAvailable(); // Check for available data from temperature sensor
77+
7178

7279
protected:
7380
int readRegister(uint8_t address);

0 commit comments

Comments
 (0)