Design a site like this with WordPress.com
Get started

Physical Computing · Week 6

Lab: Intro to Asynchronous Serial Communications

Preparing my breadboard: Connected my Arduino Nano, ADXL335 accelerometer, soldered my sensor board to the pins, and added a push button to power and ground

Initializing communication: Sending the value from one sensor, the first analog sensor (the first axis of the accelerometer) and divide the output to give a maximum value of 1023

Initializing Serial Communication

To get the output into a single byte, mapping the output to a range from 0-255

Getting Output into a Single Byte

Sending data in many formats: Trying the program and viewing the results in my serial monitor

Sending Data in Multiple Formats
Screenshot Image of My Serial Output

Sending the values for all 3 sensors: Formatting multiple serial data with punctuation (to punctuate each set of data uniquely)

Sending Multiple Serial Data with Punctuation
My Serial Output

Running a program that reads the two analog sensors on the board along with a digital switch (printing them out in the format of analog1, analog2, switch)

Sending Data for 2 Analog Sensors with a Switch
My Serial Output

Flow control — call and response (handshaking): Asynchronous serial communication is asynchronous, you can run into a problem when the sender sends faster than the receiver can read, and thus need to implement flow control. Adding handshaking to code above:

Flow Control by Adding to and Slightly Adjusting the Punctuation Code Above

The code worked for me and I was able to see the word ‘hello’ and a string of sensor values at the end of your hellos after typing a character in the output box.

Reflection: Through this lab, I was able to get a basic understanding of serial data, but definitely hope to continue to familiarize myself with it even more through working on future labs and assignments. I feel like overall all the code and serial data output was working for me, but I was slightly confused a little at the beginning of the lab while initializing communication. From my understanding, I was supposed to get a maximum value of 1023 but the value of my serial outputs are roughly in the 200-300 range and far from reaching 1023. After adjusting the code to get the output into a single byte (output range from 0-255), my serial values are roughly around 60-ish and also not reaching close to 255. I’m assuming that this was either due to the values of what my specific accelerometer produces, or because I wasn’t putting that much movement or vibration to my accelerometer. I plan on going back to experiment and test out my accelerometer a bit more to hopefully get a clearer understanding of the how it works and the serial values it produces. Other than that, I was able to move through this lab fairly smoothly and view the various formats of serial data in my Arduino’s serial monitor.

Lab: Serial Input to p5.js

Preparing my breadboard: Connected my Arduino Nano to power and ground, and added a potentiometer to my breadboard

Connecting to computer with p5.js serial port: After programing my Arduino to read the analog input, I installed the p5 serial control app and followed the instructions to connect my Arduino to my p5.js editor

I then downloaded the “p5.serialport.js” file and uploaded it to my p5.js sketch, and also added in the provided line to my index.html file as instructed to.

The p5.js sketch: To know what serial ports are available in the operating system, I ran the provided code and was able to get a list of ports in my console log

List of Ports in Console Log

Serial events: After inserting my port name to the sketch, I also added in the serial events in the setup( ) function, and added in the new functions to respond to the callbacks which I just declared underneath. After modifying the serialEvent( ) function, setting up a canvas, and making my draw( ) function to print the sensor value to the screen, I received an error message and the sensor value being undefined (as shown in the image bellow)

Error Message with Sensor Value Undefined

After troubleshooting the error, I realized that it was due to the fact that I was working in Safari, and was able to resolve the issue after switching to Chrome.

Issue Resolved, Code Working, and Sensor Value Defined

Drawing a graph with the sensor values: Modifying the code and adding in a drawData( ) function, I was able to run my sketch and get an output of my sensor values in the form of a graph. I tried to play around and turned my potentiometer up and down fairly quickly, and bellow is a screenshot of my graph.

Drawing Graph with Sensor Values

Reading serial data as a string: After changing my Arduino program code and editing my serialEvent( ) function with the provided code, I seem to be having some issues as the graph doesn’t show up. However, I didn’t receive an error message in the console log either and thus was slightly confused and will continue to work on troubleshooting the part to my best ability.

Sensor Value Seemed to be Undefined as No Graph is Showing Up

Reflection: As I don’t feel that confident with programing, I feel like I still need to further inspect the provided code to better understand how the serialEvent functions work. I struggled a little bit while trying to get my sensor values working in p5.js at the beginning, but was able to figure out what I was doing wrong or missing in my code to get it to work. However, in the last part of the lab of trying to read my serial data as a string instead, I couldn’t figure out what I was doing wrong as my graph wasn’t showing up after modifying my code. I will definitely try to come back to this part again after I familiarize myself more with woking with serial data.

Lab: Serial Output from p5.js

Preparing my breadboard: Connected my Arduino Nano to power and ground, and added a LED to my breadboard

Programming my microcontroller: Programmed my Arduino to read the analog input with the provided code

Programming Microcontroller in Arduino

Programming p5.js for serial communication: After following the same instructions as the serial input lab to allow access to my port with the serial control app, I then programmed in p5.js with the provided code for setup( ), serial events( ), draw( ), mouseDragged( ), and keyPressed( ) functions

Programming in p5.js

After running my sketch, I was able to get my LED to go brighter or dimmer with both the vertical mouse drag and through pressing on the keys 0 to 9.

Brightness of LED Controlled by Vertical Mouse Drag
Brightness of LED Controlled by Pressing on Keys 0 to 9

Sending ASCII-Encoded Serial Data: Re-programmed my microcontroller code in Arduino

Programming Microcontroller in Arduino

Programming p5.js to control LED: Changed the keyPressed( ) function to read H or L instead of 0 through 9

Changing keyPressed( ) Function to Control with H and L Keys

After running my sketch, I was able to get my LED to turn on as I pressed on the H key and turn off as I pressed on the L key.

LED Turning On and Off Controlled by Pressing on Keys H and L

Processing ASCII-encoded strings with Arduino: Modifying the Arduino sketch with the provided code using Serial.parseInt( ) this time

Processing ASCII-encoded strings with Arduino

I was able to send in ASCII numeric string numbers and see the return values in my serial monitor. I then edited the serial.write( ) command in my p5.js sketch as instructed, and was able to get it to work as well.

Reflection: I personally feel that this serial output lab is slightly more straightforward compared to the input lab as I was able to move through the lab without getting stuck on any parts. I enjoyed learning to connect my Arduino with my computer and to control my LED with my computer keys instead of external buttons like before. I am definitely looking forward to further brainstorming on what I can create and hopefully execute my idea with this newly learned skill in my second project.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s