Skip to content

Sending data from Arduino

You have already seen how we can send data to the serial port and view it on the serial monitor. In this step, we just need to do the same as before, and send data to the serial port. In the next step, we will set up a Processing sketch which will receive the data and do something with it.

The code below will send data to the serial port. You should have seen all of these commands before except for the one at line 9 which increments (adds 1 to) the counter variable. Copy this code into a new sketch, run it and check the serial output with the monitor.

1
2
3
4
5
6
7
8
9
    void setup() {
      counter = 0;
      Serial.begin(9600);
    }

    void loop() {
      Serial.println(counter++);
      delay(1000);
    }

Receiving data in Processing

In this step, we need to create a Processing sketch which listens for data on the COM port that the Arduino is connected to. You can check this by looking on the Tools menu in the Arduino IDE. The code in the box below will do this and it will print out what it receives in the Processing console (the black area below the code editor).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
    Serial arduinoPort;
    String data;
    String portName;

    void setup() {
      portName = "COM4";
      arduinoPort = new Serial(this, portName, 9600);
    }

    void draw() {
      if ( arduinoPort.available() > 0) {
        data = arduinoPort.readStringUntil('\n');
        if (data != null) {
          println(data);
        }
      }
    }

Here is a line-by-line explanation of the code:

Line Command Description
1 import processing serial.*; Use Processing's serial library
3 Serial arduinoPort; Creates a variable which represents the serial connection. Technically, this is an object of the Serial class. Serial class documentation
4 String data; Declare a variable to hold the incoming data. Why a string? Serial data is usually sent as a stream of characters (bytes). It is possible to use other datatypes, but it takes more work. If you are sure that you will only receive one character at a time, you could use char.
5 String portName; Declare a variable to hold the name of the port where the Arduino is connected. Did you check this in the previous step?
8 portName = "COM4"; Set the name of the port
9 arduinoPort = new Serial(this, portName, 9600); Open the port. this represents the sketch (technically, the process running the sketch). You must be sure to use the same speed as the Arduino sketch (9600 is usual).
12 if ( arduinoPort.available() > 0) { If serial data is waiting...
13 data = arduinoPort.readStringUntil('\n'); Read the data up to the first newline character (\n). The newline character is used to end a string in most character sets.
14 if (data != null) { As long as the data is not null (maybe it was only the newline!)...
15 println(data); ... print it on the Processing console.

Can you see the numbers? Excellent - Arduino is talking to Processing.

If you see an error message that says the port is busy, you probably have the serial monitor open. Close the serial monitor and the error should go away.

If you want to use the data in a condition (if-statement), you can test the string value using the equals() function as shown below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    import processing.serial.*;

    Serial arduinoPort;
    String data;
    String portName;

    void setup() {
      portName = "COM4";
      arduinoPort = new Serial(this, portName, 9600);
    }

    void draw() {
      if ( arduinoPort.available() > 0) {
        data = arduinoPort.readStringUntil('\n');
        if (data != null) {
          data = trim(data);
          if (data.equals("1")) {
            println("Found option 1!");
          }
          println(data);
        }
      }
    }

This last example also demonstrates the use of the trim() function which removes any whitespace characters from the beginning and the end of the string.

You can read about both of these new functions on the Processing reference page.