# Serial connection between Processing and Arduino doesn’t work

**URL:** https://discourse.processing.org/t/serial-connection-between-processing-and-arduino-doesn-t-work/39557
**Category:** Electronics (Arduino, etc.)
**Created:** [November 2, 2022, 9:06pm UTC](https://discourse.processing.org/t/serial-connection-between-processing-and-arduino-doesn-t-work/39557 "2022-11-02T21:06:40Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Mathis](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@Mathis](https://discourse.processing.org/u/Mathis)
#### Post date: [November 2, 2022, 9:06pm UTC](https://discourse.processing.org/t/serial-connection-between-processing-and-arduino-doesn-t-work/39557/1 "2022-11-02T21:06:40Z")

</div>

Hello,  
I’m trying to send data from Arduino to processing via serial.  
I’m getting no errors on either side, and all seems to work.  
Just one problem : the data doesn’t arrive in processing…

Here is the code from Arduino:

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

void loop()
{
Serial.println("Hello world!");
delay(100);
}

```

Here is the code from Processing:

```auto
import processing.serial.*;

Serial myPort; // Create object from Serial class
String val; // Data received from the serial port

void setup()
{
  // I know that the first port in the serial list on my mac
  // is Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
   //change the 0 to a 1 or 2 etc. to match your port
  String arduinoPort = Serial.list()[0];
  myPort = new Serial(this, arduinoPort, 115200);
}
void serialEvent(Serial myPort) 
{
  val = myPort.readStringUntil('\n'); // read it and store it in val
  println(val); //print it out in the console
}

```

Thanks in advance for your help!  
Mathis

---

<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: [November 2, 2022, 10:05pm UTC](https://discourse.processing.org/t/serial-connection-between-processing-and-arduino-doesn-t-work/39557/2 "2022-11-02T22:05:00Z")

</div>

Hi

This is your example it’s working

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

---

<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: [November 2, 2022, 11:57pm UTC](https://discourse.processing.org/t/serial-connection-between-processing-and-arduino-doesn-t-work/39557/3 "2022-11-02T23:57:24Z")

</div>

Hello @Mathis,

An initial observation is that your baud rates do not match:

> [@Mathis](#):
>
> `Serial.begin(9600);`

> [@Mathis](#):
>
> `myPort = new Serial(this, arduinoPort, 115200);`

The baud rates should be the same.

`:)`

---

<div class="post-metadata">

### Author: ![Mathis](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@Mathis](https://discourse.processing.org/u/Mathis)
#### Post date: [November 3, 2022, 9:58am UTC](https://discourse.processing.org/t/serial-connection-between-processing-and-arduino-doesn-t-work/39557/4 "2022-11-03T09:58:09Z")

</div>

Thanks it was just a mistake in my copy paste.  
It still doesn’t work, void serialEvent() is never called, for whatever reason 😕

---

<div class="post-metadata">

### Author: ![Mathis](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@Mathis](https://discourse.processing.org/u/Mathis)
#### Post date: [November 3, 2022, 10:10am UTC](https://discourse.processing.org/t/serial-connection-between-processing-and-arduino-doesn-t-work/39557/5 "2022-11-03T10:10:28Z")

</div>

I’ve just found the problem.  
I was using an esp8266 which seems not to work with Processing unfortunately ☹ .  
I’ve changed it with an arduino board and all is working properly.

---

<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: [November 7, 2022, 11:29pm UTC](https://discourse.processing.org/t/serial-connection-between-processing-and-arduino-doesn-t-work/39557/6 "2022-11-07T23:29:44Z")

</div>

Hi @Mathis, I have two NodeMCU boards. I think these are simply ESP8266 mounted on a larger PCB. There are a few differences from Arduino, but for me nothing to stop it working with Processing. I’ve found the most significant difference is that the ESP program does not reset when Processing (or anything on the PC) opens the port. So I never see any prints from the setup() function. This code prints everything the ESP sends. I’m not using serialEvent.

```auto
import processing.serial.*;
Serial myPort;
int val;

void setup() {
  frameRate(40);
  String portName = "COM4";
  myPort = new Serial(this, portName, 9600);
}

void draw() {
  int val;
  while (myPort.available() > 0) {
    val = myPort.read();
    print((char) val);
  }
}

```

Output:

```auto
IP: 192.168.1.135, Temp: = 20 *C, Humid: = 74 %, Temps: = 20, 20
IP: 192.168.1.135, Temp: = 20 *C, Humid: = 74 %, Temps: = 20, 20
IP: 192.168.1.135, Temp: = 20 *C, Humid: = 74 %, Temps: = 20, 20

```

---

<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: [November 7, 2022, 11:47pm UTC](https://discourse.processing.org/t/serial-connection-between-processing-and-arduino-doesn-t-work/39557/7 "2022-11-07T23:47:29Z")

</div>

This version of your sketch works. Note the four //!

```auto
import processing.serial.*;

Serial myPort;
String val;

void setup()
{
  frameRate(40); //!
  String arduinoPort = "COM4";
  myPort = new Serial(this, arduinoPort, 9600);
  myPort.bufferUntil('\n'); //!
}
void serialEvent(Serial myPort) {
  val = myPort.readString(); //!
  print(val);
}
void draw(){} //!

```

---

<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: [November 8, 2022, 12:45pm UTC](https://discourse.processing.org/t/serial-connection-between-processing-and-arduino-doesn-t-work/39557/8 "2022-11-08T12:45:59Z")

</div>

Hello @RichardDL,

> [@RichardDL](#):
>
> `frameRate(40); //!`

Why this `frameRate(40)` ?

Just curious…

In this example it will even work with `noLoop().`

Reference:  
[https://processing.org/reference/libraries/serial/Serial\_serialEvent\_.html](https://processing.org/reference/libraries/serial/Serial_serialEvent_.html)

`:)`

---

<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: [November 8, 2022, 12:56pm UTC](https://discourse.processing.org/t/serial-connection-between-processing-and-arduino-doesn-t-work/39557/9 "2022-11-08T12:56:53Z")

</div>

I think I’ve seen somewhere that you can’t repeatedly do serial.available() at infinite speed. In any case not good do something like that too fast. Have to give the serial hardware and software time to do its own thing. 40 gives 25 mS/cycle. That’s probably faster than most users/applications care about and gives time for 25 chars to arrive at 9600 baud.

When I added the loop(), it started working, frameRate(40) was better. After I added the .bufferUntil I didn’t go back and try without the first two fixes.
