Transmitter and receiver with HC12

Hi, I am creating a DIY drone, the flight controller I wrote will have 200Hz refresh rate (5000 microseconds cycle).
I am also trying to use the DIY transmitter and receiver made with HC12 module. But the problem is, the receiver takes a long time(25000 microseconds) to get the data from the transmitter. is it due to serial communication?
can anyone help me how can I improve my code or reduce the transmission time? I have tried the NRF24l01, but those didn't work for me, ended up just wasting money by buying bunch of new modules, so I came across to use the HC12 module.

Another question, can anyone tell me the radio.read() function used in the NRF24l01, how much time is takes to execute?

code for receiver

#include <SoftwareSerial.h>
#include <Wire.h>

#define RXp2 16
#define TXp2 17

SoftwareSerial HC12;

int data[4];
String input;

void setup() {
  Serial.begin(115200);

  HC12.begin(9600, SWSERIAL_8N1, RXp2, TXp2);   // using esp32 as receiver
  HC12.setTimeout(10);
}


void loop() {
  long int start = micros();
  input = "";
  
  if (HC12.available()==0) {
    input = HC12.readStringUntil('\n');   //incoming string format: "1200 1500 1500 1500"
    if (input.length() > 0) {
      Serial.println(input); // should print: "1200 1500 1500 1500"
    }
  }

  convert_input(); // for converting the string input to throttle, yaw, pitch, roll
  Serial.println(micros() - start);  // just measuring the time it takes to receive 
}

Really, software serial on an ESP32? Does it not have multiple hardware serial ports?

For digital radio, 25ms is not a really unusually large latency...

@saif_hossain At this point, you are just going to have to get better equipment. HC 12 is NOT good for low latency applications. At best, you can average about 30-35ms, which is more than enough for most hobby projects. I myself have used HC 12 in RC vehicles, and it performs decently. If you want it to be as fast as 5ms, you should use a wifi network / better, more powerful transmitter/receiver.

This code looks something weird...

Why do you read the port ONLY when it has nothing to read? Did you understand< what is

operator do?

And if you're worried about speed - don't use the readStringUntil() method, it will wait until timeout.

The serial input basics tutorial will show better methods of receiving and sending serial data.

Can you suggest me some more powerful transmitter and receiver option?

HC12.avaiale() always shows 0 even when the data is coming. It seemed very weird to me too.

What can I use instead of readstringuntil()?

See post #5.

What sort of range\distance do you want ?

More power is not always the answer ..............

range is around 1000m, and more powerful means least latency in this range

The HC12 does have a built in latency, the interface waits a set period when there is no characters ariving on the serial interface, before transmiting the actual packet.

Typical SPI connected radio devices send the data as soon as the outgoing data buffer is filled.

You might get a NRF24 setup to work at that distance, but the SX1280 2.4Ghz LoRa device seems a good choice, it should cover that distance and has lower latency, higher speed and no legal duty cycle limits versus the UHF LoRa devices.

See here;

I have tried to use nrf24l01, but could not come up to a working solution. that's why choose hc12,
lora 2.4ghz is not available in my country at local shop. however, sx1278 433MHz is available here. how is the latency in 433mhz module? can I use it?

Lora is really slow...

UHF LoRa has been used for quite a few years for RC control, so safe to assume its fast enough. It will run at 37,500bps, so an RC control packet of say 16 bytes will have an air time of 6ms, so you could send packets at circa 100hz, which is a faster than a lot of standard RC control systems.

2.4Ghz LoRa, which would do the range you suggested, is much faster, and a 16byte packet would have an air time of 1.3ms.

Probably within the range you need is the FLRC mode of the SX1280 LoRa modules and that would have an air time of circa 0.15mS, so 40 times faster than UHF LoRa.

LoRa can be fast, see the link I provided on high performance RC links using LoRa.

Okay, bought two lora today, let me see

There is a working example of using one of those el-cheapo 'Arduino' joysticks to remotely drive a couple of servos in the LoRa library below.

The TX interval for the control packets is set for 9 times the transmit time to keep the transmit duty cycle to 10%, which is the legal limit in a lot of places for UHF.

Normally no duty cycle limits for 2.4Ghz LoRa though.

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