Error coding HC-12 (SOLVED)

#define RXD2 44 //(RX2)
#define TXD2 43 //(TX2)
#define HC12 Serial2 //Hardware serial 2 on the ESP32

int counter = 0;
bool cont = true;

void setup()
{
Serial.begin(115200); // Serial port to computer
pinMode(5, OUTPUT);
digitalWrite(5, LOW); //Normally HIGH, LOW for settings
delay(1000);
HC12.begin(9600, SERIAL_8N1, RXD2, TXD2); // Serial port to HC12

}

void loop()
{
while (HC12.available())
{
// If HC-12 has data]
Serial.println(β€œ1”);
Serial.println(HC12.read()); // Send the data to Serial monitor
}
while (Serial.available())
{
// If we have data from Serial monitor
Serial.println(β€œ2”);
HC12.write(Serial.read()); // Send that data to HC-12
}
counter++;
}

I am trying to program a custom ESP32-S3-WROOM-1 chip connected to an HC-12 transmitter. I found the code above online on this link ESP32 I2C Communication: Set Pins, Multiple Bus Interfaces and Peripherals | Random Nerd Tutorials and modified it slightly to see if my HC-12 works. According to the website, when I run this code and enter β€œAT” in my serial monitor, the monitor should print β€œOK”. However, I don’t get any such response. The modifications I introduced in the code are the print lines in the different while loops, to basically see whether the Serial and HC12 are active. When I upload the code, I get the following output:

1
224

The β€œ1” comes from my print statement, which means the HC-12 is receiving data, while the 224 comes from the HC-12. Not sure what that means. (EDIT 1: I decided to completely disconnect the HC-12, and re-run, and the 224 is still printed. No idea why)

However, after that, whenever I enter any data on the serial monitor, I get back a bunch of "2"s, which means my Serial is sending data, but I don’t see any other "1"s, which means the HC-12 isn’t receiving any data. I have no idea what I am doing wrong, could anybody help?

which ESP32S3 board are you using?
if I run the following on a ESP32-S3-DevKitC-1

// ESP32S3 - HC-12   test AT commands (take SET to GND)
// Serial1 test - for loopback test connect pins 18 and 17

// ESP32S3 - HC-12 AT commands (take SET to GND)

// note: power HC-12 from 5V - 3.3V gives spurious characters and does not transmit all the buffer
//  use a potential divider in HC-12 TXD to ESP32S3 Rx to convert 5V signals to 3.3V

#define RXD1 18 // to HC-12 TXD
#define TXD1 17 // to HC-12 RXD

void setup() {
  // initialize both serial ports:
  Serial.begin(115200);
  //Serial1.begin(115200, SERIAL_8N1, RXD1, TXD1);
  Serial1.begin(115200, SERIAL_8N1, RXD1, TXD1);
  Serial1.begin(9600, SERIAL_8N1, RXD1, TXD1);
  Serial.println();
  Serial.println("\n\nESP32S3 serial1  test Rx pin 18 Tx pin 17");
  Serial.write("   for loopback test connect pin 18 to pin 17\n");
  Serial.println("ESP32S3 - HC-12 AT commands (take SET to GND)");
}

void loop() {
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    byte inByte = Serial1.read();
    Serial.write(inByte);
  }

  // read from port 0, send to port 1:
  if (Serial.available()) {
    byte inByte = Serial.read();
    Serial.write(inByte);
    Serial1.write(inByte);
  }
}

I get the serial monitor output in response to AT commands

ESP32S3 serial1  test Rx pin 18 Tx pin 17
   for loopback test connect pin 18 to pin 17
ESP32S3 - HC-12 AT commands (take SET to GND)
AT
OK
AT+V
www.hc01.com HC-12 v2.6
AT+RX
OK+B9600
OK+RC001
OK+RP:+20dBm
OK+FU3

\224 is octal for 0x94 which is a double quotation mark.

The chip on my board is "ESP32-S3-WROOM-1", but I have been using ESP32S3 Dev Module on Arduino IDE, and that worked fine when I was connecting another sensor, GY-91.

For the test, could I double check, when you say the following:

you are asking me to connect pin 18 to pin 17, which means that the TXD and RXD would essentially be connected to the same pin?

Yes, that is what a loop back test is. An output pin connected to an input pin. So what you send is what you receive.

yes - as @Grumpy_Mike states it is a loopback test

say you connect the HC-12 to the ESP32S3
if does not work?

  1. is HC-12 the problem
  2. does the ESP32 serial IO program have a problem
  3. is the ESP32S3 Serial1 port faulty
  4. have you connected to the incorrect ESP32S3 pins

loopback tests 2 to 4 above to ensure the basic ESP32S3 program and hardware is working
if it works you remove the link and connect the external equipment - in this case a HC-12

does it still not work? e.g.

  1. have you connected the ESP32S3 Tx and RX to HC-12 RX and Tx respectively?
  2. have you powered the HC-12 correctly
  3. have you taken the HC-12 SET pin to GND to enable AT commands
  4. is the Serial1 baudrate correct for HC-12 AT commands?
  5. do the AT commands need to be in upper case?
  6. etc etc

when one has a multi-module system which is not performing to specification attempt to test each module in turn to identify the problem module(s)

