# Lab 1: Physical Computing

Nathan Khuu | Professor Kimiko Ryokai | INFO C262 Fall 2021

## Description

The goal of this lab was to blink an LED light using Arduino. First, this involved using the proper code to blink an LED and uploading that code to the Arduino. From there, I had to create a circuit, so that it could all work. Based on what we learned in class and the textbook, this circuit needed to be a “closed loop containing a source of electrical energy (a battery) and a load (a light bulb)”. In this case, the electrical energy source was from the Arduino and load was the LED and resistor. I also needed to make sure that I had a flow from a higher to a lower state of energy, which would be represented as a numbered pin on the Arduino (higher state) and GND on the Arduino (lower state / ground).

To accomplish all of this, I first connected a black wire from GND on the Arduino to a spot on the breadboard. In that same row, I made sure to insert shorter leg of the LED, as this leg must go to ground. I then inserted the longer leg of the LED into a different row. In the same row as this longer leg, I connected one leg of the 220Ω resistor. With the other leg of the resistor, it went to a new row along with one side of a red wire that was attached to pin 13 of the Arduino, which completed the circuit as we are back at the Arduino.

For the initial lab, I used the example code to blink the LED. I reduced the delay to 200 seconds so that the LED would blink quicker. I also did further exploration, which is described at the end of this post.

## Code

Following the lab instructions, I used the “Blink” example code below in order to blink the hardware LED:

`// the setup function runs once when you press reset or power the boardvoid setup() {  // initialize digital pin LED_BUILTIN as an output.  pinMode(LED_BUILTIN, OUTPUT);}// the loop function runs over and over again forevervoid loop() {  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)  delay(1000);                       // wait for a second  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW  delay(1000);                       // wait for a second}`

To change the blinking frequency, I modified the code by changing both of the the `delay(1000)` to `delay(200)` , which made the blinking faster due to the shorter delay.

## Images

This circuit is based on this schematic, as linked in the lab instructions.

# Further Exploration

To further explore these concepts, I wanted to see how this would all work with multiple LEDs. This involved changes to both the code and the circuit. One major change to the code is that I needed to specific pin numbers (13, 12, 11) instead of `LED_BUILTIN`. For the circuit, one major change (besides adding in the extra LEDs, resistors, and wires) was plugging GND into the “-” column on the breadboard; this way, all of the LEDs could be connected to ground by being connected to the “-” column.

To give myself some sort of task to work towards, I decided to match the blink delays to a song. Since there were some repeated parts of the song, I also looked into writing functions, so that I could try to modularize the code a bit.

## Code

`void setup() {  pinMode(13, OUTPUT);  pinMode(12, OUTPUT);  pinMode(11, OUTPUT);}void loop() {  for (int i = 0; i <= 2; i++) {      light(13, 1);    }  light(11, 0.875);   lightAll(0.625);   lightAll(0.25);   lightAll(0.25);  lightAll(0.5);  light(11, 0.5);    light(12, 1);   delay(526);for (int i = 0; i <= 2; i++) {      light(13, 1);    }    light(11, 0.5);    lightSequence();}void lightAll(float multiplier) {   float val = multiplier * 263;   for (int i = 11; i <= 13; i++) {     digitalWrite(i, HIGH);   }   delay(val);                       for (int i = 11; i <= 13; i++) {     digitalWrite(i, LOW);   }   delay(val);}void light(int num, float multiplier) {  float val = multiplier * 263;  digitalWrite(num, HIGH);    delay(val);                      digitalWrite(num, LOW);  delay(val);  }void lightSequence() {  digitalWrite(12, HIGH);  delay(263);  switchLights(12, 13);  switchLights(13, 12);  switchLights(12, 11);  switchLights(11, 12);  switchLights(12, 13);  switchLights(13, 12);  switchLights(12, 11);  digitalWrite(11, LOW);  delay(263);}void switchLights(int off, int on) {  digitalWrite(off, LOW);  digitalWrite(on, HIGH);  delay(263);}`

## Video

Turn on your volume to hear the accompanying music!

If I were to go beyond this, I think it would great to learn how to automate the blinking lights to match the beat of the song, rather than me figuring it out manually.

I’m a second year master’s student at UC Berkeley School of Information. My interests are in user research, data visualization, and developer experience.

## More from Nathan K

I’m a second year master’s student at UC Berkeley School of Information. My interests are in user research, data visualization, and developer experience.