231a ak lab5

From CSclasswiki
Jump to: navigation, search

Main | Lab | Project | Demos | Resources

Lab5: Interface simple input and output devices to Arduino

Introduction to Electricity and Electronics

  • The output of a logic gate: high and low voltages
    • A circuit must be connected to Vcc and GND. Current flows from high voltage to low voltage.
  • Resistors
    • The unit for resistors is Ohm. Most commonly used resistors are from 1kOhm - 2kOhm. ]
    • Ohm's Law: V = R * I
  • Diodes
    • LED - Light Emitting Diode: Short leg connects to input; long leg connects to GND.
    • CANNOT directly connect an LED to +5 and GND, must have resistor connecting in between, otherwise will create short circuit.
    • LED only ON when there is current flowing through. (Must have high and low voltages on each leg)
  • Switches
    • Close the switches lets the current through. Open the switch disconnects the circuit.
  • Buzzer
    • When connected to a circuit, it will makes noise.
  • Light-Sensitive Resistor
    • When there is light over the LSR, it falls and lets current flows through. Otherwise it disconnects the circuit.

Resistors

Taken from 231 class resources page:

  • Resistor Color Values
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

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

  • Turn off the Arduino by disconnecting it from the USB cable.
  • Locate the +5V, GND, and #13 pins on the Arduino side connector. Be careful, it is easy to put a wire in the wrong hole.
  • Create your circuit following the diagram on the right with one LED and one 1KOhm resistor on the digital kit (anything between 1KOhm and 20KOhm should work).
    • We used a 1KOhm resistor
  • Use the 5V and GND of the Arduino, not those of the Kit.
  • 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, our LED blinked - both the on board one and the external one.
  • Indicate on your report what is the right way to wire up an LED.
    • Long leg - input; Short leg - GND
  • 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 steps as in the previous section, but use the picture on the right as the example. (connecting +5V to resistor to LED to output)
  • Same question: is your LED blinking in phase or out of phase with the on-board LED?
    • It blinks in phase with the on-board LED.
  • Make sure you explain in your report the difference between the two wiring approaches.
    • With the second wiring, the off-board LED is dimmer than the on-board LED. Because the 1st wiring let the current flows through LED before the resistor, so the LED is brighter. While 2nd wiring the current was shared by the resistor before it flows through LED, so the LED is dimmer.

Connecting an LED to Pin 11

  • You are now going to connect your LED to Pin 11. This way you can control two separate LEDs, on on Pin 13 (on-board), and one on Pin 11.
  • Modify your sketch so that it makes the Pin 11 LED blink.
int ledPin = 11;                
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
}
  • Modify your sketch so that it makes both LEDs blink. Make them blink in phase and out of phase by changing only the software, not the hardware.
    • Both LEDs blink in phase:
int ledPin1 = 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(ledPin1, OUTPUT);      // sets the digital pin as output
  pinMode(ledPin2, OUTPUT)
}


void loop()                     // run over and over again
{
  digitalWrite(ledPin1, HIGH);   // sets the LED on Pin 11 on
  digitalWrite(ledPin2, HIGH);   // sets the LED on Pin 13 on
  delay(1000);                  // waits for a second
  digitalWrite(ledPin1, LOW);    // sets the LED on Pin 11 off
  digitalWrite(ledPin2, LOW);    // sets the LED on Pin 13 off
  delay(1000);                  // waits for a second
}
  • Both LEDs blink out of phase:
int ledPin1 = 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(ledPin1, OUTPUT);      // sets the digital pin as output
  pinMode(ledPin2, OUTPUT)
}


void loop()                     // run over and over again
{
  digitalWrite(ledPin1, HIGH);   // sets the LED on Pin 11 on
  digitalWrite(ledPin2, LOW);    // sets the LED on Pin 13 off
  delay(1000);                  // waits for a second
  digitalWrite(ledPin1, LOW);    // sets the LED on Pin 11 off
  digitalWrite(ledPin2, HIGH);   // sets the LED on Pin 13 on
  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.
int ledPin1 = 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(ledPin1, OUTPUT);      // sets the digital pin as output
  pinMode(ledPin2, OUTPUT)
}


