Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Arduino
120 min
Share

Transmitting Temperature and Humidity Wirelessly with HC-12

Imagine being able to stay relaxed and secure indoors while still keeping an eye on the weather conditions outside. With the HC-12 sensor, you can wirelessly transmit and receive temperature and humidity readings without ever having to step outside!

Project Video

Overview

In this tutorial, we will learn how to use an HC-12 module and an Arduino to send temperature and humidity data from a DHT11 sensor, and then receive the data using another Arduino and HC-12 module. The HC-12 can transmit and receive data from distances up to 1 kilometer.

Getting the Items

Arduino Uno R3 (Voltaat Version)
Get Item
DHT11 Temperature & Humidity Sensor
Get Item
HC-12 RF Wireless Remote Serial Port Module
Get Item
2×16 LCD with I2C Module
Get Item
Half-size Breadboard
Get Item
Jumper Wires - Male to Male (40 Pack)
Get Item
Jumper Wires – Male to Female (40 Pack)
Get Item

Steps

Wiring it Up

The transmitter wiring:

Follow the wiring diagram shown in the image below to connect the DHT11 sensor and the HC-12 module to the Arduino.


 Connections from the Arduino to the breadboard:


   • Arduino 5v pin → breadboard 5v line
   • Arduino GND pin → breadboard GND line

 Connection form the DHT11:


    • GND pin → breadboard GND line
    • Signal pin → Arduino digital pin 2
   • VCC pin → breadboard 5v line

 Connection from the HC 12 module:


   • VCC pin → breadboard 5v line
    • GND pin → breadboard GND line
   • TX pin → Arduino pin 3
   • RX pin → Arduino pin 4

The receiver wiring:

Follow the wiring diagram shown in the image below to connect the HC-12 module and the LCD to the Arduino.


 Connections from the Arduino to the breadboard:


    • Arduino 5v pin → breadboard 5v line
    • Arduino GND pin → breadboard GND line


 Connection from the HC 12 module:


    • VCC pin → breadboard 5v line
    • GND pin → breadboard GND line
    • TX pin → Arduino pin 10
    • RX pin → Arduino pin 11


 Connections from the LCD:


    • VCC pin → breadboard 5v line
   • GND pin→ breadboard GND line
   • SDA pin→ Arduino analog pin A4
   • SCL pin → Arduino analog pin A5


Coding

The Transmitter code:

The code function is to read the temperature and humidity values from a DHT11 sensor and transmitting them wirelessly via an HC-12 module.

/*
 Voltaat learn (http://learn.voltaat.com)
 Link for full tutorial:
 DHT11 library:

 This is the Transmitter code.

 Tutorial: Transmitting Temperature and Humidity Wirelessly with HC-12

 Connections from the Arduino to the breadboard:
 • Arduino 5v pin → breadboard 5v line
 • Arduino GND pin → breadboard GND line

 Connection form the DHT11:
 • GND pin → breadboard GND line
 • Signal pin → Arduino digital pin 2
 • VCC pin → breadboard 5v line

 Connection from the HC 12 module:
 • VCC pin → breadboard 5v line
 • GND pin → breadboard GND line
 • TX pin → Arduino pin 3
 • RX pin → Arduino pin 4


*/

// Define libraries
#include "SoftwareSerial.h"
#include "DHT.h"

// HC-12 module pins
#define HC12_TX_PIN 3
#define HC12_RX_PIN 4

// Create a DHT instance
DHT dht(2, DHT11);

// Create a SoftwareSerial instance for HC-12
SoftwareSerial HC12(HC12_TX_PIN, HC12_RX_PIN);

//Commands inside void setup run once
void setup() {
 // Start serial communication
 Serial.begin(9600);
 HC12.begin(9600);

 // Initialize DHT11 sensor
 dht.begin();
}

//Commands inside void loop run forever
void loop() {
 // Read temperature and humidity from DHT11 sensor
 float temperature = dht.readTemperature();
 float humidity = dht.readHumidity();

 // Print the temperature and humidity values to serial monitor
 Serial.print("Temperature: ");
 Serial.print(temperature);
 Serial.print(" °C\tHumidity: ");
 Serial.print(humidity);
 Serial.println(" %");

 // Transmit the temperature and humidity values via HC-12 module
 HC12.print(temperature);
 HC12.print(",");
 HC12.println(humidity);

 // Wait for 1 seconds before sending next readings
 delay(1000);
}

The receiver code:

The code function is to receive the temperature and humidity data from the transmitter module via the HC12 wireless module and displays it on an LCD.

 Voltaat learn (http://learn.voltaat.com)
 Link for full tutorial:
 wire library:
 LiquidCrystal I2C Library:

 This is the receiver code.

 Tutorial: Transmitting Temperature and Humidity Wirelessly with HC-12

 Connections from the Arduino to the breadboard:
 • Arduino 5v pin → breadboard 5v line
 • Arduino GND pin → breadboard GND line


 Connection from the HC 12 module:
 • VCC pin → breadboard 5v line
 • GND pin → breadboard GND line
 • TX pin → Arduino pin 10
 • RX pin → Arduino pin 11

 Connections from the LCD:
 • VCC pin → breadboard 5v line
 • GND pin→ breadboard GND line
 • SDA pin→ Arduino analog pin A4
 • SCL pin → Arduino analog pin A5

*/

// Define libraries
#include "Wire.h"
#include "LiquidCrystal_I2C.h"
#include "SoftwareSerial.h"

SoftwareSerial HC12(10, 11); // RX, TX pins of HC12 module
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display

float lastTemperature = 0.0;
float lastHumidity = 0.0;

//Commands inside void setup run once
void setup() {
 Serial.begin(9600);
 HC12.begin(9600);
 lcd.init(); // initialize the LCD
 lcd.backlight(); // turn on the backlight

 // Print the initial labels for the temperature and humidity values
 lcd.setCursor(0, 0);
 lcd.print("Temp: ");
 lcd.setCursor(0, 1);
 lcd.print("Humidity: ");
}

//Commands inside void loop run forever
void loop() {
 if (HC12.available() > 0) {
   String message = HC12.readString();
   Serial.println(message);

   // Split the message into an array of substrings based on the comma delimiter
   char *tempString = strtok(const_cast<char*>(message.c_str()), ",");
   char *humString = strtok(NULL, ",");

   // Convert the temperature and humidity values to floats
   float temperature = atof(tempString);
   float humidity = atof(humString);

   // Only update the LCD if the temperature or humidity value has changed
   if (temperature != lastTemperature || humidity != lastHumidity) {
     // Move to the beginning of the first line
     lcd.setCursor(6, 0);

     // Clear the temperature value and print the new value
     lcd.print("    "); // Clear the space for the value
     lcd.setCursor(6, 0);
     lcd.print(temperature);
     lcd.print((char)223);
     lcd.print("C");

     // Move to the beginning of the second line
     lcd.setCursor(10, 1);

     // Clear the humidity value and print the new value
     lcd.print("       "); // Clear the space for the value
     lcd.setCursor(10, 1);
     lcd.print(humidity);
     lcd.print("%");

     // Update the last temperature and humidity values
     lastTemperature = temperature;
     lastHumidity = humidity;
   }
 }
}

Testing it Out

Once you’ve wired the two circuits and uploaded the code, put the transmitter circuit outdoors to monitor the weather status.

The readings from the DHT11 will be transmitted through the HC-12 wireless module and displayed on the LCD by the receiver circuit. The readings will update every one second because of the delay that we have added to the code.

Resources

No items found.