+ All Categories
Home > Documents > EMS1EP Lecture 6 Digital Inputs Dr. Robert Ross. Overview (what you should learn today) Hardware:...

EMS1EP Lecture 6 Digital Inputs Dr. Robert Ross. Overview (what you should learn today) Hardware:...

Date post: 01-Apr-2015
Category:
Upload: alexia-reyes
View: 212 times
Download: 0 times
Share this document with a friend
Popular Tags:
16
EMS1EP Lecture 6 Digital Inputs Dr. Robert Ross
Transcript
Page 1: EMS1EP Lecture 6 Digital Inputs Dr. Robert Ross. Overview (what you should learn today) Hardware: Connecting switches to the Arduino Revision of setting.

EMS1EP Lecture 6Digital Inputs

Dr. Robert Ross

Page 2: EMS1EP Lecture 6 Digital Inputs Dr. Robert Ross. Overview (what you should learn today) Hardware: Connecting switches to the Arduino Revision of setting.

Overview (what you should learn today)

• Hardware: Connecting switches to the Arduino

• Revision of setting pin directions

• Using digitalRead()

• Debouncing Switches

Page 3: EMS1EP Lecture 6 Digital Inputs Dr. Robert Ross. Overview (what you should learn today) Hardware: Connecting switches to the Arduino Revision of setting.

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

Page 4: EMS1EP Lecture 6 Digital Inputs Dr. Robert Ross. Overview (what you should learn today) Hardware: Connecting switches to the Arduino Revision of setting.

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)

Page 5: EMS1EP Lecture 6 Digital Inputs Dr. Robert Ross. Overview (what you should learn today) Hardware: Connecting switches to the Arduino Revision of setting.

Switch connection to the LArduino

• R = 10K• Switch connected to a digital input

Page 6: EMS1EP Lecture 6 Digital Inputs Dr. Robert Ross. Overview (what you should learn today) Hardware: Connecting switches to the Arduino Revision of setting.

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

Page 7: EMS1EP Lecture 6 Digital Inputs Dr. Robert Ross. Overview (what you should learn today) Hardware: Connecting switches to the Arduino Revision of setting.

pinMode() function

• Syntax:pinMode(<pin_number>, <direction>)

• Normally some pins will be assigned at the top of your code

• e.g. – int LED1Pin = 10;– int switchPin = 11;

• These assignments are used for the pin number• Direction should be OUTPUT or INPUT• e.g.

– pinMode(LED1Pin, OUTPUT);– pinMode(switchPin, INPUT);

Page 8: EMS1EP Lecture 6 Digital Inputs Dr. Robert Ross. Overview (what you should learn today) Hardware: Connecting switches to the Arduino Revision of setting.

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:int digitalRead(<Pin_Number>);

• e.g. int pinValue;//Check if voltage on pin is high or lowpinValue = digitalRead(sw1Pin); If(low == pinValue){//do something – switch is pressed

}else{//do something – switch not pressed

}

Page 9: EMS1EP Lecture 6 Digital Inputs Dr. Robert Ross. Overview (what you should learn today) Hardware: Connecting switches to the Arduino Revision of setting.

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

Page 10: EMS1EP Lecture 6 Digital Inputs Dr. Robert Ross. Overview (what you should learn today) Hardware: Connecting switches to the Arduino Revision of setting.

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

Switch pressed

Bouncing

Contacts settled

Page 11: EMS1EP Lecture 6 Digital Inputs Dr. Robert Ross. Overview (what you should learn today) Hardware: Connecting switches to the Arduino Revision of setting.

Switch bounce

Switch pressed

Bouncing

Contacts settled

Page 12: EMS1EP Lecture 6 Digital Inputs Dr. Robert Ross. Overview (what you should learn today) Hardware: Connecting switches to the Arduino Revision of setting.

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

Page 13: EMS1EP Lecture 6 Digital Inputs Dr. Robert Ross. Overview (what you should learn today) Hardware: Connecting switches to the Arduino Revision of setting.

Bounce()

• 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

Page 14: EMS1EP Lecture 6 Digital Inputs Dr. Robert Ross. Overview (what you should learn today) Hardware: Connecting switches to the Arduino Revision of setting.

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 valueif(HIGH == debounce1.fallingEdge()){ //If button has been pressed

Serial.println(“Button Pressed”); //Send message}

}

Page 15: EMS1EP Lecture 6 Digital Inputs Dr. Robert Ross. Overview (what you should learn today) Hardware: Connecting switches to the Arduino Revision of setting.

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

Page 16: EMS1EP Lecture 6 Digital Inputs Dr. Robert Ross. Overview (what you should learn today) Hardware: Connecting switches to the Arduino Revision of setting.

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()


Recommended