Physical Computing

[PCOMP] Synchronous Serial Communication (I2C and SPI)

For this week, I decided to do the OLED lab for the I2C lab and the Playing WAV file for the SPI lab.

OLED Screen Display using I2C

Firstly, I put together the circuit wiring with the OLED and the potentiometer. To get the OLED to work, I had to import the Adafruit OLED model library and the GFX library, and then initialize the screen.

Next, I imported a font library and changed the text size using the display.setFont() function in Arduino code. On the picture to the left, you can see that the text for “sensor” is a lot bigger than it was before.

I also tried out Richard Moore’s QR code library by downloading it in the QR code manager. I used the code provided from the lab page to use the string I sent into Serial to generate a QR code, and it created a lovely QR graphic to display on the OLED.

This is what I sent through the Serial Monitor. When I scanned the QR code, it sent me to “hi this is my message” on my browser.

Lab: Playing .WAV Files from an Arduino using I2S and SPI

For the sound lab, I checked out a micro SD Card reader, audio breakout board and an I2S amplifier from the shop. I borrowed a microSD from a friend and loaded my favorite doja cat song onto it as a .wav file. Then, I plugged my hardware onto the breadboard using the following connections:

SD Card Reader

  • Vcc – voltage in. Connects to microcontroller voltage out

  • CS – Chip select. Connects to microcontroller CS (pin D10 on the Nano/Uno)

  • DI – SPI data in. Connects to microcontroller SDO (pin D11 on the Nano/Uno)

  • SCK – SPI clock.. Connects to microcontroller SCLK (pin D13 on the Nano/Uno)

  • DO – SPI data out. Connects to microcontroller SDI (pin D12 on the Nano/Uno)

  • CD – card detect. Not connected in this example

  • GND – ground. Connects to microcontroller ground

Amplifier

  • BCLK connects to A3 of the Nano 33 IoT board

  • LRC connects to A2 of the Nano 33 IoT board

  • DIN connects to D4 (SDA Pin) of the Nano 33 IoT board

  • Vin connects to 3.3V

  • GND connects to ground

  • + connects to the left and right sides of a 3.5mm audio jack

  • – connects to the center pin of a 3.5mm audio jack

Next, I loaded in the code provided from the lab into the microcontroller. When running it, I couldn’t figure out why the SD card wasn’t initializing at first. Then, I realized I forgot to connect the SD card to power and ground.

The picture on the left is the correct wiring! The audio jack plug is the black attachment at the bottom of the breadboard. I tried plugging in my earbuds and couldn’t hear anything… The Serial monitor said the SD card and .wav file were valid and the file was playing, but no sound was coming out, and I couldn’t figure out why. Doja cat and I weren’t meant to be today.

[PCOMP] Final Project Proposal

Final project idea: “Three Little Pigs” Full Book

Assignment requirements

  • Microcontroller to PC (Serial Communication)

  • Physical Interaction Design Principles 

  • Design principles

For my pcomp final project, I’m working with Chris again to refine our midterm project for the Winter show. We got a lot of good feedback during the critique that we are interested in addressing for this improved version. I am also looking forward to work more with soft materials and explore using e-textile sensors, switches, and conductive thread.

This time, we want to make an interactive book with minimum three pages. We are sticking with the OG story of “Three Little Pigs” since we already have a foundation with it, but want to tell the story from the wolf’s point of view. As the reader is flipping through the pages, they're asked to help the wolf achieve his goals. For the serial communication aspect, it can be an interface for picking the genre of how you read the story. For example, the background music and sfx it plays when the person flips through the story depend on the mode people clicks (funny light hearted music for comedy, eerie creepy laughter in distance for horror).

I think it'd be really cool to have more pages, but not all of them need an interaction. Some of them can be isolated simple circuits or not have any pcomp at all (so the readers can still have a fleshed out story, and are encouraged to slowly discover and find the interactive components over time).

