Problem with Teensy 4.1 and HC12 Comunication via SoftwareSerial library

Luca

Active member
Hi, i am trying to make a radio comunication beetween an arduino uno and a teensy using HC12 module but i have incountered some difficulties.
Following this tutorial https://howtomechatronics.com/tutorials/arduino/arduino-and-hc-12-long-range-wireless-communication-module/ on two original arduinoes no problem, but when i try it on the teensy it does not work.
I read that for the teensy is not that good to use the software serial library, but for me is very important having multiple serial devices connected together and because of that i can't use only the 0 and 1 pins on the teensy.

BTW the code that i am trying to use is this:
Code:
/*    Arduino Long Range Wireless Communication using HC-12
                     Example 01
  by Dejan Nedelkovski, www.HowToMechatronics.com
*/

#include <SoftwareSerial.h>

SoftwareSerial HC12(7, 8); // HC-12 TX Pin, HC-12 RX Pin

void setup() {
  pinMode(10, OUTPUT);
  digitalWrite(10, LOW);
  Serial.begin(9600);             // Serial port to computer
  HC12.begin(9600);               // Serial port to HC12

}

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

How can i solve this issue? Thanks Luca.
 
Code:
SoftwareSerial HC12(7, 8); // HC-12 TX Pin, HC-12 RX Pin
You did not swap RX and TX accidentally?
From this page it states:
Code:
[COLOR="#FF0000"]SoftwareSerial [/COLOR]mySerial =  [COLOR="#FF0000"]SoftwareSerial[/COLOR](rxPin, txPin);

Paul
 
I am sure that's not the problem simply because trying the same code on a regular arduino with the same schematic it works corectly. Thanks Paul, Luca.
 
Found this message: So far, SoftwareSerial is transmit only on Teensy 4.x, when not using pins which are a real hardware serial port.
On a Teensy 4.1, pins 7 & 8 are hardware serial ports: pin 7 = RX2, pin 8 = TX2.

Capture.PNG

So you don't need to use softwareSerial.
See this page how to use Serial2.

Paul
 
Code:
// set this to the hardware serial port you wish to use
#define HWSERIAL Serial2

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

void loop() {
  while (HWSERIAL.available()) {        // If HC-12 has data
    Serial.write(HWSERIAL.read());      // Send the data to Serial monitor
  }
  while (Serial.available()) {      // If Serial monitor has data
    HWSERIAL.write(Serial.read());      // Send that data to HC-12
  }
}
This i the code i am trying to use know and it also does not comunicate (BTW i am doing this test wiring the SET pin on HC12 to ground in order to send some AT comands)
 
Last edited:
If you do a search of the forum you will that the most common issue with using the HC-12 is miss wiring of the rx/tx pins. Since you are using Serial2 you need to make sure that pin 7 is going to the TX pin on the HC-12 and the pin 8 is going to the RX Pin.

The second thing in you your example sketch you are not really set up to use AT Commands (look at the sketch that is on the link you provided). If you want to run the sketch in post #5 don't connect the set pin to ground. And you will need a second HC-12 sending and receiving data as well.
 
I don't know why, but i think that one of the HC12 just brocke cause of a short circuit, (the chip and the power cables were so warm (the one connected to the teensy with a breadboard power suply)). I cant swear it was not broken already before tryin it out but i wil order some more radios and i will let you know.
 
Purchased 2pc genuine HC-12 modules and hooked them up to a Teensy 4.1 and a Teensy LC.

Teensys_HC-12.png

They are communicating as expected, see this screendump using Arduino IDE 2.0:

Capture.jpg

Used Luca's code from message #5.
I was happily surprised that I could open a serial monitor for each Teensy at the same time - is this a new feature of IDE 2.0?

Paul
 
Thank you very mutch guys for your time and at this point i think that the problem with the second code that i shared was a brocken HC-12. the new one is on the way and if it works i will let you know. Luca
 
Hey guys, i have just tested outthe boards and they works just fine.
Thanks everybody for the help and have a nice day. Luca
 
Back
Top