231a-ad lab5

From CSclasswiki
Jump to: navigation, search

Lab 5

Designing simple input and output interfaces for the Arduino.

Connecting an LED to Pin 13

Outputting LED to Resistor and GND

Outledlow.gif

Pin 13 is used as the output. A wire runs from pin 13 to the LED on the breadboard, another from the LED to the resistor, and a third from the resistor to ground. The LED on the breadboard blinks in phase with the LED on the Arduino.

+5V to Resistor to LED to Output

This time a wire runs from +5V on the Arduino to the resistor, another from the resistor to the LED, and a third from the LED to pin 13. This time, the breadboard LED blinks out of phase with the Arduino LED.

The current flows when there is a difference between voltage, so in the first wiring setup, when pin 13 was set to HIGH, the current flowed between ground and pin 13, lighting up the LED. Here, there is no voltage difference when pin 13 is HIGH since the other end of the wiring is at +5V. So this time, when pin 13 is LOW, the current flows between pin 13 and +5V, causing the LED to blink out of phase.

Connecting an LED to Pin 11

Pin 11 Makes the Breadboard LED Blink

/*
 * Blink
 *
 * Modified from the basic Arduino example.  
 * Uses pin 11 to control an external LED, rather than 
 * using pin 13 to control the Arduino's LED.
 *
 * http://www.arduino.cc/en/Tutorial/Blink
 */

int ledPin = 11;                // we will connect the LED 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(500);                  // waits for 1/2 of a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(500);                  // waits for 1/2 of a second
}


Make Both LEDs Blink Simultaneously

/*
 * Blink
 *
 * Modified from the basic Arduino example.  
 * Uses pin 11 to control an external LED, as well
 * as pin 13 to control the Arduino's LED.  In this
 * example, pins 11 and 13 blink simultaneously.
 *
 * http://www.arduino.cc/en/Tutorial/Blink
 */

int ledPin = 11;                // we will connect the LED to digital pin 11
int ledPin2 = 13;              // LED connected to digital pin 13

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 breadboard LED on
  digitalWrite(ledPin2, HIGH);  //sets the Arduino LED on
  delay(500);                  // waits for 1/2 of a second
  digitalWrite(ledPin, LOW);    // sets the breadboard LED off
  digitalWrite(ledPin2, LOW);  // sets the Arduino LED off
  delay(500);                  // waits for 1/2 of a second
}

Make Both LEDs Blink in Opposing Phases

/*
 * Blink
 *
 * Modified from the basic Arduino example.  
 * Uses pin 11 to control an external LED, as well
 * as pin 13 to control the Arduino's LED.  In this
 * example, pin 13's LED blinks out of phase with
 * pin 11's.
 *
 * http://www.arduino.cc/en/Tutorial/Blink
 */

int ledPin = 11;                // we will connect the LED to digital pin 11
int ledPin2 = 13;              // LED connected to digital pin 13

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 breadboard LED on
  digitalWrite(ledPin2, LOW);  //sets the Arduino LED off
  delay(500);                  // waits for 1/2 of a second
  digitalWrite(ledPin, LOW);    // sets the breadboard LED off
  digitalWrite(ledPin2, HIGH);  // sets the Arduino LED on
  delay(500);                  // waits for 1/2 of a second
}

Arduino LED blinks at .5Hz, breadboard LED blinks at 1Hz

/*
 * Blink
 *
 * Modified from the basic Arduino example.  
 * Uses pin 11 to control an external LED, as well
 * as pin 13 to control the Arduino's LED.  In this
 * example, pin 13's blinks every 1/2 second and
 * pin 11's LED blinks every second.
 *
 * http://www.arduino.cc/en/Tutorial/Blink
 */

int ledPin = 11;                // we will connect the LED to digital pin 11
int ledPin2 = 13;              // LED connected to digital pin 13

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 breadboard LED on
  digitalWrite(ledPin2, HIGH);  //sets the Arduino LED on
  delay(500);                  // waits for 1/2 of a second

  digitalWrite(ledpin2, LOW); //sets Arduino LED off
  delay(500);                  // waits for 1/2 of a second

  digitalWrite(ledPin, LOW);    // sets the breadboard LED off
  digitalWrite(ledPin2, HIGH);  // sets the Arduino LED on
  delay(500);                  // waits for 1/2 of a second

  digitalWrite(ledPin2, LOW);  //sets the Arduino LED off
  delay(500);                 // waits for 1/2 of a second
}

High Frequency

Make the LED blink as fast as possible

/*
 * Blink
 *
 * Modified from the basic Arduino example.  
 * Uses pin 11 to control an external LED,
 * which is blinking as fast as possible.
 *
 * http://www.arduino.cc/en/Tutorial/Blink
 */

int ledPin = 11;                // we will connect the LED 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 breadboard LED on
  digitalWrite(ledpin2, LOW);  // sets Arduino LED off
                                          // there is no delay, so this happens
                                          // as quickly as the Arduino can manage.
}

We measured the blink rate by wiring the Arduino up to an oscilloscope, which showed the Arduino frequency was 100kHz. Incidently, when the oscilloscope was wired to pin 13 instead of pin 11, the frequency was shown to be 133kHz. Apparently different pins have different frequencies.

Buzzer

In order to protect the buzzer from receiving too much voltage, a resistor must be used. A wire goes from pin 11 to the buzzer, another wire goes from the buzzer to a resistor (about 200 ohms), and third wire goes from the resistor to ground.

Connect a switch to Pin 11

231a-afSwitchPullUp.png
Wire runs from pin 13 (instead of Vcc, which is shown here) to a resistor. From the resistor run two wires: one to pin 11 (Vout) and one to the switch. From the switch, another wire runs to ground. When the switch is pressed, the current runs between ground and pin 13, so pin 11 will read as low and the LED will be turned on.

LED turns on and off with pressing and releasing of switch

/*
 * Button
 * modified from
 * http://www.arduino.cc/en/Tutorial/Button 
 *
 * Turns on and off a light emitting diode(LED) connected to digital  
 * pin 13, when pressing a pushbutton attached to pin 11. 
 *
 */
 
int ledPin = 13;                // LED controlled by digital pin 13
int inputPin = 11;               // digital pin 11 will be set to input
int val = 0;                       // will be used to store input

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare pushbutton as input
}

void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, LOW);  // turn LED OFF
  } else {
    digitalWrite(ledPin, HIGH); // turn LED ON
  }
}

Counter increments everytime switch changes state

/*
* Blink switch with counter
*
* Modification of the basic Arduino example. 
* LED turns on and off with pressing and releasing
* of button. A counter should be incremented and 
* displayed on the screen everytime the button 
* changes state. Unfortunately, it does not seem 
* to be working correctly and nothing is displayed.
*
* 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
int val = 0;                                         // will be used to store input
int lastX = -1;                                     // will be used to check button state 
int count = 0;                                      // counter to be incremented

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

void loop()                                       // run over and over again
{
 val = digitalRead(ledPin2);               // store 1 or 0 in val
if ( val != lastX ) {                           // if val does not equal lastX
    count = count+1;                        // counter is incremented and printed 
    Serial.print(count);
}
lastX = val;                                   // sets lastX to val, val will be changed when
                                                   // the button changes state, so when lastX and
                                                   // are found to not match, we know the button
                                                   // has changed state
}
Switch-photo.jpg
Arduino wired up to a switch and breadboard.