Schedule‎ > ‎

Lab 03: LEDs and Resistors

Objectives

  • Get you started using the Arduino.
  • Breadboard a circuit using LEDs and resistors.
  • Learn how to change colors of an RGB LED under program control.
  • Understand the relationship between the hardware circuits and the code.

Academic Honesty

Read the Scholastic Honesty Policy and Assignment Integrity policies of the syllabus. Here are some clarifications for this particular assignment:

  • You may get help from others breadboarding this lab.
  • You must breadboard the circuits yourself.
  • You may discuss the code with other students for this lab.
  • You must enter all the code yourself and not copy from another student.

Part 1: Interactive Binary Counter

In this exercise we add user I/O to the Four-LED Binary Counter circuit.

Specifications

  1. Start with the Four-LED Binary Counter circuit (bin4counter.ino) that you did in class.

    If you do not have the circuit then go to the 02B lesson and follow the instructions to complete the Four-LED Binary Counter Exercise.

  2. Open the bin4counter sketch and then save it as a new sketch named binio.
  3. At the end of the setup() function, add the following command to prompt the user:
    cout << "Enter a number 0 to 15: " << endl;
    
  4. Replace the loop() function with the following code:
    void loop() {
    if (Serial.available()) {
    int num = 0;
    cin >> num;

    for (int i = 0; i < numBits; i = i + 1) {
    digitalWrite(i + startPin, LOW);
    if (bitRead(num, i) == 1) {
    digitalWrite(i + startPin, HIGH);
    }
    }
    }
    }
  5. Add a while loop after the cin >> num; statement to get rid of any line-end characters.
  6. Compile, upload and run your code to verify it works correctly.

    When run, your code should prompt you for a number and then display the binary equivalent on the LEDs. If you have problems, ask a classmate or the instructor for help.

  7. Add code to print out the number that the user entered onto the Serial Monitor.  "You entered: "
  8. Show your board working to the teacher at the beginning of the next class and submit your binio.ino file to Canvas.

Part 2: Change RGB LED Colors

In this lab we control the colors displayed by an RGB resistor.

Note that the RGB LED from recommended kit is a common anode type, so uncomment #define COMMON_ANODE (delete the 2 forward slashes // at the beginning of the line). To organize the lab, match the wire colors to LED leads for red, green and blue. Substitute a 560 Ohm resistor for the 270 Ohm resistor specified (if you don't have a 270 Ohm resistor).

Specifications

  1. Complete the lab, RGB LEDs.
  2. Name the code file "rgb.ino" (without the quotes).
  3. Submit your rgb.ino file as part of this assignment.

Notes and Explanations


Lab Self-Test

Be prepared to answer the following questions after completing this lab:
  1. How many leads are there on an RGB LED?
  2. Does the LED from the recommended kit have a common anode or cathode?
  3. RGB is an acronym for R___ G____ B____.
  4. True of false: the function used to set the brightness of each LED is named "analogWrite".
  5. PWM is an acronym for P____ W____ M____.
  6. The colors of the LED are controlled by the ____.

Grading:  Part 1 10 points  (Header 1pt, Compiles 2 pts, Proper Formatting 1 pt,  Works as Specified 6 pts)
                Part 2 10 points (Header 1pt, Compiles 2 pts, Proper Formatting 1 pt, Works as Specified 6 pts)

Submit your files to Canvas

COMPLETED CODE FOR binio.ino:

#include<ArduinoSTL.h>
using namespace std;


int startPin = 7;
int numBits = 4;

void setup() {
  for (int i = 0; i < numBits; i = i + 1) {
    pinMode(i + startPin, OUTPUT);
  }

  Serial.begin(9600);
  cout << "Enter a number 0 to 15: ";
}
void loop() {
  if (Serial.available()) {
    int num = 0;
    cin >> num;
    while (Serial.available())  {
      Serial.read();
    }
    cout << "You entered: " << num << endl;
    for (int i = 0; i < numBits; i = i + 1) {
      digitalWrite(i + startPin, LOW);
      if (bitRead(num, i) == 1)  {
        digitalWrite(i + startPin, HIGH);
      }
    }
  }

}

COMPLETED CODE FOR rgb.ino:
#include <ArduinoSTL.h>
using namespace std;

/*
  Adafruit Arduino - Lesson 3. RGB LED
*/
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
//uncomment this line if using a Common Anode LED
#define COMMON_ANODE
void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}
void loop()
{
  setColor(255, 0, 0); // red
  delay(1000);
  setColor(0, 255, 0); // green
  delay(1000);
  setColor(0, 0, 255); // blue
  delay(1000);
  setColor(255, 255, 0); // yellow
  delay(1000);
  setColor(80, 0, 80); // purple
  delay(1000);
  setColor(0, 255, 255); // aqua
  delay(1000);
  setColor(0x4B,0x0,0x82);// indigo
  delay(1000);
}
void setColor(int red, int green, int blue)
{
#ifdef COMMON_ANODE
  red = 255 - red;
  green = 255 - green;
  blue = 255 - blue;
#endif
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
}