I want to send a character from a Zigbee transmitter to an Arduino Mega 2560. After the character is received, Arduino should send some data back to Zigbee. But Arduino is always receiving junk values at the console. If I send a character, suppose 'a', instead of 'a', Arduino console is always showing 'y'. It is the same for every character I send.

#include <EEPROM.h>
#include <SoftwareSerial.h>
SoftwareSerial XBee(19, 18); // RX, TX
int addr = 0;
unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 1000;  //the value is a number of milliseconds
void setup()

  startMillis = millis();  //initial start time
void loop()
  int id = 1;
  EEPROM.write(addr, id);
  if (Serial1.available()>0)
      currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis - startMillis >= period)  //test whether the period has elapsed
    char recbyte=Serial.read();
    // delay(1500);
    Serial1.write(" 30 ");
    Serial1.write(" 99 ");
    Serial1.write(" 72 ");
    Serial1.write(" 1098 ");
    Serial1.write(" 728 ");
    Serial1.write(" 578 ");
    // Serial1.write("$");
    startMillis = currentMillis;
  • There is a code formatting button on the editor toolbar but your code indentation is a big mess. If you are that careless then you can expect trouble. Use the edit link under your question to fix it and check the preview before you press "Save Edits".
    – Transistor
    Nov 26, 2018 at 17:59
  • @Transistor I got it.... For Akhila, have you tried other baud rates?
    – user23244
    Nov 26, 2018 at 18:02
  • @King: I approved your edit but it's still not right. Look at the close braces at the bottom. There are other problems too. The author should fix the question.
    – Transistor
    Nov 26, 2018 at 18:04
  • 1
    Whatever your actual issue is, having an EEPROM write in the the outermost uncondition part of your loop() is a mistake. Nov 26, 2018 at 18:10
  • 1
    Your problem however is likely that you seem to be trying to take serial input from the PC. Perhaps you mean to take it from Serial1 to which it seems you have the XBEE connected? Nov 26, 2018 at 18:12

1 Answer 1


You have created a SoftwareSerial instance called XBee which is taking pins 17 and 18. You then use Serial1 which is pins 17 and 18.

You do not want, nor need, the XBee SoftwareSerial instance, since you are using hardware serial pins. Delete it and all references to it.

Also, you are turning off Serial1 after using it, which means it will never work again:


You would only use that instruction in very very specific circumstances. Delete it.

After checking to see if there is data available to read on Serial1 you then proceed to try and read it from Serial:

char recbyte=Serial.read();

That should be:

char recbyte=Serial1.read();

You are writing to EEPROM on every single iteration of loop:

EEPROM.write(addr, id);

It is likely that you have already killed your EEPROM. C'est la vie...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.