180 likes | 409 Views
Lab 3 EGR 262 – Fundamental Circuits Lab. EGR 262 Fundamental Circuits Lab Presentation for Lab #3: Switches and 7-Segment Displays. Lab 3 EGR 262 – Fundamental Circuits Lab. Protecting Microprocessor Inputs and Outputs
E N D
Lab 3 EGR 262 – Fundamental Circuits Lab EGR 262 Fundamental Circuits Lab Presentation for Lab #3: Switches and 7-Segment Displays
Lab 3 EGR 262 – Fundamental Circuits Lab Protecting Microprocessor Inputs and Outputs Microprocessors can be easily destroyed by excessive voltages and currents. Inputs and outputs are often protected using current-limiting resistors and pull-down resistorsor pull-up resistors as discussed below. Microprocessor Outputs Most microprocessor outputs can only produce currents in the mA range. Arduino UNO specifications: DC current per I/O pin (at 5V): 40 mA max (20 mA recommended) Total DC current: 200 mA Be sure to include current-limiting resistorsin series with LEDs that are connected to outputs of the Arduino. If the resistor is omitted, excessive current through the LED might destroy the Arduino. Typical values for current-limiting resistors are 220 , 330 , and 470 . If devices other than LEDs (such as motors, bulbs, etc.) are connected to Arduino outputs, be sure that their current does not exceed the limits listed above. Other methods for controlling high current outputs (such as using transistors, motor controllers, relays, etc) may be covered in later labs.
Lab 3 EGR 262 – Fundamental Circuits Lab 220 Arduino UNO Arduino UNO LED LED Incorrect way to connect an LED to the output of the Arduino Correct way to connect an LED to the output of the Arduino Illustration: Protecting Microprocessor Outputs X Note: If a 7-segment display is controlled using 7 Arduino outputs, 7 current-limiting resistors will be needed.
Lab 3 EGR 262 – Fundamental Circuits Lab 4 Microprocessor Inputs Digital inputs on the Arduino are high-impedance inputs that are not connected to HIGH or LOW, but are “floating.” The inputs might randomly be read as HIGH or LOW values, which is why a pull-up resistoror pull-down resistoris needed. Inputs without pull-up or pull-down resistors are also subject to damage due to static discharge. See the illustration below. +5V +5V “Pull-up resistor” Arduino UNO Arduino UNO 10 k “Pull-down resistor” 10 k Pushbutton switch with a pull-up resistor. Pushing the switch connects ground (LOW) to the input. When the switch is not pressed, the resistor pulls the input to 5V (HIGH). Pushbutton switch with a pull-down resistor. Pushing the switch connects 5V (HIGH) to the input. When the switch is not pressed, the resistor pulls the input to ground (LOW).
Lab 3 EGR 262 – Fundamental Circuits Lab Illustration: Protecting Microprocessor Inputs X +5V +5V Arduino UNO Arduino UNO Incorrect way to connect 5V to an Arduino input using a pushbutton switch 10 k Correct way to connect 5V to an Arduino input using a pushbutton switch • Notes: • We will typically use pull-down resistors with input switches in lab. • 10 k is a typical value for a pull-up or pull-down resistor • The Arduino also has an optional built-in pull-up resistor (see next slide).
Lab 3 EGR 262 – Fundamental Circuits Lab 6 • Configuring Arduino Pins as Inputs and Outputs • Arduino Digital I/O pins may be configured in three ways: • Inputs • Example: pinMode(3, INPUT); // Make pin 3 an input • This is the default, so you can often omit this statement • Outputs • Example: pinMode(3, OUTPUT); // Make pin 3 an output • Inputs with Internal Pull-up Resistors • Example: pinMode(3, INPUT_PULLUP) // Make pin 3 an input • // with an internal pull-up R • No pull-up resistor is connected by default • The internal pull-up resistor has a value of 20 k - 50 k
Lab 3 EGR 262 – Fundamental Circuits Lab 7 Reading Inputs and Outputs Inputs: Arduino digital inputs can be read using digitalRead( ). It is good practice to configure the pin as an input first (although this is the default). Example: Value = digitalRead(6); // Value = LOW or HIGH based on pin 6 state Example: if (digitalRead(7) == HIGH) // typically 3V or higher Serial.println(“Pin 7 is HIGH”); else Serial.println(“Pin 7 is HIGH”); Outputs: Arduino digital outputs can be read using digitalWrite( ). Be sure to configure the pin as an output first. Example: pinMode(6, OUTPUT); // Make pin 6 an output digitalWrite(6, LOW); // Make pin 6 LOW (0V) digitalWrite(6, HIGH); // Make pin 6 HIGH (5V)
Lab 3 EGR 262 – Fundamental Circuits Lab switch switch thrown thrown HIGH HIGH HIGH HIGH LOW LOW LOW LOW t t Debounced switch Switch with contact bounce Switch Bounce Standard switches, such as toggle switches, slide switches, and button switches, typically exhibit “switch bounce.” When the switch is thrown, the contacts will bounce for several milliseconds before settling down. This could cause several transitions which can cause problems in many circuits, including microcontroller inputs. This problem can often be handled by either changes to hardware or to software. A hardware solution involves purchasing or constructing “debounced” switches which insure only one transition from LOW to HIGH or from HIGH to LOW. Software solutions often involve adding small delays to give the switch contacts time to settle. The figures below illustrate the difference in debounced switches and switches that experience contact bounce. You pressed the button once, but the microcontroller thinks you pressed it 3 times!
Lab 3 EGR 262 – Fundamental Circuits Lab Button switch Poor Example (switch bounce ignored) Suppose that you wanted to toggle an LED on D13 every time you pressed a button connected to D9. The problem is that each time you press the button, the microprocessor might think that you pressed it 4 or 5 times, so the result is unpredictable. Arduino UNO +5V 220 D13 D9 LED 10 k Poor program! Do not use. A better solution is on the following slide.
Lab 3 EGR 262 – Fundamental Circuits Lab Good Example (switch bounce properly handled) This example is similar to the previous one except that it handles switch bounce properly. It ignores switch transitions for up to 50ns. You can copy the highlighted sections into your program when you need to work with pushbutton switches. Use the function readButton( ) in your program.
Lab 3 EGR 262 – Fundamental Circuits Lab 5V LED lights when LED lights when output is LOW output is HIGH Active-HIGH LED connection Active-LOW LED connection • Active-HIGH and active-LOW outputs • Outputs of many logic devices are configured as active-HIGH or active-LOW. • Active-HIGH output: • Output = HIGH (1) to activate (turn on) the output • Output = LOW (0) to de-activate (turn off) the output • Active-LOW output: • Output = LOW (0) to activate (turn on) the output • Output = HIGH (1) to de-activate (turn off) the output • Example: • In the figure on the left, the LED lights when the output of the logic gate is HIGH. In the figure on the right, the LED lights when the output of the logic gate is LOW. 220 220
Lab 3 EGR 262 – Fundamental Circuits Lab a a a b b 7-segment decoder/driver IC or microcontroller c c b f g d d anode cathode e e c e f f d a g g Common cathode Typical segment 7 - segment display 7-segment displays A 7-segment display is made up of seven LED’s configured to display the decimal digits 0 through 9. There are two types of 7-segment displays: 1) common anode (all anodes at +5V) 2) common cathode (all cathodes at ground) Common cathode displays Common cathode displays require active-HIGH outputs. When the output of the decoder or microcontroller is HIGH for one segment, 5V is connected to the anode. Since all cathodes are grounded, the segment lights. 220
Lab 3 EGR 262 – Fundamental Circuits Lab Common anode displays Common anode displays require active-LOW outputs. When the output of the decoder or microcontroller is LOW for one segment, 0V is connected to the cathode. Since all anodes are connected to 5V, the segment lights. 220 a a a b b 7-segment decoder/driver IC or microcontroller c c f b anode g d d cathode +5V e e e c f f d a g g Common anode Typical segment A “bubble” is sometimes used to indicate an active-LOW output 7-segment display We will use common anode displays in lab.
Lab 3 EGR 262 – Fundamental Circuits Lab a f b g e c d Common-anode 7-segment display Truth table for common anode displays For a common anode display: 0 – used to turn a segment ON 1 – used to turn a segment OFF So we can easily develop a truth table showing which segments should be ON and which segments should be OFF for each digit. To display the digit 0, light all segments except g
Lab 3 EGR 262 – Fundamental Circuits Lab Button switch Schematic for Lab #3 Arduino UNO +5V 220 +5V D2 D3 D4 D5 D6 D7 D8 D9 a a b c f b 10 k g d e e c d f g Common anode 7-segment display (see data sheet for pinout) • Notes: • A pull-down resistor is used with the input switch. • Current-limiting resistors are used with each of the 7 outputs. • Active-LOW outputs are used (LOW outputs used to light segments).
Lab 3 EGR 262 – Fundamental Circuits Lab Writing a function to control a 7-segment display • Using a function is an efficient way to communicate with a 7-segment display. • Write a function such that: • Segments a-g are tied to digital outputs 2-8 as shown on the previous schematic. • A common anode 7-segment display will be used. • Refer to the table generated on a previous slide for which segments to light for each digit. • Cases 1-9 are not shown, but will be completed as part of the Pre-Lab work.
Lab 3 EGR 262 – Fundamental Circuits Lab Button switch Arduino UNO +5V 220 +5V D2 D3 D4 D5 D6 D7 D8 D9 a a b c f b 10 k g d • Writing a program for Lab 3 • The program should operate as follows: • Display brief instructions on the computer screen. • The count should be initialized to 0 and shown on the 7-segment display and the computer screen. • When the user presses the button the count should advance and the new value should be displayed on the 7-segment display and the computer screen. • The count should be mod-10 (0 to 9 and repeat). • Use the display_digit( ) function described on the previous slide. • Use the readButton( ) function provided on an earlier slide to allow for switch debounce. • See the following slide for more details on the code. e e c d f g Common anode 7-segment display (see data sheet for pinout)
Lab 3 EGR 262 – Fundamental Circuits Lab • Writing a program for Lab 3 // Global variables for readButton( ) function (see Lab #3 Lecture) // Other global variables void setup( ) { // Define input and output pins // Set up serial communication // Display instructions on the computer screen // Display initial value (0) on 7-segment display // Display initial value (0) on computer screen } void loop( ) { // If button is pressed: // Increment counter // Adjust counter for mod-10 operation // Display count on computer screen // Display count on 7-segment display } void readButton(intbuttonPin) { // Use code provided in Lab #3 Lecture } void display_digit(N) { // See Lab #3 Lecture for details }