# Unable to receive serial data from Arduino

**URL:** <https://discourse.processing.org/t/unable-to-receive-serial-data-from-arduino/14100>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [September 21, 2019, 10:43pm UTC](https://discourse.processing.org/t/unable-to-receive-serial-data-from-arduino/14100 "2019-09-21T22:43:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![micoxon](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micoxon/32/6429_2.png) [@micoxon](https://discourse.processing.org/u/micoxon)\
**Post date:** [September 21, 2019, 10:43pm UTC](https://discourse.processing.org/t/unable-to-receive-serial-data-from-arduino/14100/1 "2019-09-21T22:43:11Z")

</div>

Hi fellows,

I’m trying for weeks to transmit data from an Arduino Leonardo to a Processing program via the serial port.

I first took over programs that I used, back in 2011, with an Arduino Duemilanove and Processing 1.5. They don’t work anymore, maybe because I since updated my system from XP to Window 7 (32 bits), or because of changes in the IDE.

I then changed to a Leonardo, with the current version of Processing (3.5.3). I reproduce below one of the programs I used.

The Arduino’s serial monitor show exactly what is expected: values from 0 to 1023 as I turn the potentiometer connected to analog input. On the Processing side, I just get random numbers, independents of the pot position (Arduino serial monitor disabled, yes).

Arduino program::

```auto
//To send on the serial port a value defined by the
//cursor of a potentiometer

#define POT 0//assigns input 0 to the potentiometer
int val = 0; //val is the input variable

void setup() {
  Serial.begin(9600);//open the serial port and
  // set the speed at 9600 bit/sec
}

void loop() {
  val = analogRead(POT);//read the voltage on pin0 and name it val
  Serial.print(val);//transmit val to the serial port
  delay (10); //wait 10mS
}

```

Processing program:

```auto
import processing.serial.*;
Serial port;
int var;
void setup() {

  port = new Serial(this, "COM5", 9600);// com5 is the port currently used
}//by the Arduino

void draw() {
  if (port.available()>0) {
    var=port.read();
    println(var);
    delay(100);
  }
}

```

Thanks for any 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:** [September 22, 2019, 12:50am UTC](https://discourse.processing.org/t/unable-to-receive-serial-data-from-arduino/14100/2 "2019-09-22T00:50:23Z")

</div>

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

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

You are receiving what you are sending:

> val = 255;  
> Serial.print(val);

Will send ASCI characters 50, 53, 53 for 2, 5, 5, and that is what you are seeing.

[https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html](https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html)

Try:

> val = 255;  
> Serial.write(val);

And Processing will receive:

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

Also, you will not be able to send greater than 255 **with this example** since you are sending bytes and the int will be cast to a byte.  
Try the code with the val++ and you will see this.  
I will leave that as an exercise for further exploration.

🙂

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [September 22, 2019, 2:09am UTC](https://discourse.processing.org/t/unable-to-receive-serial-data-from-arduino/14100/3 "2019-09-22T02:09:43Z")

</div>

i would avoid byte things at all,  
stick with readable ASCII you can follow in arduino monitor ( terminal )  
and easy debug in processing.  
ARDUINO

```auto

// https://discourse.processing.org/t/unable-to-receive-serial-data-from-arduino/14100
// kll tested also with a arduino leonardo

//Arduino program::
//To send on the serial port a value defined by the
//cursor of a potentiometer

#define POT 0 //assigns input 0 to the potentiometer
int val = 0; //val is the input variable

void setup() {
  Serial.begin(9600);//open the serial port and
  // set the speed at 9600 bit/sec
}

void loop() {
  val = analogRead(POT);//read the voltage on pin A0 and name it val
// Serial.print(val);//transmit val to the serial port
  Serial.println(val);//transmit val to the serial port
// delay (10); //wait 10mS ( 100 lines per sec ?)
  delay (100); //wait ( 10 lines per sec better start slow )
}

```

PROCESSING 3.5.3

```auto
import processing.serial.*;
Serial myPort;
String data, val="NaN";
int var;

void setup_serial() { // USB arduino..
  printArray(Serial.list());
  String portName = Serial.list()[1]; // adjust you arduino USB port
  myPort = new Serial(this, portName, 9600);
  myPort.clear();
  myPort.bufferUntil('\n');
  println("try connect to "+portName);
}

void serialEvent(Serial p) { // handle serial data
  data = trim(p.readStringUntil('\n'));
  if (data != null) {
    val = data;
    var = int(val);
    println("val "+val+" var "+var); // print every GOOD line and if it is understood 
  }
}

void setup() {
  size(640, 360);
  setup_serial();
}

void draw(){}

```

---

<div class="post-metadata">

**Author:** ![micoxon](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micoxon/32/6429_2.png) [@micoxon](https://discourse.processing.org/u/micoxon)\
**Post date:** [September 22, 2019, 4:43pm UTC](https://discourse.processing.org/t/unable-to-receive-serial-data-from-arduino/14100/4 "2019-09-22T16:43:00Z")

</div>

Yes, now it works! I tried the loop you suggested, then the analogRead, and it prints what is expected. Thanks a lot for your help! I had fiddled a bit with the various ways of sending and receiving data, but probably not in the right combination. Now my question is: how do I deal with the 10 bits value coming from the analog input? How can I receive it at the Processing side and convert it in a integer value?

---

<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 22, 2019, 5:41pm UTC](https://discourse.processing.org/t/unable-to-receive-serial-data-from-arduino/14100/5 "2019-09-22T17:41:31Z")

</div>

I am pleased that you took the path of exploration! 🙂

Try the example provided on the Processing site:

> **[bufferUntil() / Libraries](https://processing.org/reference/libraries/serial/Serial_bufferUntil_.html)**
>
> Sets a specific byte to buffer until before calling serialEvent().

The only thing I did was remove the font since I did not wish to create one.

> // Example by Tom Igoe
> 
> import processing.serial.\*;
> 
> Serial myPort; // The serial port  
> PFont myFont; // The display font  
> String inString; // Input string from serial port  
> int lf = 10; // ASCII linefeed
> 
> void setup() {  
> size(400,200);  
> // You’ll need to make this font with the Create Font Tool  
> //myFont = loadFont(“ArialMS-18.vlw”);  
> //textFont(myFont, 18);  
> // List all the available serial ports:  
> printArray(Serial.list());  
> // I know that the first port in the serial list on my mac  
> // is always my Keyspan adaptor, so I open Serial.list()[0].  
> // Open whatever port is the one you’re using.  
> //myPort = new Serial(this, Serial.list()[0], 9600);  
> myPort = new Serial(this, “COM7”, 9600);  
> myPort.bufferUntil(lf);  
> }
> 
> void draw()  
> {  
> background(0);  
> text("received: " + inString, 10,50);  
> }
> 
> void serialEvent(Serial p)  
> {  
> inString = p.readString();  
> }

On the Arduno side you are going back to sending ASCII characters:

> val++;  
> Serial.println(val);

You are now sending ASCII characters along with a LF (Line Feed) and CR (Carriage Return) and Processing is receiving these into a string; you will have to “trim” and convert the received string to an int if you want to use an int variable.

Consider:

> **[int() / Reference](https://processing.org/reference/intconvert_.html)**
>
> Converts any value of a primitive data type (boolean, byte, char, color, float, int, or long) or String to its integer representation. Whe…

This is a simple example without extra characters in string to “trim”:

> String p = “1023”;  
> print (int ( p));

Also consider:

> **[trim() / Reference](https://processing.org/reference/trim_.html)**
>
> Removes whitespace characters from the beginning and end of a String. In addition to standard whitespace characters such as space, carriage return, and tab, this function also removes the Unicode "nbs…

🙂

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

---

<div class="post-metadata">

**Author:** ![micoxon](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micoxon/32/6429_2.png) [@micoxon](https://discourse.processing.org/u/micoxon)\
**Post date:** [September 22, 2019, 6:44pm UTC](https://discourse.processing.org/t/unable-to-receive-serial-data-from-arduino/14100/6 "2019-09-22T18:44:22Z")

</div>

It works fine too, thank you!
