# Arduino filling the serial Buffer with nulls

**URL:** <https://discourse.processing.org/t/arduino-filling-the-serial-buffer-with-nulls/38674>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [September 7, 2022, 7:39pm UTC](https://discourse.processing.org/t/arduino-filling-the-serial-buffer-with-nulls/38674 "2022-09-07T19:39:23Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![maro](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@maro](https://discourse.processing.org/u/maro)\
**Post date:** [September 7, 2022, 7:39pm UTC](https://discourse.processing.org/t/arduino-filling-the-serial-buffer-with-nulls/38674/1 "2022-09-07T19:39:23Z")

</div>

I just wrote some code that sends a String from Arduino to Processing. Processing prints the String and display the number of characters in the buffer…  
After uploading the sketch to the Arduino and starting Processing for the first time, The serial Buffer is intially filled with few things(it turns out that these are nulls; 0 in ASCII). Can anyone explain why this is happening?  
I noticed that if the String is smaller then everything works as expected and the Buffer will not be filled with nulls.

**Arduino Code**

```auto
void setup() {
  Serial.begin(9600);
//delay(1000)
 Serial.println("the quick brown fox jumps over the lazy dog");
 delay(1000);
}

void loop() {
}

```

**Processing Code**

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

void setup() {
  myPort= new Serial(this, "COM3", 9600);
}

void draw() {
  if (myPort.available()>0) {
    println("Buffer:"+myPort.available());
    String string = myPort.readStringUntil('\n');
    if (string!=null) {
      println("String:"+string);
    }
  }
}

```

---

<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:** [September 7, 2022, 9:21pm UTC](https://discourse.processing.org/t/arduino-filling-the-serial-buffer-with-nulls/38674/2 "2022-09-07T21:21:07Z")

</div>

Hi @maro, when I run your sketches I get:

```auto

Buffer:3
Buffer:18
Buffer:34
Buffer:45
String:the quick brown fox jumps over the lazy dog

```

I think what we are seeing is this. The characters are arriving over a period of time. At 9600 baud approximately 1 char/mS. myPort.available() is correctly showing how many have arrived. At the same time readStringUntil is giving you null because it doesn’t find a \n yet.

You can alter the situation for demonstration. Delay before reading, you won’t see the nulls because the \n has arrived. readStringUntil(‘q’) as it arrives sooner. Try a longer string, more null values waiting for the \n.

In most programs I prefer a frameRate(20) e.g., so I know how often I’m reading the port. It’s quite normal to readStringUntil many times, it returns null until what you want has arrived.

---

<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 8, 2022, 6:26am UTC](https://discourse.processing.org/t/arduino-filling-the-serial-buffer-with-nulls/38674/4 "2022-09-08T06:26:33Z")

</div>

Hello @maro,

> [@maro](#):
>
> it turns out that these are nulls; 0 in ASCII

> [@maro](#):
>
> I noticed that if the String is smaller then everything works as expected and the Buffer will not be filled with nulls.

The buffer does not contain nulls.

See the 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.

Try this example to see what is received:  
[https://processing.org/reference/libraries/serial/Serial\_readChar\_.html](https://processing.org/reference/libraries/serial/Serial_readChar_.html)

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

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/8/6/869fb5735dec9835c868a7b77770802cc7fcec10.png)

The _ODOA_ (in hex) at the ends if from this:  
[https://www.arduino.cc/reference/en/language/functions/communication/serial/println/](https://www.arduino.cc/reference/en/language/functions/communication/serial/println/)

Which states:

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

_UPDATE_ after further examination:

~~The buffer does not contain nulls.~~

See updated post below:  
[https://discourse.processing.org/t/arduino-filling-the-serial-buffer-with-nulls/38674/14](https://discourse.processing.org/t/arduino-filling-the-serial-buffer-with-nulls/38674/14)

`:)`

---

<div class="post-metadata">

**Author:** ![maro](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@maro](https://discourse.processing.org/u/maro)\
**Post date:** [September 8, 2022, 8:56am UTC](https://discourse.processing.org/t/arduino-filling-the-serial-buffer-with-nulls/38674/5 "2022-09-08T08:56:59Z")

</div>

I am using an Ardiuno Uno genuine

---

<div class="post-metadata">

**Author:** ![maro](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@maro](https://discourse.processing.org/u/maro)\
**Post date:** [September 8, 2022, 9:15am UTC](https://discourse.processing.org/t/arduino-filling-the-serial-buffer-with-nulls/38674/6 "2022-09-08T09:15:16Z")

</div>

Hello @glv,

here you can see that the buffer contains nulls. It is so confusing because sometimes nulls are throwen sometimes not. I had to upload and run the code couple of times until this case happened.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/1/1/11e52a470ae4a91448a46f090c8441bd83ecde3e.png)

---

<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:** [September 8, 2022, 10:31am UTC](https://discourse.processing.org/t/arduino-filling-the-serial-buffer-with-nulls/38674/7 "2022-09-08T10:31:11Z")

</div>

IMO better to post code (and ouptut) as text not image. That way people e.g. me, can copy it with one click.

---

<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 8, 2022, 10:37am UTC](https://discourse.processing.org/t/arduino-filling-the-serial-buffer-with-nulls/38674/8 "2022-09-08T10:37:48Z")

</div>

Hello @maro,

That visual helps to see the output.

Arduino Uno genuine or clone? We had issues with some clones one year…

I have experienced “garbage” in the buffers in the past:

> [@Problem with the use of several sensors, Arduino / Processing](https://discourse.processing.org/t/problem-with-the-use-of-several-sensors-arduino-processing/18118/5):
>
> Hello, Some code I wrote to explore Arduino transmitting data to Processing. It runs fine at a BAUD rate of 1000000. slight_smile I use an Arduino MEGA 2560 R3. 
> 
> > **Arduino Code**
> >
> > // Serial Incoming Test (Arduino Tx) // v1.0.0 // GLV 2020-02-29 long count; void setup() { //Serial.begin(9600); Serial.begin(1000000); delay(1000); Serial.print('\n'); //same as Serial.println() Serial.print("incoming"); Serial.print('\n'); //same as Serial.println() } void loop() …

I will explore this further when I have time… 12 hr days for me this time of year.

`:)`

---

<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:** [September 8, 2022, 12:05pm UTC](https://discourse.processing.org/t/arduino-filling-the-serial-buffer-with-nulls/38674/9 "2022-09-08T12:05:53Z")

</div>

With your code I don’t get any nulls, just the codes for the text as sent.

```auto
,C: 116,C: 104,C: 101,C: 32,C: 113,C: 117,C:...
      t h e space q u

