Skip to content

Commit 7d15a9f

Browse files
committed
Initial commit
0 parents  commit 7d15a9f

File tree

8 files changed

+353
-0
lines changed

8 files changed

+353
-0
lines changed

README.adoc

Lines changed: 22 additions & 0 deletions
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
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
HS300x - Read Sensors
3+
4+
This example reads data from the on-board HS300x 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 R2
10+
11+
This example code is in the public domain.
12+
*/
13+
14+
#include <Arduino_HS300x.h>
15+
16+
void setup() {
17+
Serial.begin(9600);
18+
while (!Serial);
19+
20+
if (!HS300x.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 = HS300x.readTemperature();
29+
float humidity = HS300x.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+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
HS300x - Read Sensors Imperial
3+
4+
This example reads data from the on-board HS300x 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 R2
10+
11+
This example code is in the public domain.
12+
*/
13+
14+
#include <Arduino_HS300x.h>
15+
16+
void setup() {
17+
Serial.begin(9600);
18+
while (!Serial);
19+
20+
if (!HS300x.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 = HS300x.readTemperature(FAHRENHEIT);
30+
float humidity = HS300x.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

Lines changed: 26 additions & 0 deletions
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_HS300x KEYWORD1
8+
HS300x KEYWORD1
9+
HS3003 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

Lines changed: 10 additions & 0 deletions
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_HS300x.h

Lines changed: 25 additions & 0 deletions
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 "HS300x.h"
24+
25+
#endif

src/HS300x.cpp

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
This file is part of the Arduino_HS300x 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+
#include "HS300x.h"
22+
23+
#define HS300X_ADR 0x44
24+
#define HS300X_TEMP_MULTY 0.010071415
25+
#define HS300X_TEMP_MIN 40
26+
#define HS300X_HUMD_MULTY 0.006163516
27+
#define HS300X_MAX_ITERATION 100
28+
#define HS300X_DELAY_MEASUREMENT 35 // typical on datasheet 16.90 ms, rounded up a little (35ms = 1 try)
29+
#define HS300X_DELAY_ITERATION 1
30+
31+
#define HS300X_STALE_DATA 2
32+
#define HS300X_OK 1
33+
#define HS300X_ERROR_SENSOR_BUSY 0
34+
#define HS300X_ERROR_COLLISION_I2C -1
35+
36+
int8_t HS300xClass::_measurementReq(){
37+
int8_t _status = 0;
38+
uint8_t _iteration = 0;
39+
_wire->beginTransmission(HS300X_ADR);
40+
_wire->write((uint8_t)0);
41+
if (_wire->endTransmission(true) != 0) {
42+
return HS300X_ERROR_COLLISION_I2C;
43+
}
44+
delay(HS300X_DELAY_MEASUREMENT);
45+
do {
46+
_status = _readSensor();
47+
_iteration ++;
48+
if (_iteration > HS300X_MAX_ITERATION) return HS300X_ERROR_SENSOR_BUSY;
49+
delay(HS300X_DELAY_ITERATION);
50+
} while (!_status);
51+
return _status;
52+
}
53+
54+
uint8_t HS300xClass::_readSensor(){
55+
uint16_t _rawTempMSB;
56+
uint16_t _rawTemp;
57+
uint16_t _rawHumMSB;
58+
uint16_t _rawHum;
59+
uint8_t _rawStatus;
60+
61+
_wire->requestFrom(HS300X_ADR, 4); //true, stop message after transmission & releas the I2C bus
62+
if (_wire->available() != 4) {
63+
return 0;
64+
}
65+
_rawHumMSB = _wire->read() << 8; // MSB
66+
_rawHum = _wire->read(); // LSB
67+
_rawTempMSB = _wire->read() << 8;
68+
_rawTemp= _wire->read();
69+
70+
_rawHum |= _rawHumMSB;
71+
_rawTemp |= _rawTempMSB;
72+
73+
_rawStatus = _rawTemp >> 14;
74+
_rawHum = _rawHum & 0x3FFF; // mask 2 bit first
75+
_rawTemp = _rawTemp >>2; // mask 2 bit last
76+
if (_rawHum == 0x3FFF) return 0;
77+
if (_rawTemp == 0x3FFF) return 0;
78+
_temperature = (_rawTemp* HS300X_TEMP_MULTY) - HS300X_TEMP_MIN;
79+
_humidity = _rawHum * HS300X_HUMD_MULTY;
80+
return _rawStatus + 1;
81+
}
82+
83+
HS300xClass::HS300xClass(TwoWire& wire) :
84+
_wire(&wire)
85+
{
86+
}
87+
88+
int HS300xClass::begin()
89+
{
90+
_wire->begin();
91+
return 1;
92+
}
93+
94+
void HS300xClass::end()
95+
{
96+
// TODO: add disable HS300x
97+
98+
_wire->end();
99+
}
100+
101+
float HS300xClass::readTemperature(int units)
102+
{
103+
if (_measurementReq() <= 0) {
104+
return NAN;
105+
}
106+
107+
if (units == FAHRENHEIT) { // Fahrenheit = (Celsius * 9 / 5) + 32
108+
return (_temperature * 9.0 / 5.0) + 32.0;
109+
} else {
110+
return _temperature;
111+
}
112+
}
113+
114+
float HS300xClass::readHumidity()
115+
{
116+
if (_measurementReq() <= 0) {
117+
return NAN;
118+
}
119+
120+
return _humidity;
121+
}
122+
123+
#ifdef ARDUINO_ARDUINO_NANO33BLE
124+
HS300xClass HS300x(Wire1);
125+
#else
126+
HS300xClass HS300x(Wire);
127+
#endif

src/HS300x.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 _HTS_H_
21+
#define _HTS_H_
22+
23+
#include <Arduino.h>
24+
#include <Wire.h>
25+
26+
enum {
27+
FAHRENHEIT,
28+
CELSIUS
29+
};
30+
31+
class HS300xClass {
32+
public:
33+
HS300xClass(TwoWire& wire);
34+
35+
int begin();
36+
void end();
37+
38+
float readTemperature(int units = CELSIUS);
39+
float readHumidity();
40+
41+
private:
42+
TwoWire* _wire;
43+
float _humidity;
44+
float _temperature;
45+
uint8_t _status;
46+
uint8_t _readSensor();
47+
int8_t _measurementReq();
48+
};
49+
50+
extern HS300xClass HS300x;
51+
52+
#endif

0 commit comments

Comments
 (0)