# Serial connection between Processing and Arduino doesn't work

**URL:** https://discourse.processing.org/t/serial-connection-between-processing-and-arduino-doesnt-work/34054
**Category:** Electronics (Arduino, etc.)
**Created:** [December 11, 2021, 1:18pm UTC](https://discourse.processing.org/t/serial-connection-between-processing-and-arduino-doesnt-work/34054 "2021-12-11T13:18:11Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![grudovz](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@grudovz](https://discourse.processing.org/u/grudovz)
#### Post date: [December 11, 2021, 1:18pm UTC](https://discourse.processing.org/t/serial-connection-between-processing-and-arduino-doesnt-work/34054/1 "2021-12-11T13:18:11Z")

</div>

I’m trying to connect an Arduino Nano Every and Processing via serial. I’m getting no errors on either side, and the arduino serial functionality works otherwise on the arduino IDE.

I’m connecting on the right port, and the port is busy while the processing code is running (as you would expect), however, the variable inside the arduino is not updating. In fact, the whole if serial.available statement is not executing.

Arduino:

```auto
int var;

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available()) {
    var = Serial.read();
  }
  Serial.println(var);
  delay(100);
}

```

Processing:

```auto
import processing.serial.*;

// The serial port
Serial myPort;

void setup() {
  // List all the available serial ports:
  printArray(Serial.list());
  // open port
  myPort = new Serial(this, Serial.list()[2], 9600);
}

void draw() {
  try {
    myPort.write(1);
  }
  catch(Exception e) {
    // Print detailed error information to the console.
    System.err.println(e);
    e.printStackTrace();
  }
  delay(50);
}

```

---

<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: [December 11, 2021, 8:38pm UTC](https://discourse.processing.org/t/serial-connection-between-processing-and-arduino-doesnt-work/34054/2 "2021-12-11T20:38:48Z")

</div>

> [@grudovz](#):
>
> and the port is busy while the processing code is running (as you would expect),

It is busy if another application is using the serial port such as :

- Arduino SerialMonitor
- Arduino Serial Plotter
- And so on…

Your Processing code is sending data to the Arduino and the Arduino is sending the data back to Processing as a string.

Reference:  
_[Serial.print() - Arduino Reference](https://www.arduino.cc/reference/en/language/functions/communication/serial/print/)_

Modified Processing code to receive the data to demonstrate that it is working:

```auto
import processing.serial.*;

// The serial port
Serial myPort;

byte b;

void setup() 
  {
  // List all the available serial ports:
  printArray(Serial.list());
  // open port
  myPort = new Serial(this, Serial.list()[0], 9600);
  delay(1000);
  }

void draw() 
  {
  try 
    {
    myPort.write(b++); // Counts from 0 to 255 and repeats since it is a byte
    }
  catch(Exception e) 
    {
    // Print detailed error information to the console.
    System.err.println(e);
    e.printStackTrace();
    }
  //delay(50);
  
  if(myPort.available() > 0)
    {
    int incoming = myPort.read();
    println(incoming);
    }
  }

```

I also added a `delay(1000)` in Processing in the `setup()` to allow the Arduino to reboot before sending data and flooding the buffers.

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

Related topic:

> [@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 only modified your code to show what it is doing _as is_.  
You still have some exploration to do to understand serial communications.

`:)`

---

<div class="post-metadata">

### Author: ![grudovz](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@grudovz](https://discourse.processing.org/u/grudovz)
#### Post date: [December 12, 2021, 3:07pm UTC](https://discourse.processing.org/t/serial-connection-between-processing-and-arduino-doesnt-work/34054/3 "2021-12-12T15:07:17Z")

</div>

Thank you very much for taking the time to respond, and you are most definitely correct - lots to learn! Thank you again for helping out.

---

<div class="post-metadata">

### Author: ![grudovz](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@grudovz](https://discourse.processing.org/u/grudovz)
#### Post date: [December 12, 2021, 3:21pm UTC](https://discourse.processing.org/t/serial-connection-between-processing-and-arduino-doesnt-work/34054/4 "2021-12-12T15:21:53Z")

</div>

Would you be able to suggest a good resource to get to grips with the basics of serial communication? I have basic/intermediate C++ and can only work with the arduino IDE and starting out with processing.

---

<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: [December 14, 2021, 8:55am UTC](https://discourse.processing.org/t/serial-connection-between-processing-and-arduino-doesnt-work/34054/5 "2021-12-14T08:55:22Z")

</div>

> [@grudovz](#):
>
> Would you be able to suggest a good resource to get to grips with the basics of serial communication?

Hello,

There is a stuff out there!

Try a Google search for:

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

I have gleaned through a lot of this quickly to get some insight and then write my own code and routines.

Let us know what you find useful.

My background is in assembly and C and my understanding of serial communications is much different than someone new to this topic.

This is my world (from [ATmega328P datasheet](https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf)) and how I started programming serial ports:

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

This low level programming may not be useful to a beginner but has given me a much deeper insight into what is going on at a hardware level and I can better understand serial communications.

`:)`

---

<div class="post-metadata">

### Author: ![grudovz](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@grudovz](https://discourse.processing.org/u/grudovz)
#### Post date: [December 14, 2021, 10:20am UTC](https://discourse.processing.org/t/serial-connection-between-processing-and-arduino-doesnt-work/34054/6 "2021-12-14T10:20:05Z")

</div>

That’s really cool, thank you for sharing! And for taking time to help out newbies on this forum.

---

<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: [December 14, 2021, 11:59am UTC](https://discourse.processing.org/t/serial-connection-between-processing-and-arduino-doesnt-work/34054/7 "2021-12-14T11:59:22Z")

</div>

Hello,

Serial communications:  
[https://learn.sparkfun.com/tutorials/serial-communication/all](https://learn.sparkfun.com/tutorials/serial-communication/all)

Connecting Arduino to Processing:  
[https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/all](https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/all)

Some good stuff here:  
[https://itp.nyu.edu/physcomp/](https://itp.nyu.edu/physcomp/) You will have to search through categories.

Processing Serial Library:  
[https://processing.org/reference/libraries/serial/index.html](https://processing.org/reference/libraries/serial/index.html)  
This examples and documentation are simple but could use some updates and corrections for clarity.

As I said before do a search and find what best suits you.

There are plenty of topics in this forum as well.

Serial communications is simple but can be daunting at first… once you get over the initial learning curves it gets easier.

`:)`

---

<div class="post-metadata">

### Author: ![grudovz](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@grudovz](https://discourse.processing.org/u/grudovz)
#### Post date: [December 14, 2021, 1:14pm UTC](https://discourse.processing.org/t/serial-connection-between-processing-and-arduino-doesnt-work/34054/8 "2021-12-14T13:14:30Z")

</div>

Already started going through these resources, thank you for sharing them. Will post on the forum as things develop. Hope you have good holidays 🙂
