231a-ab Lab 5

From CSclasswiki
Jump to: navigation, search

Objective

The objective of this lab is to familiarize with different input and output devices that can be used with the Arduino.

Format of Report

The report is based on the instructions given by the lab prepared by D. Thiebaut which can be found here. All figures referred to in the report are under the section Wiring Figures. There are supplementary images under the Images section of this report to how setup is done during the lab.

Exercises

Connecting LED to Pin 13

Below is the Blink sketch from the example digital sketches of the Arduino.

 int ledPin = 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 LED on
   delay(1000);                  // waits for a second
   digitalWrite(ledPin, LOW);    // sets the LED off
   delay(1000);                  // waits for a second
 }

This sketch is used to observe how an on-board LED blinks in phase and out of phase with the pin 13 LED. Different wirings are used to generate in phase and out of phase blinking of both LEDs. Make sure that when you are wiring a LED to the board, the LED is horizontal. That is, that the pins of the LED are on the same line of the board. Remember that the LED has polarity so if the LED does not light up when you expect it to, just turn it around to reverse its polarity.
When the wiring is done according to Figure 1, the on-board LED blinks in phase with the pin 13 LED. The current goes in the direction of the LED arrow. If output of pin 13 is 1, pin 13 lights up and LED receives an input of 1 and thus, it lights up as well.
On the other hand, when the wiring is done according to Figure 2, the on-board LED blinks out of phase with the pin 13 LED. In this wiring, when output of pin 13 is 1, pin 13 lights up. However, the LED does not light up because at the other end of the circuit is +5V which means the current does not go through (electrons do not flow). On the other hand, if output of pin 13 is 0, the LED lights up because the current goes through.

Connecting LED to Pin 11

For this exercise, the wiring used is illustrated in Figure 3. In this wiring, pin 11 is connected to the LED.
The following sketch is used to make the pin 11 LED blink. Instead of setting the ledPin to 13, it is set to 11.

 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
 }

To make both LEDs blink in phase, the sketch below is used. This is done by setting the output of the two pins to be the same at the same time.

int ledPin = 11;                // LED connected 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 pins as output
  pinMode(ledPin2, OUTPUT);
}

void loop()                     // run over and over again
{
  digitalWrite(ledPin, HIGH);   // sets the LEDs on
  digitalWrite(ledPin2, HIGH);
  delay(1000);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LEDs off
  digitalWrite(ledPin2, LOW);
  delay(1000);                  // waits for a second
}

To make both LEDs blink out of phase, the sketch below is used. Notice that when the output of pin 11 is HIGH, the output of pin 13 is LOW and vice versa. This way, the LEDs do not light up at the same time.

int ledPin = 11;                // LED connected 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 pins as output
  pinMode(ledPin2, OUTPUT);
}

void loop()                     // run over and over again
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  digitalWrite(ledPin2, LOW);   // sets the LED off
  delay(1000);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  digitalWrite(ledPin2, HIGH);  // sets the LED off
  delay(1000);                  // waits for a second
}

The sketch below is used to make the pin 13 LED blink 0.5 Hz and pin 11 LED blink at 1 Hz, still out of phase. When pin 11 is turned on, the delay is still 1 second but when pin 13 is on, the delay is only half a second.

int ledPin = 11;                // LED connected 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
  pinMode(ledPin2, OUTPUT);
}

void loop()                     // run over and over again
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  digitalWrite(ledPin2, LOW);   // sets the LED off
  delay(1000);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  digitalWrite(ledPin2, HIGH);  // sets the LED off
  delay(500);                   // waits for half a second
}

High Frequency

The wiring for this exercise is same as used in the previous exercise (Figure 3). To have the pin 11 LED blink as fast as possible, delete the delays between having the LED on and off.

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
  digitalWrite(ledPin, LOW);    // sets the LED off
}

Using an oscilloscope, we measured that pin 11 is blinking 100 kHz. When the pin used is changed to pin 13, the speed of the blinking changed to 133 kHz. This is the same for pin 12.

Using a buzzer

For this exercise, use the wiring in Figure 3 but replace the LED with a buzzer. Note that unlike the LED, the buzzer does not have polarity. It is still important to use a resistor when using a buzzer because the buzzer cannot be subjected to more than 3V of power and the Arduino output can be 0 to about 5V. If the buzzer does not make a sound when the sketch below is run, the resistor being used is probably to high so change to a smaller resistor. The sketch below allows the buzzer to make a sound when output from pin 11 is 1 and not make a sound when output from pin 11 is 0.

 int buzzPin = 11;               // buzzer connected to digital pin 11
 
 void setup()                    // run once, when the sketch starts
 {
   pinMode(buzzPin, OUTPUT);     // sets the digital pin as output
 }
 
 void loop()                     // run over and over again
 {
   digitalWrite(buzzPin, HIGH);  // turns buzzer on
   delay(1000);                  // waits for a second
   digitalWrite(buzzPin, LOW);   // turns buzzer off
   delay(1000);                  // waits for a second
 }

Using a switch

The wiring for this exercise is given in Figure 4. For this lab, I used two wires as a switch: when the wires are connected, the "switch" is activated since the current is able to flow from one to the other. The sketch below turns on the pin 13 LED when the switch is activated (is on) and turns the LED off when the switch is deactivated (turned off). The following sketch also keeps track of how many times the switch is activated.

int myPin = 13;                 // LED connected to pin 13
int myPin2 = 11;                // part of switch connected to pin 11
int x;                          // keeps track of output (1 or 0) of pin 11
int counter = 0;                // counter that keeps track of # of changed state of switch
 
void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // guarantees output to screen
  pinMode(myPin, OUTPUT);       // sets the digital pin 13 as output
  pinMode(myPin2, INPUT);       // sets pin 11 as input
 
}

void loop()                     // run over and over again
{
  x = digitalRead(myPin2);      // read output from pin 13
  if (x==1){                    // if switch is activated (is on), counter is incremented by 1
    counter = counter + 1;
  }
  Serial.println(counter);      // prints counter on the screen
  digitalWrite(myPin, x);       // sets the pin 13 on if switch is on, sets pin 13 off if switch is off  
}

The output of counter is not what I expected. I expected the output of counter when I have the wires of my switch connected to go from 0 to 1 in the beginning and if I activate my switch again to be 1 to 2. However, the counter displayed big jumps from 0 to 38 then when switch is activated again, 38 to 61, and so on. It seemed that the counter is still reading input of fast exchange from 0 to 1 even though the switch is activated.

Wiring Figures

Figure 1

Figure 1

Figure 2

Figure 2

Figure 3

Figure 3

Figure 4

Figure 4

Images

Resistors

231a-ab Resistors.jpg

Connecting LED to pin 11

231a-ab LEDpin11.jpg

Frequency of pin 11 LED

231a-ab Freqpin11LED.jpg

Using a buzzer

231a-ab Buzzer.jpg

Deactivated switch

231a-ab DSwitch.jpg

Activated switch

231a-ab ASwitch.jpg

Conclusion

In this lab, I learned how to use and wire different devices such as LED, buzzer and switch to the Arduino. I also learned that different pins can be used as input or output as needed by switching the modes of the pin in the sketch. In this lab, resistors were very important. They were used to avoid short-circuits and overheating of the Arduino.

Navigation

  • Click here to go back to main page.