CxD Archive
Internet of Things Project
Internet of Things Project
  • IoT Project Background
  • IoT Project Challenge
  • IoT Project Outline
    • 1-1 Smartphone Research
    • 1-2 Smart Thermostat Research
    • 1-3 Smart Device Research
    • 1-4 Introduction to Photon Kit
    • 1-5 Build Smart Lightbulb
    • 1-6 Generate Project Ideas
    • 1-7 Build Smart Thermostat
    • 2-1 Evaluate Project Ideas
    • 2-2 Plan User Research
    • 2-3 Compile Research Data
    • 2-4 Construct Personas
    • 2-5 Construct Journey Map
    • 2-6 Define Value Proposition
    • 2-7 Define Design Requirements
    • 3-1 Generate Solution Ideas
    • 3-2 Evaluate and Refine Solution Ideas
    • 3-3 Create System Model of Solution
    • 3-4 Create Wireflow for Web App
    • 3-5 Create Storyboard for Solution
    • 3-6 Evaluate Solution Design
    • 3-7 Present Project Proposal
    • 4-1 Track Progress and Issues
    • 4-2 Build Smart Device
    • 4-3 Create Photon App
    • 4-4 Create Web App
    • 4-5 Conduct Integration Testing
    • 4-6 Create Product Demo Video
    • 4-7 Create Product Marketing Website
    • 5-1 Evaluate Smart Device and Web App
    • 5-2 Evaluate Product Marketing Website
    • 5-3 Improve Solution Based on Evaluations
    • 6-1 Create Project Poster
    • 6-2 Present Project to Public
    • 6-3 Write Personal Reflection
  • References for Wiring & Coding
    • Creating Circuits
    • Wiring Programming Language
    • Particle Cloud
    • Hello World App for Photon
    • LED Light
    • Push Button
    • Micro OLED Display
    • Humidity and Temp Sensor
    • Speaker
    • Servo Motor
    • Motion Sensor
    • Magnetic Switch
    • Potentiometer
    • Photocell
    • Moisture Sensor
    • Accelerometer
    • RFID Reader
    • Fingerprint Scanner
    • GPS Receiver
    • Camera
    • Microphone
    • Force Sensitive Resistor
    • Keypad
    • Continuous Rotation Servo Motor
  • Glossary
  • Updated Project Guidebook
  • Updated Code Guidebok
Powered by GitBook
On this page
  • Code for RHT03 Sensor
  • #Include Library
  • Global Variables
  • setup( ) function
  • Read Sensor Measurements
Export as PDF
  1. References for Wiring & Coding

Humidity and Temp Sensor

PreviousMicro OLED DisplayNextSpeaker

Last updated 6 years ago

The can measure the relative humidity and temperature of the air.

The front of the RHT03 sensor has openings to allow air in. Line up the sensor pins with different numbered rows on a breadboard, and push down to insert the sensor pins. Then use jumper wires to connect these rows to the Photon.

Looking at the front of the sensor, if these pins were numbered left to right as 1-4, the jumper wire connections would be:

RHT03 Pin

Photon Pin

Pin 1 - Power

3.3V or V-USB (5V)

Pin 2 - Data

any I/O pin

Pin 3 - Unused

(none)

Pin 4 - Ground

GND

The RHT03 sensor has 4 pins, but only 3 of them need to be connected.

Code for RHT03 Sensor

#Include Library

Your Photon app must include a code library with special functions that will allow you to use the RHT03 sensor.

  1. In Particle Build, click on the bookmark icon to open the Libraries sidebar.

  2. Type "rht" into the search field. Select the result called: SPARKFUNRHT03

  3. Click the button to "Include in Project"

  4. Select the name of your Photon app, and then click the "Confirm" button

  5. Particle Build will automatically insert an #include statement at the top of your app code

// This #include statement was automatically added by the Particle IDE.
#include <SparkFunRHT03.h>

Global Variables

In the global variables, you should declare which pin is being used as the RHT03 data pin. The example below declares a variable called "rhtPin" (but you could use a different variable name).

You also need to declare a RHT03 object variable that you will use to run the special functions in the RHT03 library. The example below declares an object variable called "rht" (but you could use a different variable name).

// if necessary, change data pin number to match your wiring
int rhtPin = D3;

// declare RHT03 object variable called "rht"
RHT03 rht;

You will also want to declare a global variable for the temperature reading and/or humidity reading. The readings from the sensor are float values (numbers with decimals), but you can convert these into an int value (whole number) by rounding them after reading them (because people usually just want to know the temperature to the nearest degree, not to multiple decimal places).

// global variables
int roomTemp;
int humidity;

TIP: If you aren't planning on using the humidity reading in your app, then don't create a global variable for it. (The same for the temperature variable if you only care about the humidity reading.)

setup( ) function

You have to include this statement within the setup() function to start the RHT03 sensor:

rht.begin(rhtPin);

Read Sensor Measurements

Code for reading the RHT03 sensor measurements would mostly likely be placed within the loop() function or within a custom function.

Every time you want to get a new sensor reading, you first have to call a special function called rht.update(). If the update is successful, the function will return a value of 1 (true). If so, then you can get the new measurement(s).

Each measurement is a float value (number with decimals). In the code example below, we are rounding the readings to store them as global int variables.

// if sensor update successful, get readings
if (rht.update()) {
    float tempReading = rht.tempF();
    roomTemp = round(tempReading);

    // or can read temp in Celsius
    // float tempC = rht.tempC();

    float humidityReading = rht.humidity();
    humidity = round(humidityReading);
}

// add code to do something with sensor measurements

The humidity reading will be a value between 0-100 representing the relative humidity of the air (as a percentage). Higher numbers are more humid (more water vapor in air).

TIP: If you aren't planning on using the humidity reading in your app, then don't include the lines of code to take the humidity reading. (The same for the temperature reading if you only care about the humidity reading.)

of the online SparkFun Photon Experiment Guide shows how to connect the RHT03 sensor. Here is the connection diagram for Experiment 6 (ignore the wiring for the photocell and resistor):

Experiment 6
RHT03 sensor
RHT03 Humidity and Temperature Sensor