HC12: Easy Arduino Wireless Communication

The HC-12 is an RF device able to transmit data nearly half a kilometer away in less than ideal circumstances. Due to its high signal transmission distance I’m unable to execute a line of sight test, but will develop a remedy in the near future. Due to this high transmission distance and its ease of use, I’ve opted to develop wireless projects around the HC12 in place of the NRF24L01. Today’s project will be a simple proof of concept; we will send a text from one arduino to another.

Required Items:

HC12(x2)        Antenna Attachment(x1)        Diode(x2)        100µFCapacitor(x2)

 

Fritzing Sketch:

 

Transceiver Assembly

As seen above, the antennas should be spaced far from each other to ensure proper signal transmission and reception. There have been times where I’ve had them too close and mistakenly thought there was a problem with my coding, only to discover the antennas were near touching each other. Looking to both sides of the breadboard power rails you’ll notice a diode on each HC12. This is a recommendation by the manufacturer when the supply voltage is greater that 4.5 volts and the unit is constantly transmitting. Being that I don’t know what projects will utilize the HC12 I’ve opted to provide the utmost protection to guarantee signal reliability and microcontroller safety. The decoupling capacitor is connected in parallel with the power pins and is an electrical reservoir that guards the HC12 from power fluctuations that would adversely effect its operation.

The Rx, Tx, and Set pins of the unit are what I love most with all three addressable to any digital pin! The Set pin is the most interesting out of the trio in that it sets the unit’s mode as being in either command or transparent mode. Transparent mode is when the unit operates via sending/receiving code as written while command mode is where the unit settings can be adjusted. To adjust to command mode, address the Set pin as ‘LOW’, doing the opposite to return it to normal operations.

By simultaneously connecting both Arduinos to unique USB ports and opening two separate IDEs you’re able to code both units individually. Copy/paste the two codes below, ensuring each code goes to a separate IDE window and that each window has a different COM port selected, then upload.

After uploading, open up the serial ports of both IDEs and everything should be working correctly. Below the pasted codes is a detailed explanation of how to send AT commands alter the HC12’s settings.

 

HC12 Transmitter Code

 

HC12 Receiver Code

 

 

AT Commands

Using the document below, let’s highlight the various AT Commands you can issue to manipulate the HC12’s settings. Note that to issue AT Commands the Set pin must be LOW or else no changes will be made.

 

 

Sending AT Commands

AT Commands are executed in the following sequence:

  1. Adjust Set pin to LOW.
  2. Set a delay by at least 200 ms (milliseconds)
  3. Send AT Command by typing HC12.write(“AT+xxxxx”); where xxxxx represents the command itself, which will be explained shortly.
  4. Set a delay by at least 200 ms (milliseconds)
  5. Adjust the Set pin to HIGH.
  6. Complete!

Baud Rate Adjustment

There are two types of baud rates, over the air baud and serial port baud. Both are interconnected as changing one directly changes the other as seen in the table below:

Serial Port Baud Rate 1,200
b/s
2,400
b/s
4,800
b/s
9,600
b/s
19,200
b/s
38,400
b/s
57,600
b/s
115,200
b/s
Over The Air Baud Rate 5,000 b/s 15.000 b/s 8,000 b/s 236,000 b/s

Changing the baud rate to the lowest value generates the farthest communication distance and minimum data transmission, represented a b/s (bits per second). Transmitting high volume data requires increasing the baud rate, which lowers the communication distance. Unless the transmitted data approaches pages of information or dumping all data from a data logger, you don’t have to concern yourself too much with increasing the baud rate.

Manipulating the baud rate requires you send AT+Bxxxx where xxxx represents the serial baud rate such as AT+B19200 to set the over the air baud to 58,000 b/s.

DO NOT include commas when sending AT Commands!

 

Setting The Channel

The HC12 has one hundred communication channels available, ensuring that all but the most complicated of projects will have enough channels available. Adjusting the channel requires both units lower their set pin and send  identical AT+Cxxxx commands where xxxx represents any number between 001-100.

Increment channels by no less than five if the serial baud rate is greater than or equal to 9,600 b/s. Also increment channels by no less than five if baud rate is less than or equal to 9,600 b/s and communication distance is less than 10 meters (32 feet)

 

Adjusting Transmission Mode

Transmission mode adjustment requires sending the AT+FUx command, where x equals 1,2, or 3. The table below details the different modes as well as their benefits/ restrictions. The baud rate table has been duplicated for ease of reference.

Mode FU1 FU2 FU3
Idle Current 3.6 mA 80μA 16mA
Current Description Semi Power Saving Power Saving Normal Ops
Transmission Delay 15-25 ms 500ms 4-80 ms
Supported Baud Rates All 8 Up To 4,800 b/s ALL
Supported Air Baud Rates 250,000 b/s 250,000 b/s ALL

 

Serial Port Baud Rate 1,200 b/s 2,400 b/s 4,800 b/s 9,600 b/s 19,200 b/s 38,400 b/s 57,600 b/s 115,200 b/s
Over The Air Baud Rate 5,000 b/s 15,000 b/s 58,000 b/s 236,000 b/s

 

Transmitting Power

Transmission power is unique as it directly adjusts the range regardless of the antenna used. This is useful when multiple yet independent systems are developed and you don’t have the know-how to create a unit authentication system. Instead, you could place a range finder near the conflicting system, having a buzzer activate on the receiver as you adjust the transmitter’s transmission power down. Once you don’t receive a signal, you know what setting to adjust the conflicting power level to. Sending AT+Px where x is a number between 1-8, The table below explains the decibel boost.

 

X Value of AT+Px 1 2 3 4 5 6 7 8
Transmission Power (dBm) -1 2 5 8 11 14 17 20

The higher the number, the higher the transmission power, with each 6db decrement the range is halved.

 

Closing Commands

AT+DEFAULT resets all parameters so serial port= 9600b/s, channel= 001, transmission power= 8, and transmission mode us FU3

AT+SLEEP will send the unit in an ultra low power state of 22μA and disables the command mode. Reactivating command mode requires you send ‘AT’ without the quotes.