Skip to content

Commit 09f7f20

Browse files
Rocketctsandeepmistry
authored andcommitted
Add imperial unit support
Added imperial units support to MKRENV library - Added enum scale factors - Renamed readLux in readIlluminance to comply the others illuminance scale factors - Added scale factor support to pressure temperature and illuminance API - added example to show how to read in fahrenheit and foot candle the temperature and the illuminance sensor - added PSI unit Support to readPressure - Added enum's words in keywords.txt
1 parent 0d91a6d commit 09f7f20

File tree

4 files changed

+116
-12
lines changed

4 files changed

+116
-12
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
MKR ENV Shield - Read Sensors Imperial
3+
4+
This example reads the sensors on-board the MKR ENV shield
5+
and prints them in imperial units to the Serial Monitor once a second.
6+
7+
The circuit:
8+
- Arduino MKR board
9+
- Arduino MKR ENV Shield attached
10+
11+
This example code is in the public domain.
12+
*/
13+
14+
#include <Arduino_MKRENV.h>
15+
16+
void setup() {
17+
Serial.begin(9600);
18+
while (!Serial);
19+
20+
if (!ENV.begin()) {
21+
Serial.println("Failed to initialize MKR ENV shield!");
22+
while (1);
23+
}
24+
}
25+
26+
void loop() {
27+
// Passing in FAHRENHEIT as the unit paramter to ENV.readTemperature(...),
28+
// PSI to readPressure(...) and FOOTCANDLE to readIlluminance(...)
29+
// allows you to read the sensors values in imperial units
30+
float temperature = ENV.readTemperature(FAHRENHEIT);
31+
float humidity = ENV.readHumidity();
32+
float pressure = ENV.readPressure(PSI);
33+
float illuminance = ENV.readIlluminance(FOOTCANDLE);
34+
float uva = ENV.readUVA();
35+
float uvb = ENV.readUVB();
36+
float uvIndex = ENV.readUVIndex();
37+
38+
// print each of the sensor values
39+
Serial.print("Temperature = ");
40+
Serial.print(temperature);
41+
Serial.println(" °F");
42+
43+
Serial.print("Humidity = ");
44+
Serial.print(humidity);
45+
Serial.println(" %");
46+
47+
Serial.print("Pressure = ");
48+
Serial.print(pressure);
49+
Serial.println(" psi");
50+
51+
Serial.print("Illuminance = ");
52+
Serial.print(illuminance);
53+
Serial.println(" fc");
54+
55+
Serial.print("UVA = ");
56+
Serial.println(uva);
57+
58+
Serial.print("UVB = ");
59+
Serial.println(uvb);
60+
61+
Serial.print("UV Index = ");
62+
Serial.println(uvIndex);
63+
64+
// print an empty line
65+
Serial.println();
66+
67+
// wait 1 second to print again
68+
delay(1000);
69+
}

keywords.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,12 @@ readUVIndex KEYWORD2
2626
#######################################
2727
# Constants
2828
#######################################
29+
30+
FAHRENHEIT LITERAL1
31+
CELSIUS LITERAL1
32+
PSI LITERAL1
33+
MILLIBAR LITERAL1
34+
KILOPASCAL LITERAL1
35+
FOOTCANDLE LITERAL1
36+
METERCANDLE LITERAL1
37+
LUX LITERAL1

src/MKRENV.cpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ void ENVClass::end()
109109
pinMode(LIGHT_SENSOR_PIN, INPUT);
110110
}
111111

112-
float ENVClass::readTemperature()
112+
float ENVClass::readTemperature(int units)
113113
{
114114
// trigger one shot
115115
i2cWrite(HTS221_ADDRESS, HTS221_CTRL2_REG, 0x01);
@@ -121,8 +121,12 @@ float ENVClass::readTemperature()
121121

122122
// read value and convert
123123
int16_t tout = i2cRead16(HTS221_ADDRESS, HTS221_TEMP_OUT_L_REG);
124-
125-
return (tout * _hts221TemperatureSlope + _hts221TemperatureZero);
124+
float reading = (tout * _hts221TemperatureSlope + _hts221TemperatureZero);
125+
if (units == FAHRENHEIT) { // Fahrenheit = (Celsius * 9 / 5) + 32
126+
return (reading * 9.0 / 5.0) + 32.0;
127+
} else {
128+
return reading;
129+
}
126130
}
127131

128132
float ENVClass::readHumidity()
@@ -141,7 +145,7 @@ float ENVClass::readHumidity()
141145
return (hout * _hts221HumiditySlope + _hts221HumidityZero);
142146
}
143147

144-
float ENVClass::readPressure()
148+
float ENVClass::readPressure(int units)
145149
{
146150
// trigger one shot
147151
i2cWrite(LPS22HB_ADDRESS, LPS22HB_CTRL2_REG, 0x01);
@@ -151,19 +155,31 @@ float ENVClass::readPressure()
151155
yield();
152156
}
153157

154-
// read value and convert
155-
return (i2cRead(LPS22HB_ADDRESS, LPS22HB_PRESS_OUT_XL_REG) |
158+
float reading = (i2cRead(LPS22HB_ADDRESS, LPS22HB_PRESS_OUT_XL_REG) |
156159
(i2cRead(LPS22HB_ADDRESS, LPS22HB_PRESS_OUT_L_REG) << 8) |
157160
(i2cRead(LPS22HB_ADDRESS, LPS22HB_PRESS_OUT_H_REG) << 16)) / 40960.0;
161+
162+
if (units == MILLIBAR) { // 1 kPa = 10 MILLIBAR
163+
return reading * 10;
164+
} else if (units == PSI) { // 1 kPa = 0.145038 PSI
165+
return reading * 0.145038;
166+
} else {
167+
return reading;
168+
}
158169
}
159170

160-
float ENVClass::readIlluminance()
171+
float ENVClass::readIlluminance(int units)
161172
{
162173
// read analog value and convert to mV
163174
float mV = (analogRead(_lightSensorPin) * 3300.0) / 1023.0;
164175

165176
// 2 mV per lux
166-
return (mV / 2.0);
177+
float reading = (mV / 2.0); // Readings are in Lux scale
178+
if (units == FOOTCANDLE) { // 1 Lux = 0.092903 Foot-Candle
179+
return reading * 0.092903;
180+
} else {
181+
return reading; // 1 Lux = 1 Meter-Candle
182+
}
167183
}
168184

169185
// UV formula's and constants based on:

src/MKRENV.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
#include <Arduino.h>
2424
#include <Wire.h>
2525

26+
enum {
27+
FAHRENHEIT,
28+
CELSIUS,
29+
PSI,
30+
MILLIBAR,
31+
KILOPASCAL,
32+
FOOTCANDLE,
33+
METERCANDLE,
34+
LUX = METERCANDLE
35+
};
2636

2737
class ENVClass {
2838
public:
@@ -31,16 +41,16 @@ class ENVClass {
3141
int begin();
3242
void end();
3343

34-
float readTemperature();
44+
float readTemperature(int units = CELSIUS);
3545
float readHumidity();
36-
float readPressure();
37-
float readIlluminance();
46+
float readPressure(int units = KILOPASCAL);
47+
float readIlluminance(int units = LUX);
3848
float readUVA();
3949
float readUVB();
4050
float readUVIndex();
4151

4252
// deprecated, use readIlluminance() instead
43-
float readLux() { return readIlluminance(); }
53+
float readLux() { return readIlluminance(LUX); }
4454

4555
private:
4656
int i2cRead(uint8_t address, uint8_t reg);

0 commit comments

Comments
 (0)