How to send data from Arduino to Processing

Plotting analog signals acquired with Arduino is a critical step in the design and implementation of an electronics system or applications such as in AI(Artificial Intelligence). Plotting analog signals allows us to visualize the data we are collecting and identify trends and patterns, which can be used to make informed decisions on the design of algorithms and the behavior of the involved circuit or AI system. Additionally, plotting analog signals collected with Arduino enables rapid prototyping of our system, facilitating the debugging and tweaking process. Furthermore, plotting analog signals provides insight into the effects of different parameters on the behavior of the environment that is being measured or monitored and in AI system. The advantages of plotting analog signals for monitoring of IIoT(Industrial Internet of Things) or AI applications are numerous and have led to the widespread use of Arduino for testing, development and deployment of IIoT(Industrial Internet of Things) systems and in AI systems. 

Processing is a powerful coding language and software environment used for data analysis and visualizing data. It can be used to plot sensor data from various sources, such as temperature sensors, pressure sensors, accelerometers, gyroscopes, gas sensor and more. Processing is especially useful for plotting large datasets that involve many dimensions. Using Processing, these data points can be plotted in a variety of ways, including line graphs, scatter plots, histograms, and contour maps. This allows users to visualize their data in a way that is meaningful and informative. It also allows them to compare different data sets or analyze trends over time. By using Processing to plot sensor data, users can gain valuable insights into the behavior of their systems and make decisions or predictions accordingly

Arduino can be used to acquire signal from various kinds of sensors such as temperature sensors, pressure sensors, ultrasonic sensors, humidity sensor, accelerometers, gyroscopes etc. To visualize the captured sensor data on a graph processing from processing.org can be used. To send data from Arduino to processing we can use the serial communication. The Arduino sends out sensor data using its serial port and in processing we can use the serial library to get the sent data and draw them.

Here we will use a potentiometer but any sensor could be used. A 10KOhm potentiometer is connected to analog input A0 of Arduino. We read in the value from this analog input and send it serially over the USB to the PC where processing is run. In the processing sketch we will read the data from the serial port and draw it. The following schematic drawing illustrates how the 10KOhm potentiometer is connected to Arduino A0 pin and the serial data transfer from Arduino USB to PC USB where the processing program is run.


The following picture shows the Arduino and potentiometer on breadboard.

Programming

Arduino code

The following Arduino code reads in the data from the potentiometer through its ADC pin A0 and sends it serially over the USB to the PC.

//source: https://ee-diary.com

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);
    }
}

In the above Arduino code, the "setup()" function sets up a serial connection with the baud rate of 9600 and prints a value of "0,0". The "loop()" function reads in data from the serial connection and measures the values of two potentiometers (A0 and A1) using the analogRead() function. It then prints those values to the serial connection separated by a comma. The delay(1) call is used to ensure that both readings are accurate. The code continues to loop and read in the values of the potentiometers and send them to the serial connection. The code does not contain any other instructions, so it will continue looping and sending the readings from the potentiometers to the serial connection until it is stopped or interrupted.

Processing code

The following is the processing code which is run on the PC. The serial port COM3 is where the Arduino sends the data. This com port is monitored using the serialEvent() function for any incoming data(0 to 1023). The data received is then mapped to the width of the output screen. We have used the incoming data as the x-axis location of a ball made using the ellipse() function.

//source: https://ee-diary.com
import processing.serial.*;

Serial myport;
float x;

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

void draw(){
  background(255);
  fill(0);
  ellipse(x,height/2,20,20);
}


void serialEvent(Serial myport) {
  String portData = myport.readString();
  float xvalue = float(portData);
  x = map(xvalue, 0, 1023, 0, width);
  myport.write('s');
}
The processing code above sets up a serial connection to the port "COM3" at a baud rate of 9600 which is used to read data from the serial port. In the draw function, a circle is drawn in each frame with its x position determined by the data sent from the serial port. The serialEvent function is called every time new data is received from the port and is used to convert the data into a float and map it to a value between 0 and width of the window. Lastly, the script sends back an 's' character to acknowledge that the data has been received.

Video demonstration

The video below demonstrates how data is sent from an Arduino board to Processing in order to plot an analog signal. The video begins with a setup of the Arduino board and a Processing sketch that includes a library for plotting analog signals. Next, the Arduino board is connected to an input voltage source and a variable resistor that can be adjusted to vary the input voltage. The Arduino board is then configured to read the input voltage signal in analog format and transmit it to the Processing sketch. The Processing sketch is then configured to receive the incoming data and plot it using the included library. In the video, the results are shown by plotting an analog signal with a range of 0-5V on the x-axis and showing the corresponding values for the input voltage on the y-axis. The output of the plot is a real-time visualization of the varying input voltage. The video ends with a demonstration showing how changes to the input voltage are reflected in the plot instantly. Overall, this video demonstration demonstrates an effective method of visualizing analog signals in real-time in Processing by sending data from the Arduino board. This is a useful technique that can be used to monitor and analyze signals in applications such as robotics, automation, and control systems.

Summary

This tutorial demonstrated how to send one potentiometer data from Arduino to processing and plot it. But we can also send multiple data. The following tutorial link shows how to send two potentiometer values from Arduino to processing as x and y value to control movement of a ball on the processing graph. Such can be helpful in Virtual Reality application(see Virtual Reality Example using Arduino and MPU-6050 sensor) and game application development.


Post a Comment

Previous Post Next Post