Skip to content

Commit 6011b5d

Browse files
author
JoelEB
committed
Updated code for new revision V12
1 parent cecfe30 commit 6011b5d

File tree

7 files changed

+1235
-0
lines changed

7 files changed

+1235
-0
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
2+
3+
/*
4+
Weather Shield Example
5+
By: Nathan Seidle
6+
SparkFun Electronics
7+
Date: June 10th, 2016
8+
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
9+
10+
This example prints the current humidity, air pressure, temperature and light levels.
11+
12+
The weather shield is capable of a lot. Be sure to checkout the other more advanced examples for creating
13+
your own weather station.
14+
15+
Updated by Joel Bartlett
16+
03/02/2017
17+
Removed HTU21D code and replaced with Si7021
18+
19+
*/
20+
21+
#include <Wire.h> //I2C needed for sensors
22+
#include "SparkFunMPL3115A2.h" //Pressure sensor - Search "SparkFun MPL3115" and install from Library Manager
23+
#include "SparkFun_Si7021_Breakout_Library.h" //Humidity sensor - Search "SparkFun Si7021" and install from Library Manager
24+
25+
MPL3115A2 myPressure; //Create an instance of the pressure sensor
26+
Weather myHumidity;//Create an instance of the humidity sensor
27+
28+
//Hardware pin definitions
29+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
30+
const byte STAT_BLUE = 7;
31+
const byte STAT_GREEN = 8;
32+
33+
const byte REFERENCE_3V3 = A3;
34+
const byte LIGHT = A1;
35+
const byte BATT = A2;
36+
37+
//Global Variables
38+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
39+
long lastSecond; //The millis counter to see when a second rolls by
40+
41+
void setup()
42+
{
43+
Serial.begin(9600);
44+
Serial.println("Weather Shield Example");
45+
46+
pinMode(STAT_BLUE, OUTPUT); //Status LED Blue
47+
pinMode(STAT_GREEN, OUTPUT); //Status LED Green
48+
49+
pinMode(REFERENCE_3V3, INPUT);
50+
pinMode(LIGHT, INPUT);
51+
52+
//Configure the pressure sensor
53+
myPressure.begin(); // Get sensor online
54+
myPressure.setModeBarometer(); // Measure pressure in Pascals from 20 to 110 kPa
55+
myPressure.setOversampleRate(7); // Set Oversample to the recommended 128
56+
myPressure.enableEventFlags(); // Enable all three pressure and temp event flags
57+
58+
//Configure the humidity sensor
59+
myHumidity.begin();
60+
61+
lastSecond = millis();
62+
63+
Serial.println("Weather Shield online!");
64+
}
65+
66+
void loop()
67+
{
68+
//Print readings every second
69+
if (millis() - lastSecond >= 1000)
70+
{
71+
digitalWrite(STAT_BLUE, HIGH); //Blink stat LED
72+
73+
lastSecond += 1000;
74+
75+
//Check Humidity Sensor
76+
float humidity = myHumidity.getRH();
77+
78+
if (humidity == 998) //Humidty sensor failed to respond
79+
{
80+
Serial.println("I2C communication to sensors is not working. Check solder connections.");
81+
82+
//Try re-initializing the I2C comm and the sensors
83+
myPressure.begin();
84+
myPressure.setModeBarometer();
85+
myPressure.setOversampleRate(7);
86+
myPressure.enableEventFlags();
87+
myHumidity.begin();
88+
}
89+
else
90+
{
91+
Serial.print("Humidity = ");
92+
Serial.print(humidity);
93+
Serial.print("%,");
94+
float temp_h = myHumidity.getTempF();
95+
Serial.print(" temp_h = ");
96+
Serial.print(temp_h, 2);
97+
Serial.print("F,");
98+
99+
//Check Pressure Sensor
100+
float pressure = myPressure.readPressure();
101+
Serial.print(" Pressure = ");
102+
Serial.print(pressure);
103+
Serial.print("Pa,");
104+
105+
//Check tempf from pressure sensor
106+
float tempf = myPressure.readTempF();
107+
Serial.print(" temp_p = ");
108+
Serial.print(tempf, 2);
109+
Serial.print("F,");
110+
111+
//Check light sensor
112+
float light_lvl = get_light_level();
113+
Serial.print(" light_lvl = ");
114+
Serial.print(light_lvl);
115+
Serial.print("V,");
116+
117+
//Check batt level
118+
float batt_lvl = get_battery_level();
119+
Serial.print(" VinPin = ");
120+
Serial.print(batt_lvl);
121+
Serial.print("V");
122+
123+
Serial.println();
124+
}
125+
126+
digitalWrite(STAT_BLUE, LOW); //Turn off stat LED
127+
}
128+
129+
delay(100);
130+
}
131+
132+
//Returns the voltage of the light sensor based on the 3.3V rail
133+
//This allows us to ignore what VCC might be (an Arduino plugged into USB has VCC of 4.5 to 5.2V)
134+
float get_light_level()
135+
{
136+
float operatingVoltage = analogRead(REFERENCE_3V3);
137+
138+
float lightSensor = analogRead(LIGHT);
139+
140+
operatingVoltage = 3.3 / operatingVoltage; //The reference voltage is 3.3V
141+
142+
lightSensor = operatingVoltage * lightSensor;
143+
144+
return (lightSensor);
145+
}
146+
147+
//Returns the voltage of the raw pin based on the 3.3V rail
148+
//This allows us to ignore what VCC might be (an Arduino plugged into USB has VCC of 4.5 to 5.2V)
149+
//Battery level is connected to the RAW pin on Arduino and is fed through two 5% resistors:
150+
//3.9K on the high side (R1), and 1K on the low side (R2)
151+
float get_battery_level()
152+
{
153+
float operatingVoltage = analogRead(REFERENCE_3V3);
154+
155+
float rawVoltage = analogRead(BATT);
156+
157+
operatingVoltage = 3.30 / operatingVoltage; //The reference voltage is 3.3V
158+
159+
rawVoltage = operatingVoltage * rawVoltage; //Convert the 0 to 1023 int to actual voltage on BATT pin
160+
161+
rawVoltage *= 4.90; //(3.9k+1k)/1k - multiple BATT voltage by the voltage divider to get actual system voltage
162+
163+
return (rawVoltage);
164+
}
165+
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
2+
3+
/*
4+
Weather Shield Example
5+
By: Nathan Seidle
6+
SparkFun Electronics
7+
Date: June 10th, 2016
8+
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
9+
10+
This example prints the current humidity, air pressure, temperature and light levels.
11+
12+
The weather shield is capable of a lot. Be sure to checkout the other more advanced examples for creating
13+
your own weather station.
14+
15+
*/
16+
17+
#include <Wire.h> //I2C needed for sensors
18+
#include "SparkFunMPL3115A2.h" //Pressure sensor - Search "SparkFun MPL3115" and install from Library Manager
19+
#include "SparkFun_Si7021_Breakout_Library.h" //Humidity sensor - Search "SparkFun Si7021" and install from Library Manager
20+
21+
MPL3115A2 myPressure; //Create an instance of the pressure sensor
22+
Weather myHumidity;//Create an instance of the humidity sensor
23+
24+
//Hardware pin definitions
25+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
26+
const byte STAT_BLUE = 7;
27+
const byte STAT_GREEN = 8;
28+
29+
const byte REFERENCE_3V3 = A3;
30+
const byte LIGHT = A1;
31+
const byte BATT = A2;
32+
33+
//Global Variables
34+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
35+
long lastSecond; //The millis counter to see when a second rolls by
36+
37+
void setup()
38+
{
39+
Serial.begin(9600);
40+
Serial.println("Weather Shield Example");
41+
42+
pinMode(STAT_BLUE, OUTPUT); //Status LED Blue
43+
pinMode(STAT_GREEN, OUTPUT); //Status LED Green
44+
45+
pinMode(REFERENCE_3V3, INPUT);
46+
pinMode(LIGHT, INPUT);
47+
48+
//Configure the pressure sensor
49+
myPressure.begin(); // Get sensor online
50+
myPressure.setModeBarometer(); // Measure pressure in Pascals from 20 to 110 kPa
51+
myPressure.setOversampleRate(7); // Set Oversample to the recommended 128
52+
myPressure.enableEventFlags(); // Enable all three pressure and temp event flags
53+
54+
//Configure the humidity sensor
55+
myHumidity.begin();
56+
57+
lastSecond = millis();
58+
59+
Serial.println("Weather Shield online!");
60+
}
61+
62+
void loop()
63+
{
64+
//Print readings every second
65+
if (millis() - lastSecond >= 1000)
66+
{
67+
digitalWrite(STAT_BLUE, HIGH); //Blink stat LED
68+
69+
lastSecond += 1000;
70+
71+
//Check Humidity Sensor
72+
float humidity = myHumidity.getRH();
73+
74+
if (humidity == 998) //Humidty sensor failed to respond
75+
{
76+
Serial.println("I2C communication to sensors is not working. Check solder connections.");
77+
78+
//Try re-initializing the I2C comm and the sensors
79+
myPressure.begin();
80+
myPressure.setModeBarometer();
81+
myPressure.setOversampleRate(7);
82+
myPressure.enableEventFlags();
83+
myHumidity.begin();
84+
}
85+
else
86+
{
87+
Serial.print("Humidity = ");
88+
Serial.print(humidity);
89+
Serial.print("%,");
90+
float temp_h = myHumidity.getTempF();
91+
Serial.print(" temp_h = ");
92+
Serial.print(temp_h, 2);
93+
Serial.print("F,");
94+
95+
//Check Pressure Sensor
96+
float pressure = myPressure.readPressure();
97+
Serial.print(" Pressure = ");
98+
Serial.print(pressure);
99+
Serial.print("Pa,");
100+
101+
//Check tempf from pressure sensor
102+
float tempf = myPressure.readTempF();
103+
Serial.print(" temp_p = ");
104+
Serial.print(tempf, 2);
105+
Serial.print("F,");
106+
107+
//Check light sensor
108+
float light_lvl = get_light_level();
109+
Serial.print(" light_lvl = ");
110+
Serial.print(light_lvl);
111+
Serial.print("V,");
112+
113+
//Check batt level
114+
float batt_lvl = get_battery_level();
115+
Serial.print(" VinPin = ");
116+
Serial.print(batt_lvl);
117+
Serial.print("V");
118+
119+
Serial.println();
120+
}
121+
122+
digitalWrite(STAT_BLUE, LOW); //Turn off stat LED
123+
}
124+
125+
delay(100);
126+
}
127+
128+
//Returns the voltage of the light sensor based on the 3.3V rail
129+
//This allows us to ignore what VCC might be (an Arduino plugged into USB has VCC of 4.5 to 5.2V)
130+
float get_light_level()
131+
{
132+
float operatingVoltage = analogRead(REFERENCE_3V3);
133+
134+
float lightSensor = analogRead(LIGHT);
135+
136+
operatingVoltage = 3.3 / operatingVoltage; //The reference voltage is 3.3V
137+
138+
lightSensor = operatingVoltage * lightSensor;
139+
140+
return (lightSensor);
141+
}
142+
143+
//Returns the voltage of the raw pin based on the 3.3V rail
144+
//This allows us to ignore what VCC might be (an Arduino plugged into USB has VCC of 4.5 to 5.2V)
145+
//Battery level is connected to the RAW pin on Arduino and is fed through two 5% resistors:
146+
//3.9K on the high side (R1), and 1K on the low side (R2)
147+
float get_battery_level()
148+
{
149+
float operatingVoltage = analogRead(REFERENCE_3V3);
150+
151+
float rawVoltage = analogRead(BATT);
152+
153+
operatingVoltage = 3.30 / operatingVoltage; //The reference voltage is 3.3V
154+
155+
rawVoltage = operatingVoltage * rawVoltage; //Convert the 0 to 1023 int to actual voltage on BATT pin
156+
157+
rawVoltage *= 4.90; //(3.9k+1k)/1k - multiple BATT voltage by the voltage divider to get actual system voltage
158+
159+
return (rawVoltage);
160+
}
161+

0 commit comments

Comments
 (0)