Lab 2: Digital I/O with Arduino Boards

Lauren Yuniskis
4 min readSep 15, 2022

--

Lauren Yuniskis. Chris Myers. Tangible User Interfaces, Fall 2022

Description

The purpose of this lab is to familiarize ourselves with Pulse Width Modulation (PWM) which replicates analog behavior using digital signals. Additionally, we are aiming to enhance our serial communication skills utilizing the laptop which will allow for greater design flexibility.

To put these learning objectives into action, we were tasked with using our RGB LEDs to create a unique diffuser that helps diffuse the light shining from the RGB LEDs. Secondly, we will control the RGB values with multiple key presses to alter features of the LED such as intensity of brightness. The exploration of these features will allow us to fade multiple LEDs at once and even create new colors!

Components Used:

  • Arduino Uno
  • Breadboard
  • 4 Jumper Wires
  • 3 220 Ohm Resistors
  • 3 LEDs (Red, Green, Blue)
  • Dixie Cup

Building off of last week’s assignment to execute this lab, I followed suit of the same set up but now adding additional LEDs. Rather than blinking a single LED, we will transition to fading lights with multiple LEDs colored: red, green, and blue. It was imperative I specifically used pinholes 9,10, and 11 on the Arduino board as they facilitate PWM. I then connected each LED bulb to the Breadboard with the negative ends on the negative side and the positive ends on the positive side. I positioned each LED in the same row as a wire and did so for the resistors. Lastly, I connected the yellow jumper wire to the ground which was in line with the other end of the wire in the same row as the negative end of the LED light. Finally, when uploading the provided code, I was able to create fading LEDs!

Fading LEDs

Task 1: Designing a Diffuser

I experimented with multiple types of diffusers including: crumbled printing paper, tissue paper in the shape of a cave, but finally settled on a Dixie Cup. What appealed to me most was the thinness of the cup as it allows light to easily disperse and you can clearly see the colors change. I also thought it looked like a cool Dixie Cup Ad.

Dixie Cup Diffuser

Task 2: Serial Communications

To practice serial communications, I altered the serial communication code to control the RGB values with multiple key presses. For instance, by pressing ‘g’ (5) times, will set the brightness of the green LED bulb to 50% (aka brightness = 127) and pressing ‘g’ (10) times will set it to 100% (aka brightness = 255). To demonstrate my knowledge of serial communication, I manipulated the code to where every time I press a key that corresponds to that color bulb on the Breadboard, the brightness intensity will increase by 5% (255 x 0.05 = 12.75 initial value). After the code reads what key the user has selected, the next step was to write a function that increases the value of the LED by 10%. As the limit of the color value is at 255, there had to be a function to restart the color value if it went beyond 255. If a user exceeds 255, the LED will reset to a value of 0. See below for video of serial communication.

Code

char serInString[100];  

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
float redVal = 12.75; // set the initial value for each pin. We set it to 5% (out of the 255 range).
float greenVal = 12.75;
float blueVal = 12.75;
// initial setup
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, redVal); // We take the value that we have set at the beginning. The analogWrite function is used to write a value between 0 and 255 inclusive to the LED pin.
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
Serial.println("Each Pin is at 5% Color intensity. Press a key ('r', 'g', or 'b') to increase intensity.");
}
void loop () {
// clear the string
memset(serInString, 0, 100);

readSerialString(serInString);
colorCode = serInString[0];
if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
Serial.print("changing Color intensity of Pin ");
Serial.print(colorCode);
Serial.print(": ");
serInString[0] = 0; // indicates we've used this string
if(colorCode == 'r') {
redVal = setColor(redVal);
analogWrite(redPin, redVal);
} else if(colorCode == 'g') {
greenVal = setColor(greenVal);
analogWrite(greenPin, greenVal);
}
else if(colorCode == 'b') {
blueVal = setColor(blueVal);
analogWrite(bluePin, blueVal);
}
}

delay(100); // wait a bit, for serial data
}
float setColor (float key) { // setColor calculates the color based on the times a key is pressed.
float keyvalue; // the keyvalue is the final value we set our lights to.
if (key == 255) { // if the value is 255 or higher, we reset the light to 0.
Serial.print("Maximum color intensity of 255 / 100% for Pin ");
Serial.print(colorCode);
Serial.print(" ");
Serial.print("is reached. Setting intensity to 0.");
keyvalue = 0;
} else {
keyvalue = key + 25.5; // take the current keyvalue and add 10%
}
Serial.print(keyvalue);
Serial.println();
return keyvalue;
}
//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()) { // Get the number of bytes (characters) available for reading from the serial port.
return;
}
while (Serial.available()) {
strArray[i] = Serial.read(); // put the number from the serial port into strArray
i++;
}
}

--

--

Lauren Yuniskis
0 Followers

I’m a 4th year undergrad student majoring in Media Studies with a minor in Political Economy and completing the Berkeley Certificate in Design Innovation.