```

The same on Nano with FTDI chip, Nano or Mega with CH340 chip.

Re-loading the code into the Ard should not make any difference. In Ard sketch, please try that 1 Sec delay after Serial.begin. Do you have more than one Ard? Please try each that you have.

---

<div class="post-metadata">

**Author:** ![maro](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@maro](https://discourse.processing.org/u/maro)\
**Post date:** [September 9, 2022, 7:39am UTC](https://discourse.processing.org/t/arduino-filling-the-serial-buffer-with-nulls/38674/10 "2022-09-09T07:39:50Z")

</div>

Hello @RichardDL  
thanks for trying to help. I also tried with the delay after Serial.begin and got same problem.  
It is wierd that this is only happening to my Arduino.

---

<div class="post-metadata">

**Author:** ![JSGauthier](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jsgauthier/32/9910_2.png) [@JSGauthier](https://discourse.processing.org/u/JSGauthier)\
**Post date:** [September 9, 2022, 4:20pm UTC](https://discourse.processing.org/t/arduino-filling-the-serial-buffer-with-nulls/38674/11 "2022-09-09T16:20:12Z")

</div>

Try using Serial.write() instead of Serial.println() and see of the null are illiminated.

---

<div class="post-metadata">

**Author:** ![maro](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@maro](https://discourse.processing.org/u/maro)\
**Post date:** [September 9, 2022, 4:39pm UTC](https://discourse.processing.org/t/arduino-filling-the-serial-buffer-with-nulls/38674/12 "2022-09-09T16:39:54Z")

</div>

I got also the same problem with Serial.write()

---

<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:** [September 10, 2022, 9:13am UTC](https://discourse.processing.org/t/arduino-filling-the-serial-buffer-with-nulls/38674/13 "2022-09-10T09:13:46Z")

</div>

Try other PCs/Laptops/Mac/Pi.

---

<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 10, 2022, 12:57pm UTC](https://discourse.processing.org/t/arduino-filling-the-serial-buffer-with-nulls/38674/14 "2022-09-10T12:57:43Z")

</div>

Hello @maro,

> **Arduino Code**
>
> ```auto
> void setup() {
> Serial.begin(9600);
> Serial.println();
> Serial.println("0123456789ABCDEF");
> }
> 
> void loop() 
> {
> }
> 
> ```

> **Processing Code**
>
> ```auto
> // Example by Tom Igoe
> 
> import processing.serial.*;
> 
> int lf = 10; // Linefeed in ASCII
> String myString = null;
> Serial myPort; // The serial port
> 
> void setup() {
> // List all the available serial ports
> printArray(Serial.list());
> // Open the port you are using at the rate you want:
> myPort = new Serial(this, Serial.list()[4], 9600);
> myPort.clear();
> // Throw out the first reading, in case we started reading 
> // in the middle of a string from the sender.
> //myString = myPort.readStringUntil(lf); // glv - I commented this
> myString = null;
> }
> 
> void draw() {
> while (myPort.available() > 0) {
> myString = myPort.readStringUntil(lf);
> if (myString != null) {
> //println(myString); // glv - I commented this
>       
> // ******************************************
> // glv - I added this:
> int sl = myString.length();
> println(sl); 
> for(int i = 0; i < sl; i++)
> print(hex(myString.charAt(i), 2));
> println(); 
> // ******************************************  
>     
> }
> }
> }
> 
> ```

Console output directly after:

- Uploading Arduino code to Arduino Uno R3
- Running Processing 4.0.1 sketch the first time

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/3/4/348f768655c9e556574b686d81a04aef75e761ae.png)

The _zeroes_ are not there after an upload followed by a USB disconnect\connect (power off\on) of the board and then running the Processing sketch.

The _zeroes_ are not there on subsequent runs of of the Processing sketch which resets the Arduino.

Related:

> **[Serial Monitor contains garbage after upload](https://forum.arduino.cc/t/serial-monitor-contains-garbage-after-upload/972312)**
>
> This is what I see: This only seems to happen right after an upload, only on a UNO, using IDE 2.0 RC5. But not after a reset or when I unplug and replug the USB. While it means little functionally, it is cosmetically ugly.

> <https://github.com/arduino/arduino-ide/issues/927>
>
> \### Describe the problem
> 
> The first output printed to Serial Monitor after an …upload is some "garbage" characters.
> 
> !\[image\](https://user-images.githubusercontent.com/8572152/159872636-d8e0c920-e628-4c91-9c91-65611fcfcaaa.png)
> 
> \### To reproduce
> 
> \#### Equipment
> 
> \- Arduino board that uses the ATmega16U2 USB chip:
> - Arduino Uno
> - Arduino Uno Mini
> - Arduino Mega
> 
> \#### Steps
> 
> 1. Upload a sketch to your board that prints to \`Serial\`:
> \`\`\`cpp
> void setup() {
> Serial.begin(9600);
> Serial.println("hello world");
> }
> void loop() {}
> \`\`\`
> 1. Open the "\*\*Serial Monitor\*\*" view.
> 
> 🐛 The output starts with some unexpected content:
> 
> \`\`\`text
> □□□□□□□□□□□hello world
> \`\`\`
> 
> \### Expected behavior
> 
> Serial monitor output always reflects the data sent by the board.
> 
> \### Arduino IDE version
> 
> 2.0.0-rc5-snapshot-4de7737
> 
> \### Operating system
> 
> Windows
> 
> \### Operating system version
> 
> 10
> 
> \### Additional context
> 
> The number of \`□\` in the demonstration matches the number of characters that are printed by the sketch.
> 
> \---
> 
> The spurious output does not occur when the output is triggered by resetting the board, so it is specific to the upload operation.
> 
> \---
> 
> The spurious output still occurs even if you add a delay before the print, so it is not a matter of timing from initialization as is sometimes the cause of corrupted serial output.
> 
> \---
> 
> The issue does not occur when using Arduino IDE 1.x
> 
> \---
> 
> I have only been able to reproduce this issue with the Arduino boards that use an ATmega16U2 USB to serial adapter chip.
> 
> I could not reproduce it when using boards with other USB interfaces:
> 
> \- Native USB (Leonardo, Nano 33 IoT)
> \- FTDI FT232R (Nano, Pro Mini)
> \- WCH CH340 (3rd party boards)
> 
> \---
> 
> Originally reported at https://forum.arduino.cc/t/serial-monitor-contains-garbage-after-upload/972312
> 
> \### Issue checklist
> 
> \- \[X\] I searched for previous reports in \[the issue tracker\](https://github.com/arduino/arduino-ide/issues?q=)
> \- \[X\] I verified the problem still occurs when using the latest \[nightly build\](https://github.com/arduino/arduino-ide#nightly-builds)
> \- \[X\] My report contains all necessary details

Related but not exactly ? the same issue…  
Consider opening an issue that is specific to your observations.

`:)`
