1 / 16

EMS1EP Lecture 6 Digital Inputs

EMS1EP Lecture 6 Digital Inputs. Dr. Robert Ross. Overview (what you should learn today). Hardware: Connecting switches to the Arduino Revision of setting pin directions Using digitalRead() Debouncing Switches. Hardware – inputs to an Arduino.

syshe
Download Presentation

EMS1EP Lecture 6 Digital Inputs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EMS1EP Lecture 6Digital Inputs Dr. Robert Ross

  2. Overview (what you should learn today) • Hardware: Connecting switches to the Arduino • Revision of setting pin directions • Using digitalRead() • Debouncing Switches

  3. Hardware – inputs to an Arduino • Lots of things can be inputs into the Arduino • Many of these are analog sensors (pressure, acceleration, pressure, temperature, ect.) • In this lecture we will be looking at digital inputs – specifically switches • Switches allow humans to interact with microcontrollers and allow them do control things

  4. Hardware – Switch Inputs • Switches are connected to a pin configured as a digital input • A pull-up resistor used to ensure switch is not floating (when it is not pressed) • This resistor should be between 10K and 100K • (small enough to pull-up the voltage but large enough not to waste too much current when the button is pressed)

  5. Switch connection to the LArduino • R = 10K • Switch connected to a digital input

  6. Setting up pins • Before using the digital pins we need to set them up as either inputs or outputs • This is called setting the direction of the pins (input direction or output direction) • To do this we use the pinMode() function

  7. pinMode() function • Syntax: pinMode(<pin_number>, <direction>) • Normally some pins will be assigned at the top of your code • e.g. • int LED1Pin = 10; • intswitchPin = 11; • These assignments are used for the pin number • Direction should be OUTPUT or INPUT • e.g. • pinMode(LED1Pin, OUTPUT); • pinMode(switchPin, INPUT);

  8. digitalRead() • Once pins have been set as inputs the digitalRead() function can be used to see if there is a low (0V) or high (5V) voltage on the pins • Syntax: intdigitalRead(<Pin_Number>); • e.g. intpinValue; //Check if voltage on pin is high or low pinValue = digitalRead(sw1Pin); If(low == pinValue){ //do something – switch is pressed } else{ //do something – switch not pressed }

  9. Class Quiz • A switch is connected to pin 7 • An LED is connected to pin 9 • Use pinMode() to setup the pins as inputs and outputs • If the switch is pressed (LOW voltage) turn the LED on (set pin LOW). Otherwise turn the LED off

  10. A problem with switches • Switches are mechanical devices – two pieces of metal touching each other • When the switch is pressed these switches will often bounce up and down a few times • This bouncing can be a real is a real problem if it is important how many times the switch has been pressed Contacts settled Switch pressed Bouncing

  11. Switch bounce Contacts settled Switch pressed Bouncing

  12. Solving debouncing • There are several different ways switches can be ‘debounced’ (removing the bounce) • Hardware solution: Capacitors with a Schmitt trigger can be used to remove the bounce • Software solution: Use the switch to start a small delay then check the switch input – after this it should be debounced • For Arduinos an easy function is provided: Bounce() • Documentation: http://www.arduino.cc/playground/Code/Bounce • If you are using it at home you need to copy this folder into your libraries folder on your home PC

  13. This library makes debouncing really easy to do Functions to detect rising and falling edge Use Bounce(<Pin>,<debounceInterval>) to setup pin (5ms should be plenty for the debounce interval) When buttons are pressed falling edge generated – so use fallingEdge() function Use update() before reading risingEdge() or fallingEdge() to update the result Bounce()

  14. Bounce() example #include <Bounce.h> int sw_input = 7; Bounce debounce1 = Bounce(sw_input, 5); //Button to be debounced void setup() { pinMode(sw_input, INPUT); Serial.begin(9600); } void loop() { debounce1.update(); //Sample button value if(HIGH == debounce1.fallingEdge()){ //If button has been pressed Serial.println(“Button Pressed”); //Send message } }

  15. Class Quiz • A switch is connected to pin 7 • A LED is connected to pin 13 • Draw a circuit sketch of this circuit • Write code using pinMode() to setup the pins • Use the bounce function to toggle the LED on and off each time the button is pressed (if button is pressed 3 times the LED should go on-off-on) • If the button is held down the LED should keep the same value

  16. Summary(What you learnt in this session) • Switches are interfaced to the Arduino with a pull-up resistor • digitalRead() is used to check the voltage on a single pin • If the number of presses of a switch is important debouncing is required – use bounce()

More Related