# Arduino RFID and Processing Connection

**URL:** https://discourse.processing.org/t/arduino-rfid-and-processing-connection/32108
**Category:** Electronics (Arduino, etc.)
**Created:** [September 5, 2021, 7:45pm UTC](https://discourse.processing.org/t/arduino-rfid-and-processing-connection/32108 "2021-09-05T19:45:09Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![angoldini01](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@angoldini01](https://discourse.processing.org/u/angoldini01)
#### Post date: [September 5, 2021, 7:45pm UTC](https://discourse.processing.org/t/arduino-rfid-and-processing-connection/32108/1 "2021-09-05T19:45:09Z")

</div>

Hey,

I’m currently working on a project where I want to switch the colors of a square in processing through using rfid chips with arduino. So everytime I place a rfid-chip on my rfid-reader, arduino should send the data to processing, and depending on the card-number the color of the square in processing should change.  
The connection is working and I cannot switch the colors of the square in processing. I think the problem lays in the processing code.  
I use the Arduino Elegoo Uno R3 and the joy-it RFID Reader.  
Would love to get some help, how to solve this problem!

Here is the Processing-Code

```auto
import processing.serial.*;

Serial myPort; // The serial port  
String finalRead = ""; // Variable with final data  
String inputRead = ""; // Variable that recieves the data  
int state = 0; // Variable to store the program state  

void setup() {

  size(800, 800); //Size of the program window
  background(50, 50, 150); //Background colour

  // List all the available serial ports
  printArray(Serial.list());

  // Open the port you are using at the rate you want:
  myPort = new Serial(this, Serial.list()[4], 9600);

  // The rectangle will be placed based on it's own center
  rectMode(CENTER);

}

void draw() {

  /*When the serial port is available the data from the RFID is 
    loaded into the finalRead variable.*/

  while (myPort.available() > 0)
  {
    //The c variable is updated with the read method

    char c=(char)myPort.read();

    /*
      When processing recieves data from Arduinos println command 
      it is recieved with a \r in the beginning
      and a c\ in the end. 
      Therefore we add c to inputRead when 
      we are not encountering \r.

      If we encounter \n we know that the string is done reading. 
      The string is loaded into the finalRead variable and 
      the data is cleared from inputRead.
     */

    if (c=='\n')
    {
      finalRead = inputRead;
      inputRead = ""; 
      println("data: " + finalRead);
    } else if (c!='\r')
    {
      inputRead+=c;
    }
  } 

   /*Now the program checks if the data is equal to the data
   on a wanted RFID-chip. If thats the case the state of the   
   program changes. Here you need to fill in data from a specific 
   RFID-chip*/

  if (finalRead.equals("7320574163")) {
    state = 1;
  }

  /*Another RFID-chip if-statement with another state. You can do this with as many RFID-chips you want.*/

  if (finalRead.equals("21110723902")) {
    state = 2;
  }

  /*If the state is changed to 1, the colour of the center Rectangle 
    is changed to red.*/

  if (state == 1) {
    fill(#ffffff);
  }

  /*If the state is change to 2, the colour of the center Rectangle is changed to green.*/
  if (state == 2) {
    fill(#000000);
  }

  /*The program now draws a rectangle with a fill colour dependent on the state variable*/
  rect(width/2, height/2, width-100, height-100);
}

```

here is the Arduino-Code

```auto
/*
 * ----------------------------------------------------------------------
 * Example program showing how to read new NUID from a PICC to serial.
 * ----------------------------------------------------------------------
 * https://circuits4you.com
 * 
 * RC522 Interfacing with NodeMCU
 * 
 Typical pin layout used:
 * ---------------------------------------------------------------------
 * MFRC522 Arduino Arduino Arduino Arduino Arduino
 * Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
 * Signal Pin Pin Pin Pin Pin Pin
 * ----------------------------------------------------------------------
 * RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
 * SPI SS SDA(SS) 10 53 D10 10 10
 * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
 * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
 * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
 */

#include <SPI.h>
#include <MFRC522.h>

constexpr uint8_t RST_PIN = 9; // Configurable, see typical pin layout above
constexpr uint8_t SS_PIN = 10; // Configurable, see typical pin layout above
 
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

MFRC522::MIFARE_Key key; 

// Init array that will store new NUID 
byte nuidPICC[4];

void setup() { 
  Serial.begin(9600);
  SPI.begin(); // Init SPI bus
  rfid.PCD_Init(); // Init MFRC522 

  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }

  Serial.println(F("This code scan the MIFARE Classsic NUID."));
  Serial.print(F("Using the following key:"));
  printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
}
 
void loop() {

  // Look for new cards
  if ( ! rfid.PICC_IsNewCardPresent())
    return;

  // Verify if the NUID has been readed
  if ( ! rfid.PICC_ReadCardSerial())
    return;

  Serial.print(F("PICC type: "));
  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  Serial.println(rfid.PICC_GetTypeName(piccType));

  // Check is the PICC of Classic MIFARE type
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&  
    piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
    piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println(F("Your tag is not of type MIFARE Classic."));
    return;
  }

  if (rfid.uid.uidByte[0] != nuidPICC[0] || 
    rfid.uid.uidByte[1] != nuidPICC[1] || 
    rfid.uid.uidByte[2] != nuidPICC[2] || 
    rfid.uid.uidByte[3] != nuidPICC[3] ) {
    Serial.println(F("A new card has been detected."));

    // Store NUID into nuidPICC array
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];
    }
   
    Serial.println(F("The NUID tag is:"));
    Serial.print(F("In hex: "));
    printHex(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();
    Serial.print(F("In dec: "));
    printDec(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();
  }
  else Serial.println(F("Card read previously."));

  // Halt PICC
  rfid.PICC_HaltA();

  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();
}

/**
 * Helper routine to dump a byte array as hex values to Serial. 
 */
void printHex(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}

/**
 * Helper routine to dump a byte array as dec values to Serial.
 */
void printDec(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], DEC);
  }
}

```

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [September 5, 2021, 8:21pm UTC](https://discourse.processing.org/t/arduino-rfid-and-processing-connection/32108/2 "2021-09-05T20:21:35Z")

</div>

Hello,

Your Processing Code is flashing with this simple test code from Arduino:

```auto
void setup() 
  {
  Serial.begin(9600);
  delay(1000);
  }
  
void loop() 
  {
  Serial.println("7320574163");
  delay(1000);
  Serial.println("21110723902");
  delay(1000);
  }

```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/2ec4db3d6c70a32c6fcc88d13bfb0d5ff6d2cd18.png)

To get you started:

Most of those `Serial.print()` statements are intended for the Arduino serial monitor for development; only send the required data to Processing for processing.

Do not use the Arduino serial monitor or plotter when making a serial connection to Processing or other software\devices when using a single COM port.

_Edited comment about delay()_  
I always add a `delay(1000)` at end of setup() in Processing to allow the Arduino to reset before receiving data. Watch the lights on the Arduino to get familiar with these.  
This may not apply to this example.

There are many serial examples in this forum; start simple and build on that.

Reference:

> **[Serial / Libraries](https://processing.org/reference/libraries/serial/index.html)**
>
> Send data between Processing and external hardware through serial communication (RS-232).

`:)`

---

<div class="post-metadata">

### Author: ![angoldini01](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@angoldini01](https://discourse.processing.org/u/angoldini01)
#### Post date: [September 13, 2021, 5:33pm UTC](https://discourse.processing.org/t/arduino-rfid-and-processing-connection/32108/3 "2021-09-13T17:33:19Z")

</div>

Hello,

I added the delay as you requested, but unfortunately I have still the same problem :/.  
Would love to hear other tips 😃  
Anna

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [September 13, 2021, 6:07pm UTC](https://discourse.processing.org/t/arduino-rfid-and-processing-connection/32108/4 "2021-09-13T18:07:35Z")

</div>

Hello,

Did the code example I provided for Arduino work with your Processing code?  
You should see output similar to what I have.

The delay() was related to this:

> [@Processing to Arduino Serial Library Example](https://discourse.processing.org/t/processing-to-arduino-serial-library-example/11143/5):
>
> I modified the existing “SimpleWrite” example to: save and check for the state of mouseOverRect() print state to console only writes serial data once a delay() to wait for Arduino to reset once connected to it; in my case, it is an Arduino Mega 2560 R3. I kept the spirit of the original example intact and left it as a “Simple Write” program. My edit of “SimpleWrite” example so it only sends data once: /\*\* \* Simple Write. \* \* Check if the mouse is over a rectangle and writes the status …

I edited my last post on this.

`:)`

---

<div class="post-metadata">

### Author: ![angoldini01](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@angoldini01](https://discourse.processing.org/u/angoldini01)
#### Post date: [September 13, 2021, 7:50pm UTC](https://discourse.processing.org/t/arduino-rfid-and-processing-connection/32108/5 "2021-09-13T19:50:40Z")

</div>

Yes I can see the number of the rfid chip in the compiler of processing. But the square is not changing the colors. When I scan the rfid chip the color of the square in processing should change depending on the rfid chip number. Any ideas why that is? 😄 #beingveryhelpful

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [September 13, 2021, 10:53pm UTC](https://discourse.processing.org/t/arduino-rfid-and-processing-connection/32108/6 "2021-09-13T22:53:48Z")

</div>

Hi  
Try background in draw function

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [September 13, 2021, 11:21pm UTC](https://discourse.processing.org/t/arduino-rfid-and-processing-connection/32108/7 "2021-09-13T23:21:49Z")

</div>

Hello,

When I run the Arduino example I provided and your Processing code the rectangle changes color from black to white as expected and repeats every 1000 ms.

Can you share a screen grab of what you are seeing on the serial monitor of the Arduino IDE when you start Arduino and scan a card?

`:)`

---

<div class="post-metadata">

### Author: ![angoldini01](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@angoldini01](https://discourse.processing.org/u/angoldini01)
#### Post date: [September 16, 2021, 8:27am UTC](https://discourse.processing.org/t/arduino-rfid-and-processing-connection/32108/8 "2021-09-16T08:27:04Z")

</div>

I’m glad it works from your side!  
Yeah of course,I can send you a screenshot, here is what I see:

 ![Bildschirmfoto 2021-09-16 um 10.26.09](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/86202237ba8de34353985f1bdbb6c240b6aecdd7.jpeg)

---

<div class="post-metadata">

### Author: ![angoldini01](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@angoldini01](https://discourse.processing.org/u/angoldini01)
#### Post date: [September 16, 2021, 8:48am UTC](https://discourse.processing.org/t/arduino-rfid-and-processing-connection/32108/9 "2021-09-16T08:48:14Z")

</div>

I wish it was that easy, but that’s not working neither

---

<div class="post-metadata">

### Author: ![angoldini01](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@angoldini01](https://discourse.processing.org/u/angoldini01)
#### Post date: [September 16, 2021, 9:28am UTC](https://discourse.processing.org/t/arduino-rfid-and-processing-connection/32108/10 "2021-09-16T09:28:56Z")

</div>

![Bildschirmfoto 2021-09-16 um 11.28.28](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/481830c00137a657ede43746b0a3e87311706d5f.jpeg)  
Here is one photo with the processing screen on

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [September 16, 2021, 10:47am UTC](https://discourse.processing.org/t/arduino-rfid-and-processing-connection/32108/11 "2021-09-16T10:47:13Z")

</div>

Hello,

Your Processing code is looking for this:

```auto
  if (finalRead.equals("7320574163")) {
    state = 1;
  }

```

You are sending this:

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/572680fd74fb700e8a0cd7fe9bd2de89010ccaf6.png)

That is why it is not working.

I previously made this suggestion:

> [@glv](#):
>
> Most of those `Serial.print()` statements are intended for the Arduino serial monitor for development; only send the required data to Processing for processing.

You have to decide what to do:

- Modify your Processing code to look for the String you are sending with the Arduino code you are using as is:  
`"data: 49 CD 4A A3"`
- Modify your Arduino code to send a String to match your Processing code.

The data seems to be correct:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/6c00801364f84360b46d2d5ae7b682b76d1100d7.png)

The example I provided [here](https://discourse.processing.org/t/arduino-rfid-and-processing-connection/32108) sends the correctly formatted String data and Processing is receiving it and it working as expected.

I kept it simple as this is your project.

Give this some more thought.  
Try to get it working as is.  
Clean it up later.

`:)`

---

<div class="post-metadata">

### Author: ![angoldini01](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@angoldini01](https://discourse.processing.org/u/angoldini01)
#### Post date: [September 16, 2021, 1:19pm UTC](https://discourse.processing.org/t/arduino-rfid-and-processing-connection/32108/12 "2021-09-16T13:19:53Z")

</div>

Thank you so so much, it is finally working!!! 😍

---

<div class="post-metadata">

### Author: ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)
#### Post date: [September 16, 2021, 1:51pm UTC](https://discourse.processing.org/t/arduino-rfid-and-processing-connection/32108/13 "2021-09-16T13:51:22Z")

</div>

I don’t know if your Processing code is using the printed output from that printDec function, but it’s confused. Clearly printHex prints the numbers 0…255 as 2 digit hex values with leading zero where required. printDec is a copy of printHex with HEX changed to DEC, but without rethinking the requirement and logic for leading zeroes. The result is this:

00 01 02 03 04 05 06 07 08 09 010 011 012 013 014 015 16 17 18 19 … 99 100 101 … 255
