231a-af Lab 5 Report

From CSclasswiki
Jump to: navigation, search

Lab 5 Report

This lab is designed to make you interface simple input and output devices to the Arduino.

Connecting an LED to Pin 13

In option 1, current flows in the direction of power to the LED, while current flows in the opposite direction in option 2. Therefore, when voltage is turned on in option 1, the LED turns on, though the LED turns off in option 2.

Option 1: Output to LED, to resistor, and to GND

  • Create your circuit using the following diagram:

Outledlow.gif

  • Power up the Arduino and download the Blink sketch.
    • The LEDs blink in phase!

231a-afLED13.1.jpg

Option 2: +5V to resistor to LED to output

  • Create your circuit using the following diagram:

Outled.gif

  • Power up the Arduino and download the Blink sketch.
    • The LEDs blink out of phase!

231a-afLED13.2.jpg

Connecting an LED to Pin 11

  • Now connect your LED to Pin 11.

231a-afLED11.jpg

  • Modify the blink sketch to make the Pin 11 LED blink.
/*
 * Blink 11
 *
 * Modification of the original blink example.  Blinks the Pin 11 LED at 1Hz.
 *
 * http://www.arduino.cc/en/Tutorial/Blink
 */

int ledPin = 11;                   // LED connected to digital pin 11

void setup()                       // run once, when the sketch starts
{
  pinMode(ledPin, OUTPUT);         // sets the digital pin as output
}

void loop()                        // run over and over again
{
  digitalWrite(ledPin, HIGH);      // sets the LED on
  delay(1000);                     // waits for a second
  digitalWrite(ledPin, LOW);       // sets the LED off
  delay(1000);                     // waits for a second
}
  • Modify the blink sketch to make the Pin 11 LED and the on-board Pin 13 LED blink in phase.
/*
 * Blink in phase
 *
 * Modification of the original blink example.  Blinks the Pin 11 LED and the on-board Pin 13 LED in phase at 1 Hz.
 *
 * http://www.arduino.cc/en/Tutorial/Blink
 */

int ledPin = 13;                   // LED connected to digital pin 13
int ledPin2 = 11;                  // LED connected to digital pin 11

void setup()                       // run once, when the sketch starts
{
  pinMode(ledPin, OUTPUT);         // sets the digital pin as output
  pinMode(ledPin2, OUTPUT);        // sets the digital pin as output
}

void loop()                        // run over and over again
{
  digitalWrite(ledPin, HIGH);      // sets the Pin 13 LED on
  digitalWrite(ledPin2, HIGH);     // sets the Pin 11 LED on
  delay(1000);                     // waits for a second
  digitalWrite(ledPin, LOW);       // sets the Pin 13 LED off
  digitalWrite(ledPin2, LOW);      // sets the Pin 11 LED off
  delay(1000);                     // waits for a second
}
  • Modify the blink sketch to make the Pin 11 LED and the on-board Pin 13 LED blink out of phase.
/*
 * Blink out of phase
 *
 * Modification of the original blink example.  Blinks the Pin 11 LED and the on-board Pin 13 LED out of phase at 1 Hz.
 *
 * http://www.arduino.cc/en/Tutorial/Blink
 */

int ledPin = 13;                   // LED connected to digital pin 13
int ledPin2 = 11;                  // LED connected to digital pin 11

void setup()                       // run once, when the sketch starts
{
  pinMode(ledPin, OUTPUT);         // sets the digital pin as output
  pinMode(ledPin2, OUTPUT);        // sets the digital pin as output
}

void loop()                        // run over and over again
{
  digitalWrite(ledPin, HIGH);      // sets the Pin 13 LED on
  digitalWrite(ledPin2, LOW);      // sets the Pin 11 LED off
  delay(1000);                     // waits for a second
  digitalWrite(ledPin, LOW);       // sets the Pin 13 LED off
  digitalWrite(ledPin2, HIGH);     // sets the Pin 11 LED on
  delay(1000);                     // waits for a second
}
  • Modify the blink sketch so that the on-board LED blinks at 0.5 Hz and the Pin 11 LED blinks at 1 Hz.
/*
 * Blink syncopated
 *
 * Modification of the original blink example.  Blinks the Pin 13 LED at 0.5 Hz and the Pin 11 LED at 1 Hz.
 *
 * http://www.arduino.cc/en/Tutorial/Blink
 */

int ledPin = 13;                   // LED connected to digital pin 13
int ledPin2 = 11;                  // LED connected to digital pin 11

void setup()                       // run once, when the sketch starts
{
  pinMode(ledPin, OUTPUT);         // sets the digital pin as output
  pinMode(ledPin2, OUTPUT);        // sets the digital pin as input
}

void loop()                        // run over and over again
{
  digitalWrite(ledPin, HIGH);      // sets the Pin 13 LED on
  digitalWrite(ledPin2, HIGH);     // sets the Pin 11 LED on
  delay(500);                      // waits for 0.5 seconds
  digitalWrite(ledPin, LOW);       // sets the Pin 13 LED off
  delay(500);                      // waits for 0.5 seconds

  digitalWrite(ledPin, HIGH);      // sets the Pin 13 LED on
  digitalWrite(ledPin2, LOW);      // sets the Pin 11 LED off
  delay(500);                      // waits for 0.5 seconds
  digitalWrite(ledPin, LOW);       // sets the Pin 13 LED off
  delay(500);                      // waits for 0.5 seconds
}

High Frequency

  • Make your Pin 11 LED blink as fast as possible.
    • Using an oscilloscope, we find that it blinks at about 130 kHz.

Replace the LED and Resistor with a buzzer

  • The buzzer has a maximum voltage of 3V, and therefore should not be subjected to more than 3V of power. However, the outputs of the Arduino can vary between 0 and (close to) 5V. Solution?
    • Use a 100 ohm resistor (brown-black-brown).

231a-afBuzzer.jpg

Connect a Switch to Pin 11

  • Modify your sketch and make Pin 11 an input pin.
  • Now connect a switch (with a resistor) to Pin 11, and create your circuit using the following diagram:

231a-afSwitchPullUp.png

  • Change your sketch so that when the Arduino sees that the switch has been pressed, it switches the LED connected to Pin 13.
/*
 * Blink switch
 *
 * Modification of the basic Arduino example.  Switches the Pin 13 LED on and off with a switch connected to Pin 11.
 *
 * http://www.arduino.cc/en/Tutorial/Blink
 */

int ledPin = 13;                                  // LED connected to digital pin 13
int ledPin2 = 11;                                 // LED connected to digital pin 11

void setup()                                      // run once, when the sketch starts
{
  pinMode(ledPin, OUTPUT);                        // sets the digital pin as output
  pinMode(ledPin2, INPUT);                        // sets the digital pin as input
}

void loop()                                       // run over and over again
{
  digitalWrite(ledPin, digitalRead(ledPin2));     // sets the Pin 13 LED on when the switch is on
}

Navigation

Return to Main Page