Skip to content

Commit 74437d7

Browse files
committed
Initial import (based on MKRENV lib)
0 parents  commit 74437d7

File tree

8 files changed

+395
-0
lines changed

8 files changed

+395
-0
lines changed

README.adoc

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
= HTS221 Library for Arduino =
2+
3+
Allows you to read the temperature and humidity sensors of your Nano 33 BLE Sense.
4+
5+
6+
== License ==
7+
8+
Copyright (c) 2019 Arduino SA. All rights reserved.
9+
10+
This library is free software; you can redistribute it and/or
11+
modify it under the terms of the GNU Lesser General Public
12+
License as published by the Free Software Foundation; either
13+
version 2.1 of the License, or (at your option) any later version.
14+
15+
This library is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18+
Lesser General Public License for more details.
19+
20+
You should have received a copy of the GNU Lesser General Public
21+
License along with this library; if not, write to the Free Software
22+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

examples/ReadSensors/ReadSensors.ino

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
HTS221 - Read Sensors
3+
4+
This example reads data from the on-board HTS221 sensor of the
5+
Nano 33 BLE Sense and prints the temperature and humidity sensor
6+
values to the Serial Monitor once a second.
7+
8+
The circuit:
9+
- Arduino Nano 33 BLE Sense
10+
11+
This example code is in the public domain.
12+
*/
13+
14+
#include <Arduino_HTS221.h>
15+
16+
void setup() {
17+
Serial.begin(9600);
18+
while (!Serial);
19+
20+
if (!HTS.begin()) {
21+
Serial.println("Failed to initialize humidity temperature sensor!");
22+
while (1);
23+
}
24+
}
25+
26+
void loop() {
27+
// read all the sensor values
28+
float temperature = HTS.readTemperature();
29+
float humidity = HTS.readHumidity();
30+
31+
// print each of the sensor values
32+
Serial.print("Temperature = ");
33+
Serial.print(temperature);
34+
Serial.println(" °C");
35+
36+
Serial.print("Humidity = ");
37+
Serial.print(humidity);
38+
Serial.println(" %");
39+
40+
// print an empty line
41+
Serial.println();
42+
43+
// wait 1 second to print again
44+
delay(1000);
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
HTS221 - Read Sensors Imperial
3+
4+
This example reads data from the on-board HTS221 sensor of the
5+
Nano 33 BLE Sense then, prints the temperature and humidity sensor
6+
values in imeprial units to the Serial Monitor once a second.
7+
8+
The circuit:
9+
- Arduino Nano 33 BLE Sense
10+
11+
This example code is in the public domain.
12+
*/
13+
14+
#include <Arduino_HTS221.h>
15+
16+
void setup() {
17+
Serial.begin(9600);
18+
while (!Serial);
19+
20+
if (!HTS.begin()) {
21+
Serial.println("Failed to initialize humidity temperature sensor!");
22+
while (1);
23+
}
24+
}
25+
26+
void loop() {
27+
// Passing in FAHRENHEIT as the unit parameter to ENV.readTemperature(...),
28+
// allows you to read the sensor values in imperial units
29+
float temperature = HTS.readTemperature(FAHRENHEIT);
30+
float humidity = HTS.readHumidity();
31+
32+
// print each of the sensor values
33+
Serial.print("Temperature = ");
34+
Serial.print(temperature);
35+
Serial.println(" °F");
36+
37+
Serial.print("Humidity = ");
38+
Serial.print(humidity);
39+
Serial.println(" %");
40+
41+
// print an empty line
42+
Serial.println();
43+
44+
// wait 1 second to print again
45+
delay(1000);
46+
}

keywords.txt

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#########################################
2+
# Syntax Coloring Map For Arduino_HTS221
3+
#########################################
4+
# Class
5+
#########################################
6+
7+
Arduino_HTS221 KEYWORD1
8+
HTS221 KEYWORD1
9+
HTS KEYWORD1
10+
11+
#########################################
12+
# Methods and Functions
13+
#########################################
14+
15+
begin KEYWORD2
16+
end KEYWORD2
17+
18+
readTemperature KEYWORD2
19+
readHumidity KEYWORD2
20+
21+
#########################################
22+
# Constants
23+
#########################################
24+
25+
FAHRENHEIT LITERAL1
26+
CELSIUS LITERAL1

library.properties

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=Arduino_HTS221
2+
version=1.0.0
3+
author=Arduino
4+
maintainer=Arduino <[email protected]>
5+
sentence=Allows you to read the temperature and humidity sensors of your Nano 33 BLE Sense.
6+
paragraph=
7+
category=Sensors
8+
url=http://github.com/arduino-libraries/Arduino_HTS221
9+
architectures=*
10+
includes=Arduino_HTS221.h

src/Arduino_HTS221.h

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
This file is part of the Arduino_HTS221 library.
3+
Copyright (c) 2019 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef _ARUDINO_HTS221_H_
21+
#define _ARUDINO_HTS221_H_
22+
23+
#include "HTS.h"
24+
25+
#endif

src/HTS.cpp

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
This file is part of the Arduino_HTS221 library.
3+
Copyright (c) 2019 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include <Wire.h>
21+
22+
#include "HTS.h"
23+
24+
#define HTS221_ADDRESS 0x5F
25+
26+
#define HTS221_WHO_AM_I_REG 0x0f
27+
#define HTS221_CTRL1_REG 0x20
28+
#define HTS221_CTRL2_REG 0x21
29+
#define HTS221_STATUS_REG 0x27
30+
#define HTS221_HUMIDITY_OUT_L_REG 0x28
31+
#define HTS221_TEMP_OUT_L_REG 0x2a
32+
#define HTS221_H0_rH_x2_REG 0x30
33+
#define HTS221_H1_rH_x2_REG 0x31
34+
#define HTS221_T0_degC_x8_REG 0x32
35+
#define HTS221_T1_degC_x8_REG 0x33
36+
#define HTS221_T1_T0_MSB_REG 0x35
37+
#define HTS221_H0_T0_OUT_REG 0x36
38+
#define HTS221_H1_T0_OUT_REG 0x3a
39+
#define HTS221_T0_OUT_REG 0x3c
40+
#define HTS221_T1_OUT_REG 0x3e
41+
42+
HTS221Class::HTS221Class(TwoWire& wire) :
43+
_wire(&wire)
44+
{
45+
}
46+
47+
int HTS221Class::begin()
48+
{
49+
_wire->begin();
50+
51+
if (i2cRead(HTS221_WHO_AM_I_REG) != 0xbc) {
52+
end();
53+
54+
return 0;
55+
}
56+
57+
readHTS221Calibration();
58+
59+
// enable HTS221
60+
i2cWrite(HTS221_CTRL1_REG, 0x80);
61+
62+
return 1;
63+
}
64+
65+
void HTS221Class::end()
66+
{
67+
// disable HTS221
68+
i2cWrite(HTS221_CTRL1_REG, 0x00);
69+
70+
_wire->end();
71+
}
72+
73+
float HTS221Class::readTemperature(int units)
74+
{
75+
// trigger one shot
76+
i2cWrite(HTS221_CTRL2_REG, 0x01);
77+
78+
// wait for completion
79+
while ((i2cRead(HTS221_STATUS_REG) & 0x01) == 0) {
80+
yield();
81+
}
82+
83+
// read value and convert
84+
int16_t tout = i2cRead16(HTS221_TEMP_OUT_L_REG);
85+
float reading = (tout * _hts221TemperatureSlope + _hts221TemperatureZero);
86+
if (units == FAHRENHEIT) { // Fahrenheit = (Celsius * 9 / 5) + 32
87+
return (reading * 9.0 / 5.0) + 32.0;
88+
} else {
89+
return reading;
90+
}
91+
}
92+
93+
float HTS221Class::readHumidity()
94+
{
95+
// trigger one shot
96+
i2cWrite(HTS221_CTRL2_REG, 0x01);
97+
98+
// wait for completion
99+
while ((i2cRead(HTS221_STATUS_REG) & 0x02) == 0) {
100+
yield();
101+
}
102+
103+
// read value and convert
104+
int16_t hout = i2cRead16(HTS221_HUMIDITY_OUT_L_REG);
105+
106+
return (hout * _hts221HumiditySlope + _hts221HumidityZero);
107+
}
108+
109+
int HTS221Class::i2cRead(uint8_t reg)
110+
{
111+
_wire->beginTransmission(HTS221_ADDRESS);
112+
_wire->write(reg);
113+
if (_wire->endTransmission(false) != 0) {
114+
return -1;
115+
}
116+
117+
if (_wire->requestFrom(HTS221_ADDRESS, 1) != 1) {
118+
return -1;
119+
}
120+
121+
return _wire->read();
122+
}
123+
124+
int HTS221Class::i2cWrite(uint8_t reg, uint8_t val)
125+
{
126+
_wire->beginTransmission(HTS221_ADDRESS);
127+
_wire->write(reg);
128+
_wire->write(val);
129+
if (_wire->endTransmission() != 0) {
130+
return 0;
131+
}
132+
133+
return 1;
134+
}
135+
136+
void HTS221Class::readHTS221Calibration()
137+
{
138+
uint8_t h0rH = i2cRead(HTS221_H0_rH_x2_REG);
139+
uint8_t h1rH = i2cRead(HTS221_H1_rH_x2_REG);
140+
141+
uint16_t t0degC = i2cRead(HTS221_T0_degC_x8_REG) | ((i2cRead(HTS221_T1_T0_MSB_REG) & 0x03) << 8);
142+
uint16_t t1degC = i2cRead(HTS221_T1_degC_x8_REG) | ((i2cRead(HTS221_T1_T0_MSB_REG) & 0x0c) << 6);
143+
144+
int16_t h0t0Out = i2cRead16(HTS221_H0_T0_OUT_REG);
145+
int16_t h1t0Out = i2cRead16(HTS221_H1_T0_OUT_REG);
146+
147+
int16_t t0Out = i2cRead16(HTS221_T0_OUT_REG);
148+
int16_t t1Out = i2cRead16(HTS221_T1_OUT_REG);
149+
150+
// calculate slopes and 0 offset from calibration values,
151+
// for future calculations: value = a * X + b
152+
153+
_hts221HumiditySlope = (h1rH - h0rH) / (2.0 * (h1t0Out - h0t0Out));
154+
_hts221HumidityZero = (h0rH / 2.0) - _hts221HumiditySlope * h0t0Out;
155+
156+
_hts221TemperatureSlope = (t1degC - t0degC) / (8.0 * (t1Out - t0Out));
157+
_hts221TemperatureZero = (t0degC / 8.0) - _hts221TemperatureSlope * t0Out;
158+
}
159+
160+
HTS221Class HTS(Wire1);

0 commit comments

Comments
 (0)