ad space

Sending multiple data from Arduino to Processing

Sometimes we need to send multiple data from Arduino or micrcontroller to Processing software. Example of multiple data is data from different sensors such as humidity sensor and soil sensor. Here we illustrate how to send multiple data from Arduino to processing. For sending just data from just one sensor see the tutorial How to send data from Arduino to Processing.


To illustrate how we can send multiple data we will use two potentiometers connected to two analog input A0 and A1. The Arduino will sample the data from A0 and A1 pins and send the data via the USB serial port. Then in the PC using the processing software we use the serial object to receive the data coming from the USB. We will use the two potentiometer reading to move a ball along the x and y axis. The data from first potentiomter connected to the A0 pin of Arduino is used for the x-axis movement and the data from coming from the second potentiomter connected to the A1 pin of Arduino is used for the y-axis movement.

 

Hardware, Schematic and Interfacing

The following picture shows how the two potentiometers are connected to Arduino.

 


The following is the schematic diagram of how to connect the potentiometers to the Arduino A0 and A1 pins.


Software, Programming & Codes

Arduino Programming

The Arduino program below reads the value of the two potentiomter connected to A0 and A1 and sends them serially over the USB to PC. We first we send analog value from A0 which ranges from 0 to 1023. Then we send a comma "," and then send the analog value from A1 also in the range from 0 to 1023. After that we send a new line character. For example 248, 540 \n is send. 248 is from A0 then a comman then 540 from A1 and then a newline character. The new line character is send using println() method of Serial object.


void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
}

void loop() {
  if (Serial.available()>0){
    char input = Serial.read();
    //read potentiometers
    int pot0Val = analogRead(A0);
    Serial.print(pot0Val);
    Serial.print(',');
    delay(1);
    int potVal = analogRead(A1);
    Serial.println(potVal);
    }
}


Processing Programming

The processing program to read the send data from Arduino and plot it on a graph window is below. Here we create and use the Serial object called myport to capture that the data serially. We use the bufferUntil() method to look for new line character and invoke the serialEvent() function. In the serialEvent() function we read in the whole string of the data until the new line character for example, 248, 540. Then using the split() function we split the x and y string data and store in the string array called stringData. These string value data is converted to float array called values. Then we first array value values[0] is the x value from the first potentiometer and the second array value values[1] is the y value from the second potentiometer. map() function is used to get the x and y value in the appropriate range converting the input range 0 to 1023 to 0 to width for x and height for y. The draw() function contains the ellipse() function where we fed in our varying x and y value. This then is causes the movement of the small circle ball in the processing graph window.


import processing.serial.*;

Serial myport;
float x, y;

void setup(){
  size(800,600);
  myport = new Serial(this, "COM3", 9600);
  myport.bufferUntil('\n');
}

void draw(){
  background(255);
  fill(0);
  ellipse(x,y,20,20);
}


void serialEvent(Serial myport) {
  String portData = myport.readString();
  String[] stringData = split(portData, ',');
  float[] values = float(stringData);
  x = map(values[0], 0, 1023, 0, width);
  y = map(values[1], 0, 1023, 0, height);
  myport.write('s');
}

Video Demonstration

In this way we can send and processing multiple data sent from Arduino and  processing. See the following video.


This type of data transfer can be useful for not only sending sensor data but also for virtual reality based games(see Virtual Reality Example using Arduino and MPU-6050 sensor).

Post a Comment

Previous Post Next Post