HC-12 button

There doesn't appear to be a lot of documentation I can find when it comes to using the HC-12 transceivers with Arduino. I am trying to do something that, in theory, should be simple but I'm hitting some odd roadblocks.

I first tried the simplest of things from this video here - Getting startet with the HC-12 and Arduino for wireless communication - from Banggood - YouTube

//send

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.print("Hello World");
  delay(2000);
}
//Receive

void setup() {
    Serial.begin(9600);
    pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
    if(Serial.available() > 0)
    {
        String input = Serial.readString();
        if(input == "Hello World")
        {
            digitalWrite(LED_BUILTIN, HIGH);
            delay(500);
            digitalWrite(LED_BUILTIN, LOW);
        }
    }

}

It didn't work so I thought the modules might be faulty. I then found this website and their code worked so I knew the modules were functioning. Arduino HC12 example 2019 library range

//send

#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin
int pot = A2;
void setup() {
  Serial.begin(9600);             // Serial port to computer
  HC12.begin(9600);               // Serial port to HC12
  pinMode(pot,INPUT);
}
void loop() {  
  int val = map(analogRead(pot),0,1024,0,255);
  HC12.write(val);      // Send that data to HC-12  
}
//Receive

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

void setup() {
  Serial.begin(9600);             // Serial port to computer
  HC12.begin(9600);               // Serial port to HC12
  pinMode(LED,OUTPUT);
}
void loop() {
  while (HC12.available()) {        // If HC-12 has data
    int val = HC12.read();
    Serial.println(HC12.read());      // Send the data to Serial monitor
    analogWrite(LED,val);
  }  
}

What I need to do is push a button on one side and an led turns off for 2 seconds on the other side and then back on. So I then found this code on a website:

//send

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); //RX, TX

int buttonPin = 8;
boolean onOff = 0;
void setup() {
  pinMode(buttonPin, INPUT);
  mySerial.begin(9600);
}

void loop() {
 
  int buttonState = digitalRead(buttonPin);//read button state
  
  if(buttonState == 1){//if button is down
    mySerial.println(1111);//send unique code to the receiver to turn on. In this case 1111
    onOff = 1;//set boolean to 1
  }
  if(buttonState == 0 && onOff == 1){//Verifier to send off signal once
    mySerial.println(0000);//send unique code to the receiver to turn off. In this case 0000
  }
  delay(20);//delay little for better serial communication
}
//Receive

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

int ledPin = 13;

void setup() {
  mySerial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop() {
   
  if(mySerial.available() > 1){    
    int input = mySerial.parseInt();//read serial input and convert to integer (-32,768 to 32,767)    
    if(input == 1111){//if on code is received
      digitalWrite(ledPin, HIGH);//turn LED on
    }
    if(input == 0000){//if off code is received
      digitalWrite(ledPin, LOW);//turn LED off
    }
  }
  mySerial.flush();//clear the serial buffer for unwanted inputs     
  
  delay(20);//delay little for better serial communication
 
}

However this is also a no go. I'm more than confused why one worked fine and two did not. The only change I can see is that in the one that worked, it used HC12 to call functions instead of mySerial. I have also noticed some wiring shows RX to RX and TX to TX and others have them swapped.

I'm not sure if certain HC-12 modules require different stuff or what. Anyone have any suggestions? I'm currently using a Mega and an Uno until I get some Nanos in.

well there is a difference on where you connect things:

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

You need to ensure you connect the right Arduino pins to Rx and Tx of your module - and unless the labels are misleading, Tx needs to be connected to Rx and Rx to Tx. Voltage also need to match expectations, don't send 5V through Tx into a 3.3V Rx....

I understand the ports are different. But I see wiring diagrams where sometimes it says connect RX to RX etc and other where it's the right way (RX to TX)

The picture you show seems to say Rx to Tx

J-M-L:
The picture you show seems to say Rx to Tx

Not from the HC-12 but let's ignore the RX and TX stuff for now. That I can suss out myself at this point. The more important part is trying to figure out how to get this working consistently and achieve what I'd like to do through understanding.

What they do here is trying to get the HC12 as a transparent clone of the hardware Serial.

I found the uno a bit supportive of this type of wiring but not other equipments, you should not have 3 serial ports connected to each other. It’s not a good practice if both would start speaking at the same time and one sending a 0 and the other one a 1. So depends what they do in that tutorial.

Post the true code that is not functioning and explain on which pins you have stuff connected

If you look up above, the third set of codes I posted are the ones that are the closest to what I need but isn't working compared to the second set.

Here is my basic question: what is the protocol for sending/receiving a signal from one of these HC-12 modules to another. That is the info I can't seem to find anywhere.

what is the protocol for sending/receiving a signal from one of these HC-12 modules to another. That is the info I can't seem to find anywhere.

The HC-12 is a half-duplex wireless serial communication module. The transmitter is paired with a receiver . UART requires an agreed upon timing scheme (baud rate, there is no clock signal), frequency band and your self decided data structure

My notes

  • 100 channels in the 433.4-473.0 MHz range
  • supposedly capable of transmitting up to 1 km with line of sight
  • 4-pin building up a TTL-level UART interface (Vcc, Gnd, Tx, Rx)
  • a 5th pin used to set the command mode. when driven to LOW, you can change settings using AT commands sent to the "RXD" pin at 9600 bauds by default.
  • Changing the baud rate changes the Over-the-Air Baud Rate and Receiver Sensitivity. So paired device needs to adjust too.
  • The default (AT+DEFAULT ) config is called FU3: 20mW transmit, 9600 bps (AT+9600) on Channel 1 (AT+C001).
  • To avoid interferences at high power With antennas, the transceivers must be separated by at least 1.4 meters.
  • The power supply needs to be at least able to provide 200mA.
  • if Vcc Is > 4.5V, insert a 1N4007 diode in series with the HC-12 Vcc pin
  • connect a 22 µF to 1 mF capacitor in between the "Gnd" and "Vcc" pins

the third set of codes I posted are the ones that are the closest to what I need but isn't working compared to the second set.

as already mentioned, the pins are different, so it is expected that one works and not the other one with the same setup. Describe YOUR setup and exact code YOU use