Serial read() Arduino


I have successfully established a Serial connection from Arduino to Processing using the read()-function (read() / Libraries / This only returns numbers between 0 and 255 though and I was wondering what the best method for receiving higher number would be?

Do I have to use readString() and convert it to an integer?


Map / Examples /

Thanks, but I want to receive let’s say numbers from 0 to 4000 from Arduino and not “map it down” to 0–255…

You didn’t post your code you can send and receive any value you need depending on your code and need

Look at this example


Please consider the following demo and see if you can adapt it to your project. The demo will send random integers from 255 to 1024 from Arduino followed by a line feed. The Processing code will put the incoming data into a string and break it at the line feed. The resulting string is then reconverted to an integer and printed to the console.

Processing Code:

import processing.serial.*;

Serial myPort;
String inStr;

void setup() {
  // Select port for your system
  myPort= new Serial(this, Serial.list()[4], 9600);

void draw() {

void serialEvent( Serial myPort) {
  //put the incoming data into a String -
  //the '\n' is delimiter indicating the end of a complete packet
  inStr = myPort.readStringUntil('\n');
  //make sure our data isn't empty before continuing
  if (inStr != null) {
    //trim whitespace and formatting characters (like carriage return)
    inStr = trim(inStr);
    int val = Integer.valueOf(inStr);
    println("value = ", val);

Arduino Code:

long randNumber;

void setup() {

void loop() {
  // print a random number from 255 to 1024
  randNumber = random(255, 1024);


Yes, I wasn’t very specific sorry. I simply want to send sensor values higher than 255 from Arduino to Processing – I understand now Serial.write() and read() in not the way to go. Thank you for your help!

This is very useful, thank you so much!