Weather Station (Arduino Wiring) + Lightning.Providers

Learn how to create an Arduino Wiring sketch on Raspberry Pi 2 or 3 and set up your own Weather Station using the power of Lightning.

The Sparkfun Weather Shield lets you collect weather data from your surroundings when connected to your Raspberry Pi2 2 or 3.

The shield utilizes two sensors:

  1. HTU21D Humidity and Temperature Sensor
  2. MPL3115A2 Altitude/Pressure Sensor

and can also be expanded to read wind and rain levels.

You can replicate this entire project with the shield itself or with the individual sensors.

Read on to get started!

Hardware Set Up

Hook your Raspberry Pi 2 or 3 up to the Sparkfun Weather Shield. Use the wiring diagram and photos in the carousel below for reference.

NOTE

This feature requires JavaScript. If you are experiencing problems with the carousel, make sure that JavaScript is enabled on your browser.

You can also refer to the fritzing diagram below the reference images.

Pinout Diagram (Raspberry Pi 2 or 3 –> Sparkfun weather shield):


    GND--------(black)-------GND 

    5V----------(red)--------VIN
	 
    3V3--------(brown)-------5V (shield hack; not a typo) 

    GPIO2------(yellow)------SDA 

    GPIO3------(orange)------SCL 

    GPIO5-------(green)-------D8 

    GPIO6--------(blue)-------D7 

Fritzing Diagram


RPI Pinouts

Software Set Up

Enable Lightning on your Raspberry Pi2 or 3 device using the the steps outlined in the Lightning Setup Guide.

There are two ways to go about seting up the software.

Option 1: Clone the Entire Solution

The easiest way to get your Weather Station up and running is to clone the entire solution from the "Lightning" folder of this GitHub Repo onto your local machine.

Option 2: Set Up Your Solution Manually

