# \[SOLVED\] Problems sending data from Processing to Arduino through serial port

**URL:** https://discourse.processing.org/t/solved-problems-sending-data-from-processing-to-arduino-through-serial-port/20138
**Category:** Electronics (Arduino, etc.)
**Created:** [April 25, 2020, 2:13pm UTC](https://discourse.processing.org/t/solved-problems-sending-data-from-processing-to-arduino-through-serial-port/20138 "2020-04-25T14:13:10Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![bacum](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@bacum](https://discourse.processing.org/u/bacum)
#### Post date: [April 25, 2020, 2:13pm UTC](https://discourse.processing.org/t/solved-problems-sending-data-from-processing-to-arduino-through-serial-port/20138/1 "2020-04-25T14:13:10Z")

</div>

Hello everybody!

I’m newbie in Processing. I’m writing here hoping that someone will be able to help me… Thanks in advance;)

I’m trying to communicate two XBEE S1 modules between the computer and an Arduino Mega, in AT mode. The configuration of the XBEEs were done, and works well, I just need to program the communication from Processing to Arduino through the Serial Port.

I had found a lot of examples, were the Arduino send data to Processing, but neither in the other direction (From Processing to Arduino).

This are the sketches that I’ve created, but I do no get connect them and no receive any data:

\_PROCESSING\_Tx

```auto
/** Processing sketch */
 
import processing.serial.*;
 
Serial xbee;
 
void setup() {
  size(300, 300);
  println(Serial.list()); // print list of available serial ports
  // you might need to change the Serial.list()[VALUE]
  xbee = new Serial(this, Serial.list()[5], 9600);
}
 
void draw() {
}

void keyPressed(){
    if (key == '0') {
      xbee.write('0');
      println("ENVIO 0");
    } 
    if (key == '1') {
      xbee.write("1");
      println("ENVIO 1");
    } 
    if (key == '2') {
      xbee.write("2");
      println("ENVIO 2");
    }     
    if (key == '3') {
      xbee.write("3");
      println("ENVIO 3");
    } 
}

```

\_ARDUINO\_Rx

```auto
/** Arduino sketch */
 
#include <SoftwareSerial.h>
 
#define xbeeRx 10 // XBee DOUT
#define xbeeTx 11 // XBee DIN
 
SoftwareSerial xbee = SoftwareSerial(xbeeRx, xbeeTx);
char serialData;

void setup() {
  Serial.begin(9600);
  xbee.begin(9600);
  
  pinMode(xbeeRx, INPUT);
  pinMode(xbeeTx, OUTPUT);
  
   serialData = (char)(('0'));
  
 if (xbee.isListening()) 
    Serial.println("El puerto XBEE esta a la escucha!");
}
 
void loop() {
  
  if (xbee.available()) {
      serialData = xbee.read();
      Serial.println("HAS REBUT: ");
      Serial.println(serialData);
      if (serialData == '0') {
          Serial.println("HAS REBUT UN 0");
      }

      if (serialData == '1') {
          xbee.println("HAS REBUT UN 1");
      }
      
      if (serialData == '2') {
          xbee.println("HAS REBUT UN 2");
      }      
      
      if (serialData == '2') {
          xbee.println("HAS REBUT UN 3");
      }      
  }
}

```

Waiting for your proposals, thanks for your time and help!  
Best!

---

