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