Electronic Systems Leds and Push Buttons 2013 Universitat
Electronic Systems Leds and Push Buttons © 2013 Universitat Politècnica de Catalunya (EPSEVG) José Antonio Soria Pérez - Associate Professor - ETSEIAT (UPC). Office hours: EET – D 244 (TR 2, 2 nd floor): MON 10 -12 h; 17 -19 h; WED: 15 -19 h E-mail: jose. antonio. soria@eel. upc. edu, jasoria@eel. upc. edu
Resources l Electronics in general: l l www. sparkfun. com/tutorials (Components, PCB manufacturing, μC, robotics, . . . ) For the lab… l l www. arduino. cc (Arduino homepage: installation, getting started, c++ programming…) i. MEM accelerometer (adxl 345): l Datashet: http: //www. analog. com/en/mems-sensors/mems-inertialsensors/adxl 345/products/product. html l Arduino library: http: //www. loveelectronics. co. uk/Tutorials/12/adxl 345 -accelerometer-arduinotutorial ; https: //www. sparkfun. com/tutorials/240 ; http: //bildr. org/2011/03/adxl 345 -arduino/ l l Labview (tutorial + DAQ acquisition): http: //www. ni. com/pdf/gettingstarted/introduction_to_labview_tutorial. pdf Vendors Open andand freedistributors ebooks for online: downloading (http: //it-ebooks. info/ ; http: //ebooks-it. org ) l l l http: //www. digikey. com/ (USA) Arduino: A quick start guide – Mike Schmidt http: //it-ebooks. info/book/5/ http: //dx. com/ - Deale. Xtreme (Asia; Gadgets and accesories) Learn electronics with Arduino – Don Wilcher (http: //it-ebooks. info/book/2321/) http: //es. rs-online. com (RS Amidata - Spain) José Antonio Soria Pérez Departament d’Enginyeria Electrònica Universitat Politècnica de Catalunya 2/18
Leds l l The Breadboard is a base for prototyping electronics “without soldering” Resistors Capacitors and Inductors Leds are diodes working as light indicators Led i-v characteristics Symbol and i. D conventions i. D Reverse bias Reverse operation (OFF) Foward operation (ON): i. D + Foward bias Vγ - + i. D i D > 0 → v D = Vγ v. D Ànode 0 - v. D < Vγ → i. D = 0 Càtode + Threshold voltage to turn the Led ON Arduino (Typ. Vγ = 2 V) ATMega 238 v v. D - R=1 kΩ PIN 13 + VOUT = 0 V 5 V _ LED ILED Light ON OFFoperation José Antonio Soria Pérez Departament d’Enginyeria Electrònica Universitat Politècnica de Catalunya 3/18
Working with Leds l l 1) Mount the circuit connecting the 1 kΩ resistor and the led as shown in the diagram 2) Open the blink project again (blink. ino). Compile and upload the program onto the Arduino board as usual. Check the expected behaviour of the Led VPIN 13 5 V 1 s 2 s 3 s 4 s t Alternating LOW and HIGH levels for 1 seg. is equivalent to generating a square waveform signal of 0. 5 Hz José Antonio Soria Pérez Departament d’Enginyeria Electrònica Universitat Politècnica de Catalunya 4/18
Custom functions: Binary dice (1 st. version) l l l 1) Mount the following circuit. 2) Write the following program that emulates the throwing of a binary dice each time it is executed. Compile and upload the code to check its behaviour. Notation: Led names (pins 13, 12 and 11) return_type func_name(data_type 1 var 1, data_type 2 var 2, … ) { Declarations outside “void setup()” and Led pins defined as OUTPUT // Function code loop()” “void l Calls can be inside “void setup()” or “void loop()” l } Initilization of random generator* Random number from 1 to 6** Corresponding Leds Function are turned call ON Infinite loop (code executed once)*** Led representation for each combination of the dice In order to send the output of each pin the. Function result declaration is masked (bitwise AND) against its boolean representation * It is better to read an unconnected pin in order to generate a fairly random sample ** Number 7 is excluded when using random() *** It is necessary to RESET the μC for another roll of the dice José Antonio Soria Pérez Departament d’Enginyeria Electrònica Program code Universitat Politècnica de Catalunya 5/18
Push Buttons l l l The Breadboard is a base for prototyping electronics “without soldering” Resistors Capacitors and Indictors Leds are diodes working as light indicators Push Buttons allow external user commands to be handled by the μC l Input logic Top 2 Connection diagram 5 V (Front view ) Pull-up 5 V Front 1 4 resistor Side 1 2 R 2 INPUT Arduino 1 3 ATMega 238 INPUT 5 V Arduino ATMega 238 R 0 V 1 3 2 4 Current flow Pull-down resistor HIGH level logic (“ 1” when pressed) José Antonio Soria Pérez Departament d’Enginyeria Electrònica “ 0” (or NOT) logic (“ 0” when pressed) Universitat Politècnica de Catalunya 6/18
Working with Push Buttons l l In this circuit, the following program makes the Led (pin 13) glow when holding down the Push Button key (pin 7) 1) Exercise: Modify the program to toggle the Led when pushing the key down once. Try to avoid electronic glitches by optimizing the code l Solution: Button pressed Button and V Led pin names Button Connection diagram released OUTPUT Button is INPUT HIGH and Led is OUTPUT Push Button and Led pin names Variables for storing PREVIOUS and CURRENT button state LOW LED pin defined as OUTPUT Push Button defined as INPUT The user presses the key whenever the pin current HIGH t level has changed in relation to the previous reading Electronic glitches Reading Push Button state In this case we toggle the Led and wait 5 ms for avoiding bouncig effects Turn the Led ON when Input is HIGH (key pushed down) Electronic glitches: Undesired transitions in the form of “electrical pulses” mainly caused by “old_button_state” variable updated before design errors (not well. Turn optimized and/or fault tolerances (bouncing of mechanical buttons) the Ledcode) OFF otherwise entering into the next loop cycle José Antonio Soria Pérez Departament d’Enginyeria Electrònica Universitat Politècnica de Catalunya 7/18
Working with Push Buttons l 2) Exercise: Adapt the binary dice circuit by including a Push Button and write the necessary code to roll the dice each time the key is pressed. l Solution: Try understanding the code yourself Connection diagram José Antonio Soria Pérez Departament d’Enginyeria Electrònica Universitat Politècnica de Catalunya 8/18
- Slides: 8