Lab 2: Digital I/O with Arduino Boards

J M
4 min readSep 16, 2021

Joanne Ma. Professor Kimiko Ryokai. INFO C262 Fall 2021

Description

The goal of this lab was to apply what we learned about pulsewidth modulation (PWM), an analog input, to “dimming” our LEDs. PWM is all about varying the width of on/off within a cycle. Since microcontrollers like Arduino can only produce either high (5) or low voltage (0), we can “fake” fading and brightness control with the LEDs with PWM. Not all pins on the Arduino can do PWM; only pins 3, 5, 6, 9, 10, and 11 has built in PWM circuits.

Components used

  • 1 Arduino
  • 1 Breadboard
  • 3 LED lights, one of each color (R, G, B)
  • Jumper wires
  • 220 Ω resistors
  • diffuser (rice paper)

Part 1: From Blinking to Fading

Fading One LED Light

I started by fading one LED light, and moved the jumper wire to pin 9 so that the example code could work.

Part 2: Fading Three LED Lights

Next, I used the schematic and the fading code provided by the lab (thanks Zeke!) to fade three LED lights. By coincidence, my extended lab from last week also blinked three LED lights in a row, so setting up the circuit for this was a breeze.

Part 3: Serial Communications

We then had to incorporate serial communications into the LED setup using the lab provided code. To complete the lab, we needed to change the brightness of each LED not by manually adjusting the values in the code, which was confusing at first, but had really nice results.

RBG lights at duty cycle of 127. 127. and 127 (50% brightness)

Homework 1: Designing a diffuser

My original plan was to create a tiny version of the Campanile out of Vietnamese rice paper sheets, as a homage to my neighbors in San Jose that make the most delicious foods out of seemingly simple ingredients. I was excited by the diffusive quality and opacity of completely dry rice paper sheets, and the extremely sticky quality of the sheets once briefly dipped in water (so many states of texture and rigidity!). I also, like many others in this class, thought about using folded paper to construct voluminous diffusers out of a 2D medium. Of course, this week ended up being totally chaotic, so I had to scrap my ambitious plans of actually constructing the diffuser and just show off my very creative diffuser that I made out of necessity, the jumper cable box.

I used the jumper wire box to cover my LEDs in the first lab because of how bright the LEDs were, not realizing we would have to design one the following week. I used the LED setup as my design constraint to try designing a diffuser around the LEDs. However, the box was not great at diffusing the light as something that would open up towards the top. This prompted me to alter my LED formation in the end so that it was cluster instead of in a line.

[add video of box]

Eventually, I did get around to finding a travel sized round bottle cap and playing around with rice paper to make different V shaped diffusers. This is what I came up with, but it didn’t do a good job of blending the colors.

Homework 2: RBG Key Press Code

char serInString[100];  // array that will hold the different bytes of the string. 100=100characters;
// -> you must state how long the array will be else it won't work properly
char colorCode;
int colorVal;

int redPin = 9; // Red LED, connected to digital pin 9
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 11

void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, 127); // set them all to mid brightness
analogWrite(greenPin, 127); // set them all to mid brightness
analogWrite(bluePin, 127); // set them all to mid brightness
Serial.println("enter color command (e.g. 'r43') :");
}

void loop () {
// clear the string
memset(serInString, 0, 100);
//read the serial port and create a string out of what you read
readSerialString(serInString);

colorCode = serInString[0];
if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
colorVal = atoi(serInString+1);
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(colorVal);
Serial.println();
serInString[0] = 0; // indicates we've used this string
if(colorCode == 'r')
analogWrite(redPin, colorVal);
else if(colorCode == 'g')
analogWrite(greenPin, colorVal);
else if(colorCode == 'b')
analogWrite(bluePin, colorVal);
}

delay(100); // wait a bit, for serial data
}

//read a string from the serial and store it in an array
//you must supply the array variable
void readSerialString (char *strArray) {
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
}
}

--

--

J M

Grad student @ UC Berkeley’s School of Information. Interests include social computing, usable security, Being Online™️ and reflective tech.