# I am having problems connecting Processing to an Arduino

**URL:** <https://discourse.processing.org/t/i-am-having-problems-connecting-processing-to-an-arduino/35599>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [March 6, 2022, 7:31pm UTC](https://discourse.processing.org/t/i-am-having-problems-connecting-processing-to-an-arduino/35599 "2022-03-06T19:31:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimh](https://avatars.discourse-cdn.com/v4/letter/j/a4c791/32.png) [@jimh](https://discourse.processing.org/u/jimh)\
**Post date:** [March 6, 2022, 7:31pm UTC](https://discourse.processing.org/t/i-am-having-problems-connecting-processing-to-an-arduino/35599/1 "2022-03-06T19:31:10Z")

</div>

Hello, I need help connecting Processing to my Arduino.

I am new to Processing and Arduino.  
I am running on a Mac and using an Arduino Uno.

I don’t know what I am doing…

I have used the following code and methods that I found online:  
I am trying to read the Arduino serial out with Processing.  
If I get this to work I will then work on writing to the Arduino.

* * *

Arduino code:  
\<void setup()  
{  
//initialize serial communications at a 9600 baud rate  
Serial.begin(9600);  
}

void loop()  
{  
//send ‘Hello, world!’ over the serial port  
Serial.println(“Hello, world!”);  
//wait 100 milliseconds so we don’t drive ourselves crazy  
delay(100);  
}\>

* * *

Processing code:  
\<import processing.serial.\*;

## Serial myPort; // Create object from Serial class String val; // Data received from the serial port void setup() { String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port myPort = new Serial(this, portName, 9600); } void draw() { if ( myPort.available() \> 0) { // If data is available, val = myPort.readStringUntil(‘\n’); // read it and store it in val } println(val); //print it out in the console }\>

When I run the Arduino code I get a series of "Hello Worlrt"s on it’s I/O  
window, as expected.

For the Processing Code I was not sure which serial port to specify.  
I tried 3 different methods to find out.

Method 1:  
I looked at the Arduino IDE, Tools, Port dropdown.  
It listed:  
/dev/cu.BLTH  
/dev/cu.Bluetooth-Incoming\_Port  
/dev/cu.UART  
/dev/cu.usbmodem1412401  
The forth one down is selected.  
The forth one down looks correct, so it’s port3 ?

Method 2:  
I started a new, separate Processing sketch with the following code:

* * *

## Processing code: \< import processing.serial.\*; void setup() { println(Serial.list()); }\>

Running this resulted in a list of the same ports, in the same order, as  
the Arduino IDE Tools Port list.  
… although it printed the list out twice for some reason.

Method 3:  
I tried all the port numbers in order.  
The results are as follows:

Ports 1, 2, 5, 6:  
Produced a vertical string of "null"s in the Processing console screen.

Ports 0, 3, 4, 7 produced the following error:  
RuntimeException: Error opening serial port /dev/cu,BLTH: Port busy  
Each error had the different name for each of the different devices.

I also got a small empty Processing I/O widow when I ran the  
Processing sketch.

The end result is that nothing seems to be connecting.

Well, this was a lot of stuff.  
I am obviously interested in getting this working.  
I am trying to make a sort of Gui interface for a Arduino bench setup.

Does anyone have any ideas?  
Thanks much for any help.

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [March 6, 2022, 8:11pm UTC](https://discourse.processing.org/t/i-am-having-problems-connecting-processing-to-an-arduino/35599/2 "2022-03-06T20:11:08Z")

</div>

The following two sets of code for Arduino and Processing works on my Mac:

**ARDUINO**

```auto
void setup() {
//initialize serial communications at a 9600 baud rate
Serial.begin(9600);
}

void loop() {
//send ‘Hello, world!’ over the serial port
Serial.println("Hello, world!");
//wait 100 milliseconds so we don’t drive ourselves crazy
delay(100);
}

```

**PROCESSING**

```auto
import processing.serial.*;

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

void setup(){
 printArray(Serial.list());
 String portName = Serial.list()[3]; //change the 0 to a 1 or 2 etc. to match your port
 println("portName = " + portName);
 myPort = new Serial(this, portName, 9600);
 println("port = " + myPort);
}

void draw(){
if ( myPort.available() > 0) { // If data is available,
// println("port is available: ");
 val = myPort.readStringUntil('\n'); // read it and store it in val
 println(val);
}

}

```

Make sure that you print the serial.list so that you can see that you selected the correct port. This port should match what Arduino IDE says it is using to send the data.

---

<div class="post-metadata">

**Author:** ![jimh](https://avatars.discourse-cdn.com/v4/letter/j/a4c791/32.png) [@jimh](https://discourse.processing.org/u/jimh)\
**Post date:** [March 6, 2022, 10:50pm UTC](https://discourse.processing.org/t/i-am-having-problems-connecting-processing-to-an-arduino/35599/3 "2022-03-06T22:50:49Z")

</div>

Thanks much for your help!

I changed to your code.  
Now it is sort of working.  
A step in the right direction.

The console now displays "Hello, world"s with randomly interspaced lines with  
“null” on them. The “null” lines occur mostly every “Hello world” but not always.  
The time between "Hello world"s can vary from about 1 sec to 20 sec or more.

Strange.  
Any ideas?

---

<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:** [March 6, 2022, 11:07pm UTC](https://discourse.processing.org/t/i-am-having-problems-connecting-processing-to-an-arduino/35599/4 "2022-03-06T23:07:34Z")

</div>

Hello @jimh,

> [@jimh](#):
>
> lines with  
> “null” on them.

Take a look at this reference:

> **[readStringUntil() / Libraries](https://processing.org/reference/libraries/serial/Serial_readStringUntil_.html)**
>
> Combination of readBytesUntil() and readString(). Returns null if it doesn't find what you're looking for.

`:)`

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [March 6, 2022, 11:24pm UTC](https://discourse.processing.org/t/i-am-having-problems-connecting-processing-to-an-arduino/35599/5 "2022-03-06T23:24:09Z")

</div>

> The “null” lines occur mostly every “Hello world” but not always.

Welcome to serial communication. You would think that neat little packets of data are sequentially sent, but in reality there is a lot of slicing and dicing, and it falls upon us to unscramble the soup. I have also caught it sending invisible characters which are a lot of fun to find and eliminate.

---

<div class="post-metadata">

**Author:** ![jimh](https://avatars.discourse-cdn.com/v4/letter/j/a4c791/32.png) [@jimh](https://discourse.processing.org/u/jimh)\
**Post date:** [March 7, 2022, 3:11am UTC](https://discourse.processing.org/t/i-am-having-problems-connecting-processing-to-an-arduino/35599/6 "2022-03-07T03:11:19Z")

</div>

Yes…  
I am beginning to understand…  
This is going to take some work.  
But it is interesting!  
It is such a coot thing to be able to do…  
Ill get there,  
Thanks for your help Jim
