Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 11 of 11

Thread: Problem with Teensy 4.1 and HC12 Comunication via SoftwareSerial library

  1. #1
    Junior Member
    Join Date
    Jun 2022
    Posts
    7

    Problem with Teensy 4.1 and HC12 Comunication via SoftwareSerial library

    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/tutori...cation-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.

  2. #2
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    863
    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:
    SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
    Paul

  3. #3
    Junior Member
    Join Date
    Jun 2022
    Posts
    7
    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.

  4. #4
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    863
    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.

    Name:  Capture.PNG
Views: 49
Size:  53.6 KB

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

    Paul

  5. #5
    Junior Member
    Join Date
    Jun 2022
    Posts
    7
    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 by Luca; 09-20-2022 at 08:33 PM.

  6. #6
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    8,406
    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.

  7. #7
    Junior Member
    Join Date
    Jun 2022
    Posts
    7
    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.

  8. #8
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    863
    Purchased 2pc genuine HC-12 modules and hooked them up to a Teensy 4.1 and a Teensy LC.

    Click image for larger version. 

Name:	Teensys_HC-12.png 
Views:	4 
Size:	729.6 KB 
ID:	29451

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

    Click image for larger version. 

Name:	Capture.jpg 
Views:	8 
Size:	91.4 KB 
ID:	29452

    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

  9. #9
    Junior Member
    Join Date
    Jun 2022
    Posts
    7
    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

  10. #10
    Junior Member
    Join Date
    Jun 2022
    Posts
    7
    Hey guys, i have just tested outthe boards and they works just fine.
    Thanks everybody for the help and have a nice day. Luca

  11. #11
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    8,406
    Quote Originally Posted by Luca View Post
    Hey guys, i have just tested outthe boards and they works just fine.
    Thanks everybody for the help and have a nice day. Luca
    Glad you got it working

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •