diff --git a/CHANGELOG b/CHANGELOG index f0e0d0d..871209d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ Arduino_LSM6DSM ?.?.? - ????.??.?? +* Add functions to use temperature sensor. + Arduino_LSM6DSM 1.0.0 - 2019.07.19 * Initial release diff --git a/examples/SimpleTempSensor/SimpleTempSensor.ino b/examples/SimpleTempSensor/SimpleTempSensor.ino new file mode 100644 index 0000000..74c183c --- /dev/null +++ b/examples/SimpleTempSensor/SimpleTempSensor.ino @@ -0,0 +1,46 @@ +/* + Arduino LSM6DS3 - Simple Gyroscope + + This example reads the temperature values from the LSM6DS3 + sensor and continuously prints them to the Serial Monitor + or Serial Plotter. + + The circuit: + - Arduino Uno WiFi Rev 2 or Arduino Nano 33 IoT + + created 15 Mar 2020 + by Alex Hoppe + + This example code is in the public domain. +*/ + +#include + +void setup() { + Serial.begin(9600); + while (!Serial); + + if (!IMU.begin()) { + Serial.println("Failed to initialize IMU!"); + + while (1); + } + + Serial.print("Temperature sensor sample rate = "); + Serial.print(IMU.temperatureSampleRate()); + Serial.println(" Hz"); + Serial.println(); + Serial.println("Temperature reading in degrees C"); + Serial.println("T"); +} + +void loop() { + float t; + + if (IMU.temperatureAvailable()) { + // after IMU.readTemperature() returns, t will contain the temperature reading + IMU.readTemperature(t); + + Serial.println(t); + } +} diff --git a/keywords.txt b/keywords.txt index 30a0846..7ccab0a 100644 --- a/keywords.txt +++ b/keywords.txt @@ -1,6 +1,6 @@ ####################################### # Syntax Coloring Map For Arduino_LSM6DS3 -####################################### +####################################### # Class ####################################### @@ -9,18 +9,21 @@ LSM6DS3 KEYWORD1 IMU KEYWORD1 ####################################### -# Methods and Functions -####################################### +# Methods and Functions +####################################### begin KEYWORD2 end KEYWORD2 readAcceleration KEYWORD2 readGyroscope KEYWORD2 +readTemperature KEYWORD2 gyroscopeAvailable KEYWORD2 accelerationAvailable KEYWORD2 +temperatureAvailable KEYWORD2 accelerationSampleRate KEYWORD2 gyroscopeSampleRate KEYWORD2 +temperatureSampleRate KEYWORD2 ####################################### # Constants diff --git a/library.properties b/library.properties index 1bd88d8..ce5c9d8 100644 --- a/library.properties +++ b/library.properties @@ -2,7 +2,7 @@ name=Arduino_LSM6DS3 version=1.0.2 author=Arduino maintainer=Arduino -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. +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. paragraph= category=Sensors url=https://www.arduino.cc/reference/en/libraries/arduino_lsm6ds3/ diff --git a/src/LSM6DS3.cpp b/src/LSM6DS3.cpp index 1386620..d3dde96 100644 --- a/src/LSM6DS3.cpp +++ b/src/LSM6DS3.cpp @@ -149,14 +149,43 @@ float LSM6DS3Class::gyroscopeSampleRate() return 104.0F; } +int LSM6DS3Class::readTemperature(float& t) +{ + int16_t data[1]; + + if (!readRegisters(LSM6DS3_OUT_TEMP_L, (uint8_t*)data, sizeof(data))) { + t = NAN; + + return 0; + } + + t = data[0] / 16.0 + 25; + + return 1; +} + +int LSM6DS3Class::temperatureAvailable() +{ + if (readRegister(LSM6DS3_STATUS_REG) & 0x04) { + return 1; + } + + return 0; +} + +float LSM6DS3Class::temperatureSampleRate() +{ + return 52.0F; +} + int LSM6DS3Class::readRegister(uint8_t address) { uint8_t value; - + if (readRegisters(address, &value, sizeof(value)) != 1) { return -1; } - + return value; } diff --git a/src/LSM6DS3.h b/src/LSM6DS3.h index c81f19f..ac3d7e3 100644 --- a/src/LSM6DS3.h +++ b/src/LSM6DS3.h @@ -33,6 +33,8 @@ #define LSM6DS3_CTRL7_G 0X16 #define LSM6DS3_CTRL8_XL 0X17 +#define LSM6DS3_OUT_TEMP_L 0X20 + #define LSM6DS3_OUTX_L_G 0X22 #define LSM6DS3_OUTX_H_G 0X23 #define LSM6DS3_OUTY_L_G 0X24 @@ -68,6 +70,11 @@ class LSM6DS3Class { virtual float gyroscopeSampleRate(); // Sampling rate of the sensor. virtual int gyroscopeAvailable(); // Check for available data from gyroscope + // Temperature Sensor + virtual int readTemperature(float& t); // Results are in deg. C + virtual float temperatureSampleRate(); // Sampling rate of the sensor. + virtual int temperatureAvailable(); // Check for available data from temperature sensor + protected: int readRegister(uint8_t address);