Lab 2: Digital I/O with Arduino Boards

Terrie Joo
4 min readSep 15, 2022

--

Terrie Joo. Tangible User Interfaces, Fall 2022

Description

During this lab, I learned how to create fading LED lights by combining red, blue, green LED’s together using the same Arduino board as the first lab. By putting the three lights close together, we can create the visual effect changing lights through the use of a diffuser. There were two main things we were looking at: Pulse Width Modulation (PWM) which “fakes” analog behavior using digital signals and serial communication with the laptop allowing for greater design flexibility.

Components Used

  • 1 — Arduino
  • 1 — Breadboard
  • 4 — wires
  • 3 — LED
  • 3 — Resistor (220Ω)
  • 1 — Old oil diffuser

From Blinking to Fading

The first part of the lab was to simply fade one LED light, which was fairly simple as I used the similar circuit set up as the first lab, but instead moved the wire to one of the pins that support PWM (9–11).

Fading Three LED’s

Next, I extended my circuit so that it included 3 LEDs using all of the pins marked PWM using the code given to us in lab.

Serial Communications

The last part of the lab was to use a modified version of the Seriel_RGB_LED sample code and Arduino’s Serial Monitor. The sample code allowed us to input characters to change the brightness of the light, more specifically a colorCode and a number between 0 and 255. I changed the code so that “rr” would set the red LED to 20% brightness and “rrrrrr” would set it to 60%.

Finally, I found an oil diffuser I had a while ago to create an effective color diffuser. At first, I used tissues to experiment but I really love how the final product turned out!

Code

har serInString[100]; // array that will hold the different bytes of the string.
char colorCode;
int colorVal;
int bluePin = 9; // Red LED, connected to digital pin 9
int redPin = 10; // Green LED, connected to digital pin 10
int greenPin = 11; // Blue LED, connected to digital pin 11
int MAX_VAL = 255; // Constant for maximumvoid 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.‘rrr’, or ‘rrrrrr") :”);
}
void loop () {
memset(serInString, 0, 100); // clear the string
readSerialString(serInString); // read the serial port and create a string out of what you read
processString(serInString);
colorCode = serInString[0];
delay(100); // wait a bit, for serial data
}
// read a string from the serial and store it in an array
void readSerialString (char *strArray) {
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
}
}
// process string according to multiple rules
void processString(char *strArray) {
colorCode = strArray[0];
char decidingFactor = strArray[1];
if( colorCode == ‘r’ || colorCode == ‘g’ || colorCode == ‘b’ ) {
if (isDigit(decidingFactor)) {
// set value directly to number between 0 and 255
colorVal = atoi(strArray+1);
} else if (isAlpha(decidingFactor)) {
// set value by percentage
double percent = 1;
for (int i = 1; i < strlen(strArray); i++) {
if(strArray[i] == colorCode) {
percent ++;
}
}
percent = min(percent, 10);
colorVal = MAX_VAL * (percent / 10.0);
} else {
}
colorVal = max(colorVal, 0);
colorVal = min(colorVal, MAX_VAL);
Serial.print(“setting color “);
Serial.print(colorCode);
Serial.print(“ to “);
Serial.print(colorVal);
Serial.println();
if(colorCode == ‘r’)
analogWrite(redPin, colorVal);
else if(colorCode == ‘g’)
analogWrite(greenPin, colorVal);
else if(colorCode == ‘b’)
analogWrite(bluePin, colorVal);
}
}

--

--

Terrie Joo

I’m a third year undergraduate majoring in cognitive science in L&S. I’m interested in HCD and UI/UX design.