RFID reader problems with 2560

Hi guys,
one and the same code works with arduino uno without problems in contrast Arduino Mega 2560 is a serial read not possible. I get
with val = mySerial.read() -1. Any idea?

#include <SoftwareSerial.h>

#define rxPin 2
#define txPin 3
#define ledPin 13

// set up a soft serial port
SoftwareSerial mySerial(rxPin, txPin); 

int  val = 0; 
char code[10]; 
int bytesread = 0; 

void setup() { 
  // define pin modes for tx, rx, led pins:
  pinMode(rxPin, INPUT);    // Set rxPin as INPUT to accept SOUT from RFID pin
  pinMode(txPin, OUTPUT);   // Set txPin as OUTPUT to connect it to the RFID /ENABLE pin
  pinMode(ledPin, OUTPUT);  // Let the user know whats up

  // set the data rate for the serial ports
  mySerial.begin(2400);    // RFID reader SOUT pin connected to Serial RX pin at 2400bps
  Serial.begin(9600);      // Serial feedback for debugging in Wiring

  // say something
  Serial.println("Hello World!");

 void loop() { 
  digitalWrite(txPin, LOW);           // Activate the RFID reader
  digitalWrite(ledPin, LOW);          // Turn off debug LED

  if((val = mySerial.read()) == 10) { // check for header 
    bytesread = 0; 
    while(bytesread<10) {             // read 10 digit code 
      val = mySerial.read(); 
      if((val == 10)||(val == 13)) {  // if header or stop bytes before the 10 digit reading 
        break;                        // stop reading 
      code[bytesread] = val;          // add the digit           
      bytesread++;                    // ready to read next digit  
    if(bytesread == 10) {             // If 10 digit read is complete 
      digitalWrite(txPin, HIGH);      // deactivate RFID reader 
      digitalWrite(ledPin, HIGH);     // activate LED to show an RFID card was read
      Serial.print("TAG code is: ");  // possibly a good TAG 
      Serial.println(code);           // print the TAG code 
    bytesread = 0; 
    delay(2000);                      // wait for a second to read next tag

The Mega has 4 hardware serial ports. Why use software when there is already a dedicated hardware port available? Only minor mods to your code needed.

edit: I knew there was another reason. The coffee had to kick in before I remembered it. Thsi is from the SoftwareSerial page:

Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69

I do not see pins 2 and 3 there.

How can I use a hardware Serial port in the code?

I have read in the mega documentation Serial: 0 (RX) and 1 (TX); Serial 1: 19 (RX) and 18 (TX); Serial 2: 17 (RX) and 16 (TX); Serial 3: 15 (RX) and 14 (TX).

If I want to communicate with the reader via pin 18 and 19 I need to use Serial 1.
I must replace mySerial.begin(2400); with Serial1.begin(2400);
Is this correct?
Do you know can I register a interrupt on pin 19. The reading of the rfid key should be done via an interrupt.

Thanks for your help.

The hardware setup is easy!

void setup() {

  // rest of your setup

Then refer to your new hardware port as Serial1 like this:


The original serial port is still Serial.

Do you know can I register a interrupt on pin 19. The reading of the rfid key should be done via an interrupt.

The arrival of serial data on that pin already triggers an interrupt. The interrupt handler copies the serial data to a buffer, so that you can read the data later. You do NOT need an interrupt to tell you when there is serial data. Just call Serial1.available() to see if a tag has been scanned.

The code for the UNO did not have an interrupt on the pin that the data appeared on, except that provided by the SoftwareSerial instance.

Hi guys.

Desperately trying to finish up my senior design project, of which the RFID is only a small portion. I’ve got a similar setup to what was posted above: Arduino Mega, Parallax RFID reader. Need “Serial” to be used with USB to the PC for other communication, so I’ve tied the SOUT reader pin to RX1 (pin 19), and copied the code from the reference page. Here’s what I’ve got:

int val = 0;
char tag[10];
int bytesRead;

void setup() {
  // Start both hardware serial ports.
  pinMode(2, OUTPUT);  // ENABLE pin on reader

void loop() {
  if(Serial1.available() > 0) {
    if((val = Serial1.read()) == 10) {
      bytesRead = 0;
      while(bytesRead<10) {
        if(Serial1.available() > 0) {
          val = Serial1.read();
          if((val == 10) || (val == 13)) {
          tag[bytesRead] = val;
      if(bytesRead == 10) {
        Serial.print("Tag code is ");
      bytesRead = 0;
      digitalWrite(2, HIGH);
      digitalWrite(2, LOW);    

I checked the value of Serial1.available() and it maintains zero, even when I wave the card over it. I know the reader isn’t faulty: I tried the first hardware serial, leaving the reader unplugged while the sketch uploaded, and then plugged in the SOUT to RX0, and watched the Serial Monitor. Everything worked as intended. I’ll try Serial2 and Serial3 in case of dead pins, but… I’m kind of at a loss.

Any help would be appreciated!