Sending data from Arduino to Processing

From Interaction Station Wiki
Revision as of 10:09, 25 November 2022 by Annasa (talk | contribs)
Jump to navigation Jump to search

Sometimes you will want to send your Arduino's sensors (or others) data to Processing to generate a visual output. This is possible by using Arduino and Processing Serial communication. With this function, you can easily trigger visuals accordingly to the data coming from Arduino.

Arduino send from serial port

The first step you will have to take is to initiate Serial communication in your Arduino code. In this example we are sending distance data coming from an Ultrasonic sensor to Processing via serial communication.

 1const int trigPin = 3;
 2const int echoPin = 2;
 3long duration;
 4int distance
 5
 6void setup() {
 7  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
 8  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
 9  Serial.begin(9600); // Starts the serial communication
10}
11
12void loop() {
13  // Clears the trigPin
14  digitalWrite(trigPin, LOW);
15  delayMicroseconds(2);
16  // Sets the trigPin on HIGH state for 10 micro seconds
17  digitalWrite(trigPin, HIGH);
18  delayMicroseconds(10);
19  digitalWrite(trigPin, LOW);
20  // Reads the echoPin, returns the sound wave travel time in microseconds
21  duration = pulseIn(echoPin, HIGH);
22  // Calculating the distance
23  distance = duration * 0.034 / 2;
24  // Prints the distance on the Serial Monitor
25  Serial.print("Distance: ");
26  Serial.println(distance);
27}

If you are using data coming from other sensors or actuators you should just make sure this gets send to Processing via the Serial.print() function.

1void setup(){
2Serial.begin(9600);
3}
4void loop(){
5Serial.print(your data!);
6}

Open the Arduino Serial Monitor to test if the data gets printed. Close it again before testing the Processing code. This was all on the Arduino side.

Processing receive from serial port

Now we are going to receive the previously sent data on Processing.

 1import processing.serial.*;
 2
 3Serial myPort;
 4String val;
 5
 6void setup()
 7{
 8  // I know that the first port in the serial list on my mac
 9  // is Serial.list()[0].
10  // On Windows machines, this generally opens COM1.
11  // Open whatever port is the one you're using.
12  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
13  myPort = new Serial(this, portName, 9600);
14}
15
16void draw()
17{
18  if ( myPort.available() > 0) 
19  {  // If data is available,
20  val = myPort.readStringUntil('\n');         // read it and store it in val
21  } 
22  println(val); //print it out in the console
23}

Now you should be able to see if the data gets printed in the console. If it has it means that you succeeded in receiving data from your Arduino. Now you can use this data to trigger your preferred visuals in Processing. It is also possible to revert this communication and send data from Processing to Arduino. You can do it by following this guidelines.