If you'd prefer to set up your solution manually, follow these steps:

  1. Create a new project by following the Arduino Wiring Project Setup Guide.
  2. Clone the following libraries from GitHub into the WeatherShield folder(at the same level as your .vcxproj file): MPL3115A2 Breakout and HTU21D Breakout.
  3. Replace the existing code in your main .ino file ({yourProject}.ino, where {yourProject} is whatever you named the project when you created it) with the following code:
            /*
            HTU21D Humidity Sensor Example Code
            By: Nathan Seidle
            SparkFun Electronics
            Date: September 15th, 2013
            License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
            Uses the HTU21D library to display the current humidity and temperature
            Open serial monitor at 9600 baud to see readings. Errors 998 if not sensor is detected. Error 999 if CRC is bad.
            Hardware Connections (Breakoutboard to Arduino):
            -VCC = 3.3V
            -GND = GND
            -SDA = A4 (use inline 10k resistor if your board is 5V)
            -SCL = A5 (use inline 10k resistor if your board is 5V)
            */
    
            #include <Wire.h>
            #include "HTU21D\Libraries\Arduino\src\SparkFunHTU21D.h"
            #include "MPL3115A2\Libraries\Arduino\src\SparkFunMPL3115A2.h"
    
            //Create an instance of the object
            HTU21D myHumidity;
            MPL3115A2 myPressure;
    
            bool barometerMode = true;
    
            void setup()
            {
                Wire.begin();
                Log( "WeatherShield Example!" );
    
                myHumidity.begin();
                myPressure.begin();
    
                if( barometerMode )
                {
                    myPressure.setModeBarometer(); // Measure pressure in Pascals from 20 to 110 kPa
                }
                else
                {
                    myPressure.setModeAltimeter(); // Measure altitude above sea level in meters
                }
    
                myPressure.setOversampleRate( 7 ); // Set Oversample to the recommended 128
                myPressure.enableEventFlags(); // Enable all three pressure and temp event flags
            }
    
            void loop()
            {
                /*
                * from HTU21D Sample - SparkFun_HTU21D_Demo.ino
                */
                float humd = myHumidity.readHumidity();
                float temp = myHumidity.readTemperature();
    
                Log( "Time:" );
                Log( millis().ToString()->Begin() );
                Log( " Temperature: " );
                Log( temp.ToString()->Begin() );
                Log( "C" );
                Log( " Humidity: " );
                Log( humd.ToString()->Begin() );
    
                if( barometerMode )
                {
                    /*
                    * From MPL3115A2 Sample - SparkFunPressure.ino
                    * MODE MUST BE IN BAROMETER
                    */
    
                    float altitude = myPressure.readAltitude();
                    Log( " Altitude(m): " );
                    Log( altitude.ToString()->Begin() );
    
                    altitude = myPressure.readAltitudeFt();
                    Log( " Altitude(ft): " );
                    Log( altitude.ToString()->Begin() );
    
                    float pressure = myPressure.readPressure();
                    Log( " Pressure(Pa): " );
                    Log( pressure.ToString()->Begin() );
    
                    float temperature = myPressure.readTemp();
                    Log( " Temp(c): " );
                    Log( temperature.ToString()->Begin() );
    
                    temperature = myPressure.readTempF();
                    Log( " Temp(f): " );
                    Log( temp.ToString()->Begin() );
    
    
                    /*
                    * From MPL3115A2 Sample - SparkFunBarometricHgInch.ino
                    * MODE MUST BE IN BAROMETER
                    */
                    const int station_elevation_m = 1638; //Accurate for the roof on my house
                                                          //1 pascal = 0.01 millibars
                    pressure /= 100; //pressure is now in millibars
    
                    float part1 = pressure - 0.3; //Part 1 of formula
    
                    const float part2 = 8.42288 / 100000.0;
                    float part3 = pow( ( pressure - 0.3 ), 0.190284 );
                    float part4 = (float)station_elevation_m / part3;
                    float part5 = ( 1.0 + ( part2 * part4 ) );
                    float part6 = pow( part5, ( 1.0 / 0.190284 ) );
                    float altimeter_setting_pressure_mb = part1 * part6; //Output is now in adjusted millibars
                    float baroin = altimeter_setting_pressure_mb * 0.02953;
    
                    Log( " Altimeter setting InHg: " );
                    Log( baroin.ToString()->Begin() );
                }
                else    //altimeter mode
                {
                    /*
                    * From SparkFunAltimeter.ino
                    * MODE MUST BE IN ALTIMETER
                    */
                    float altitude = myPressure.readAltitude();
                    Log( "Altitude(m): " );
                    Log( altitude.ToString()->Begin() );
    
                    altitude = myPressure.readAltitudeFt();
                    Log( " Altitude(ft): " );
                    Log( altitude.ToString()->Begin() );
    
                    float pressure = myPressure.readPressure();
                    Log( "Pressure(Pa): " );
                    Log( pressure.ToString()->Begin() );
    
                    float temperature = myPressure.readTemp();
                    Log( " Temp(c): " );
                    Log( temperature.ToString()->Begin() );
    
                    temperature = myPressure.readTempF();
                    Log( " Temp(f): " );
                    Log( temperature.ToString()->Begin() );
                }
    
                Log( "\n" );
    
                delay( 1000 );
            }
    
          

Note:

Pay special attention to this line near the top of the sketch file: bool barometerMode = true;. The MPL3115A2 sensor has two modes, and can operate differently under each mode. You can change this variable to false to disable barometer mode and enable altimeter mode instead! The result of the sketch will automatically change under the new mode!

Build and Deploy Your App

Press F5 to build and deploy your project. Refer to the Arduino Wiring Project Guide for more instructions on how to deploy your app!

Once your project is deployed, you will see data appear in the output console while the program is running.

Result

Watch your Output window in Visual Studio. The sketch will start reporting the data it gathers from the Weather Shield!

Having trouble?

Refer to the Arduino Wiring Porting Guide for common issues and concerns when working with Arduino Wiring sketches.


« Return to Samples

Sample code

Language: Wiring

Tags: wiring, beginner, I2C

Verified to work with:
Windows 10 IoT Core: Version 10.0.10586.0
Visual Studio 2015 update 2
Windows SDK: Version 10586
(Included with Visual Studio Update 2)


Edit this page on GitHub


Was this page helpful?
Additional feedback?
Thank you! We appreciate your feedback.
Follow us