Quantcast
Channel: Sensors - Arduino Forum
Viewing all articles
Browse latest Browse all 1078

Elegoo mega2560 r3 wih M6E nano rfid using hardware serial

$
0
0

I am trying to connect my Arduino/elegoo mega 2560 r3 with the m6e nano rfid using hardware serial I made the changes to the code for the library for reading the tag, I made sure the switch is in the HW - UART, and used the serial2 of the mega and after uploading code it shows this in the serial monitor:

and it is not detecting the tag at all

This is the connection, and I ensured the wiring is also right.

here is the code:

/*
  Reading multiple RFID tags, simultaneously!
  By: Nathan Seidle @ SparkFun Electronics
  Date: October 3rd, 2016
  https://github.com/sparkfun/Simultaneous_RFID_Tag_Reader

  Single shot read - Ask the reader to tell us what tags it currently sees. And it beeps!
*/

// Library for controlling the RFID module
#include "SparkFun_UHF_RFID_Reader.h"

// Create an instance of the RFID module
RFID rfidModule;

// Use Hardware Serial2 for communication with the RFID module
#define rfidSerial Serial2 // Use Serial2 for the Elegoo Mega 2560 R3

// Set the baud rate for the RFID module
#define rfidBaud 115200 // Set baud rate to 115200 for hardware serial

// Specify which module you are using
#define moduleType ThingMagic_M6E_NANO
// #define moduleType ThingMagic_M7E_HECTO

#define BUZZER1 9
//#define BUZZER1 0 //For testing quietly
#define BUZZER2 10

void setup()
{
  Serial.begin(115200);

  pinMode(BUZZER1, OUTPUT);
  pinMode(BUZZER2, OUTPUT);

  digitalWrite(BUZZER2, LOW); //Pull half the buzzer to ground and drive the other half.

  while (!Serial);
  Serial.println();
  Serial.println("Initializing...");

  if (setupRfidModule(rfidBaud) == false)
  {
    Serial.println("Module failed to respond. Please check wiring.");
    while (1); // Freeze!
  }

  rfidModule.setRegion(REGION_NORTHAMERICA); // Set to North America

  rfidModule.setReadPower(500); // 5.00 dBm
}

void loop()
{
  Serial.println(F("Press a key to scan for a tag"));
  while (!Serial.available()); // Wait for user to send a character
  Serial.read(); // Throw away the user's character

  byte myEPC[12]; // Most EPCs are 12 bytes
  byte myEPClength;
  byte responseType = 0;

  while (responseType != RESPONSE_SUCCESS)
  {
    myEPClength = sizeof(myEPC); // Length of EPC is modified each time .readTagEPC is called

    responseType = rfidModule.readTagEPC(myEPC, myEPClength, 500); // Scan for a new tag up to 500ms
    Serial.println(F("Searching for tag"));
  }

  // Beep!
  tone(BUZZER1, 2093, 150); // C
  delay(150);
  tone(BUZZER1, 2349, 150); // D
  delay(150);
  tone(BUZZER1, 2637, 150); // E
  delay(150);

  // Print EPC
  Serial.print(F(" epc["));
  for (byte x = 0; x < myEPClength; x++)
  {
    if (myEPC[x] < 0x10) Serial.print(F("0"));
    Serial.print(myEPC[x], HEX);
    Serial.print(F(" "));
  }
  Serial.println(F("]"));
}

// Gracefully handles a reader that is already configured and already reading continuously
boolean setupRfidModule(long baudRate)
{
  rfidModule.begin(rfidSerial, moduleType); // Tell the library to communicate over serial port

  // Test to see if we are already connected to a module
  rfidSerial.begin(baudRate); // Start serial at desired baud rate
  delay(100); // Wait for port to open

  // Ignore initial messages from the module
  while (rfidSerial.available())
    rfidSerial.read();

  rfidModule.getVersion();

  if (rfidModule.msg[0] == ERROR_WRONG_OPCODE_RESPONSE)
  {
    rfidModule.stopReading(); // Stop continuous reading
    Serial.println(F("Module continuously reading. Asking it to stop..."));
    delay(1500);
  }
  else
  {
    // The module did not respond, assume it's been powered on and is communicating at 115200bps
    rfidSerial.begin(115200); // Start serial at 115200
    rfidModule.setBaud(baudRate); // Set module baud rate

    rfidSerial.begin(baudRate); // Start the serial port at user's chosen baud rate
    delay(250);
  }

  // Test the connection
  rfidModule.getVersion();
  if (rfidModule.msg[0] != ALL_GOOD)
    return false; // Something is not right

  // Set protocol and antenna
  rfidModule.setTagProtocol(); // Set protocol to GEN2
  rfidModule.setAntennaPort(); // Set TX/RX antenna ports to 1

  return true; // We are ready to rock
}

Please kindly let me know what I am doing wrong. Thank you

4 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 1078

Trending Articles