Arduino radio hc-12

From wikiluntti
Revision as of 18:02, 20 January 2025 by Mol (talk | contribs) (→‎Set)

Introduction

LoRa, hc12, hc-12

  • Frequency band is from 433.4 MHz to 473.0 MHz
  • 100 channels with a stepping of 400 KHz between each channel
  • Transmitting power is from -1dBm (0.79mW) to 20dBm (100mW). A half-duplex 20 dBm (100 mW) transmitter
  • Receiving sensitivity is from -117dBm (0.019pW) to -100dBm (10pW)

Connect a 22 µF to 1 mF reservoir capacitor in parallel with the HC-12 "Gnd" and "Vcc" pins.


Set

Pins:

  • Set pin of the module to Ground (set the pin to low logic level).
  • Vcc 3.3V and GND to GND
  • TXD -> 11
  • RXD -> 10

Note that the pins needs to be reversed: The TX pin in the HC-12 needs to be plugged to RX pin of the Arduino.

The syntax of the softaware serial is:

SoftwareSerial(rxPin, txPin, inverse_logic)

Code for setting AT commands:

#include <SoftwareSerial.h>
SoftwareSerial HC12(11, 10);      // HC-12 TX Pin, HC-12 RX Pin

void setup() {
  Serial.begin(9600); 
  HC12.begin(9600); 
}

void loop() {
  while (HC12.available()) {        // If HC-12 has data
    Serial.write(HC12.read());      // Send the data to Serial monitor
  }
  while (Serial.available()) {      // If Serial monitor has data
    HC12.write(Serial.read());      // Send that data to HC-12
  }
}

AT commands. The most important commands are (remember to use capital letters)

  1. AT – test command. It will return OK if AT interface is enabled
  2. AT+Bxxxx – set serial port baud rate. For example, AT+B57600 set baud rate to 57600bps
  3. AT+Cxxx – set radio channel. Channels start from 001 at 433,4MHz. Each next channel adds 400kHz. Channel 100 is 473,0MHz. AT+C002 will set frequency to 433,8MHz. Two HC-12 devices that creates a wireless link have to operate on the same frequency
  4. AT+FUx – set device mode: FU1, FU2, FU3 or FU4. Two HC-12 devices that creates a wireless link have to use the same mode
  5. AT+Px – set device transmitting power. For example AT+P2 sets power to 2dBm (1.6mW)
    1. -1dBm (0.8mW)
    2. 2dBm (1.6mW)
    3. 5dBm (3.2mw)
    4. 8dBm (6.3mW)
    5. 11dBm (12mW)
    6. 14dBm (25mW)
    7. 17dBm (50mW)
    8. 20dBm (100mW)
  6. AT+RX – retrieve all parameters: mode, channel, baud rate, power
  7. AT+V – retrieve module version
  8. AT+DEFAULT – reset module parameters to default settings

Serial vs Software Serial

Transmit Tx

Receive Rx