Lab 2 — Digital I/O with Arduino Boards

Dimitri Knight
5 min readOct 23, 2023

Dimitri Knight. Professor Kimiko Ryokai. Tangible User Interfaces, Fall 2023

Description

In this lab, I got the opportunity to experiment with some of the Arduino’s cool features. Our main focus was on Pulse Width Modulation (PWM), learning how to make digital signals act like they’re analog. We also played around with Serial Communication, which lets us talk to the Arduino board using a laptop, making it easier to control what we want it to do. Rather than just making a single light blink on and off; this time, I managed to control multiple lights and even change their brightness.

I made a diffuser for our RGB (red, green, blue) lights using simple stuff like low-density polyethylene wrap inside a jar. This helps spread the light out more evenly. Then, I tweaked the code so you can control the color and brightness of the lights with a bunch of keypresses. For example, if you press the ‘r’ key, the red light brightens. Additionally, I added some more ways to change the light colors using keyboard inputs. Say you type in the word “orange” — the system would automatically adjust the red, green, and blue lights to give you that specific color.

Components Used

  • 1 x Arduino
  • 1 x Breadboard
  • 3 x LEDs
  • 4 x Wires
  • 3 x Resistors (220Ω)
  • Low-density polyethylene wrap
  • Plastic cup

Changing Brightness

This program lets you control a colorful LED light using simple keyboard commands. If you type the letter ‘r’, the light will gradually turn more red each time you press it. The same goes for ‘g’ and ‘b’, which make the light turn greener and bluer, respectively. You can mix these basic colors to create your own custom shades. The program keeps track of how many times you’ve pressed each letter, so the more times you press, the brighter that particular color will get. The light starts off almost off, with just a tiny bit of each color.

char serInString[100];  // Serial input string
char colorCode; // Color code ('r', 'g', or 'b')
int colorVal; // Color value (0-255)

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

int redCounter = 0; // Counter for 'r' keypresses
int greenCounter = 0; // Counter for 'g' keypresses
int blueCounter = 0; // Counter for 'b' keypresses

void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);

// Initialize
analogWrite(redPin, 1);
analogWrite(greenPin, 1);
analogWrite(bluePin, 1);

Serial.println("Enter color command (e.g., 'r' for red):");
}

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') {
if (colorCode == 'r') {
redCounter += 5; // Increase by 5 for each 'r' keypress
redCounter = min(redCounter, 100); // Cap at 100
colorVal = map(redCounter, 0, 100, 0, 255); // Map to 0-255 range
analogWrite(redPin, colorVal);
}
else if (colorCode == 'g') {
greenCounter += 5;
greenCounter = min(greenCounter, 100);
colorVal = map(greenCounter, 0, 100, 0, 255);
analogWrite(greenPin, colorVal);
}
else if (colorCode == 'b') {
blueCounter += 5;
blueCounter = min(blueCounter, 100);
colorVal = map(blueCounter, 0, 100, 0, 255);
analogWrite(bluePin, colorVal);
}

// Output the current setting
Serial.print("Setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(colorVal);
Serial.println();
}

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++;
}
}

Custom Colors

This program allows you to control the color of a light by simply typing commands into your computer. You can either use single letters like ‘r’ for red, ‘g’ for green, and ‘b’ for blue to set the light to a medium brightness of that color, or you can type in full color names like “orange,” “purple,” or “pink” to get those specific shades. Once you type your command and hit Enter, the program instantly changes the light to match your request.

char serInString[100];  // Serial input string
int serInIndx = 0; // index of serInString[]

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);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
// Initialize
analogWrite(redPin, 1);
analogWrite(greenPin, 1);
analogWrite(bluePin, 1);

Serial.println("Enter color command (e.g., 'r' for red or named colors like 'orange'):");
}
void loop() {
// Read the serial port and create a string out of what you read
readSerialString();
delay(100); // Wait a bit for serial data
}
void readSerialString() {
while (Serial.available()) {
char inChar = Serial.read();
if (inChar == '\\n') {
serInString[serInIndx] = '\\0'; // null terminate the string
handleInputString(serInString);
serInIndx = 0; // reset index to start filling string again
} else {
serInString[serInIndx] = inChar;
serInIndx++;
}
}
}
void handleInputString(char *input) {
if (strcmp(input, "orange") == 0) {
setNamedColor("orange");
} else if (strcmp(input, "purple") == 0) {
setNamedColor("purple");
} else if (strcmp(input, "pink") == 0) {
setNamedColor("pink");
} else if (input[0] == 'r' || input[0] == 'g' || input[0] == 'b') {
handleColorCode(input[0]);
}
}
void handleColorCode(char code) {
int colorVal;
if (code == 'r') {
colorVal = 127;
analogWrite(redPin, colorVal);
} else if (code == 'g') {
colorVal = 127;
analogWrite(greenPin, colorVal);
} else if (code == 'b') {
colorVal = 127;
analogWrite(bluePin, colorVal);
}
Serial.print("Setting color ");
Serial.print(code);
Serial.println();
}
void setNamedColor(char *colorName) {
if (strcmp(colorName, "orange") == 0) {
analogWrite(redPin, 179);
analogWrite(greenPin, 127);
analogWrite(bluePin, 0);
} else if (strcmp(colorName, "purple") == 0) {
analogWrite(redPin, 127);
analogWrite(greenPin, 0);
analogWrite(bluePin, 127);
} else if (strcmp(colorName, "pink") == 0) {
analogWrite(redPin, 255);
analogWrite(greenPin, 105);
analogWrite(bluePin, 180);
}
Serial.print("Setting color to ");
Serial.println(colorName);
}

Circuit Design

Arduino Uno and Breadboard Diagram

--

--