Help with basic HC12 communication using 2x HC12 + 2x Arduino Nano

Thread Starter

KorrinF

Joined May 29, 2018
11
@MarkHughes

Hello, I am new to arduinos and HC12 and just programming in general. Iv got a project that I am working on and the HC12 module was recommended to me by my local robotics club at the university who advised me on wiring as well. To start with I just wanted to test them using Mark Hughes, first sample script on his HC12 article and unfortunately they are not behaving accordingly.

FUTURE PROJECT SUMMARY: (bit of back story for the future of the project, not entirely relevant for this thread)

These wireless modules are actually a late edition to the project, something that was always intended to be incorporated into the programming but only after the initial programming was finished and the other electronics were working correctly. Ill give you a brief description of the project but it is not super relevant to this thread as all I want for this thread is the get that basic script to work. So my project is a game involving 'Targets', in total there is 10 physical targets all the same that include various electronics on a PCB, controlled by an arduino nano each (china version) and each with a HC12 module. The programming for these Targets, stage one is complete ie, all the electronics behave correctly and are a functional game I am now looking to expand the functionality by using the HC12 modules. This was always the intention so the PCB's were designed and made with space to add the HC12 modules later. These Targets will not be communicating with themselves instead I want them to communicate to a 'Controller' the controller is currently on a breadboard equipped with an Arduino Nano, HC12 module and a few buttons (to be ignored for now). The idea is a target gets 'hit' and wirelessly sends a signal to the controller (maybe it sends an 'a'). The controller receives the 'a' and sends that to a laptop to which it is connected to via USB. Thats the aim for the future but for now like I said I am simply trying to get the sample code to work from the article.

MAIN POINTS:

'Target' : 10 available, I am using one for this experiment. Its a PCB with an arduino Nano and HC12 already soldered to it.
'Controller': 1 available, relevant components on a breadboard include arduino nano and HC12 connected with jumper cables.

Both arduinos have been tested with my other program and all the electronics are working apart from the HC12 using the code below.

The HC12’s are wired to the following arduino pins:
VCC= 5v arduino pin (in series with the specified capacitor and diode)
GND = GND
RX = d11
TX = d10
SET = d8

Here is the code that has been uploaded to both arduino's, the pins have been changed to match my selection. Whats supposed to happen is when I type and send something in one serial monitor it should be received and displayed in the other serial monitor:

Code:
/*  HC12 Send/Receive Example Program 1
    By Mark J. Hughes
    for AllAboutCircuits.com
   
    Connect HC12 "RXD" pin to Arduino Digital Pin 4 <<mine is 11
    Connect HC12 "TXD" pin to Arduino Digital Pin 5 << mine is 10
    Connect HC12 "Set" pin to Arduino Digital Pin 6 << mine is 8                                                                                                     
  
    Do not power over USB.  Per datasheet,
    power HC12 with a supply of at least 100 mA with
    a 22 uF - 1000 uF reservoir capacitor.
    Upload code to two Arduinos connected to two computers.
   
    Transceivers must be at least several meters apart to work.
  
*/

#include <SoftwareSerial.h>

const byte RX = 11;                  // Recieve Pin on HC12
const byte TX = 10;                  // Transmit Pin on HC12

SoftwareSerial HC12(TX,RX); // Create Software Serial Port

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

void loop() {
  if(HC12.available()){                     // If Arduino's HC12 rx buffer has data
    Serial.write(HC12.read());              // Send the data to the computer
    }
  if(Serial.available()){                   // If Arduino's computer rx buffer has data
    HC12.write(Serial.read());              // Send that data to serial
  }
}

To test this code I set the 'target' and 'controller' up on 2 separate computers with the arduino IDE installed and serial monitor open. The computers were also a few meters apart to rule out disturbances.

POWER: This may turn out to be the issue, hopefully not as the robotics club recommended usb power as sufficient. In my main project, the 'Targets' are powered via the usb port on the arduinos and a power bank supplying 5v, but there are also other components draining power including 20 programmable LEDs (always on) and a piezo buzzer. The 'Controller' will also be powered by the usb port and then connected to a laptop, there are no other components that drain power. But whilst testing the above code obviously both the target and controller are plugged into a laptop for communication and power.

PHOTOS:

'Target' heres the back of the PCB showing the relevant connections and the last photo showing the front soldered arduino pins.
Photo 01-05-2019, 11 14 59.jpg
Photo 01-05-2019, 11 15 25.jpg
Photo 01-05-2019, 11 16 11.jpg

'Controller' sorry about the angle, the buttons are not relevant for this test.

Photo 01-05-2019, 11 16 40.jpg

Hopefully we can figure something out I am at a loss and completely new to wireless communications but am keen to learn. If we can get these working with the above code then its not to far off of achieving my end project goal so thats exciting. Thanks.
 

AlbertHall

