HC12 data issues

I have two Nano's each with an HC12, and the sketches below. This is just a simple test setup to get the hang of using them. On the transmitter side, the onboard LED blinks as it should, and the TX LED also blinks, though more of a flicker, but that's to be expected.

On the receiving end, the RX LED flashes when the transmitter TX LED flashes, but not the on board LED. I added a line to print the variable to the serial monitor, and it's just blasting out zeros. So I'm inclined to think that it is receiving OK, but something is going wrong with the data. Maybe I need sleep and missed something stupid, but I can't figure out what it is.

On a side note, I had tried HC12.write instead of Serial.write and no luck. Same with Serial.print. But, when I used HC12.write (1); it was OK with that. But change the one to a zero and I would get and error "call of overloaded 'write(int)' is ambiguous"

Transmit sketch first, then the receiver sketch second.

#include <SoftwareSerial.h>

SoftwareSerial HC12(1,0); //TX pin and RX pin


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

void loop() {
  Serial.write (1);
  digitalWrite(13, HIGH);
  delay (500);
  Serial.write (2);
  digitalWrite(13, LOW);
  delay (500);

}
#include <SoftwareSerial.h>

SoftwareSerial HC12(1,0); //TX pin and RX pin

byte val;


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

void loop() {

  while (Serial.available()) {
    val = Serial.read();
  }

  if (val == 1) {
    digitalWrite(13, HIGH);
  }
  else if (val == 2) {
    digitalWrite(13, LOW);
  }
}
SoftwareSerial HC12(1,0); //TX pin and RX pin

Don't use pins 0 and 1 for SoftwareSerial because they are already used for the hardware Serial interface

Thank You. That seems to be the ticket. So much for YouTube.

But, any idea why you can't use HC12.write(0); ?

Sorry, no. Please post the full sketch that caused the error and the full error copied from the IDE using the "Copy error message" button

Same thing except HC12.write instead of Serial.write, but here it is:

#include <SoftwareSerial.h>

SoftwareSerial HC12(2,3); //TX pin and RX pin

byte val = 0;

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

void loop() {
  HC12.write (1);
  delay (500);
  HC12.write (0);
  delay(500); 


}
#include <SoftwareSerial.h>

SoftwareSerial HC12(2, 3); //SRX = DPin-2, STX = DPin-3

byte val;


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

void loop() {

  while (HC12.available()) {
    val = HC12.read(); //Serial.read();
  }

  if (val == 1) {
    digitalWrite(13, HIGH);
  }
  else if (val == 2) {
    digitalWrite(13, LOW);
  }
}

I did use a 2 instead and it works. What I'm trying to figure out is why does it not like using a 0?

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