Skip to content

Purposed Version 1.1.0 - Temperature Sensor Support #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Arduino_LSM6DSM ?.?.? - ????.??.??

* Add functions to use temperature sensor.

Arduino_LSM6DSM 1.0.0 - 2019.07.19

* Initial release
46 changes: 46 additions & 0 deletions examples/SimpleTempSensor/SimpleTempSensor.ino
Original file line number Diff line number Diff line change
@@ -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 <Arduino_LSM6DS3.h>

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);
}
}
11 changes: 7 additions & 4 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#######################################
# Syntax Coloring Map For Arduino_LSM6DS3
#######################################
#######################################
# Class
#######################################

Expand All @@ -9,19 +9,22 @@ 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
#######################################
#######################################
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name=Arduino_LSM6DS3
version=1.0.0
author=Arduino
maintainer=Arduino <[email protected]>
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/en/Reference/Arduino_LSM6DS3
Expand Down
35 changes: 33 additions & 2 deletions src/LSM6DS3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,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
Expand Down Expand Up @@ -176,14 +178,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;
}

Expand Down
7 changes: 6 additions & 1 deletion src/LSM6DS3.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ class LSM6DS3Class {
// Gyroscope
virtual int readGyroscope(float& x, float& y, float& z); // Results are in degrees/second.
virtual float gyroscopeSampleRate(); // Sampling rate of the sensor.
virtual int gyroscopeAvailable(); // Check for available data from gyroscopeAvailable
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


private:
Expand Down