Materials

  • Arduino

  • Android phone (to run p5 sketch

  • Sewable LED’s

  • Conductive thread or copper tape

  • Photoresistors

Interactive experience 

  • Depending on the different ways you interact with it, you get different results. 

    • How can you allow for more discovery and curiosity? 

  • Let the user dictate what they can control.

  • Keeping it portable

  • Q for Pedro: What’s the best way to incorporate the serial communication interaction? 

  • Ideas

    • Serial communication to control the mood of the scene? 

      • if DAY = white led lights + sound of rooster

      • if NIGHT = Orange led lights + owl hoot

      • if CALM = motor is off

      • if WINDY = motor comes on + wind gust

    • There could be different placements on the page where you can place a character that completes the circuit to perform different actions (i.e. connects the circuit to the lights/to the motor)

      • if circuit is complete, sound plays or led is on

    • Start with three pages

      • If photoresistor 1 has light, page 1 is open

        • If page 1 is open, page 1 interactions are active

      • If photoresistor 2 has light, page 2 is open

        • If page 2 is open, page 2 interactions are active

      • If photoresistor 3 has light, page 3 is open

        • If page 3 is open, page 3 interactions are active

Feedback from Pedro: 

  • Look at past interactive book projects because there's a lot out there

    • Maybe focus more on the interactive element than the pop up book element

  • Self contained vs. connecting to a computer are conflicting goals

    • We can use HID with phone to trigger interactions in the book instead of computer? 

    • USB-OTG (on the go) allows android phone to show up as a keyboard that can send input into the arduino. Using a phone will both power and give sound to the story.  

    • Use android from ER and run p5 from browser. 

    • Phone can be connected to arduino inside the book (and play different animations depending on the page that's open). 

    • Can also just use the phone to play the sounds

  • Light sensors can be used with holes cut out to tell which page has been flipped.

[PCOMP] Two-way Serial Communication Lab

For this week’s lab, we practiced using two-way serial communication between P5 and Arduino.

Firstly, I set up my analog inputs on the breadboard, plugging in two potentiometers and one button. The breadboard can be found on the left picture below. The arduino code for testing out the analog and digital inputs (separated with punctuation) can be found on the right picture below.

Next, I plugged in a p5 sketch importing p5.webSerial to take the inputs and translate them into the movement of the circle in the sketch. One pot is tied to the circle’s X position, the other is tied to it’s circle’s Y position, and the button makes the circle disappear when pressed. The majority of the interaction code is under serialEvent() and draw().

Next, I adjusted the code so that the Arduino is only reading and sending back the input data when prompted (call-and-response / handshaking). The code isn’t shown below but the program prints “hello\n\r” until it receives a prompt (can be anything, it’s mostly just to tell the program to start), and then print the input data whenever it receives a prompt to do so.

Next, it’s time to implement this interaction on the p5 sketch as well. The main change is to add “serial.print(‘x’);” in initiateSerial()

At first, I made this error where I thought initiateSerial() had to be its own independent function. The code did not work.

Then, I realized that the initiateSerial() was within the openPort() function and that’s the one I needed to modify. This code works!

Next, I explored my own application by adding serial communication to an ICM homework exercise I did in the past. I modified the p5 sketch to respond to the red circle (position on sketch controlled by two pots) instead of mouse interaction. To click a button on the remote, your circle would need to be in the correct position and the button would need to be pressed (instead of mouse click). It took a bit of time adjusting the code, but I was excited to find my fake “mouse” work. The circle was quite jittery in movement so if I were to repeat this exercise in the future, I would add some code to filter out the noise and make the circle movement smoother. I accidentally closed this sketch without saving and screenshotting the code. Lesson learned, always save your work!

[PCOMP] Serial Communication

Firstly, I tried out communicating with the port (receiving analog input) through the terminal. The code was first uploaded through the Arduino. Then, once the code was uploaded, I was able to read it using the “cat” command in the terminal.

ls command in this folder shows the ports in my computer. The “usbmodem” is only visible when the Arduino is connected.

This the data that was being read from the port. I used Control + C to quit out of the port.

Next, I went back into the Arduino IDE and serial monitor to take a look at the code while modifying it to be easy to read. The output on the left is using “Serial.write” which outputs the information in binary code converted all in one line and is notably difficult to read. The information in the right uses “\t” tab and println() new line characters to make the data easier to read.

The output is shown in raw binary value, ASCII-encoded binary value, decimal value, hexadecimal value, and octal value respectively.

Next, I put together three sensors to manage serial communication between two inputs. I used several different Serial print methods to format the input reading in a readable way.

For this one, the serial data is formatted and differentiated using punctuation.

Next, I learned how to send all of the data received into a CSV file, which can later be read and imported into a spreadsheet. This is useful information for the future when I’d need to document different readings, import readings into other applications, etc. The image to the right of this text shows the CSV file opened.

Next I worked on reading analog input as serial input from a potentiometer, particularly into p5.webserial. Firstly, I set up my potentiometer.

Next, I imported p5.webserial into the index.html file and I changed the the p5 sketch code to utilize serial communication.

The indata variable is read in from the sensor and can be interpreted into any which way to be used in the p5 sketch. I can imagine this to be used in a lot of fun ways to create interactive digital artworks and games with a controller.

The console.log() and print() functions change inData into a String of bytes which can then be read… I think? Still in the process of understanding bytes.

The inData is a byte that can then be converted into a number and charted into a visual.

Next, I moved onto the third lab, which involves serial output. I set up a p5 sketch, and LED to communicate with each other.

mouseY is mapped to the brightness (0-255) of the LED.

PhysicalPixel code (found in the Examples > Communication section of Arduino IDE). In here, if you send a ‘H’, the LED will turn on and if you send a ‘L’, the LED will turn off.

The number entered (0-255) will result in the corresponding level of brightness in the LED.

[PCOMP] Transistors and DC Motor Controls

I was doing well in the past two labs, but started to struggle again with this one. Floor resources are low and I wasn’t able to locate some of the materials. I did my best to follow along, but I also feel the concepts behind transistors (NPN, PNP, MOSFET, etc) are quite complex and I haven’t fully absorbed them. I will definitely be asking them during class today.

Adding a potentiometer

Adding a transistor. They ran out of TIP120, so I used the TIP 102 as a placeholder just so I could practicing wiring everything.

It works with a DC motor! The blurry blue is a piece of masking tape spinning.

It does not work with a gear motor… Is it because I have the wrong transistor? I wonder if it’s because the gear motor I borrowed from the shop didn’t have wires attached and I didn’t install them correctly.

I wasn’t able to do the portion with a DC incadescent bulb because I wasn’t able to find one on the floor. I hope to try the last part of the transistor lab once I find one. This is definitely a lab I want to revisit again in the future, especially if I’m going to work with motors.

Next, to start on the second lab, I soldered header pins to my motor driver from my arduino kit. They were a bit globby, I noticed soldering this time felt more challenging than connecting two wires together because I had to be more precise in placement. I accidentally soldered two pins together, so I had to practice desoldering as well.

After soldering the motor driver, I plugged it into my breadboard and started working on the connections. There were a lot of cables and connections for the motor driver.

Attempt 1 with additional DC power supply attached. I used a gear motor with this set up (gear motor not pictured).

Attempt 2 with additional DC power supply attached. I used a regular DC motor with this set up.

This time, the motor wouldn’t respond… thus I was unable to test out the code to control the motor. I feel like the code itself is pretty straightforward, I think I’d need some assistance troubleshooting the circuit building component.

[PCOMP] Midterm Project - Pop up Book (Preliminary Notes)

Title: “Three Little Pigs” Pop-up Book

Description: Based on the childhood story of Three Little Pigs, this project will bring the scene of the wolf blowing down the three little pigs’ house down to life, using light-up and interactive components

Schematic sketch for LED and flex sensor component

Schematic sketch for fan component

List of potential interactions: 

  • Flex sensor

    • When the flex sensor is stretched, book is closed (LED off) 

    • When the flex sensor is loose, book is open (LED on)

    • Backup sensors if this one doesn’t work: Photoresistor, stretch sensor

  • Button to change light mode on LED’s

  • Fan: Analog input → FSR press, Analog output → fan speed in motor

Pseudocode: 

  • If the flex sensor is loose (low reading), then the LED shines white 

  • If the button is pressed the first time, LED shines white

    • If the button is pressed the second time, LED shines red

    • If the button is pressed the third time, LED shines green

    • If the button is pressed the fourth time, LED shines blue

  • The stronger the FSR press, the faster the fan speed.


Timeline:

  • Week 1: 

    • Draw schematic

    • Build working circuit with LED’s and flex sensor

  • Week 2: 

    • Add Fan component

Pop up book paper prototype (the cover is cardboard and the flex sensor is attached on the bac where the spine is).

Working flex sensor wiring with FSR attached. The FSR hasn’t been attached to any computation or output yet.

Working code! Flex sensor method is a success. The flex sensor reading gave output from 150 (unbent) to 450 (bent). I mapped this reading to a 0 to 10 scale just to make it easier to decipher in the code. If the flex sensor gave an output to be greater than 3, then the declared boolean variable “bookOpen” will be true. This boolean “bookOpen” will be useful later on so that all the interactions only happen if bookOpen is true. All of the LED interaction that Chris has been working on this past week will go here. The number 3 was found using flex sensor testing of opening and closing the spine.

In the other parts of the code, I started working on fsr —> servo motor interaction, but it wasn’t working properly. I will continue to investigate why that may be this coming week.

[PCOMP] Speakers and Servos

I started out by setting out my materials and reading the lab. I’m still in the process of re-familiarizing myself with the different terms for sound (Hertz, period, frequency, etc). During class, I’m hoping Pedro will explain the in-depth understanding of why we can’t use analogWrite() to change the frequency and what tone() does differently to do that instead.

I forgot to take a picture of my setup, but my circuit was done according to this plan on the right. Note to self: I want to get better at drawing schematics from memory.

The first thing now that I have my setup connected is to test that the input is reading correctly. I do this by using the analogRead() command. It’s interesting to note that the analogRead() does not need a pinMode() in the setup() function as digital input/output does.

Input is confirmed to be working correctly.

After input is confirmed to be working, I check output next by asking the speaker to play just a regular one note tone.

Hooray! My input and output are both working correctly. Now, I can get into the more complicated stuff by changing up the computation. I followed the code suggested from the labs, but tried to code the for loop on my own, just to refresh my memory on programming in C++.

This code asks to play the melody just once using "pitches.h" as a reference library for all the notes.

After hearing the computation play the melody okay, I started preparing the breadboard to have multiple sensors so that the tones the speaker plays would be dictated by the analog input (using analogRead) instead of just the information stored from the code.

The code here reads whether each of the sensors are being activated, and if so, plays their designated note. I noticed that the speaker wasn't playing the note correctly until I set the threshold to 200 (so there was less noise) and added a delay(). Maybe next time, I will try using a counter against millis() instead to read the sensor every 20 ms instead of stopping the whole program for 20 ms every time the loop runs.

After finishing the speaker lab, I moved onto the servo lab. At first, I tried to connect power to the 5v prong instead of the 3.3v prong because I thought the servo would use more power and was confused why my components weren’t working. My board was powered on, but the analog reading wasn’t working properly. This was resolved as soon as I adjusted back to the 3.3v prong.

This is what the code looks likes when using analogRead() on the potentiometer and mapping it to a range that the servo is able to move (up to 180 degrees). This time, I am using millis() as suggested by the lab's code.

The servo moves! Even better when there’s a shark plushie attached on top so you can move the pot to make the shark look like it’s swimming. As my final part of the lab, I combined both the speaker and servo together into one code and one board. I also included my own song instead, choosing the most iconic line from “Take Me Home, Country Roads” by John Denver. I still wanted to keep the pot in to control the direction the shark faces when dancing at the end of the line. The way that I programmed it was to have the loop() parse through the melody array using a global counter (instead of playing the melody all at once in the setup() function). At the 15th and 16th note of the melody, the shark will move a step every time.

Note: I changed the mapping range from 0-1024 to 500-1024. I noticed that the full range the pot was giving from the analogRead() Serial printing was only within that range.

[PCOMP] Input and Output

This lab, I worked with the Arduino and digital input/output and analog input. We started using the Arduino IDE for the first time, starting with the simplest commands, such as making the LED shine and blink. I used the basic blink code to make it so that when the switch is activated, the LED turns on. It worked for the LED, but not the tilt switch component given during class. Chris and I met up to chat about the wiring outside of class, but we weren’t sure why the tilt switch wasn’t working (pictured below on the left).

Next, I wired up to LED’s so that one LED lights up when the button is pressed, and the other one lights up when the button isn’t pressed. I used the code on the right. I thinking figuring out the code is the part I enjoy most about physical computing.

I then attempted to wire up a potentiometer to the LED using analog input into the Arduino to read and interpret the pot data for the LED. I was surprised to see the LED be able to respond to the subtle changes of the knob. If I twisted the switch towards the left (power side), it became brighter and had lower resistance. If I twisted the switch towards the right (ground side), it became dimmer and had higher resistance.

I soldered wires for the first time. I put off learning how to solder for a while, but it was more fun than I expected. It makes me intrigued to do more metalworking (perhaps in the context of jewelry making) in the future.

I soldered wires to try to get my speaker to work. I used the same code as the pot / LED exercise, but changed the terminology from brightness to frequency to avoid any potential confusion. It was cool to hear the sound come out and change with every knob turn. It was a less seamless gradation between the frequencies for the speaker than the gradation for the LED brightness, which was very seamless.

Next, I wired up two FSR’s and set them to send digital output to the LED’s. It made sense that the brightness needed to mapped to a different range (from whatever the input range was and to whatever the output range needed to be. I think currently, it is not too clear to me when I should use digitalRead vs. analogRead and digitalWrite vs. analogWrite.

Next, I attempted to find the sensorValue range for the photoresistor, but the values kept jumping all over the place. I tried a couple methods, first by setting a threshold and second by calculating the average of the readings (with Kaye’s help). However, there seemed to be too much noise and not much of a difference between when I covered the photoresistor with my hand (mimicking “darkness”).

Switching back to the button, everything read smoothly again. This part was pretty fun and I’m already seeing all the possibilities for making toys, keyboards, games using physical switches to map data into art pieces I want to make. It could even be as simple as a cookie clicker. Perhaps I will make a cookie clicker game with a button in the shape of a cookie.

I felt like I was talking to my good friend the computer, and the computer was talking back to me.

The analog input worked well for the FSR as well. I changed up the Serial print text format so that it would be easier for myself to read. Currently, I only vaguely understand how the math works (with threshold, noise, and peakValue). I’m looking forward to understanding it better conceptually tomorrow in class.

[PCOMP] Getting Started

I was excited at the prospect of playing with wires and arduinos at first, but once I started sitting down with my first assignment and looking at the schematics… I was shaken. It’s been a long time since I’ve entered a space that felt so unfamiliar. While sitting in the shop going over my readings, I looked to my classmate next to me with dead eyes. “My background is in painting…” how did I find myself here?

I wish I could paint the resistors instead of connecting them. There’s a small fear inside me where I worry I’ll irrevocably damage the materials, but I know the stakes here are low. We are students. We are brought here to fail. Through failures come successes.

I started out by laying out the items all in front of me to differentiate which was which. I borrowed my dad's multimeter to help get started with the lab at home.

Next, I used the multimeter to measure the continuity and make sure all my tools were working. Very quickly, I realized I was missing some tools and decided to continue when I’m back on the floor for pcomp help from the residents.

I wired up a circuit to connect to the Arduino (which connected to my laptop as a power source). For the longest time, it seemed unclear why my LED lights weren’t working. I asked Kaye to take a look and after some inspection, they found that the top red wire wasn’t connecting the positive to the correct power source node on the Arduino. Once one LED was working, I wired up another LED to sit in series with the other one. I noticed the lights seemed a little dimmer, perhaps because the voltage provided was being split up into two loads instead of one.

Next, I added a normally open push button. It was exciting to see the LED’s flicker based on my interaction. After the one with the first LED worked, I added another LED to sit in parallel.

Next, I added a potentiometer (the three prongs representing input, output, and ground respectively) in place of where I had the resister. The more I turned the knob right, the higher the resistance got and the dimmer the LED became. The more I turned the knob left, the lower the resistance got and the brighter the LED became.

Three push buttons set up in series. All buttons must be pushed for the LED to light.

Three push buttons set up in parallel. As long as one of the buttons are pushed, the LED will light.

After experimenting with the LED lights, I wanted to try out using a DC power source as well (not just the Arduino). It’s very likely I will want to demo a project without leaving my laptop behind, so setting up a project that could be connected to a standard outlet will be good to know. I forgot to take a picture but I disconnected the micro USB cable and added a voltage regulator (IGO), which then connected to wires of the DC power cable adapter. which connected to the DC power cable, which connected to the outlet.

At this point, I got to the DC Motor part of the lab. I attempted to connect the component, but somehow it failed to spin. The LED light is still shining when I press the button so I know that the circuit is properly closed. It seems that something about the DC motor is not connected properly. It will be good to get an answer during my class tomorrow. Over time, I hope to be more proficient at using the multimeter and reading/writing schematics.