Getting two HC12 to communicate

Hi,
Newbie here.
I’ve recently bought 2 HC12 Si4463 modules and connected them properly to two arduino nano, got antennas soldered. I get “OK” response when sending AT commands to both modules via USB. Their modes are set to default on setup, so everything seems to be set up as it should be, but I do not receive any bytes at neither of the endpoints.

What could be the cause? Can it be for interference? I receive nothing at all, which is really weird. I tried to distance the modules up to 10 meters which is surely enough for them to start communicating. Maybe the modules are faulty…

You will find a basic version of the code I use that should work (and does not) attached. Also, I followed this schematic:


HC12s are powered with 5V, with 1N4007 and 470uF capacitor just as on the schematic if it makes any difference.

receiver.ino (651 Bytes)

transmitter.ino (400 Bytes)

SoftwareSerial HC12(T, R);

The software serial constructor is from the point of view of the Arduino, and is ordered as Rx,Tx

Do you have the T pin of your constructor(Arduino Rx) connected to the Tx pin of the HC12 and the R pin of your constructor(Arduino Tx) connected to the Rx pin of the HC12 module?

I think, that he transmission every 100ms and the receiver blnk(blue, 100) will cause the blink led to stay on continually.

You will get more people to look at your code if you post it in your post using code tags.
Transmitter sketch

#include <SoftwareSerial.h>

#define S 11
#define T 9
#define R 10

#define out 9

SoftwareSerial HC12(T, R);

void setup() {
  Serial.begin(9600);
  HC12.begin(9600);
  
  pinMode(S, OUTPUT);
  digitalWrite(S, LOW);
  delay(100);
  HC12.print(F("AT+DEFAULT\r\n"));
  delay(100);
  digitalWrite(S, HIGH);
  delay(100);
}
void loop() {
  HC12.write((byte)0);
  delay(100);
}

Receiver sketch

 #include <SoftwareSerial.h>

//#define S 4
#define S 13
#define T 6
#define R 5

#define red 9
#define green 10
//#define blue 11
#define blue 13
SoftwareSerial HC12(T, R);

void setup() {
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  
  Serial.begin(9600);
  HC12.begin(9600);
  
  pinMode(S, OUTPUT);
  //digitalWrite(S,LOW);
  digitalWrite(S, HIGH);
  delay(1000);
  HC12.print(F("AT+DEFAULT\r\n"));
  delay(1000);
  digitalWrite(S, LOW);
  //digitalWrite(S, HIGH);
  delay(1000);
}

void blnk(int pin, int ms) {
  analogWrite(pin, 255);
  delay(ms);
  analogWrite(pin, 0);
}

void loop() {
  while (HC12.available()) {
    HC12.read();
    blnk(blue, 100);
  }
}

@OP

Upload the following simple codes in your Sender and Receiver Arduinos to do functional check of the HC12 Modules. If the Modules are working, Serial Monitor of Receiver will be showing 45 at 1-sec interval.

hc12-4z.png

Sender Codes:

#include <SoftwareSerial.h>
SoftwareSerial HC12(9, 10); // DPin-9 = SRX of Sender; DPin-10 = STX of Sender

void setup() 
{
  Serial.begin(9600);             // Hardware Serial port to computer
  HC12.begin(9600);               // Software Serial port to HC12
 }

void loop() 
{
  HC12.write(0x45);
  delay(1000);
}

Receiver Codes:

#include <SoftwareSerial.h>
SoftwareSerial HC12(9, 10); // 

void setup()
{
  Serial.begin(9600);             // Harware Serial port to computer
  HC12.begin(9600);               // Software Serial port to HC12
}

void loop()
{
  while (HC12.available())          // If HC-12 has data
  {
    byte x = HC12.read();
    Serial.println(x, HEX);
  }
}

hc12-4z.png

cattledog:
The software serial constructor is from the point of view of the Arduino, and is ordered as Rx,Tx

Do you have the T pin of your constructor(Arduino Rx) connected to the Tx pin of the HC12 and the R pin of your constructor(Arduino Tx) connected to the Rx pin of the HC12 module?

Yes, I do and I can make each module communicate with my PC via arduino + USB. I get OK messages when sending AT commands.

cattledog:
I think, that he transmission every 100ms and the receiver blnk(blue, 100) will cause the blink led to stay on continually.

