231a-ag Lab5

From CSclasswiki
Jump to: navigation, search

Lab 5: Introduction to Electricity and Electronics

Connecting an LED to Pin 13

Look at the schematics of the Arduino. Locate Pin 13. Observe that it is already connected to an on-board LED. You are going to attach another LED to the same output, Pin 13.

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

  • Once you have your circuit wired up, power up the Arduino and download the Blink sketch. Does your LED blink? If not, turn it around and reverse its polarity.
    • Yes, my LED does blink.
  • Indicate on your report what is the right way to wire up an LED.
    • GND to resistor, to the LED, to Pin 13.
  • Does it blink in phase or out of phase with the on-board LED?
    • It blinks in phase with the on-board LED.

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

  • Same question: is your LED blinking in phase or out of phase with the on-board LED?
    • The LED is blinking out of phase with the on-board LED.
  • Make sure you explain in your report the difference between the two wiring approaches.
    • 5V to resistor, to the LED, to Pin 13.

Connecting an LED to Pin 13

  • Modify your sketch so that it makes the Pin 11 LED blink.
/*
 * Pin 11 LED Blink
 *
 * Program that makes the Pin 11 LED blink.
 *
 * http://www.arduino.cc/en/Tutorial/Blink
 */

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

void setup()                       
{
  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 your sketch so that it makes both LEDs blink. (In phase.)
/*
 * Pin 11 and Pin 13 LED Blink
 *
 * Program that makes the Pin 11 LED and Pin 13 LED blink.
 *
 * http://www.arduino.cc/en/Tutorial/Blink
 */

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

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

void loop()                        // run over and over again
{
  digitalWrite(ledPin, HIGH);      // sets the LED on
  digitalWrite(ledPin2, HIGH);
  delay(1000);                     // waits for a second
  digitalWrite(ledPin, LOW);       // sets the LED off
  digitalWrite(ledPin2, LOW);
  delay(1000);                     // waits for a second
}
  • Make them blink in phase and out of phase by changing only the software, not the hardware. (Out of phase.)
/*
 * Pin 11 and Pin 13 LED Blink
 *
 * Program that makes the Pin 11 LED and Pin 13 LED blink out of phase.
 *
 * http://www.arduino.cc/en/Tutorial/Blink
 */

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

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

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

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

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

void loop()                     // run over and over again
{
  digitalWrite(ledPin, HIGH);   
  digitalWrite(ledPin2, HIGH); 
  delay(500);       

  digitalWrite(ledPin, LOW);  
  digitalWrite(ledPin2, HIGH);
  delay(500);     

  digitalWrite(ledPin, HIGH);    
  digitalWrite(ledPin2, LOW);
  delay(500);     

  digitalWrite(ledPin, HIGH);   
  digitalWrite(ledPin2, LOW);
  delay(500);     
}

High Frequency

  • How fast is it blinking? How can you find out? How can you measure this frequency precisely? Be imaginative and resourceful... :-)
    • As we set our delay to 30, we were able to calculate the frequency:
      • delay of 1000 = 1 sec
      • delay of 30 = 0.03 sec
      • 0.030 sec x 2 = 0.06 sec
      • frequency: 1/0.06 sec = 16.6 times/second, ie. 16.6 Hz
    • We can also measure this frequency using an oscilloscope. Using the oscilloscope, we determined that the frequency was 16.56 Hz.

Replace the LED and Resistor with a buzzer

  • So you need to protect the buzzer from getting too much voltage... What's the solution?
    • The solution is the use a "red red black" resistor (22 Ohms), or any resistor under 1kOhm.

Connect a switch to Pin 11

  • 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
int val = 0;

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
{
 val = digitalRead(ledPin2);
 if (val==HIGH) {
   digitalWrite(ledPin, LOW);
 } else {
   digitalWrite(ledPin, HIGH);
 }
}
  • Make the sketch increment a counter every time your switch is found to have changed state. Make the Arduino display the counter on the PC screen. Download your sketch.

This code does not work! Amy, Yang and I could not figure out what was wrong with it... it wouldn't print the counter on the screen?

/*
* Blink Switch with Counter
*
* Modification of the basic Arduino example.  Switches the Pin 13 LED on and off with a switch connected
* to Pin 11, and increments a counter every time the switch is found to have changed state.
*
* 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;
int lastX = -1;
int count = 0;

void setup()                                      // run once, when the sketch starts
{
Serial.begin(9600);
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
{
val = digitalRead(ledPin2);
if ( val != lastX ) {
   count = count+1;
   Serial.print(count);
}
lastX = val;
}
  • Is the Arduino keeping the right count? Why or why not?
    • We couldn't get the counter to display, so we're not sure whether the Arduino was keeping the right count...

Light-sensitive Resistors

We didn't have time to complete this part of the lab.

Resistors

from CSC231 Page:

Color 1st band 2nd band 3rd band (multiplier)
Black 0 0 ×100
Brown 1 1 ×101
Red 2 2 ×102
Orange 3 3 ×103
Yellow 4 4 ×104
Green 5 5 ×105
Blue 6 6 ×106
Violet 7 7 ×107
Gray 8 8 ×108
White 9 9 ×109
Gold ×10-1
Silver ×10-2

Source