Using your code, I get the following output:

ESP32S3 serial1  test Rx pin 18 Tx pin 17
   for loopback test connect pin 18 to pin 17
ESP32S3 - HC-12 AT commands (take SET to GND)
AT
AT

What can I conclude from this?

did you try connecting pin 17 to pin 18 to form a loopback test - did it work?

if it worked check the connections to the HC-12 - could you have pin 17 and 18 connections the wrong way around?
what line ending is the serial monitor set too? I tend to use both NL and CR
how have you powered the HC-12?
post a photo showing the connections?

Yep, I connected pin 17 to 18 and ran the code that you had written, but I didn't get back the same results that you did.


Just to check, the HC-12 is supposed to be disconnected from the esp-32 during the loopback test right?

I changed the line ending to the one you suggested, but I still get the same results.

by connecting pin 17 to 18 text entered on the serial monitor should echo back to the display
if a basic loopback test is not working zero chance communicating with the HC-12

Whenever I send "AT", I get back 2 "AT"s, as you can see in this image.

Is this correct?

looks like you are running the loopback test (pin 17 connected to pin 18)
if so you are getting an echo of what you typed

if this is the case remove the link between the pins and connect the HC-12

Alright, I have connected the HC-12, and I ran the following code:

#define RXD2 18	//(RX2)
#define TXD2 17	//(TX2)
// #define HC12 Serial0  //Hardware serial 2 on the ESP32

#include <HardwareSerial.h>
HardwareSerial HC12(1);

void setup() 
{
  Serial.begin(115200);           // Serial port to computer
  pinMode(5, OUTPUT);
  digitalWrite(5, LOW);           //Normally HIGH, LOW for settings
  HC12.begin(9600, SERIAL_8N1, RXD2, TXD2); // Serial port to HC12

  while (!Serial){
    delay(100);
  }

  Serial.println("Serial monitor initialized.");

  if (!HC12) { // If the object did not initialize, then its configuration is invalid
    Serial.println("Invalid EspSoftwareSerial pin configuration, check config"); 
    while (1) { // Don't continue with invalid configuration
      delay (1000);
    }
  }
  else{
    Serial.println("HC-12 initialized.");
  }
}

void loop() {

  while (HC12.available())
  {
    Serial.println("Received Data from HC12");
    Serial.print("r = ");
    Serial.println(HC12.read());
  }
  while (Serial.available())
  {
    Serial.println("Reading user serial input");
    String userInput = Serial.readString();
    // what are we about to send?
    Serial.print("sending w = ");
    Serial.println(userInput);
    // punch it!
    for(char dataByte : userInput)
    {
      HC12.write(dataByte); 
    }
  }
}

I receive the following results in the serial monitor

Serial monitor initialized.
HC-12 initialized.
Reading user serial input
sending w = AT

I don't get back any "OK".

if I connect a HC-12 to my ESP32-S3-DevKitC-1
HC-12 Tx to ESP32S3 pin 18 RXD2
HC-12 Rx to ESP32S3 pin 17 TXD2
HC-12 VCC to 3.3V and GND to GND
HC-12 SET to GND
and run your post 13 code and enter "AT" I get

Serial monitor initialized.
HC-12 initialized.
Reading user serial input
sending w = AT

Received Data from HC12
r = 79
Received Data from HC12
r = 75
Received Data from HC12
r = 13
Received Data from HC12
r = 10

you are printing the ASCII code value of "OK" followed by CR/LF
try

while (HC12.available())
  {
    Serial.println("Received Data from HC12");
    Serial.print("r = ");
    Serial.println((char)HC12.read());
  }

which then prints

Serial monitor initialized.
HC-12 initialized.
Reading user serial input
sending w = AT

Received Data from HC12
r = O
Received Data from HC12
r = K
Received Data from HC12
r = 

Received Data from HC12
r = 

I don't have the board with me right now, I will try connecting it tomorrow, but could I check why you connect the Tx pin to the RXD pin and why you connect the Rx pin to the TXD pin?

in general a Tx on a module means transmit out and Rx means receive in

hence HC-12 to my ESP32-S3-DevKitC-1

HC-12 Tx out to ESP32S3 pin 18 RXD2 in Black wire
HC-12 Rx in to ESP32S3 pin 17 TXD2 out orange wire
HC-12 VCC to 3.3V red wire and GND to GND blue wire
HC-12 SET to GND brown wire

NOTE: above rule does not always apply - I have seen modules where Tx means transmit in etc - one has to read module data sheets otherwise you can design and build a PCB and start by cutting tracks and soldering pieces of wire onto the PCB

YESS, I get back OK now, thank you so much!! Now if I want to transmit data, do I just have to set the "SET" pin to be HIGH, or do I also have to change any connections?

Ok, I tried it and yeah, the connections remain the same but the "SET" pin should be high. Thank you so much for the help everyone!

the HC-12 SET has an internal pull-up resistor so you if don't need to use AT commands don't connect it to anything - it will be pulled HIGH and the HC-12 will be in transparent communication mode

if you need to use AT commands connect SET to a digital GPIO pin
1 set LOW to use AT mode
2 set HIGH for transparent communication