In this tutorial it is demonstrated how to send humidity and temperature sensor data wirelessly using 434MHz wireless module. The temperature and humidity data is displayed on LCD and also printed on the PC serial monitor. This can be useful to monitor remotely environment humidity and temperature inside or outside the house. Monitoring humidity and temperature inside home can be useful since high humidity can effect health and if there is temperature is too hot one can automate to turn on FAN using DC motor. Also humidity and temperature of garden plants outside and inside can be beneficial because plants can be taken better care of. It can also be useful if monitoring and broad casting of the humidity and temperature to several places is required.
Overall 434MHz Communication System
The following picture shows the 434MHz communication system showing transmitter and receiver assembled with the help of two separate breadboards.
The overall circuit diagram below shows the wireless communication system with 434MHz RF modules, Arduino boards, DHT11 sensor and LCD.
Here the sensor used is the DHT11 sensor. This sensor senses humidity and temperature from the environment. At the transmitter side, the DHT sensor and the 434MHz module is conneccted to Arduino Uno. The captured sensor data is sent to the Arduino Uno. Then Arduino Uno sends then data to the 434MHz transmitter module. The transmitter performs digital encoding of the sensor data and transfer that encoded data data packets via ASK modulation to the 434MHz receiver module. The receiver module then decodes the ASK modulated data packets.
Transmitter Hardware Setup
The 434MHz Transmitter with Arduino and DHT11 sensor module assembled on breadboard is shown below.
The schematic diagram of the transmitter setup is as shown below.
On the transmitter side we have used here Arduino Nano but one can also use other Arduino board such as Arduino Uno or Arduino Mega.
The DHT11 sensor module as shown on the right side has three pins. The middle pin is the data pin is connected to the Arduino Nano pin 7. The other two pins are Vcc and GND which are connected +5V source and common ground. In this tutorial the Adafruit DHT11 sensor library is used. To know how to install this library and how to display DHT11 temperature and humidity value on LCD using Arduino see the tutorial LCD Display of Temperature and Humidity using DHT11 & Arduino with Code.
The 433MHz transmitter module has three pins: a VCC pin, a data pin and a GND pin. The data pin is connected to the pin 12 of the Arduino nano. The VCC and GND pin are connected to +5V and ground common to Arduino nano 5V and ground.
In this tutorial the RadioHead library is used here which makes writing code for wireless communication with ASK(Amplitude Shift Keying) easier. The RadioHead library is an object-oriented library to send and receive messages in data packets transferred wirelessly using ASK using range of embedded microprocessors or microcontroller boards like Arduino. The tutorial 443MHz RF Wireless Communication with Arduino shows and explains how to download/install the radiohead library and how to transfer simple text message wirelessly. The 433MHz module transmitter data pin is connected at the Arduino pin 12 because by default the RadioHead library is configured that way.
Transmitter Program Code
The program code below is for the transmitter. It uses the DHT sensor library and the RadioHead library to capture humidity and temperature data from DHT sensor and transmit that to the receiver wirelessly.
// RF 433MHz Modules Transmitter Program
// Module Data pin connected to Arduino digital pin #12 which is used by the library default
#include <RH_ASK.h> // Include RadioHead Library
#include <SPI.h> // Required to compile
#include <DHT.h> // Include Adafruit DHT11 Sensors Library
#define DHTPIN 7 // DHT11 Output Pin connection
#define DHTTYPE DHT11 // DHT Type is DHT11
RH_ASK rftx;
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
void setup () {
dht.begin();
Serial.begin(9600); // For debugging only
if (!rftx.init())
Serial.println ("Initialization Failed!");
}
void loop (){
delay(2000); // Wait two seconds between measurements
float H = dht.readHumidity(); //Read Humidity
float T = dht.readTemperature(); // Read temperature as Celsius
// Check if any reads failed and if exit
if (isnan(H) || isnan(T)){
Serial.println("Failed to read from DHT sensor!");
return;
}
// Convert Humidity to string
String Hstr = String(H);
Serial.println(Hstr);
// Convert Temperature to string
String Tstr = String(T);
Serial.println(Tstr);
// Combine Humidity and Temperature into single string
String STR = Hstr + "," + Tstr;
const char *msg = STR.c_str();
rftx.send ((uint8_t *) msg, strlen (msg));
rftx.waitPacketSent();
}
In the above code, in order to use the RF module we have included the RH_ASK.h library. In order for this library to compile we also have to include the SPI.h library. Then we have included the DHT.h library for using the DHT sensor. We next define the DHT pin which is the Arduino pin where the DHT sensor data pin is connected which is 7. Then we also have to define the DHT Type which is DHT11. The other option would be DHT22 which we are not using. We create an object instance called rftx of class RH_ASK. The we create an instance object called dht with the DHTPIN and DHTTYPE as parameter of class DHT. Next in the setup() function, we initialize the DHT sensor using DHT.begin(), initialize the serial communication with baud rate 9600bps using Serial.begin(). Using rftx.init() we check the radio head library initialization and if there is any error encountered then we print out message Initialization Failed! on the PC serial monitor. In the loop() function we wait for 2 seconds using delay() function for sensors measurement intervals. We read in the humdity and temperature data from the DHT11 sensor using the dht.readHumidity() and dht.readTemperature() functions and store them in local float variables H and T. Then we check whether we have actually read any value from the sensor using the if statement and if there is not data then we print out message Failed to read from DHT sensor!. If we got the data then we print them on serial monitor using Serial.print(). To send it over the wireless RF module we first combine these two data in string format with a comma between them and store them in a single string variable called STR. Then we convert this string STR into an array stored in pointer variable called msg which ends with null terminating character. We then use the rftx.send() function to send the array of character over the wireless channel. This involves encoding the message array into packets and sending wirelessly by modulating the packet using Amplitude Shift Keying(ASK) which is digital version of Amplitude Modulation(AM). The rftx.waitPacketSent() function ensure that all the data is transmitted and until then creates a wait time.
Receiver Hardware Setup
The following shows the 434MHz receiver build with the help of a breadboard.
At the receiver we have used Arduino Uno. The 16x2 LCD is connected to digital pin 2, 3, 4 and 5 for the data transfer and 6 and 7 for control. The pin 6 is connected to Enable pin of the LCD and 7 is connected to RS(Register Select) pin. The R/W pin of the LCD is connected to the ground because we only write to LCD and do not read anything from it.
The 433MHz receiver module is connected with its data pin(there are two of them but anyone can be used) connected to digital pin 11. The digital pin 11 must be used because the RadioHead library is configured to use this pin number. The other two pins are VCC and GND which must be connected to the +5V supply from power source or Arduino Uno.
Receiver Program Code
The following is the program code for the receiver. This should be uploaded to Arduino Uno board.
// RF 433MHz Module Receiver Program
// Module Data pin connected to Arduino digital pin #11 which is used by the library default
#include <LiquidCrystal.h>
#include <RH_ASK.h>
#include <SPI.h> // Required to compile RadioHead Library
// Initialize the LCD library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
String Hrxstr;
String Trxstr;
String rxstr;
RH_ASK rfrx;
void setup () {
Serial.begin (9600); // For debugging only
if (!rfrx.init ())
Serial.println ("Initialization Failed!");
// Print message to Monitor
Serial.println("DHT11 Humidity & Temperature" );
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("DHT11");
lcd.setCursor(0, 1);
lcd.print("Humidity/Temp.");
}
void loop(){
uint8_t buff[11];
uint8_t bufflen = sizeof(buff);
if(rfrx.recv(buff, &bufflen)){
// Get and Convert received data into string
rxstr = String((char*)buff);
// Split string into two values
for (int i = 0; i < rxstr.length(); i++){
if (rxstr.substring(i, i+1) == ",") {
Hrxstr = rxstr.substring(0, i);
Trxstr = rxstr.substring(i+1);
break;
}
}
// Print values to Serial Monitor
Serial.print("Humidity: ");
Serial.print(Hrxstr);
Serial.print(" - Temperature: ");
Serial.println(Trxstr);
// Print values to LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Humidity:");
lcd.print(Hrxstr);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Temp.:");
lcd.print(Trxstr);
lcd.print((char)223);
lcd.print("C");
}
}
In the above code, we have included the LiquidCrystal.h library which helps in writing code to program our LCD. Again as in transmitter code, we have included the RadioHead library, RH_ASK.h and the SPI libray, SPI.h required to write code for RF module. In the next line, an instance calld lcd of luquidcrystal with pin number as parameter is created. The pin numbers corresponds to the RS, En and the four data pins for the LCD. Then some string variables are declared which are required later in the program. Again as in transmitter code, an object rfrx of the class RH_ASK is declared in order to use the RH_ASK library function later down in the program. In the setup() function, we initialized the serial monitor baud rate using the serial.baud() function with 9600bps baud rate. This is done because we want to display the sensor data on the serial monitor also. Using the rfrx.init() function we check whether it is null or not. If it is then the receiver RF module did not detect any incoming wireless data and in this case we print out message that the initialization has failed. After checking this we print on the serial monitor and on the LCD some DHT11 Humidity and Temperature welcome message. In the loop function, we create some buffer array variable called buff and with buffer length variable called bufflen of length 11. The length is 11 because the humidity and temperature data are each 5 characters with two decimal points and including the first two character and a dot like for example XX.XX form. Also in the transmitter code we have placed a comma "," that separates humidity and temperature data. Using the rfrx.recv() function we check the checksum of the received packet and if it good then we proceed to execute the statements within the if statement. The received data is stored in the buff array we declared earlier and it is converted to string data type and stored in the string variable rxstr. The rxstr variable holds but humidity and temperature data separated by comma. So a for loop is used to seperate the humidity and temperature data and stored in other two string variables called Hrfrx and Trfrx respectively. Then the humidity and temperature data are printed on the PC monitor using the Serial.print() and Serial.println() functions and on the LCD.
Video Demonstration
This following video demonstrates wireless transfer of humidity and temperature sensor data from DHT11 sensor module using 434MHz RF module and display
of the received data on LCD and PC monitor.
Conclusion
So in this tutorial we showed how we can build simple RF communication system using 434MHz RF modules to transfer humidity and temperature sensor data and display on LCD. This can be helpful in number of projects such as monitoring home humidity and temperature, can be useful in agriculture project and Internet of Things project. We can plot the data on a graph as was illustrated in the tutorial How to display Sensor data in real time on Web. To build RF module on your own instead of buying one, one has to learn RF system design and components used in RF system like oscillators, RF amplifier, RF mixers etc.
Hi,
I have a question, how to send measurements by 433 mhz? For example String STR = Hstr + "," + Tstr + "," + Pstr; and in receiver Prxstr = rxstr.substring(what should be here???);