Humidity and Temp Sensor

The RHT03 sensor 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.

Experiment 6 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):

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.)

Last updated