<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: [April 26, 2020, 2:05pm UTC](https://discourse.processing.org/t/solved-problems-sending-data-from-processing-to-arduino-through-serial-port/20138/2 "2020-04-26T14:05:45Z")

</div>

Hi @bacum,

I’m confused reading your code. Your processing is talking to the com port on the PC, but the Arduino code is reading “xbee” which is using software serial on 2 pins. The basic Arduino serial uses “Serial” which is built in serial from the PC via usb.

Taking your requirement “communication from Processing to Arduino through the Serial Port” I’ve used your Processing sketch with only 1 tiny change, and a very simplified Arduino sketch.

` xbee = new Serial(this, "COM17", 9600); // (calling it xbee can be confusing, at the moment it's only talking to the Arduino).`

My Arduino sketch started as yours, but removed xbee and software serial. Now, as you press chars on processing sketch, it flashes one of 4 LEDs. I hope you can see this work, then progressively merge in your xbee comms.

```auto

#define dW digitalWrite
 
char serialData;

int ledA = 5;
int ledB = 4;
int ledC = 3;
int ledD = 2;

void setup() 
{
  pinMode(ledA, OUTPUT);
  pinMode(ledB, OUTPUT);
  pinMode(ledC, OUTPUT);
  pinMode(ledD, OUTPUT);
  
  Serial.begin(9600);
  
  // Test the LEDs
  dW(ledA, 1); delay(100); dW(ledA, 0); delay(100);
  dW(ledB, 1); delay(100); dW(ledB, 0); delay(100);
  dW(ledC, 1); delay(100); dW(ledC, 0); delay(100);
  dW(ledD, 1); delay(100); dW(ledD, 0); delay(100);  
}
 
void loop() 
{  
  if (Serial.available()) 
  {
    // Recieve Serial from the PC
    serialData = Serial.read();
    // turn 1 led on...
    if (serialData == '0') {dW(ledA, 1);}
    if (serialData == '1') {dW(ledB, 1);}
    if (serialData == '2') {dW(ledC, 1);}      
    if (serialData == '3') {dW(ledD, 1);}      
  }
  delay(200);
  // turn all leds off.
  dW(ledA, 0);
  dW(ledB, 0);
  dW(ledC, 0);
  dW(ledD, 0);
}

```

---

<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: [April 26, 2020, 3:33pm UTC](https://discourse.processing.org/t/solved-problems-sending-data-from-processing-to-arduino-through-serial-port/20138/3 "2020-04-26T15:33:08Z")

</div>

Hello,

I have done similar with BlueTooth (serial over BT) modules.

The first thing I do is make sure the code works connecting PC directly to Arduino using the USB serial.  
If this serial communication works then adapt the code (just change the serial ports) to work with Blue Tooth serial and in your case XBEE serial.

The challenge is to configure the hardware correctly and then test the serial communications (USB serial first and then BT or XBEE serial).

I have a lot of serial to USB modules I can use with the additional Arduino serial ports (Serial 1, 2, and 3 on my Mega 2560) to communicate with PC (serial USB) as monitors\listeneners; these come in handy. If you don’t have these you can light up LEDs or use LCD displays as a data monitor during initial testing.  
I often use a serial monitor\listener and that can be the Arduino monitor, other terminal software or even another Processing sketch.

I see that you are using software serial which also works.

You can also send data back using the same serial port; I usually do not do this as it can complicate code and troubleshooting.

I am assuming:

Xbee (serial) on PC  
XBee (serial) on Arduino  
USB(serial) on Arduino used to monitor received data.

Try these:  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/73a1be564a11d8c0fb05499d7fac1bf54f64c2b9.png)

This is a “fix” for SimpleWrite example:

> [@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 …

The Arduino code is in the examples in the Processing IDE and here:

> <https://github.com/processing/processing/tree/master/java/libraries/serial/examples>
>
> //github.com/processing/processing/tree/master/java/libraries/serial/examples

This came in handy when I was setting this up:

> **[SerialPassthrough | Arduino Documentation](https://docs.arduino.cc/built-in-examples/communication/SerialPassthrough)**
>
> Demonstrates how to virtually connect Serial and Serial1.

`:)`

---

<div class="post-metadata">

### Author: ![bacum](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@bacum](https://discourse.processing.org/u/bacum)
#### Post date: [April 26, 2020, 9:59pm UTC](https://discourse.processing.org/t/solved-problems-sending-data-from-processing-to-arduino-through-serial-port/20138/4 "2020-04-26T21:59:24Z")

</div>

Thanks @glv and @RichardDL for your proposals.

I had applied your tips and I had gotten some improvements.

If I change Serial.list()[2] for “COM17”, Processing detect an error:

 ![Captura de pantalla 2020-04-26 23.49.11](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/cee3bfd0b3903283f653973abe73d9ce6d0a4d53.png)

I had changed the name my Serial Port, to no confuse anyone ;p

Working the SimpleWrite example, it seems that there is a fast connection (I receive 4 chars (LLLL) at the begin of the connection, but any char more when I move the mouse over/out of the square). If I add a delay(1000); in the setup(), I don’t receive nothing,

 ![Captura de pantalla 2020-04-26 23.57.32](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/5667522c3fe4fbc218e99fc854ad070a248d2fb5.png)

These are my sketches now:

\_PROCESSING\_Tx (SimpleWrite)

```auto
/**
 * Simple Write. 
 * 
 * Check if the mouse is over a rectangle and writes the status to the serial port. 
 * This example works with the Wiring / Arduino program that follows below.
 */

import processing.serial.*;

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

void setup() 
{
  size(200, 200);
  // I know that the first port in the serial list on my mac
  // is always my FTDI adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0];
  myPort = new Serial(this, Serial.list()[2], 9600);
}

void draw() {
  background(255);
  if (mouseOverRect() == true) { // If mouse is over square,
    fill(204); // change color and
    myPort.write('H'); // send an H to indicate mouse is over square
  } 
  else { // If mouse is not over square,
    fill(0); // change color and
    myPort.write('L'); // send an L otherwise
  }
  rect(50, 50, 100, 100); // Draw a square
}

boolean mouseOverRect() { // Test if mouse is over square
  return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
}

```

\_ARDUINO\_Rx

```auto

char serialData;

void setup() 
{
  Serial.begin(9600);
}
 
void loop() 
{  
  if (Serial.available()) 
  {
    // Recieve Serial from the PC
    serialData = Serial.read();

    if (serialData == '0') {Serial.println("HAS REBUT UN 0");}
    if (serialData == '1') {Serial.println("HAS REBUT UN 1");}
    if (serialData == '2') {Serial.println("HAS REBUT UN 2");}      
    if (serialData == '3') {Serial.println("HAS REBUT UN 3");}      
  }
}

```

Thanks for your help! 😉

---

<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: [April 26, 2020, 10:26pm UTC](https://discourse.processing.org/t/solved-problems-sending-data-from-processing-to-arduino-through-serial-port/20138/5 "2020-04-26T22:26:16Z")

</div>

Try the “fix” to SimpleWrite:

> [@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 corrected the link in previous post.

The Arduino code for it is in the examples in the Processing IDE and here:  
[https://github.com/processing/processing/tree/master/java/libraries/serial/examples:](https://github.com/processing/processing/tree/master/java/libraries/serial/examples:)

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

After all that is said and done you need to get a simple working example going and then start layering more complex code.

Some references:  
[https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/all](https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/all)  
[Serial Communication - SparkFun Learn](https://learn.sparkfun.com/tutorials/serial-communication/all) Advanced and only here as a reference

`:)`

---

<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: [April 26, 2020, 10:53pm UTC](https://discourse.processing.org/t/solved-problems-sending-data-from-processing-to-arduino-through-serial-port/20138/6 "2020-04-26T22:53:22Z")

</div>

@bacum. Good that some things are working now. There are still some possible confusions in what you present. Sorry if the confusion is mine.

COM17 is the name of my Arduino’s port. Yours will probably be different. I prefer to state the name not using the name out of the list as in the given example.

In your latest Ard IDE image you are using the Serial monitor (SM) to see the Serial.prints from the Ard sketch. This is good for testing the sketch, but you must close the SM before your Processing sketch can connect to the Ard’s serial port. Only one thing at a time can use the port. Load sketch, SM, Processing. The IDE manages load and SM, but when you start connecting Processing you have to mange the situation. When Processing is connected to Ard you can’t see the Ard’s prints unless you add Processing code to do it. That’s why in my example I used LEDs to show the action.

---

<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: [April 27, 2020, 12:17am UTC](https://discourse.processing.org/t/solved-problems-sending-data-from-processing-to-arduino-through-serial-port/20138/7 "2020-04-27T00:17:29Z")

</div>

Hello,

You may want to start here:  
[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: ![bacum](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@bacum](https://discourse.processing.org/u/bacum)
#### Post date: [April 27, 2020, 5:48am UTC](https://discourse.processing.org/t/solved-problems-sending-data-from-processing-to-arduino-through-serial-port/20138/8 "2020-04-27T05:48:24Z")

</div>

Thanks again for your inputs, guys!😉

> [@RichardDL](#):
>
> In your latest Ard IDE image you are using the Serial monitor (SM) to see the Serial.prints from the Ard sketch. This is good for testing the sketch, but you must close the SM before your Processing sketch can connect to the Ard’s serial port. Only one thing at a time can use the port. Load sketch, SM, Processing. The IDE manages load and SM, but when you start connecting Processing you have to mange the situation. When Processing is connected to Ard you can’t see the Ard’s prints unless you add Processing code to do it. That’s why in my example I used LEDs to show the action.

I was using the SM as you say, I didn’t know that I cannot use it to visualize the data sent from Processing, just now I understand your LEDs example. I will mount it today to try the communication.

On the other hand, I was indicating the serial port wrong in Processing, I think there was the problem.

> [@glv](#):
>
> You may want to start here:  
> [Connecting Arduino to Processing - SparkFun Learn](https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/all)

Great input too, with this I had had to detect my error with the serial port assigned in Processing. The communication Arduino-Processing works well, now I will try in the other direction.

Thanks again, I will tell you my advances later;)

---

<div class="post-metadata">

### Author: ![bacum](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@bacum](https://discourse.processing.org/u/bacum)
#### Post date: [April 27, 2020, 1:50pm UTC](https://discourse.processing.org/t/solved-problems-sending-data-from-processing-to-arduino-through-serial-port/20138/9 "2020-04-27T13:50:09Z")

</div>

hi guys! @glv @RichardDL SOLVED!! 🤩

Thanks a lot for your tips and time!;)) my mistakes were several:

- I was assigning an error port.
- It was very important the order of the actions how indicate me @RichardDL.
- It was definitive the tips of @glv to understand were I was failing. ([https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/all](https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/all))

Now I just need to power the Arduino Mega with a battery, separated of de computer to have solved my communication system. 😉

Thanks a lot to both!! You are masters!

---

<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: [April 27, 2020, 2:48pm UTC](https://discourse.processing.org/t/solved-problems-sending-data-from-processing-to-arduino-through-serial-port/20138/10 "2020-04-27T14:48:28Z")

</div>

Hello,

Make sure you power it correctly:  
[https://store.arduino.cc/usa/mega-2560-r3](https://store.arduino.cc/usa/mega-2560-r3) See the FAQ section

I have seen many of these damaged from incorrect power to device; I have never damaged one.

`:)`

---

<div class="post-metadata">

### Author: ![bacum](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@bacum](https://discourse.processing.org/u/bacum)
#### Post date: [April 28, 2020, 6:22am UTC](https://discourse.processing.org/t/solved-problems-sending-data-from-processing-to-arduino-through-serial-port/20138/11 "2020-04-28T06:22:00Z")

</div>

Thanks @glv, I will take care with that;)
