# 231a-ac Lab5

In this Lab, I collaborated with Alex. We have also worked with Amy on the Switch problem.

## 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

## Connecting an LED to Pin 13

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

• We created the circuit based on the diagram below.
• We used Pin 13 as output, and a 10 KOhm resistor. (R1=10k). The long leg of LED should be connected to the positive direction(where the power is coming from. In this case, it's Pin 13).
• After powering up the Arduino and downloaded the Blink sketch, the LED blinks in phase with the on-board LED, since the current flows from output to LED and to ground when the output is high. The light of our LED was faint, and we expected that the LED will be brighter if we have used a resistor with lower resistance.

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

• We changed out circuit to the picture below.
• The LED is blinking out of phase with the on-board LED since the current only flows when there is a voltage difference, thus the LED is lit when input is low.

## Connecting an LED to Pin 11

### Make Pin 11 LED blink

• Using the same setup for Option 2, we switched the wired that connected to the LED from Pin 13 to Pin 11.
• Here is our modified sketch:
``` 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 } ```

• In addition of using the on-board LED for Pin 13, we connected another LED to Pin 13 and to VCC via a 470Ohm resistor.
• modified sketch for in-phase blinking:
```int ledPin1 = 11 int ledPin2 = 13; // LED connected to digital pin 11 void setup() // run once, when the sketch starts { pinMode(ledPin1, OUTPUT); // sets the digital pin as output pinMode(ledPin2, OUTPUT); } void loop() // run over and over again { digitalWrite(ledPin1, HIGH); // sets the LED11 on digitalWrite(ledPin2, HIGH); // sets the LED13 on delay(1000); // waits for a second digitalWrite(ledPin1, LOW); // sets the LED11 off digitalWrite(ledPin2, LOW); // sets the LED13 off delay(1000); // waits for a second } ```

• modified sketch for out-phase blinking:
```int ledPin1 = 11 int ledPin2 = 13; // LED connected to digital pin 11 void setup() // run once, when the sketch starts { pinMode(ledPin1, OUTPUT); // sets the digital pin as output pinMode(ledPin2, OUTPUT); } void loop() // run over and over again { digitalWrite(ledPin1, HIGH); // sets the LED11 on digitalWrite(ledPin2, LOW); // sets the LED13 off delay(1000); // waits for a second digitalWrite(ledPin1, LOW); // sets the LED11 off digitalWrite(ledPin2,HIGH); // sets the LED13 on delay(1000); // waits for a second } ```

• modified sketch for Pin 13 LED blinks at .5Hz and Pin 11 blinks at 1 Hz.
```int ledPin1 = 11 int ledPin2 = 13; // LED connected to digital pin 11 void setup() // run once, when the sketch starts { pinMode(ledPin1, OUTPUT); // sets the digital pins as output pinMode(ledPin2, OUTPUT); } void loop() // run over and over again { digitalWrite(ledPin1, HIGH); // sets the LED11 on digitalWrite(ledPin2, HIGH); // sets the LED13 on delay(500); digitalWrite(ledPin2, LOW); // sets the LED13 off delay(500); digitalWrite(ledPin1,LOW); // sets the LED11 off digitalWrite(ledPin2, HIGH); // sets the LED13 on delay(500); digitalWrite(ledPin2, LOW); // sets the LED13 off delay(500); } ```

## High Frequency

• We first made LED 11 blink as fast as possible by decreasing delay time. The shortest delay time we could get was 30.
• Since the delay time is in mili-seconds, one cycle of the blinking of our LED (on and off) takes .06 seconds. The frequency will be 1/.06 = 16.667Hz.
• We later measured the actual frequency using an oscilloscope. The oscilloscope showed that our LED blinks at 16.56Hz, which is very close to our estimate.

## Replace the LED and Resistor with a Buzzer

• Since the buzzer we were using has the maximum voltage of 3V, in order to protect it from getting too much voltage, we connected a resistor to it. Then we connected the resister to VCC (5V), and the buzzer to Pin13. Using the blink example sketch, we were able to make the Buzzer buzz every other second.
• We first tried a 470Ohm resistor, however, the buzzer doesn't sound because the resistor is too big that it blocks most of the voltage coming from VCC. Then we tried a 22Ohm resistor and got the right effect.

## Connect a switch to Pin 11

• We modified our sketch to set Pin 11 as the input pin and downloaded it to Arduino.
• Then we wired a switch to our circuit based on the following diagram.(A 470Ohm resistor is used)
• modified sketch that switches LED 13 on and off.
```int ledPin = 13; // on-board LED of digital pin 13 int inputPin = 11; // Switch connected to digital pin 11 int val = 0; // value read from input pin void setup() // run once, when the sketch starts { pinMode(ledPin, OUTPUT); // sets the digital pin as output pinMode(inputPin, INPUT); // sets the digital pin as input } void loop() // run over and over again { val = digitalRead(inputPin); if (val==HIGH) { digitalWrite(ledPin, LOW); } else { digitalWrite(ledPin, HIGH); } } ```
• modified sketch that increments a counter everytime the switch changes state. However, this didn't work even though we tried to alter it in every possible way.
```int ledPin = 13; // on-board LED of digital pin 13 int inputPin = 11; // Switch connected to digital pin 11 int val = 0; // value read from input pin int lastX = -1; // previous input/state of LED int count = 0; // counter void setup() // run once, when the sketch starts { Serial.begin(9600); pinMode(ledPin, OUTPUT); // sets the digital pin as output pinMode(inputPin, INPUT); // sets the digital pin as input } void loop() // run over and over again { val = digitalRead(inputPin); if ( val != lastX ) { count = count+1; Serial.print(count); } lastX = val; } ```