Lab 2: Digital I/O with Arduino Boards

Wilson Torres
8 min readSep 10, 2021

--

Wilson Torres. Professor Kimiko Ryokai. INFO 262, Fall 2021.

Description

In this project we were able to experiment with the Arduino’s PWM, or pulse with modulation, which allows us to send “analog” outputs to LEDs and have them change their brightness. PWM cycles between high and low voltages at a high speed, and when these are averaged you get a parallel to an analog output.The circuit wise, the LEDs were connected in a similar way to the blinking LED lab, except that the output pins were now PWM pins.

Here Pins 9,10, and 11 are PWM pins

Part 1

The first Part of the lab required the ability to make an LED fade in and out. The code there was example code from arduino. The one thing that I was unsure about was the way the code was changing directions for the dimmings/brightness. However, I noted that they are changing the sign of the fade quantity once it reaches either a high or a low threshold, and that is how the dimming and brightness changes.

Fade Code:

/*
Fade
This example shows how to fade an LED on pin 9 using the analogWrite()
function.
The analogWrite() function uses PWM, so if you want to change the pin you're
using, be sure to use another PWM capable pin. On most Arduino, the PWM pins
are identified with a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.
This example code is in the public domain.https://www.arduino.cc/en/Tutorial/BuiltInExamples/Fade
*/
int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}

Fade Result:

Part 2

In the second part of lab, three different LEDs were connected and the brightness was changed so that they dimmed and brightened at different times. The code was really cool in that it changed which LEDs were being brightened and which were dimming based on different counter values, allowing for a cool effect.

Alternating fade code

/*
* Code for cross-fading 3 LEDs, red, green and blue, or one tri-color LED, using PWM
* The program cross-fades slowly from red to green, green to blue, and blue to red
* The debugging code assumes Arduino 0004, as it uses the new Serial.begin()-style functions
* Clay Shirky <clay.shirky@nyu.edu>
*
* 09/08/2021 - Light modifications. Zeke Medley <zekemedley@berkeley.edu>
*/
// Red LED, connected to digital pin 9
const int redPin = 9;
// Green LED, connected to digital pin 10
const int greenPin = 10;
// Blue LED, connected to digital pin 11
const int bluePin = 11;
// Variables to store the values to send to the pins.
// Initial values are Red full, Green and Blue off.
int redVal = 255;
int greenVal = 1;
int blueVal = 1;
// Tracks how many 'steps' into our fade we are.
unsigned i = 0;
// The number of ms to wait between steps in the fade. Smaller values will result
// In a faster fade.
const int wait = 5;
// Determines if debug information should be printed to the serial monitor.
const bool DEBUG = false;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// If we are debugging open a serial connection.
if (DEBUG) {
Serial.begin(9600);
}
}
void loop() {
i += 1;
if (i < 255) {
redVal -= 1; // Red down
greenVal += 1; // Green up
blueVal = 1; // Blue low
} else if (i < 509) {
redVal = 1; // Red low
greenVal -= 1; // Green down
blueVal += 1; // Blue up
} else if (i < 763) {
redVal += 1; // Red up
greenVal = 1; // Green low
blueVal -= 1; // Blue down
} else {
i = 0;
}
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
if (DEBUG) {
// Print debug information ~10 loops.
// `%` here means 'modulo'.
if (i % 10 == 0)
{
Serial.print(i); // Serial commands in 0004 style
Serial.print("\t"); // Print a tab
Serial.print("R:"); // Indicate that output is red value
Serial.print(redVal); // Print red value
Serial.print("\t"); // Print a tab
Serial.print("G:"); // Repeat for green and blue...
Serial.print(greenVal);
Serial.print("\t");
Serial.print("B:");
Serial.println(blueVal); // println, to end with a new line
}
}
delay(wait); // Pause for 'wait' milliseconds before resuming the loop
}

Alternating fade result:

Part 3

In the last part of the lab, we were able to use serial communication to “talk” with the Arduino and write how much of the color we wanted to specify. This allowed us to fine tune exactly how much of the colors we wanted. There was a lot more C implemented in this code, which was a challenge to understand at first, but was helped by the teaching team.

Serial communication code

/* 
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is "<colorCode><colorVal>", where "colorCode" is
* one of "r","g",or "b" and "colorVal" is a number 0 to 255.
* E.g. "r0" turns the red LED off.
* "g127" turns the green LED to half brightness
* "b64" turns the blue LED to 1/4 brightness
*
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
*/
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 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
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' ) {
colorVal = atoi(serInString+1);
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(colorVal);
Serial.println();
serInString[0] = 0; // indicates we've used this string
if(colorCode == 'r')
analogWrite(redPin, colorVal);
else if(colorCode == 'g')
analogWrite(greenPin, colorVal);
else if(colorCode == 'b')
analogWrite(bluePin, colorVal);
}

delay(100); // wait a bit, for serial data
}
//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++;
}
}

After putting in these inputs:

This is the result

HW

As a take home activity we were asked to create a diffuser to blend the three colors, Red, Green, Blue, to make a variety of different colors and to create a way to make the serial monitor to transform multiple key presses into brightness.

The diffuser that I used was synthetic cotton and a plastic bag. The cotton reminded me of a cloud, and as a small child I always dreamed of catching clouds, and having the plastic bag around it, fulfilled this dream in a way, I had my own little cloud. I also drew clouds as purple in as a kid, so I made a purple cloud.

For the brightness control, I chose the character ‘]’ because it reminded me of a volume control, and in a way, we are controlling the volume of brightness that we see.

I altered the code so that the the string being inputed was counted, and the number of ‘]’ characters determined the color value, which in turn determined the analog value being used.

Brightness volume code:

/* 
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is "<colorCode><colorVal>", where "colorCode" is
* one of "r","g",or "b" and "colorVal" is a number 0 to 255.
* E.g. "r0" turns the red LED off.
* "g127" turns the green LED to half brightness
* "b64" turns the blue LED to 1/4 brightness
*
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
*/
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 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
//to use | as brightness control
char bright = '|';
int counter = 0;
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. 'r]]]]]') :");
}void loop () {
// clear the string
memset(serInString, 0, 100);
//read the serial port and create a string out of what you read
readSerialString(serInString);
for (int i=0; i<sizeof(serInString); i++){
if(serInString[i] == ']'){
counter++;
}
}
//Serial.println(counter);
colorCode = serInString[0];
if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
colorVal = atoi(serInString+1);
colorVal = counter*2;
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
int colorPercentage = (colorVal/100)*100;
Serial.print(colorVal);
Serial.print("%");
Serial.println();
serInString[0] = 0; // indicates we've used this string
if(colorCode == 'r'){
analogWrite(redPin, colorVal);
counter = 0; // resets the coutner
}
else if(colorCode == 'g'){
analogWrite(greenPin, colorVal);
counter = 0;
}
else if(colorCode == 'b'){
analogWrite(bluePin, colorVal);
counter = 0;
}
}
//Serial.println(sizeof(serInString));

delay(100); // wait a bit, for serial data
}
//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++;
}
}

Brightness volume input, percentages are those needed to make a purple color:

Brightness volume output, for a purple cloud:

Components Used

  • Arduino Uno
  • Breadboard
  • Different colored wires
  • LED
  • 220 ohm-resistors
  • Synthetic cotton
  • Plastic bag

--

--

Wilson Torres
0 Followers

Hello! My name is Wilson Torres, I am a thrid year PhD student in the Mechanical Engineering department. I am interested in quantifying human hand fucntion.