Joined Jun 4, 2014
8,998
With the HC12 'SET' pin held low send 'AT+RX' for both HC12s. They will return their settings - power, channel, baud, etc. Make sure they both match.

Incidentally, note that software serial 'RX' should connect to HC12 'TX', and vice versa.
 

Thread Starter

KorrinF

Joined May 29, 2018
11
With the HC12 'SET' pin held low send 'AT+RX' for both HC12s. They will return their settings - power, channel, baud, etc. Make sure they both match.

Incidentally, note that software serial 'RX' should connect to HC12 'TX', and vice versa.

Hi thanks I will try sending that command, I have not done that before so I am assuming I would type that into the serial monitor and click send? And in regards to the pin connections is it mandatory for HC12 'RX' & 'TX' pins to be connected to the dedicated ones on the arduino for sending commands, because the library included in the code called SoftwareSerial.h allows for virtual serial ports to be created on any other pins. Thanks
 

djsfantasi

Joined Apr 11, 2010
5,913
You can use the pins defined by SoftwareSerial. But you cannot receive on more than one serial port simultaneously. Or, in other words, you are restricted to receiving data from one software serial port at a time.
 

twohats

Joined Oct 28, 2015
76
May I suggest you do a search on the 'Picaxe forum' for HC-12's.
There is a lot of information. Unfortunately not for Arduino.
Good luck.
 

Thread Starter

KorrinF

Joined May 29, 2018
11
Yes, that will work with the software you show above but the HC12 SET pin must be connected 0V first.
Ok so I gave that a go heres the updated code:

Code:
#include <SoftwareSerial.h>

const byte RX = 11;
const byte TX = 10;
const byte SET = 8;

SoftwareSerial HC12 (TX,RX); //create software serial port


void setup() {
// put your setup code here, to run once:

  pinMode(SET, OUTPUT);                  // Output High for Transparent / Low for Command
  digitalWrite(SET, LOW);               // Enter Transparent mode
  delay(80);                           // 80 ms delay before operation per datasheet
  Serial.begin(9600); //open serial port to computer
  HC12.begin(9600);// open serial port to HC-12
  Serial.println("Hello World - Software Serial");
 
}
When I upload this code the words 'Hello World - Software Serial' show up in the serial monitor twice. I think for each serial port that activates which sounds like a good thing and the LEDs on my arduino for the RX & TX flash continuously. However when I sent 'AT+RX' through the serial monitor nothing was returned. I tried this with both the 'Target' and the 'Controller'
 

AlbertHall

Joined Jun 4, 2014
8,998
When I upload this code the words 'Hello World - Software Serial' show up in the serial monitor twice. I think for each serial port that activates which sounds like a good thing and the LEDs on my arduino for the RX & TX flash continuously. However when I sent 'AT+RX' through the serial monitor nothing was returned. I tried this with both the 'Target' and the 'Controller'
The program in post #8 never sends anything to the HC-12. Use the code from #1 but make sure that the SET pin is at 0V.
 

ericgibbs

Joined Jan 29, 2010
9,305
hi K,
In case you do not have a copy of the datasheet for the HC12, ref attachment.

BTW: why are the two spiral antenna's bent at that horizontal angle.?
What sort of radiation/reception pattern are you expecting with the antenna's bent.?
The one on the PCB is too close to the ground plane and is shielded.

Add a 47uF cap close to the HC12 power pins
E
 

Attachments

Thread Starter

KorrinF

Joined May 29, 2018
11
hi K,
In case you do not have a copy of the datasheet for the HC12, ref attachment.

BTW: why are the two spiral antenna's bent at that horizontal angle.?
What sort of radiation/reception pattern are you expecting with the antenna's bent.?
The one on the PCB is too close to the ground plane and is shielded.

Add a 47uF cap close to the HC12 power pins
E
Hello, thank you for the documentation. To answer your questions:

Bent antennas: I bent them because then they would be pointing upwards towards the top of my target. I have seen little antenna that are black with brass fittings that appear to be on some sort of ball joint so you can bend them in different directions, so I thought I could get away with soldering the spiral ones at an upward angle. I am not sure why they would need to be perpendicular to the HC12 PCB boards to function, but I dont know much about these units at all. If that is the case do you think I could use the other antenna for ANT1 port and point that one upwards, I would have used those originally but I wasn't sure which ones were compatible or how they are actually connected, would you happen to have a link to one?

Ground plane and Shielded: Ok so by the sounds of things the antenna need some space around them and if they are to close to other power components this can disturb the signals? Also what do you mean by shielded? Ok so I have 3d printed a case to go around my target and its pretty snug, I was hoping to keep the antenna within the case to make a cleaner design, I figured if the signals can pass through several walls in a house whats 1mm of PLA plastic going to do right? And in practice these signals only have to travel up to 10 metres in open air so I thought I could get away with it. What would you suggest as the optimum position for this board, would you have the antenna fully exposed out of the top of the target?

Cap near power points: This sounds familiar I was reading through the comments on @MarkHughes article and he states:
You should always use decoupling capacitors in your designs. A 1 nf or 0.1 nf capacitor as close to the device as possible. These capacitors prevent a phenomena called ground-bounce. When I’m designing boards, I put decoupling capacitors within 1mm of all power rails.
Is this what your recommending? He's used a much lower sizes cap that you recommended, any particular reason why do you think?

Thanks for your help!
 

Thread Starter

KorrinF

Joined May 29, 2018
11
The program in post #8 never sends anything to the HC-12. Use the code from #1 but make sure that the SET pin is at 0V.
Hello again, ah yes sorry so the code from post #1 from my understanding which admitted is not great atm, will send text or an AT command to another HC12 unit and display it on its connected computer. But that code isn't working for me so there may be something wrong with my physical set up as others have pointed out, so instead I am just trying to send an AT command from this computer to the single connected HC12 module via the arduino. I thought this code might work but it also hasn't returned anything on my serial monitor, and I made sure to code in the SET pin to 'LOW' :

Code:
#include <SoftwareSerial.h>

const byte RX = 11;
const byte TX = 10;
const byte SET = 8;

SoftwareSerial HC12 (TX,RX); //create software serial port

void setup() {
// put your setup code here, to run once:
// pinMode(RX,INPUT);
// pinMode(TX,OUTPUT);

  pinMode(SET, OUTPUT);                  // Output High for Transparent / Low for Command
  Serial.begin(9600); //open serial port to computer
  HC12.begin(9600);// open serial port to HC-12
  Serial.println("Hello World - Software Serial");
}



//The functions "*.available()" reads the number of bytes stored in the Arduino's Serial
//or SoftwareSerial FIFO buffers. As long as there is a non-zero value returned by
//"*.available()", the "*.read()" function pulls one byte out of the buffer and the
//"*.write()" function sends that byte along to the other serial port to be read by
//either the computer or the HC-12.



void loop() {

    digitalWrite(SET, LOW);           // Set HC-12 into AT Command mode
    delay(100);                          // Wait for the HC-12 to enter AT Command mode
    HC12.print("AT+RX");               // Send AT Command to HC-12
    delay(200);
    while (HC12.available()) {           // If HC-12 has data (the AT Command response)
    Serial.write(HC12.read());         // Send the data to Serial monitor
    }
   
  }
 

Thread Starter

KorrinF

Joined May 29, 2018
11
Hi K,
Do you have a sketch showing how the TX and RX units will be located, the range required etc.??
E
Hello, in practice the targets will be speaking back and forth to a controller that is 10-20 meters away most of the time. The fact these are capable of such long ranges make them really flexible to use them at greater distances but at the moment lets stick with that.

I dont have any sketches, I could take more photos if it helps but im not sure entirely of what you are asking in terms of their locations. Really step one I just want to get the sample code from sketch one to work or even send an AT command. Ive been trying to send AT+RX and haven't got a response from the unit on my breadboard. Thats what ive got at the moment a breadboard 'Controller' and 10 PCB's with the HC12 already soldered to them. I can desolder these and move them else where in the casing for the 'Targets' to get these working if necessary.
 

Thread Starter

KorrinF

Joined May 29, 2018
11
May I suggest you do a search on the 'Picaxe forum' for HC-12's.
There is a lot of information. Unfortunately not for Arduino.
Good luck.
Oh thanks yeah I haven't heard of them before but its worth a look and I might get into using them. :)
 

ericgibbs

Joined Jan 29, 2010
9,305
hi K.
If possible I would have both spiral antenna in the vertical plane.
If the PCB was say flat and horizontal, have that antenna vertical relative to the PCB ground plane.

10 mtrs range will not be a problem.
I have always been advised that a capacitor say 22uF or 47uF should be connected on the +5v/0v lines, close the the module.
Note: if you are only using it over 10mtrs, reduce the TX power, by program SET, this will limit any nuisance to other users on the same channel, in your area.

E
 

djsfantasi

Joined Apr 11, 2010
5,913
I was wondering if the HC12 is just looking for a Low or for the transition between High and Low. The datasheet is ambiguous to me. It simply states that you must put SET to a Low. Not that it must be Low; you must put it Low.

It’s simple enough to test. At the beginning of your loop, write High to SET, pause and then continue.

I also have continued to research in what state is an Ardunio digital pin, after its defined as an OUTPUT by pinmode(). I’ve never received a definitive answer. Thus my setup() routines always write to an OUTPUT digital pin in my sketches.
 

ericgibbs

Joined Jan 29, 2010
9,305
hi dj,
The SET line has an internal pull up, so when the pin is o/c it is in the TX/RX mode, you have to pull and hold the SET pin Low to program the HC".
E
 
Top