Learn how to create a long-distance communication using two Arduinos through the HC-12 module.
We often want to perform remote actuation of several actuators such as lamps, motors, and other actuators.
However, there are cases where the use of wired systems to drive these actuators becomes impracticable due to the distance between the actuator and the drive system or the difficulty in carrying out the installation of the cabling necessary to carry out the desired operation.
In these cases the remote control is an alternative to be used, there are several radio transceivers that can be used, such as the HC-12, NRF24L01, Lora module, among others.
In this article, we will learn how to build a NEXTPCB Electronic Board to make an automation system and control several devices remotely.
The NEXTPCB PCB will have the HC-12 radio transceiver/module and the Arduino Nano that will be the receiver and an HC-12 with the Arduino Uno that will be the transmitter to perform the activation of LEDs at a distance.
Through this article you will:
- Perform the circuit assembly on the protoboard
- Understand how the HC-12 module works
- Perform the communication of the Arduino Nano / Arduino Nano with the HC-12 module
- Remote device activation
- Understand how serial communication works with a radio transceiver
- Create a NEXTPCB PCB
Construction of the Remote Control Automation System
As previously mentioned, the project consists of creating a system to remotely activate devices remotely and create a NEXTPCB Printed Circuit Board to control the devices.
The HC-12 module is a radio transceiver and its operating frequency is From 433.4 MHz to 473 MHz.
It is possible to connect two types of antennas in this module, a helical antenna that comes with the module or another antenna to be connected to the UFL connector.
The range of this module can reach up to 1 km depending on the settings and the antenna used.
These modules usually come with some factory settings, so that we can communicate between two HC-12 modules. For example, we can communicate between two Arduinos.
In order to carry out this process, we must adjust some parameters such as communication speed, communication channel, operating mode using AT commands.
To configure the module, the set pin must be connected to the GND as shown in figure 1. This process allows the module to enter configuration mode.
It is recommended to supply the HC-12 module with an external 5V source, as it has some USB-SERIAL converters that do not provide enough current to supply the module.
To improve the design, we will create a NEXTPCB Printed Circuit Board to ensure adequate power for the HC12.
The GND of the USB-SERIAL converter must be connected to the GND of the power supply.
Now, let's configure the HC-12 module.
Table 1 shows the main commands used to configure the module using AT commands.
The FU1 and FU2 modes have a lower power consumption than the FU3 mode, so the FU1 and FU2 modes are modes to be used in applications that use batteries.
The FU1 mode operates with electrical working current without transmitting 3.6 mA data.
The FU2 mode, on the other hand, operates with a working current of 80uA without transmitting data and supports only the speeds 1200 bps, 2400 bps, 4800 bps.
The operating modes are not compatible with each other, so the two modules must be configured in the same operating mode, whether FU1, FU2, FU3.
Before we start configuring the module, we must download the Termite software available at the link: Download Termite Software
After making the connections with the HC-12 module and the serial usb converter, open the Termite Software.
Configuring the HC-12 module with Termite software.
After opening the Termite software, we must click on Settings to make some settings according to figure.
After making the settings in the Termite software, click Ok and then COM3 to connect the USB - serial converter with the HC-12 module to the serial port that is connected.
Wait to connect the port with the Termite software and we type in the command bar the command AT
If all goes well, OK will appear as in figure.
After this initial test we will configure the HC-12 module with the following parameters:
- Velocidad de comunicación serial: 9600 bps
- Canal de comunicação 5
- Potencia de transmisión de 11 dBm
- Modo de operación FU3
After performing the configuration for the first module, we repeat the steps to configure the second module.
After configuring both modules, we can connect to the Arduino as shown in figure 8 for the transmitter and figure 9 for the receiver
The Arduino Uno will be the transmitter that will perform the reading of two buttons and when the button B0 is pressed, a command will be sent to light the red led and when button B1 is pressed the blue led will be lit.
To turn off the leds just press B0 or B1 to turn off the respective led.
In Arduino Uno we will have the B0 button connected to digital pin 2 of Arduino Uno and the B1 button connected to digital pin 3 of Arduino Uno.
Both buttons are activated at a low logic level when the buttons are pressed.
To establish serial communication, we will use the library called Serial software to configure these pins 7 and 8 as serial communication pins, that is, we will emulate a serial communication by software.
The HC-12 connection on the Arduino Nano that will be the receiver is the same as the transmitter circuit, the difference will be the presence of the LEDs on digital pins 4 and 3.
After assembling the transmitter and receiver circuits, we will program the two circuits.
Firstly, we have the transmitter code. The source code is presented below.
#include <SoftwareSerial.h> // Inclui a biblioteca Software Serial
SoftwareSerial HC12(8,7); // 8 RX vai ligado no Tx do modulo HC 12, 7 TX vai
ligado no RX do módulo
#define B0 2
#define B1 3
void setup()
{
pinMode(B0,INPUT);
pinMode(B1,INPUT);
Serial.begin(9600);
HC12.begin(9600);
}
void loop()
{
if(!digitalRead(B0))
{
Serial.println("B0 pressionado ");
Serial.print('1');
HC12.print('1');
while(digitalRead(B0)==0);
}
if(!digitalRead(B1))
{
Serial.println("B1 pressionado ");
Serial.print('2');
HC12.print('2');
while(digitalRead(B1)==0);
}
}
First, we included the serial software library to be able to use any digital pin as a TX or RX pin to be used as a data transmitter.
#include <SoftwareSerial.h> // Inclui a biblioteca Software Serial
We create an object with the pins that will perform the serial communication.
SoftwareSerial HC12(8,7); // 8 RX vai ligado no Tx do modulo HC 12, 7 TX vai
ligado no RX do módulo
We name pins 2 B0 and 3 B1 using define, as is shown below.
#define B0 2
#define B1 3
In the void setup function, pins 2 and 3 are configured as inputs, configure the serial communication speed of the usb and of the serial communication with the HC-12 module.
void setup()
{
pinMode(B0,INPUT);
pinMode(B1,INPUT);
Serial.begin(9600);
HC12.begin(9600);
}
After making the settings in the void setup function, go to the void loop function, which is where the logic of our program is.
In the void loop function, we read the buttons using digitalRead.
If the B0 button is pressed, it will send the value 1 to the HC-12 module, so that the module transmits this information to the receiving HC-12 through the air through electromagnetic waves.
void loop()
{
if(!digitalRead(B0))
{
Serial.println("B0 pressionado ");
Serial.print('1');
HC12.print('1');
while(digitalRead(B0)==0);
}
if(!digitalRead(B1))
{
Serial.println("B1 pressionado ");
Serial.print('2');
HC12.print('2');
while(digitalRead(B1)==0);
}
}
Next, we will present the code of the receiver.
#include <SoftwareSerial.h> // Inclui a biblioteca Software Serial
SoftwareSerial HC12(8,7); // 8 RX vai ligado no Tx do modulo HC 12, 7 TX vai
ligado no RX do módulo
#define red 4
#define blue 3
char comando = 0;
void setup()
{
pinMode(red,OUTPUT);
pinMode(blue,OUTPUT);
Serial.begin(9600);
HC12.begin(9600);
Serial.println("Receptor");
digitalWrite(blue,1);
delay(300);
digitalWrite(blue,0);
delay(300);
}
void loop()
{
while(HC12.available()>0)
{
comando = HC12.read();
Serial.println(comando);
switch(comando)
{
case '1' : digitalWrite(red,!digitalRead(red));
break;
case '2': digitalWrite(blue,!digitalRead(blue));
break;
}
}
}
The first task to be done in the receiver's code is to include the serial software library to be able to use pins 7 and 8 of the Arduino Nano to perform serial communication.
#include <SoftwareSerial.h> // Inclui a biblioteca Software Serial
We create an object with the pins that will perform the serial communication
SoftwareSerial HC12(8,7); // 8 RX vai ligado no Tx do modulo HC 12, 7 TX vai
ligado no RX do módulo
We name pins 4 red and 3 blue using define, as is shown below.
#define red 4
#define blue 3
We declare the command variable of type char to perform the reading of the commands that come via serial through the HC-12 module.
We go to the void setup function to perform the settings of pins 4 and 3 as output and the serial communication speed of the HC-12 module and the usb port.
We flash the blue led when we turn on the receiver.
void setup()
{
pinMode(red,OUTPUT);
pinMode(blue,OUTPUT);
Serial.begin(9600);
HC12.begin(9600);
Serial.println("Receptor");
digitalWrite(blue,1);
delay(300);
digitalWrite(blue,0);
delay(300);
}
After making the settings in the void setup function, we go to the void loop function, which is where the logic of our program.
void loop ()
{
while (HC12.available ()> 0)
{
command = HC12.read ();
Serial.println (command);
switch (command)
{
case '1': digitalWrite (red,! digitalRead (red));
break;
case '2': digitalWrite (blue,! digitalRead (blue));
break;
}
}
}
First we check if there is any information coming through the HC-12 while serial module (HC12.available ()> 0)
And stay in that while checking if there is information coming in the serial.
If it exists, we use the command variable to read the HC-12 module serial.
To debug what we are receiving from the serial I write on the serial monitor what we are receiving through the HC-12 module.
After checking the information being received, we make a comparison using the switch conditional structure.
switch(comando)
{
case '1' : digitalWrite(red,!digitalRead(red));
break;
case '2': digitalWrite(blue,!digitalRead(blue));
break;
}
In the figure below, we have the physical assembly of the transmitter circuit.
The blue button is the B0 button and the B1 button is the green button. In figure 11 we have the physical assembly of the receiver circuit.
So that you can assemble this project, we created a printed circuit board. Next, we present the transmitter module board and the receiver module board.
NEXTPCB Printed Circuit Board
We created a NEXTPCB printed circuit board for you to assemble your project. With it, you can connect the buttons and the HC12 module to the printed circuit board.
The transmitter board is shown below. You can download the NEXTPCB Files in the link.
Now, from these electronic schemes, you will be able to build your project and create any type of remote load control.
Conclusion
In this article, we learned how to configure the HC-12 module to perform communication between microcontrollers to drive devices over long distances.
In our experiment, we activated LEDs, but we can activate any device. To do this, you need to use a power interface to supply enough current to drive the desired load.