Lab 2: Digital I/O with Arduino Boards

Tina Teng
3 min readSep 16, 2021

Tina Teng. Professor Kimiko Ryokai. Tangible User Interfaces, Fall 2021

Description

In this lab, we are further exploring digital features of the Arduino Board to blink not only one light, but to control and fade several LEDs at once. We have two main tasks: the first task is to design a good difuser for the RGB LEDs, and the second task is to control the brightness of the RGB LED values with multiple key presses.

For the first task, we need to craft a good diffuser that helps blending the light emitted from the RGB LEDs so, instead of individual RGB colors, we can observe orange and purple by adjusting the brightness of the LEDs.

For the second task, we need to adjust the brightness of the LEDs according to our keyboard input, with ‘r’, ‘g’, ‘b’, each corresponding respectively to red, green, blue. For example, if we press ‘r’ 5 times, we should expect to see the red LED lighting at 50% brightness, with each ‘r’ corresponding to 10%.

Components Used

  • 1 Arduino UNO
  • 1 Breadboard
  • 3 LED (Red, Green, Blue)
  • 3 Resistors (220Ω)
  • Wires
  • Ping Pong Ball (Diffuser)
  • Synthetic Cotton
  • Scotch Tape

Activity

PART 1: Fading 1 LED Light and PART 2: Fading 3 LED Lights

By repeating the steps in lab 1, I replicated the steps to build the other LEDs by following the circuit diagram below.

PART 3: Serial Communications

After uploading the code provided by Zeke, I modified the code so each ‘r’ increases the red LED value by 10%, with ‘b’ and ‘g’ doing the same for blue and green.

char serInString[100];  
char colorCode;
int colorVal;
int redPin = 9;
int greenPin = 10;
int bluePin = 11;
int MAX_VAL = 255; void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, 127);
analogWrite(greenPin, 127);
analogWrite(bluePin, 127);
Serial.println("enter color command (e.g. 'r43', or 'rrr') :");
}
void loop () {

memset(serInString, 0, 100);
readSerialString(serInString);

colorCode = serInString[0];
char ruleFactor = serInString[1];

if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
if(isDigit(ruleFactor)) {
colorVal = atoi(serInString+1);
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(colorVal);
Serial.println();
serInString[0] = 0;
if(colorCode == 'r')
analogWrite(redPin, colorVal);
else if(colorCode == 'g')
analogWrite(greenPin, colorVal);
else if(colorCode == 'b')
analogWrite(bluePin, colorVal);
}
else if(isAlpha(ruleFactor)) {
double percent = strlen(serInString)-1;
colorVal = 255 * ((percent*10)/100);
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(colorVal);
Serial.println();
serInString[0] = 0;
if(colorCode == 'r')
analogWrite(redPin, colorVal);
else if(colorCode == 'g')
analogWrite(greenPin, colorVal);
else if(colorCode == 'b')
analogWrite(bluePin, colorVal);
}
}

delay(100);

}
void readSerialString (char *strArray) {
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
}
}

Creating Diffusers

For my diffuser, I have tried paper, styrofoam, plastic bags. However, the one I found the most effective in enclosing all the LED and allowing each to blend the light evenly so no LED was distinctly visible was stuffing synthetic cotton in a ping pong ball. I also gently taped down the wires to prevent it from getting in the way of the ping pong ball from encompassing the LED lights.

--

--

Tina Teng

Tina is an undergraduate at UC Berkeley, studying business administration and data science. She is interested in HCI and UX research & design.