Lab 2: Digital I/O with Arduino Boards

Julia Wang
5 min readSep 15, 2021

--

Julia Wang. Professor Kimiko Ryokai. NWMEDIA C262, Fall 2021.

Description

This week, we played around with more dynamic and continuous operations with an Arduino and RGB lights. More specifically, we learned:

  1. Pulse Width Modulation (PWM) which is essentially faking various light intensities through blinking a light at differing rates.
  2. Serial Communication which is like kind of like “texting” the Arduino (for now).

Using these two, we were able fade our lights and explicitly control the intensity of each light using our laptop keyboards. With a light diffuser, we can now mix various colors and intensities to make so many more colors and stories!

It’s interesting because I always thought of color mixing in a very physical and involved way — like using brushes to mix paint — rather than adjusting light intensities. To me, mixing paints is a very lovely process. Something about watching two different colors swirl and dance together to eventually form a new color is nice. Using the lights to mix colors seems slightly less magical (to me!) but I’m sure light technicians would think differently…

Components Used

  • 1 Arduino UNO
  • 1 Breadboard
  • 3 LED bulbs
  • 3 220-ohm resistors
  • Male-to-male wires
  • Diffuser: printer paper, dryer sheets, small plastic sheet, expo marker

1. Learning Fading and Serial Communication

In the first parts of the lab, we used provided instructions and code to learn:

  1. How to blink one light
  2. How to blink all three lights and create fading patterns
  3. How to use serial communication to control light intensities
One light blinking (left), fading patterns (middle), serial communication (right)

2. Diffusing the Light

For the diffuser, I first focused on what material I wanted to use. I tried various materials such as plastic, tissue paper, synthetic cotton, fabric, paper, etc. After a lot of attempts, I found that dryer sheets and paper worked pretty well!

When thinking about the form of the diffuser, I realized that colors are a fairly expressive medium so I thought it would be dope to connect emotions to each color!

The final diffuser is a blown-up paper box stuffed with dryer sheets. To add a face, you can draw a face (or anything really!) on a sheet of plastic and attach it to the front of the box. It was fun to explore how different colors could lead to different interpretations of one facial expression.

Trying out different emotions with various colors!

3. Custom Light Intensity Controls

Finally, I added serial communication so that people could have more control over the colors made (modified from part one). To adjust light intensity, you enter the intensity percentage (0%–100%) instead of the corresponding number values (0–255).

During filming, I realized it was pretty hard to mix colors without seeing them in front of me. I was always thinking “I have no clue how to make [insert color]”. Maybe I could show some GUI to visualize the color mixing process or make the keyboard inputs more fluid?

Some emotions…
What my serial monitor looked like during this process!

Code

/* 
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is "<colorCode><colorPercent>", where "colorCode" is
* one of "r","g",or "b" and "colorPercent" is a number 0 to 100.
* E.g. "r0" turns the red LED off.
* "g50" turns the green LED to half brightness
* "b25" turns the blue LED to 1/4 brightness
*
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
*
* Modified 14 September 2021 - Julia Wang
*/
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 colorPercent;
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' ) {
colorPercent = atoi(serInString+1);
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(colorPercent);
Serial.print("% intensity");
Serial.println();
serInString[0] = 0;
if(colorCode == 'r')
analogWrite(redPin, percentToVal(colorPercent));
else if(colorCode == 'g')
analogWrite(greenPin, percentToVal(colorPercent));
else if(colorCode == 'b')
analogWrite(bluePin, percentToVal(colorPercent));
}

delay(100); // wait a bit, for serial data
}
// translates percentage input (0-100) to color value (0-255)
float percentToVal (int percent) {
return 255 * (float(percent) / 100.0);
}
// 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++;
}
}

--

--

Julia Wang

A 4th year EECS undergraduate! I’m fascinated by creativity and sustainability — both how tech empowers them and how they are taught and understood.