Given that there will be any signal, the LED should be at least blinking. It doesn’t turn on at all. No, it’s not broken, I’ve just tested it again.

cattledog:

  digitalWrite(S, HIGH);

delay(1000);
  HC12.print(F(“AT+DEFAULT\r\n”));
  delay(1000);
  digitalWrite(S, LOW);

I can see that you’ve changed the order of digitalWrites (along with a few other things) in my receiver sketch, but when I try it out it still does not work.

GolamMostafa:
Upload the following simple codes in your Sender and Receiver Arduinos to do functional check of the HC12 Modules. If the Modules are working, Serial Monitor of Receiver will be showing 45 at 1-sec interval.

Huh, that’s quite interesting. When trying to upload your codes I get the following error logs during the compilation stage:

<path>\BeegusDeeckoos\receiver.ino: In function '_GLOBAL__sub_I_HC12':

<path>\BeegusDeeckoos\receiver.ino:27:1: internal compiler error: Segmentation fault

 }

 ^

Please submit a full bug report,

with preprocessed source if appropriate.

See <http://gcc.gnu.org/bugs.html> for instructions.

lto-wrapper.exe: fatal error: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc returned 1 exit status

compilation terminated.

c:/program files/windowsapps/arduinollc.arduinoide_1.8.21.0_x86__mdqgnx93n4wtt/hardware/tools/avr/bin/../lib/gcc/avr/5.4.0/../../../../avr/bin/ld.exe: error: lto-wrapper failed

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino Nano.

After a little bit of Scooby Dooing I found out that the foregoing error is caused by this line, for whatever the reason…

    byte x = HC12.read();

I do believe I have my Arduino Studio updated. I will try to google these errors, but if you guys have any suggestions regarding what I should do, please let me know. Thank you for your response.

EDIT:
Arduino Studio update did help me with these weird internal errors, but I’m still stuck with 2 not working HC12s.

SolidTBO:
Arduino Studio update did help me with these weird internal errors, but I’m still stuck with 2 not working HC12s.

Remove both HC12 Modules, and connect the Arduinos directly using jumpers (TX of Sender -----> RX of Receiver, RX of Sender <----- TX of Receiver, and GND <------> GND). Upload the sketches and check that the Receiver’s Serial Monitor shows 45 at 1-sec interval.

If the above setup works, then do the following:

Do not put the HC12s on the breadboard; connect them directly with their respective Arduinos using ‘continuity-checked’ jumpers.

GolamMostafa:
Remove both HC12 Modules, and connect the Arduinos directly using jumpers (TX of Sender -----> RX of Receiver, RX of Sender <----- TX of Receiver, and GND <------> GND). Upload the sketches and check that the Receiver’s Serial Monitor shows 45 at 1-sec interval.

I did as you say. Arduinos do communicate when connected with a wire (only when their ground is common).

GolamMostafa:
Do not put the HC12s on the breadboard; connect them directly with their respective Arduinos using ‘continuity-checked’ jumpers.

So I just did that and also checked the continuity between ardu and HC12 pins. It’s fine, but then I get no signal again… This is starting to drive me crazy, but before I go to the seller yelling at him because of his shitty untested equipment, I’d like to be sure that these are faulty modules (or at least one of them is). Is there anything else I should check? If there was some interference I should receive something, like gibberish but still at least one byte of data, right?

Is there anything else I should check?

Try resetting both modules to default conditions with AT+DEFAULT

AT+DEFAULT
Set serial port baud rate, communication channel, and serial port transparent
transmission mode to be default value.
e.g.:
Send “AT+DEFAULT” to module, and the module returns “OK+DEFAULT”, and the
default value is restored. The serial port baud rate is 9,600bps, communication channel
is C001, and serial port transparent transmission mode is FU3.

Even i had the same problem. first i thought it was due to faulty module.i was able to send AT commands and then communication was not happening.Sending AT+DEFAULT was not helping me.

1.so check your module version by sending AT+V
2.then send AT+RX
3.After this make sure its in U3 mode( Sending AT+DEFAULT did not help me to set it to U3 from CMF or CSF mode).so it's important both are having same configuration and working in U3 mode.
4.if its not in U3 mode Send AT+FU3 it will reply with OK-FU3.
5.Then try opening 2 instances of ide and send message from one terminal it should appear in other terminal.

Good luck. :slight_smile: