Using HC12 module for RC plane

Hi,

I'm trying to build an RC plane (with some added features, all of which work) but I can't get the HC12 modules to communicate with each other.

At the moment all I want to do is read a potentiometer on one Arduino, and then have a second Arduino receive that data using two HC12s, map it to 0 - 180, and then use that to move a servo.
I'm using just one as a test as the entire plane is effectively just four servos (and a brushless motor).

I've tried several methods of using the SoftwareSerial library, and due to my limited knowledge none of them get any useful information.

The most appropriate code I could find was this (transmitter then receiver):

//Transmitter
//visit www.rootsaid.com for full tutorial
//For Video Demo and tutorials Visit www.youtube.com/c/rootsaid


#include <SoftwareSerial.h>
#include<Servo.h>         

Servo esc; 
SoftwareSerial hc12(2, 3); //RX, TX

int thr, ail, ele, rud;

void setup()
{
esc.attach(10);
pinMode (A0, INPUT);
pinMode (A1, INPUT);
pinMode (A6, INPUT);
pinMode (A7, INPUT);
pinMode (A2, INPUT);
pinMode (13, OUTPUT);
 
Serial.begin(9600);
hc12.begin(9600);
}

void loop()
{
  ele=map(analogRead(A0), 0, 1023, 0, 180);
  rud=map(analogRead(A1), 0, 1023, 0, 180);
  thr=map(analogRead(A7), 0, 1023, 0, 180);
  ail=map(analogRead(A5), 0, 1023, 0, 180);

  hc12.print(thr);
  hc12.print(",");
  hc12.print(ail);
  hc12.print(",");
  hc12.print(ele);
  hc12.print(",");
  hc12.print(rud);
  hc12.println("");
 
  Serial.print(thr);
  Serial.print(",");
  Serial.print(ail);
  Serial.print(",");
  Serial.print(ele);
  Serial.print(",");
  Serial.print(rud);
  Serial.println("");


delay(100);  
}
//Receiver
//Visit www.rootsaid.com for full tutorial
//For Video Demo and tutorials Visit www.youtube.com/c/rootsaid

#include <SoftwareSerial.h>
#include<Servo.h>          

Servo esc; 

Servo ele;
Servo ruddr;

String input;
int throttle, th;
int aileron, ail;
int elevator, el;
int rudder, rud;

int boundLow;
int boundHigh;
const char delimiter = ',';

SoftwareSerial hc12(4, 5);

void setup()
{
ele.attach(9);
ruddr.attach(11);

esc.attach(10);
Serial.begin(9600);
hc12.begin(9600);
esc.write(170);
delay(2000);
esc.write(90);
delay(2000);
esc.write(140);
delay(2000);
esc.write(90);
delay(2000);
}

void loop()
{

if(Serial.available())
  {
  input = Serial.readStringUntil('\n');
  if (input.length() > 0)
      {
       // Serial.println(input);
       
        boundLow = input.indexOf(delimiter);
        throttle = input.substring(0, boundLow).toInt();
    
        boundHigh = input.indexOf(delimiter, boundLow+1);
        aileron = input.substring(boundLow+1, boundHigh).toInt();
    
        boundLow = input.indexOf(delimiter, boundHigh+1);
        elevator = input.substring(boundHigh+1, boundLow).toInt();
    
        rudder = input.substring(boundLow+1).toInt();

       esc.write(throttle);
       ele.write(elevator);
       ruddr.write(rudder);
       delay(10);      }

  }

}

The transmitter arduino's TX light flashed which (perhaps) suggested it was sending data, although I'm not sure as this could also just be it sending data to the serial monitor. On the receiver arduino, no lights other than the power light were on, and the serial monitor was empty.

The only other code I had a small amount of success with was a modified version from HowToMechatronic's tutorial on the HC12:

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

void setup() {
  HC12.begin(9600);               // Open serial port to HC12
  
  Serial.begin(9600);
  
}
void loop() {
      int pot = analogRead(A0);
   
      Serial.println(pot);
      String potString = String(pot);

      HC12.print("s" + potString + "e");
      delay(10);
      
    
  }
}
#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin
char incomingByte;
String readBuffer = "";

void setup() {
  Serial.begin(9600);             // Open serial port to computer
  HC12.begin(9600);               // Open serial port to HC12
 
  delay(100);
}
void loop() {
  readBuffer = "";
  boolean start = false;
  // Reads the incoming angle
  while (HC12.available()) {             // If HC-12 has data
    incomingByte = HC12.read();          // Store each icoming byte from HC-12
    delay(5);
    // Reads the data between the start "s" and end marker "e"
    if (start == true) {
      if (incomingByte != 'e') {
        readBuffer += char(incomingByte);    // Add each byte to ReadBuffer string variable
      }
      else {
        start = false;
      }
    }
    // Checks whether the received message statrs with the start marker "s"
    else if ( incomingByte == 's') {
      start = true; // If true start reading the message
    }
  }
  // Converts the string into integer
  int pos = readBuffer.toInt();

   Serial.println(pos);
 
}

I believe this should cause values between 0 and 180 to show up in the Serial Monitor from the second arduino, but there is only a string of zeroes.
Very occasionally the desired value would come through however, it was repeated, and still surrounded by a bunch of zeroes.

Is there a really good tutorial on using the HC12 (because most just show example sketches instead of explaining what each bit does).
If not could someone please explain any mistakes in the code or what I should to do get it to work.

Thank you very much

ITregear:
Is there a really good tutorial on using the HC12 (because most just show example sketches instead of explaining what each bit does).

If the HC12 was suitable for remote control of an RC plane, you would expect that since the HC12s have been around for some time, there would have been published code by now.

Did you find any ?

That's what I would have thought too, I've searched a fair amount and have only found one decent tutorial, although I just couldn't get it to work.

And thanks for the links I'll check them out and see if any work.

ITregear:
That's what I would have thought too, I've searched a fair amount and have only found one decent tutorial, although I just couldn't get it to work.

Maybe it does not work, not fast enough etc.

Hi ITregear,

Did you get any resolution about this?

I want to transmit multiple variables data using HC 12 but do not know how to do it . :frowning:

kvponkshe:
I want to transmit multiple variables data using HC 12 but do not know how to do it . :frowning:

Try a Google search on;

Arduino HC12 Tutorial

I suspect that nRF24L01+ transceivers would be more suitable for flying radio control. You could use one of the high-power modules with the external antenna in the hand held unit.

AFAIK they (or their Cypress equivalent) are used in commercial 2.4GHz model RC systems.

...R
Simple nRF24L01+ Tutorial

Try a Google search on;

Arduino HC12 Tutorial

as noted above, there are many, and none of them convey useful information

you would expect one to have an HC-12 management sketch, something that would read the current values, let you set new values, read the result of that setting - the very first thing you would do with any HC-12. nope.