HC-12 not transmitting (Arduino Nano RP2040 and ESP32)

Hello, i'm trying to transmit a simple informatinio from a ESP32 with HC-12 and a Arduino Nano RP2040 with a HC-12 has a receiver.

The code for the transmitter is the following:

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

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

void loop() {
  // put your main code here, to run repeatedly:
  Serial.print("ola");
  HC12.print("ola");
  delay(2000);
}

and for the receiver:

void setup() {
  // Set pin A1 as an output and pull low for HC-12 settings mode
  pinMode(A1, OUTPUT);
  digitalWrite(A1, LOW);

  // Start serial ports
  Serial.begin(9600);
  Serial1.begin(9600); // Use hardware serial 1 for HC-12 communication
}

void loop() {
  if (Serial1.available() > 0) {
    String input = Serial1.readString();
    Serial.println("Received: " + input); // Add this line for debug
    if (input == "ola") {
      digitalWrite(LED_BUILTIN, HIGH);
      delay(500);
      digitalWrite(LED_BUILTIN, LOW);
    }
  }
}

I have the following pins connected:
ESP32 >> HC12
3,3V >> VCC
GND >> GND
TX2 (17) >> RX
RX2(16) >> TX
D5>>SET

RP2040 >>HC12
3,3V >> VCC
GND >> GND
A3 >> RX
A2 >> TX
A1>>SET

Im I doing something wrong??

How are the things powered?

An external 5V power supply for each controller

have a look at rp2040-01-technical-reference
under UART it specifies Serial1
The pins used for UART (Universal asynchronous receiver-transmitter) are the following:

  • (Rx) - D0*
  • (Tx) - D1*

this is a test of Serial1 and Serial2 I ran on a Raspberry Pi Pico RP2040

// Raspberry Pi Pico RP2040 Serial1 and Serial2 test
//
// text entered on Serial sent to Serial1 Tx to Serial2 Rx to Serial2 Tx to Serial1 Rx to be displayed on serial monitor

// Note
// Serial is the USB serial for program loading and serial mointor
// there are two hardware serial ports UART0 and UART1
//  Serial1 is mapped to UART0 on Rx pin GP1 Tx pin GP0
//  Serial2 is mapped to UART1 on Rx pin GP5 Tx pin GP4

#define TX2 4
#define RX2 5

void setup() {
  // initialize both serial ports:
  delay(5000);
  Serial.begin(115200);
  Serial.println();
  Serial.println("\n\nRaspberry Pi Pico RP2040 hardware serial test on Serial1 and Serial2"
                 "\n  Serial1 Rx pin GP1 Tx pin GP0");
  Serial1.begin(115200);
  // assign pin numbers for Serial2
  Serial2.setTX(TX2);
  Serial2.setRX(RX2);
  Serial2.begin(115200);
  Serial.print("  Serial2 RX2 pin GP" + String(RX2));
  Serial.println(" TX2 pin GP" + String(TX2));
  Serial.println("connect GP0 to GP5 and GP4 to GP1");
  Serial.println("text entered on Serial sent to Serial1 Tx to Serial2 Rx to Serial2 Tx to Serial1 Rx to be displayed on serial monitor");
}

void loop() { 
  while (Serial2.available()) {
    String s = Serial2.readStringUntil('\n');
    Serial.println("Serial2 = " + s);
    Serial2.println("from Serial 2 " + s);
  }
  while (Serial1.available()) {
    Serial.println("Serial1 = " + Serial1.readStringUntil('\n'));
  }
  while (Serial.available()) {
    String s = Serial.readStringUntil('\n');
    Serial.println("Transmitting to Serial1 " + s);
    Serial1.print("from Serial 1 " + s);
  }
}

a run gave

Raspberry Pi Pico RP2040 hardware serial test on Serial1 and Serial2
  Serial1 Rx pin GP1 Tx pin GP0
  Serial2 RX2 pin GP5 TX2 pin GP4
