Humidity and Temp Sensor
Last updated
Last updated
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:
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):
Your Photon app must include a code library with special functions that will allow you to use the RHT03 sensor.
In Particle Build, click on the bookmark icon to open the Libraries sidebar.
Type "rht" into the search field. Select the result called: SPARKFUNRHT03
Click the button to "Include in Project"
Select the name of your Photon app, and then click the "Confirm" button
Particle Build will automatically insert an #include
statement at the top of your app code
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).
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).
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.)
You have to include this statement within the setup()
function to start the RHT03 sensor:
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.
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.)
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