Arduino and HC-12 Long Range Wireless Communication Module - HowToMechatronics
Arduino and HC-12 Long Range Wireless Communication Module - HowToMechatronics
com)
! WHAT'S NEW? " How a Capacitor Works - Capacitor Physics and Applications (http://howtomechatronics.co… $
(https://www.youtube.com/user/DejanNedelkovski)
How RFID Works and How To Make an Arduino based RFID Door Lock (http://howtomechatro…
+ (https://plus.google.com/+Howtomechatronics)
8x8 LED Matrix MAX7219 Tutorial with Scrolling Text & Android Control via Bluetooth (http:/…
& (https://www.facebook.com/howtomechatronics/)
Arduino and HC-12 Long Range Wireless Communication Module (http://howtomechatronics…
Follow me on: ()
Home (http://howtomechatronics.com) ∠
Tutorials (http://howtomechatronics.com/category/tutorials/) ∠ Arduino Tutorials
(http://howtomechatronics.com/category/tutorials/arduino/)
In this Arduino tutorial we will learn how to use the HC-12 wireless serial communication module which is capable
of making a long range wireless communication between multiple Arduino boards, with distances up to 1.8km. You
can watch the following video or read the written tutorial below for more details.
For this tutorial I made two basic examples explaining the how to connect the HC-12 module and make a basic com-
munication between two Arduinos and an additional example where using an accelerometer sensor at the first Ar-
duino I wirelessly control the position of the stepper at the second Arduino.
Its wireless working frequency band is from 433.4 MHz to 473.0 MHz
It has a total of 100 channels with a stepping of 400 KHz between each channel
These values actually depend on the selected Serial and Over-the-Air Baud Rate as seen in the table.
The HC-12 module has a microcontroller which actually doesn’t have to be programmed by the user. For configur-
ing the module we simply use AT commands, which can be sent from an Arduino, a PC, or any other microcontroller
using the serial port. For entering the AT command mode we just have to set the “Set” pin of the module to a low
logic level.
(http://howtomechatronics.com/wp-content/uploads/2017/07/Arduino-and-HC-12-Circuit-Schematic.png?x57244)
I connected the first module to an Arduino UNO and the second module to an Arduino MEGA, but of course, you
can use any board you want.
You can get the components needed for this Arduino Tutorial from the links below:
*Please note: These are affiliate links. I may make a commission if you buy the components through these links.
I would appreciate your support in this way!
Here’s the Arduino code for the first example, a basic communication between the two modules using the Serial
Monitor.
The same code is used for both Arduinos. We can connect the two Arduinos on two separate computers but also
we can use a single computer.
In that case, once we connect the first Arduino to the computer, we need to select the model and the COM port and
upload the code to the Arduino. Then we connect the second Arduino and we have to start the Arduino IDE again in
order to be able to select the other COM port to which our second Arduino is connected, and then upload the same
code.
So once we have the two Arduino IDEs running we can start the serial monitors and test whether the communica-
tion works properly. Anything we type in the serial monitor will be sent from one to the other Arduino.
How the code works: So once we type something in the serial monitor and click the Send button, at the first Ar-
duino, the while loop with the Serial.available() function will become true and using the HC12.write() function we will
send the data from the serial monitor to the HC-12 module. This module will transfer the data wirelessly to the sec-
ond HC-12 module, so at the second Arduino the while loop with the HC12.available() function will become true and
using the Serial.write() function the data will be sent to the serial monitor.
We can use the same code for sending AT Commands and configuring the module parameters. All we have to do is
connect the “Set” pin of the module to Ground or any digital pin of the Arduino and set the pin to low logic level.
To test whether we have successfully enter the mode, in the serial monitor we can type “AT” and we should get a re-
sponse message “OK”. There are total of 12 AT Commands, and they are used for changing various parameters like
the baud rate, the channel, the transmitting power etc. For example, if we type “AT+B38400” the baud rate of the
module will be set to 38400.
AT Commands:
1. AT – Test command.
Available baud rates: 1200 bps, 2400 bps, 4800 bps, 9600 bps, 19200 bps, 38400 bps, 57600 bps, and 115200 bps.
Default: 9600 bps.
Default: Channel 001, with working frequency of 433.4MHz. Each next channel is 400KHz higher.
Example: If we want to set the module to channel 006, we need to send “AT+C006” command to the module, and
the module will return “OK+C006”. The new working frequency will be 435.4MHz.
Example 02
Now let’s move the second example. Here we will use two push buttons for selecting different communication
channels and see a different method of storing the incoming data.
Note: The “Set” pins of both HC-12 modules are connected to the pins number 6 of the two Arduinos and the two
buttons, at the first Arduino, to the pins 4 and 3.
So, first we need to define the pins and set the “Set” pin to high logic level in order the module to work in normal,
transparent mode. With the first while loop we store the incoming data into a String variable, so we can better han-
dle it.
The incoming data always comes one byte at a time, so for example if we send the string “Test123” from second Ar-
duino, this while loop will do 7 iterations. Each iteration, using the HC12.read() function we will read each incoming
byte or character and add it to the String variable named “readBuffer”.
Next let’s see how we can change the communication channel using the first push button. So if we press the first
push button, using the HC12.print() function we will send the string “AT+C001” to the HC-12 module or to the sec-
ond Arduino.
1. if (button1Pressed == HIGH) {
2. HC12.print("AT+C001"); // Send the AT Command to the other module
3. delay(100);
4. //Set AT Command Mode
5. digitalWrite(setPin, LOW); // Set HC-12 into AT Command mode
6. delay(100); // Wait for the HC-12 to enter AT Command mode
7. HC12.print("AT+C001"); // Send AT Command to HC-12
8. delay(200);
9. while (HC12.available()) { // If HC-12 has data (the AT Command response)
10. Serial.write(HC12.read()); // Send the data to Serial monitor
11. }
12. Serial.println("Channel successfully changed");
13. digitalWrite(setPin, HIGH); // Exit AT Command mode
14. button1Pressed = LOW;
15. }
When this string will be received at the second Arduino, we will set the HC-12 module into AT command mode, and
then write the same string “AT+C001” to it which will set the module to communication channel number one.
We use the next while loop to print the response message from the HC-12 module whether the channel has been
successfully changed.
Back at the first Arduino, we do the same procedure of sending the AT command to the first HC-12 module. In the
same way we, using the pushing the second button, we set the communication channel number two. So using this
method we can select, at any time, with which HC-12 module we will communicate.
At the end, the checkATCommand() custom function, checks whether the received message is an AT command, by
checking whether the string starts with “AT”. If so, the module enters the AT command mode and executes the
command.
1. // ==== Custom function - Check whether we have received an AT Command via the Serial Monitor
2. void checkATCommand () {
3. if (readBuffer.startsWith("AT")) { // Check whether the String starts with "AT"
4. digitalWrite(setPin, LOW); // Set HC-12 into AT Command mode
5. delay(200); // Wait for the HC-12 to enter AT Command mode
6. HC12.print(readBuffer); // Send AT Command to HC-12
7. delay(200);
8. while (HC12.available()) { // If HC-12 has data (the AT Command response)
9. Serial.write(HC12.read()); // Send the data to Serial monitor
10. }
11. digitalWrite(setPin, HIGH); // Exit AT Command mode
12. }
13. }
Now let’s take a look at the third example. Here we control the position of the stepper motor at the second Arduino,
using the accelerometer module at the first Arduino.
The circuit also contains a microswitch for finding the initial position of the stepper motor at 0 degrees.
(http://howtomechatronics.com/wp-content/uploads/2017/07/HC-12-Wireless-Communication-Stepper-Motor-Con-
trol-using-an-Accelerometer-Circuit-Schematic.png?x57244)
You can get the components needed for this example from the links below:
*Please note: These are affiliate links. I may make a commission if you buy the components through these links.
I would appreciate your support in this way!
Note here that I already have detailed tutorials on how to connect and use both the accelerometer (http://howto-
mechatronics.com/how-it-works/electrical-engineering/mems-accelerometer-gyrocope-magnetometer-arduino/)
and the stepper motor (http://howtomechatronics.com/tutorials/arduino/how-to-control-stepper-motor-with-
a4988-driver-and-arduino/), so for this example I will only explain the HC-12 part of the code.
So first we defining the pins and initializing the modules in the setup section. Then we read the values of the X and
Y axis of the accelerometer and map them to a values from 0 to 180 degrees. The values coming from the ac-
celerometer can sometimes be unstable or shake, so for smoothing the result I used the average value of one hun-
dred readings.
1. // Makes 100 accelerometer readings and sends the average for smoother result
2. angleSum = angleSum + angleInt;
3. count++;
4. if (count >= 100) {
5. angleInt = angleSum / 100;
6. angleSum = 0;
7. count = 0;
8. // Some more smoothing of acceleromter reading - sends the new angle only if it differes
from the previous one by +-2
9. if (angleInt > lastAngle + 2 || angleInt < lastAngle - 2) {
10. Serial.println(angleInt);
11. String angleString = String(angleInt);
12. //sends the angle value with start marker "s" and end marker "e"
13. HC12.print("s" + angleString + "e");
14. delay(10);
15. lastAngle = angleInt;
16. angleSum = 0;
17. count = 0;
18. }
19. }
For even further smoothing I will send the new value of the angle only if it differs from the previous by 2.
Note here that when sending the angle to the HC-12 module, I’m also sending the character “s” in front, and the
character “e” after, which will help me when receiving the data at the second Arduino.
At the second Arduino we wait until the start marker “s” comes, then we read the value of the angle until the end
marker “e” arrive. This way we are sure that we will receive only the value of the angle.
Then we convert the value to integer, and map the value from 0 to 1600 steps, which corresponds to the selected
sixteenth step resolution at the A4988 stepper driver. Then we rotate the stepper motor to the current angle.
So that would be all for this Arduino tutorial. Feel free to ask any question in the comments section below.
Cheapest PCB Prototype: Only $2 for 10pcs 10×10cm PCBs, 24 hours Quick Turn, DHL Delivery in 3 Days
(https://easyeda.com/order)
Accelerometer (http://howtomechatronics.com/tag/accelerometer/)
HC-12 (http://howtomechatronics.com/tag/hc-12/)
Serial Communication (http://howtomechatronics.com/tag/serial-communication/)
Wireless (http://howtomechatronics.com/tag/wireless/)
RELATED POSTS
+ 83 (http://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/#comments)
(http://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/)
ARDUINO WIRELESS COMMUNICATION – NRF24L01 TUTORIAL (HTTP://HOWTOMECHATRONICS.COM/TUTORIALS/ARDUINO/ARDUINO-WIRELESS-
COMMUNICATION-NRF24L01-TUTORIAL/)
+ 20 (http://howtomechatronics.com/tutorials/arduino/arduino-wireless-communication-nrf24l01-tutorial/#comments)
(http://howtomechatronics.com/tutorials/arduino/arduino-wireless-communication-nrf24l01-tutorial/)
+ 17 (http://howtomechatronics.com/tutorials/arduino/arduino-sd-card-data-logging-excel-tutorial/#comments)
(http://howtomechatronics.com/tutorials/arduino/arduino-sd-card-data-logging-excel-tutorial/)
ARDUINO TUTORIAL 03: ANALOG INPUTS (HTTP://HOWTOMECHATRONICS.COM/TUTORIALS/ARDUINO/ANALOG-INPUTS/)
+ 2 (http://howtomechatronics.com/tutorials/arduino/analog-inputs/#comments)
(http://howtomechatronics.com/tutorials/arduino/analog-inputs/)
L E AV E A R E P LY
Comment
Name* Email*
Website
SUBMIT
(https://mydevices.com/cayenne/landing/jumpstart-projects-with-cayenne/?
utm_source=howtomechatronics%7Cs&utm_medium=display&utm_campaign=howtomechatronics%20arduino%7Cs)
L AT E S T
( Dejan Nedelkovski
(http://howtomechatronics.com/author/howtom12_wp/)
(http://howtomechatronics.com/projects/arduino-radar-project/)
(http://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/)
(http://howtomechatronics.com/tutorials/arduino/how-i2c-communication-works-and-how-to-use-it-with-
arduino/)
Arduino TFT
LCD Touch
Screen
Tutorial
(http://howtomechat
tft-lcd-
touch-
screen-
tutorial/)
( Dejan Nedelkovski
(http://howtomechatr
) December
7, 2015
+ 140
(http://howtomechatr
tft-lcd-touch-screen-
(http://howtomechatronics.com/tutorials/arduino/arduino-tft-lcd-touch-screen-tutorial/)
Get Email Updates!
Signup now and receive an email once I publish new content.
I will never give away, trade or sell your email address. You can unsubscribe at any time.
(http://www.creativityhero.com/diy-crafts/how-to-make-a-modern-
How To Make A Modern Wooden Clock
wooden-clock-diy-project/)
HOW TO MECHATRONICS
HowToMechatronics is an education website in the area of Mechanical, Electrical and Computer Engineering.
Tutorials, Tips, Tricks, How It Works, Projects, Examples, Source Codes, Download files and much more can be
found here.
Copyright © 2017 HowToMechatronics.com. All rights reserved. Terms and Conditions (http://howtomechatronics.com/disclaimer/)