connect GP0 to GP5 and GP4 to GP1
text entered on  Serial sent to Serial2 sent to Serial1 displayed on serial monitor
Transmitting to Serial1 test 1
Serial2 = from Serial 1 test 1
Serial1 = from Serial 2 from Serial 1 test 1
Transmitting to Serial1 test 2
Serial2 = from Serial 1 test 2
Serial1 = from Serial 2 from Serial 1 test 2
Transmitting to Serial1 test 3 1234567890
Serial2 = from Serial 1 test 3 1234567890
Serial1 = from Serial 2 from Serial 1 test 3 1234567890
Transmitting to Serial1 test 4 abcdefghijklmnopqrstuvwxyz
Serial2 = from Serial 1 test 4 abcdefghijklmnopqrstuvwxyz
Serial1 = from Serial 2 from Serial 1 test 4 abcdefghijklmnopqrstuvwxyz

Thank you. but i change the code to:

void setup() {
  // Set pin A1 as an output and pull low for HC-12 settings mode
  pinMode(A1, OUTPUT);
  digitalWrite(A1, LOW);

  // Start serial ports
  Serial.begin(9600);
  Serial1.begin(9600); // Use hardware serial 1 for HC-12 communication
}

void loop() {
  // Check if data is available on serial port
  if (Serial1.available() > 0) {
    String input = Serial1.readString();
    if (input == "ola") {
       digitalWrite(LED_BUILTIN, HIGH);
       delay(500);
       digitalWrite(LED_BUILTIN, LOW);
    }
  }
}

but still didn't blink the led :frowning:

Changed the connection on the receiver to:

RP2040 >>HC12
3,3V >> VCC
GND >> GND
TX >> RX
RX >> TX
A1>>SET

I 'm strugling with this for a week and nothing...:frowning:

If I connect one controller to the other wire (RX/TX from the ESP32 to TX/RX from the NANO) the LED brink, but if I use the HC-12 it doesn't do anything.

I tested both HC-12 with the code

{
  while (HC12.available()) 
  {        
    // If HC-12 has data
    Serial.write(HC12.read());      // Send the data to Serial monitor
  }
  while (Serial.available()) 
  {      
    // If we have data from Serial monitor
    HC12.write(Serial.read());      // Send that data to HC-12
  }

and I can sent AT command on both.
On AT-RX, both give me:

OK+B9600
OK+RC001
OK+RP:+20dBm
OK+FU3

I don't know what else I can do.... Everything seems ok

noted in post 5 you initialize A1 pin for output then blink LED_BUILTIN

Yes. A1 to pin SET from the HC12. Isnt that correct?

you also need to initialize

 pinMode(LED_BUILTIN, OUTPUT);
1 Like

The set pin puts the module in AT command mode. You do not want to be in AT command mode for for communication. Pin should be HIGH unless you are changing parameters in AT mode.

https://www.elecrow.com/download/HC-12.pdf

1 Like

I DID IT!!! :slight_smile: THANK YOU VERY MUCH FOR YOUR HELP !!
If you ever come to Lisbon/Portugal I'll gladly buy you a beer :slight_smile:

So to help someone else in the same situation here goes the correct code:

ESP32 Transmitter:

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

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

void loop() {
  Serial.print("ola");
  HC12.print("ola");
  delay(2000);
}

Arduino NANO P2040 Reciever:

void setup() {
  // Set pin A1 as an output and pull low for HC-12 settings mode
  pinMode(A1, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(A1, HIGH);

  // Start serial ports
  Serial.begin(9600);
  Serial1.begin(9600); // Use hardware serial 1 for HC-12 communication
}

void loop() {
  // Check if data is available on serial port
  if (Serial1.available() > 0) {
    String input = Serial1.readString();
    if (input == "ola") {
       digitalWrite(LED_BUILTIN, HIGH);
       delay(500);
       digitalWrite(LED_BUILTIN, LOW);
    }
  }
}

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