top of page
  • Writer's pictureJack Chen

pcomp week 6

This week’s lab includes the Intro to Asynchronous Serial lab, Serial Input to P5.js, and Serial output from P5.js.

For the Intro to Asynchronous Serial Lab, I used the GX-521 accelerometer since any accelerometer with three analog outputs will work for the lab, and I already had one in my kit.


The main takeaway for me from this lab in setting up asynchronous communication with an Arduino is the difference between Serial.print (ASCII coded) and Serial.write (raw binary). The formatting of coding the display of multiple sensors or same sensor in different format in the Arduino IDE will be a little tricky to remember but I was able to understand what each segment of the example code did.

When setting up the breadboard for the Serial input to P5.js lab, I couldn’t get a consistent reading with a potentiometer, the wires seem to be working ok so I suspect my potentiometer might be damaged. I used a force sensing resistor instead of a potentiometer for this setup.

input to p5 with FSR

Connection between the Arduino and P5 had to be established with the P5 serial control app opened on the computer, since the p5 web editor cannot access the port from the browser by itself. The serial monitor on the Arduino IDE also had to be closed for the connection to be established since only one app on the computer can access a port at a time. Additionally, the P5.serialport.js needed to be added into the index.html for serial communication to work between p5 and the Arduino. The p5 code for the lab consist of a series of functions from the P5.serialport.js library, the functions of each segment of the code are explained in the lab’s examples. The Serial.event() function with instructions for responding to new data, while the other functions are mostly for console logging information as the sketch runs.

Data received from the communication can be referenced in various functions in P5 to convert the data into something to be shown in the P5 sketch. In this lab’s example a graph was produced based on the input data. Adding a delay(100) in the Arduino sketch affects the frequency of the graph plotting.


The final lab of this week is the Serial output from P5.js lab. In this lab, I setup an output from P5 to control the brightness of an LED on a breadboard. The setup and code for the output from P5 is similar to that of input to P5. The main difference is instead of using “Serial.readline” to access incoming data, “Serial.write” is used to output data. In this lab’s examples, mouseDragged() and keyPressed() functions were used to output data to the Arduino.

In the videos above from first to third:

  1. different numbers on the keyboard produces different levels of brightness on the LED

  2. pressing “H” or “L” makes the LED turn on or off

  3. when mouse is being dragged, the y position of the mouse corresponds to the brightness of the LED.

1 view0 comments

Recent Posts

See All
bottom of page