Problem with HC-12 and Arduino nano

Hi guys, I'm tryig to use and configure my HC-12 on my Arduino Nano using the AT commands with the sketch below, but the HC-12 seems to not respond. I've tried to use the module on the Arduino UNO and it works great, the baud rate of the module is 9600. What should I do? Thank you in advance.

#include <SoftwareSerial.h>

// serial antena
SoftwareSerial HC12(5, 4); // RX, TX

void setup() {
  Serial.begin(9600);                  
  HC12.begin(9600);                     
  while(!Serial);
  Serial.println("Input AT commands");
}

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
  }

}

This is the photo of my setup:

there are several nano microcomputers - which one is it?
if basic nano try AltSoftSerial

assuming you are using a basic Nano try connections
Nano pin 9 Tx HC-12 RXD
Nano pin 8 Rx HC-12 TXD
Nano GND HC-12 GND
Nano 5V HC-12 VCC
HC-12 SET to GND for AT mode

using following code

// hc-12 AltSoftSerial transmit/Receive Test
//
// from https://github.com/PaulStoffregen/AltSoftSerial/blob/master/examples/ReceiveTest/ReceiveTest.ino

#include <AltSoftSerial.h>

AltSoftSerial altser;
const int mybaud = 9600;

// UNO connection to a RS232-TTL
// UNO PIN 8 to RS232 RXD
// UNO pin 9 to RS232 TXD

// Board            Serial1 TX   AltSoftSerial RX
// -----            ----------   ----------------
// UNO/nano              9              8
// Teensy 3.x            1              20
// Teensy 2.0            8 (D3)         10 (C7)
// Teensy++ 2.0          3 (D3)          4 (D4)
// Arduino Leonardo      1              13
// Arduino Mega         18              48

// Serial1 on AVR @ 16 MHz minimum baud is 245
// Serial1 on Teensy 3.2 @ 96 MHz minimum baud is 733

void setup() {
  delay(200);
  Serial.begin(115200);
  while (!Serial)
    ;                    // wait for Arduino Serial Monitor
  altser.begin(mybaud);  // to AltSoftSerial RX
  Serial.println("\n\nAltSoftSerial transmit/ Receive Test");
  Serial.println("on a UNO connect pins 8 and 9 for a loopback test");
  Serial.println("using RS232 module connect pins 2 and 3 for a loopback test");
  Serial.println("characters entered on serial monitor keyboard are echoed back via serial to the display");
}

void loop() {
  // transmit a test byte on Serial 1
  if (Serial.available() > 0) {
    char ch=Serial.read();
    Serial.print(ch);   // echo character if required
    altser.write(ch);
  }
  // attempt to receive it by AltSoftSerial
  if (altser.available() > 0) {
    Serial.write(altser.read());
  }
}

serial monitor output in response to AT commands

AltSoftSerial transmit/ Receive Test
on a UNO connect pins 8 and 9 for a loopback test
using RS232 module connect pins 2 and 3 for a loopback test
characters entered on serial monitor keyboard are echoed back via serial to the display

AT
OK
AT+V
www.hc01.com  HC-12_V2.4
AT+RX
OK+B9600
OK+RC001
OK+RP:+20dBm
OK+FU3

Ok thank you, I'll try it.

I've tried the code but it still doesn't respond when I send the AT commands:

AltSoftSerial transmit/ Receive Test
on a UNO connect pins 8 and 9 for a loopback test using RS232 module connect pins 2 and 3 for a loopback test
characters entered on serial monitor keyboard are echoed back via serial to the display
ATATAT+V

what line ending have you got set? i have "both NL and CR"

e.g. this is an ESP32 communicating with a HC-12
image

photo showing the Nano and ESP32 connected to HC-12 modules

Ok, thank you so much, now I fixed the problem, it was also a bad connection of the NANO on the breadboard. Another question, if i want to communicate the hc-12 from the arduino UNO and the one on the nano (UNO receiver, NANO transmitter) in order to send the output values of a sensor should I use the <SoftwareSerial.h> library or the <AltSoftSerial.h> on both the arduinos?

I would use AltSoftSerial on both UNO and Nano - it has advantages over SoftwareSerial in Particular it Can simultaneously transmit and receive

here is a test with the Nano (code as post 3) and ESP32 (code below) communicating
Nano serial monitor output

AltSoftSerial transmit/ Receive Test
on a UNO connect pins 8 and 9 for a loopback test
using RS232 module connect pins 2 and 3 for a loopback test
characters entered on serial monitor keyboard are echoed back via serial to the display
Nano hello from ESP32
Nano from ESP32 test2 1234567890
Nano from ESP32 test3 abcdefghijklmnopqrstuvwxyz
ESP32 hello from nano
ESP32 from Nano test2 0987654321
ESP32 from Nano abcdefghijklmnopqrstuvwxyz

ESP32 serial monitor

ESP32 serial1  test Rx pin 16 Tx pin 17
   for loopback test connect pin 16 to pin 17
Nano hello from ESP32
Nano from ESP32 test2 1234567890
Nano from ESP32 test3 abcdefghijklmnopqrstuvwxyz
ESP32 hello from nano
ESP32 from Nano test2 0987654321
ESP32 from Nano abcdefghijklmnopqrstuvwxyz

ESP32 code

// ESP32 - HC-12   Serial1 test - for loopback test connect pins 16 and 17

// note: power HC-12 from 5V - 3.3V gives spurious characters and does not transmit all the buffer
//  use a potential divider in HC-12 TXD to ESP32 Rx to convert 5V signals to 3.3V

#define RXD1 16
#define TXD1 17

void setup() {
  // initialize both serial ports:
  Serial.begin(115200);
  //Serial1.begin(115200, SERIAL_8N1, RXD1, TXD1);
  Serial1.begin(115200, SERIAL_8N1, RXD1, TXD1);
  Serial1.begin(9600, SERIAL_8N1, RXD1, TXD1);
  Serial.println();
  Serial.println("\n\nESP32 serial1  test Rx pin 16 Tx pin 17");
  Serial.write("   for loopback test connect pin 16 to pin 17\n");
}

void loop() {
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    byte inByte = Serial1.read();
    Serial.write(inByte);
  }

  // read from port 0, send to port 1:
  if (Serial.available()) {
    byte inByte = Serial.read();
    Serial.write(inByte);
    Serial1.write(inByte);
  }
}

for an example of transmitting a structure using a HC-12 see how-to-convert-hex-character-from-softwareserial-string-to-int
the code is running on an ESP32 but should by OK on a nano

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.