void loop()                     // run over and over again
{
  digitalWrite(ledPin1, HIGH);   // sets the LED on Pin 11 on
  digitalWrite(ledPin2, HIGH);   // sets the LED on Pin 13 on
  delay(500);       

  digitalWrite(ledPin1, LOW);    // sets the LED on Pin 11 off        
  digitalWrite(ledPin2, HIGH);    // sets the LED on Pin 13 on
  delay(500);     

  digitalWrite(ledPin1, HIGH);    // sets the LED on Pin 11 on
  digitalWrite(ledPin2, LOW);   // sets the LED on Pin 13 off
  delay(500);     

  digitalWrite(ledPin1, HIGH);    // sets the LED on Pin 11 off
  digitalWrite(ledPin2, LOW);   // sets the LED on Pin 13 off
  delay(500);     
}

High Frequency

  • Make your Pin-11 LED blink as fast as possible.
    • Taking out the delay in the program, this makes the LED blinks at its clock speed.
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
}
  • How fast is it blinking? How can you find out? How can you measure this frequency precisely?
    • Using an oscilloscope!
      1. Verify the hardware is connected correctly
      2. Connect a probe to the oscilloscope, and the black clip goes to GND, hook goes to input
      3. Turn on oscilloscope, push Auto Scale
      4. Examine "Time", and choose "Frequency" on the screen to get the frequency of the input.
    • Pin 11 is blinking at f = 100 kHZ; while if connected to Pin 13, Pin 13 LED blinks at 135 kHZ.

Replace the LED and Resistor with a buzzer

  • The buzzer has a maximum voltage written on one of its sides. It's typically 3V. This means that it shouldn't be subjected to more than 3V of power. However the outputs of the Arduino can vary between 0 and (close to) 5V. So you need to protect the buzzer from getting too much voltage... What's the solution?
    • Add a resistor which is less than 1 kOhm to the circuit, this will spare the voltage that goes through the buzzer.
    • We used a 200 Ohm resistor, and the buzzer makes noise when running the program.

The program is the same as the previous one:

int ledPin = 11;                // 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

  digitalWrite(ledPin, LOW);    // sets the LED off
  
}

Connect a switch to Pin 11

  • First disconnect all hardware from your Arduino
  • Modify your sketch and make Pin 11 an input pin. (TBC)
int ledPin = 11;              

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

void loop()                     // run over and over again
{
  digitalWrite(Pin13, digitalRead(ledPin));   // sets the LED on Pin13 on when Pin11 is High
}
  • Download your sketch to the Arduino.
  • Now that Pin 11 is programmed as an input, connect a switch (with a resistor) to Pin 11. Follow the diagram on the right. Vout should connect to Pin 11. (TBC)
    • When the switch is closed, LED goes Off
  • Change your sketch so that when the Arduino sees that the switch has been pressed, it switches the LED connected to Pin 13.
int ledPin = 11;              

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

void loop()                     // run over and over again
{
  digitalWrite(Pin13, digitalRead(ledPin));   // sets the LED on Pin13 on when Pin11 is 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. Activate the switch ON and OFF quickly. Is the Arduino keeping the right count? Why or why not?
    • No, the Arduino does Not keep the right count. It jumps in 4s to 6s every time the switch is turned On and Off. Because when the switch gets activated On, it stays on longer than the clock cycle of the Arduino, so the Arduino will recognize the switch as On for more than the counter is counting.
SerialBegin(9600);
int i;

void setup()                    // run once, when the sketch starts
{
  pinMode(ledPin11, INPUT);      // sets the digital pin 11 as input
  pinMode(ledPin12, INPUT);      // sets the digital pin 12 as input
  pinMode(ledPin13, OUTPUT);      // sets the digital pin 13 as output
}

void loop()                     // run over and over again
{
  if !(digitalRead(Pin11)) {
      digitalWrite(Pin13, !digitalRead(Pin12));
      i++;
      SerialPrint(i);
}

Light-sensitive Resistors

  • We didn't have time to do this part.
  • But according to the logistics of LSR, the LED should be dimmer when the light on LSR is weak, LED should be lighter when the light on LSR is strong. Becuase LSR has a high resistance in the dark, it shares more voltage, thus the LED will be dimmer.