ENGR 40M Project 3c: Switch debouncing

Similar documents
ENGR 40M Project 3c: Coding the raindrop pattern

Which LED(s) turn on? May 4, 2018 E40M Spring 2018 T. Diamandis, J. Plummer, R. Howe, C. Z. Lee 1

Which LED(s) turn on?

E85: Digital Design and Computer Engineering Lab 1: Electrical Characteristics of Logic Gates

University of Hull Department of Computer Science C4DI Interfacing with Arduinos

Introduction. Overview. Top-level module. EE108a Lab 3: Bike light

Note. The above image and many others are courtesy of - this is a wonderful resource for designing circuits.

LDR_Light_Switch1 -- Overview

ENGR 40M Project 4b: Displaying ECG waveforms. Lab is due Monday June 4, 11:59pm

Robotics and Electronics Unit 5

LDR_Light_Switch5 -- Overview

Part 1. Summary of For Loops and While Loops

Arduino IDE Friday, 26 October 2018

MAE106 Laboratory Exercises Lab # 1 - Laboratory tools

TANGIBLE MEDIA & PHYSICAL COMPUTING MORE ARDUINO

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

In math, the rate of change is called the slope and is often described by the ratio rise

CSC 258 lab notes, Fall 2003

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

Chapter Operation Pinout Operation 35

Halloween Pumpkinusing. Wednesday, October 17, 12

Digital Circuits. Page 1 of 5. I. Before coming to lab. II. Learning Objectives. III. Materials

Spectroscopic Analysis: Peak Detector

LDR_Light_Switch2 -- Overview

Touch Board User Guide. Introduction

ECGR 4101/5101, Fall 2016: Lab 1 First Embedded Systems Project Learning Objectives:

Tutorial 3: Using the Waveform Viewer Introduces the basics of using the waveform viewer. Read Tutorial SIMPLIS Tutorials SIMPLIS provide a range of t

CS/EE 3710 Computer Architecture Lab Checkpoint #2 Datapath Infrastructure

2.1 Transforming Linear Functions

Microcontrollers and Interfacing week 8 exercises

Lab 02 Arduino 數位感測訊號處理, SPI I2C 介面實驗. More Arduino Digital Signal Process

I/O handling. Purpose. Seven-segment displays. Digital Systems 15 hp

Finite State Machine Lab

Which LED(s) turn on? May 12, 2017 ENGR 40M Spring 2017 C.Z. Lee, J. Plummer, R. Howe 1

1.7 Limit of a Function

Physics 120/220 Lab Equipment, Hints & Tips

Advanced Test Equipment Rentals ATEC (2832)

Digital Design and Computer Architecture

NAME EET 2259 Lab 3 The Boolean Data Type

The first thing we ll need is some numbers. I m going to use the set of times and drug concentration levels in a patient s bloodstream given below.

Photoresistor, Transistor, and LED s

How-To: Make an RGB combination door lock (Part 1)

EE 314 Spring 2003 Microprocessor Systems. Parallel Printer Port Use and Digital-to-Analog Conversion

Strategic Series-7001 Introduction to Custom Screens Version 9.0

Arduino Workshop. Overview. What is an Arduino? Why Arduino? Setting up your Arduino Environment. Get an Arduino based board and usb cable

IME-100 ECE. Lab 3. Electrical and Computer Engineering Department Kettering University. G. Tewolde, IME100-ECE,

Digital Storage Oscilloscope

Lesson 5: Verifying RAMs with the Fluke 9010A Version 1.03

Welcome to Lab! Feel free to get started until we start talking! The lab document is located on the course website:

The first program: Little Crab

Welcome Back! Without further delay, let s get started! First Things First. If you haven t done it already, download Turbo Lister from ebay.

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

Anatomy of a Standard Transcript

4. Java Project Design, Input Methods

Assignment III: Graphing Calculator

Fall Harris & Harris

Getting Started Guide

Welcome to Lab! You do not need to keep the same partner from last lab. We will come around checking your prelabs after we introduce the lab

Software debouncing of buttons

Chapter01.fm Page 1 Monday, August 23, :52 PM. Part I of Change. The Mechanics. of Change

Interrupts, timers and counters

An introduction to plotting data

Sensors and Motor Control Lab

UV Mapping to avoid texture flaws and enable proper shading

HOUR 4 Understanding Events

Introduction to Arduino Diagrams & Code Brown County Library

University of Florida EEL 3701 Dr. Eric M. Schwartz Department of Electrical & Computer Engineering Revision 0 12-Jun-16

Using the KD30 Debugger

There are a number of ways of doing this, and we will examine two of them. Fig1. Circuit 3a Flash two LEDs.

This lesson introduces Blender, covering the tools and concepts necessary to set up a minimal scene in virtual 3D space.

CS12020 (Computer Graphics, Vision and Games) Worksheet 1

Introduction to Arduino Diagrams & Code Brown County Library

SketchUp Quick Start For Surveyors

Adapted from a lab originally written by Simon Hastings and Bill Ashmanskas

ARM: Microcontroller Touch-switch Design & Test (Part 1)

Arduino notes 3: Digital input via buttons

Name EGR 2131 Lab #6 Number Representation and Arithmetic Circuits

WINDOWS MOVIE MAKER 6.0. A Technology Tutorial

Course materials and schedule are at. This file:

Pong in Unity a basic Intro

Hello, welcome to creating a widget in MyUW. We only have 300 seconds, so let s get going.

Necessary software and hardware:

PAD ANALOG / DIGITAL TRAINER OPERATOR S MANUAL

Micro-Controllers. Module 2: Outputs Control and Inputs Monitoring. IAT Curriculum Unit PREPARED BY. August 2008

University of California at Berkeley College of Engineering Department of Electrical Engineering and Computer Science. EECS 150 Spring 2000

HiJack Hello world! Project

Construction Construction Instructions

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

MintySynth Software Manual v. 4.2

MEDIS Module 2. Microcontroller based systems for controlling industrial processes. Chapter 4: Timer and interrupts. M. Seyfarth, Version 0.

Arduino Prof. Dr. Magdy M. Abdelhameed

CSE 410 Final Exam 6/09/09. Suppose we have a memory and a direct-mapped cache with the following characteristics.

Connecting Arduino to Processing

COPYRIGHTED MATERIAL. Starting Strong with Visual C# 2005 Express Edition

CSE P567 - Winter 2010 Lab 1 Introduction to FGPA CAD Tools

Upgraded Software for the W8TEE/K2ZIA Antenna Analyzer - Version 03.0 John Price WA2FZW

Part 6b: The effect of scale on raster calculations mean local relief and slope

LeakDAS Version 4 The Complete Guide

Lab 8 - Vectors, and Debugging. Directions

The Universal Translator

Transcription:

ENGR 40M Project 3c: Switch debouncing For due dates, see the overview handout 1 Introduction This week, you will build on the previous two labs and program the Arduino to respond to an input from the environment or users. There are a number of things you can do. This handout covers one of these options. Specifically, in this lab, you will learn about switch debouncing. Ideally, when you press (or flick) a switch, the connection would be made or broken cleanly and exactly once. It turns out that in real switches, the contacts bounce off each other many times. This phenomenon is called switch bouncing, and the act of working around it is called switch debouncing. The goal of this lab is to get you on the road with using pushbutton switches correctly, and it is shorter than the music-responsive cube lab. That leaves you more time to implement something cool with your Arduino, and we will expect you to do so. See the lab overview handout (a separate handout) for some ideas. By completing this lab you will: Learn how to use the oscilloscope. Observe switch bouncing in action and understand why it needs to be avoided. Learn how to debounce a switch. 2 Prelab and new equipment 2.1 Connecting a switch P1: Draw a schematic for how you would connect a switch to your Arduino. The schematic should show the switch, the Arduino input pin, and any other components (if any) to interface the switch with the Arduino. Use the least number of other components you can. (Hint: You ve seen this circuit before.) The pushbutton switches we have look like the one in the image below. (We have a few colors.) Notice that it has four pins, but only two connections: pins 1 and 3, and pins 2 and 4, are each connected inside the

switch. It s not clear from the photo, but the flat side in the schematic (on the right) corresponds to a flat side on the switch, a bit like the cathode of an LED (if you ve noticed the flat side of an LED before). P2: How would you orientate your switch on the breadboard? Draw the position of the switch on the breadboard. Make the rows of the breadboard clear. Label the flat side. P3: Make sure there s enough space on your breadboard to support the switch! You don t need to submit anything for this question, it just pays to be prepared. P4: Decide what you re going to use your buttons for! There are some ideas in section 2.2 of the lab overview handout. 2.2 Oscilloscope An oscilloscope is one of the most useful tools for electrical engineers in debugging electronic circuits. At its core, an oscilloscope makes plots of voltage over time. This is very useful for trying to understand circuits that have rapidly changing voltages. While an oscilloscope has an intimidating number of buttons and knobs, its basic operation is straightfoward. The diagram below shows the front panel of the Agilent 54621A (which is very similar to the DSO6012 that we have in the lab), but all oscilloscopes have a similar set of controls. By the way: You might notice a button labelled Auto-scale. Don t use the Auto-scale button! While this can be a convenient shortcut, it often fails and gives you something that you didn t want. More importantly, it short-circuits the process of thinking about what you expect to see. Too many students press auto-scale and assume that whatever they see must be the right answer. Once you get the hang of it, adjusting the scope manually will be nearly as fast, and far more likely to get you the right answer. 2

Time (X-axis) controls Scale Offset Trigger controls Scale On/Off Offset Signal input Voltage (Y-axis) controls (2 independent channels) Time (X-axis, Horizontal) controls: The large knob controls the scale of the X-axis. The time covered in one vertical grid division is shown on the display (e.g, 10 ns, 50 µs). The small knob shifts the signal left and right. The offset from the center is shown on the display. Voltage (Y-axis, Vertical) controls: The BNC connector at the bottom is where you connect your probe. The On/Off button (the one with the number on it) toggles whether the channel is displayed. The button lights up when the channel is active. The large knob controls the scale. The display indicates the voltage per grid division. The small knob shifts the waveform up and down. The offset from the middle is shown on the display. The trigger tells the oscilloscope when it should start its plot of voltage versus time. You set a trigger event, and it will center that event on the display. It s easiest to describe these by example: Trigger when the voltage goes from below 3 V to above 3 V. Trigger when the voltage crosses 0 V in either direction. Trigger when the voltage stays below 1 V for at least 5 ms. To set a trigger: The little knob controls the trigger level, the voltage threshold the oscilloscope uses to judge an event. The buttons Edge, Pulse Width and so on are different types of triggers. In our work, we will always want to use the edge trigger. An edge trigger is when the voltage goes from below the trigger level to above it ( rising edge ), or from above the trigger level to below it ( falling edge ). These buttons all open menus which allow you to adjust other details about the trigger. For example, if you press Edge, you ll pull up the Edge Trigger menu, which allows you to select which channel to monitor and whether to trigger on a rising edge, falling edge or either. 3

The oscilloscope will horizontally align the plot so that the trigger event happens at t = 0. You adjust where t = 0 is on the display using the time offset knob (the small knob in the Horizontal section described above). Don t worry if triggering doesn t make sense now; it ll make more sense when you see it in action. 3 Investigating switch bouncing 3.1 What does the Arduino see? The Arduino program switch bounce.ino, available on the website, checks the state of the button on every loop(), and prints to the Serial Monitor every time it detects the button being pressed. It also prints the time since the last press. 1. Add a button switch to your breadboard and connect it an unused pin on your Arduino. 2. Download the file switch bounce.ino from the website, and load it into your Arduino. Modify the constant BUTTON to reflect the input pin that you re using. 3. Open the Serial Monitor, and set its baud rate to 115200. Press the button a few times. Does it do the same thing every time? Keep pressing until you re satisfied you have a good idea of what s happening. L1: Does the counter increment by one every time you press the button? Describe what happens. Does this seem weird? Maybe we should have a closer look at what s going on with this switch. 3.2 Connecting the oscilloscope 1. Plug an oscilloscope probe into channel 1 of the oscilloscope, and connect the probe to your Arduino input pin. (Alligator clips and bits of wire might be useful here.) 4

2. Make sure channel 1 on the oscilloscope is on, and channel 2 is off. (The light for channel 1 should be lit, and the light for channel 2 should not.) Set up the oscilloscope to inspect a signal that varies between 0 V and 5 V. You ll need to adjust the vertical scale and offset for channel 1 to do this. Adjust the time scale about 5 ms (the exact value doesn t matter, as long as it s around that range), and the time offset to zero. 3. Press the button, and verify that the signal jumps between 0 V and 5 V. 3.3 Setting the oscilloscope trigger At this stage you ve got the oscillscope to plot the voltage versus time, but it s doing it continuously, so most of the time it s just plotting when the switch is on or off. It s not very interesting just to plot voltage versus time when the switch is in a constant state. What we re interested in is the transition from on to off, or off to on. So we ll use the trigger to help us focus on this (as described in section 2.2). The trigger event we want is: Trigger when the voltage crosses 2.5 V in either direction. This way, we can catch the switch turning on or off. 1. Set the trigger level to roughly 2.5 V. (The exact value doesn t matter, so long as it s somewhere between 2 V and 3 V.) 2. Press the Mode/Coupling button in the trigger section of the oscilloscope. It will display a menu at the bottom of the screen. Use the buttons along the bottom of the screen: Set the Mode to Normal. It s normally in Auto. In Auto, the oscilloscope constantly plots voltage versus time even if the trigger event does not occur. In Normal, the oscilloscope will not generate new plot until another trigger event occurs. Set the Coupling to DC. 3. Press the Edge button in the trigger section of the oscilloscope. Then, using the menu at the bottom of the screen: Set the Source to channel 1. Set the Slope to Either. (This is the setting for in either direction. The others mean goes from below the trigger level to above, above to below and whichever direction did not happen last time, respectively.) 5

Now press the button a few times. Every time you press the button, a new plot should be displayed on the oscilloscope. The plot should show the voltage on the input pin going from HIGH to LOW, or vice versa. If this were an ideal switch, it would just transition once, precisely when you hit the button. But zoom in horizontally: adjust the time scale to about 100 µs. On this time scale, does it look like a clean transition? L2: Draw a typical waveform. Don t forget to draw and label your axes, so we have some idea of the time scale you re looking at. It only needs to be approximate. L3: The Arduino processor runs at 16 MHz, that is, it processes 16 million instructions per second. How long does one instruction take? In a single invocation of loop(), your Arduino processes a lot more than one instruction, of course. But it s still pretty fast compared to a human, so it can probably read the switch every few microseconds or so, depending on how much the loop is doing. 6

L4: Press the button a few more times and see how long you can get the bouncing to last. You might like to adjust the time scale and offset to get a better view of all the bounces. How long can you get the bounces to last? Draw the waveform for your worst case bouncing. 4 Optional: Did we catch all the bounces? This section is optional. It uses external interrupts, which are beyond the scope of ENGR 40M. However, knowledge of interrupts is not necessary to complete this section, so long as you believe the code does what we say it does. Skip to section 5 if you don t want to do this section. The loop in switch bounce.ino actually does quite a lot: it turns out that printing to the Serial Monitor takes a long time. This seems unavoidable, but it turns out there s an advanced technique you can use to make sure you never miss a switch transition. They re called external interrupts 1. The file switch bounce interrupt.ino uses them to make sure it counts every single switch bounce. For this to work, your switch must be connected to pin D2 or D3. You might have to temporarily disconnect one of your anode/cathode connections to do this. 1. Download switch bounce interrupt.ino. Modify the constant BUTTON to either 2 or 3, depending on which input pin you re using. Upload it into your Arduino. 2. Run it in the Serial Monitor. It won t print on every press: printing takes time, so if it did this, it would miss some switch bounces. But it will print a cumulative count of how many bounces it s detected. (Each transition from HIGH to LOW, sometimes called a falling edge, is counted.) Look at how many bounces there are! 1 For the curious: An interrupt is when some event causes the Arduino program to stop dead in its tracks, run a specially dedicated function (interrupt service routine), and then return to what it was doing. In our case, the interrupt event is The input on pin D2 changed state from HIGH to LOW, and our interrupt service routine increments a counter. If this didn t make sense to you, don t worry about it. Or if you re still curious, ask your TA. 7

5 Debouncing the switch Debouncing a switch is simple in concept: you just refuse to read the switch input until it s been stable for some short time, which we ll call DEBOUNCE DELAY. We ve set DEBOUNCE DELAY to 75 ms, but you should feel free to play with this value to see what happens. In more detail, our method is: On every switch transition, reset a timer. When, and only when, the timer hits DEBOUNCE DELAY, accept the current switch state. We ve provided code that does this in switch debounce.ino. 1. Download the file switch debounce.ino from the website, and load it into your Arduino. Modify the constant BUTTON to reflect the input pin that you re using. 2. Press the button a few times. Verify that the counter increases exactly once each time you press it. Take a moment to read the code and understand what s going on. 3. Increase DEBOUNCE DELAY and upload the modified program. L5: How big can DEBOUNCE DELAY be before it stops working effectively? Describe what happens, and explain why it stops working for values of DEBOUNCE DELAY that are too large. Hint: Try pressing the button repeatedly really fast. 4. Decrease DEBOUNCE DELAY and upload the modified program. L6: How small can DEBOUNCE DELAY be before it stops working effectively? Describe what happens, and explain why it stops working for values of DEBOUNCE DELAY that are too small. Hint: Try hitting the button more aggressively (not necessarily repeatedly). 8

6 Onwards and upwards Now that you ve learnt about switch debouncing, you can add buttons or other switches to your circuit and get your LED cube to interact with users! We deliberately have not provided template code that includes both switch debouncing and the LED cube driver. We leave it to you to merge the two together. If you hand in this lab (rather than the music one), we expect you to implement something fairly complex with your Arduino. See our style rubric in the lab overview handout for guidelines on this. 7 Analysis (optional) The analysis section of the debouncing lab is optional. interesting, but you don t need to do it. We ve left it in from previous years because it s A1: We had two switches in the useless box, and debounced neither of them. Why did this not turn out to be a problem? Debouncing a switch is simple in concept, but getting the implementation right can be tricky. Here are a few bad ways to debounce switches. See if you can figure out why they won t work. For each one of these, describe what would happen, and why it does not detect one switch per user press like we expect it to. In all of these code extracts, assume the following appears before the loop() function: const int BUTTON = 2; // button pin const int DEBOUNCE_DELAY = 75; // in milliseconds void setup() { pinmode(button, INPUT_PULLUP); Now, have a look at these attempts to debounce switches: void loop() { static byte lastbuttonstate = HIGH; byte buttonstate1 = digitalread(button); 9

byte buttonstate2 = digitalread(button); byte buttonstate3 = digitalread(button); if (buttonstate1 == buttonstate2 && buttonstate2 == buttonstate3 && lastbuttonstate!= buttonstate2) { // register button state and do something lastbuttonstate = buttonstate2; A2: Why won t the above code debounce the switch? void loop() { static byte lastreading = HIGH; static unsigned long lastfalltime = millis(); byte reading = digitalread(button); unsigned long now = millis(); if (lastreading == HIGH && reading == LOW) { if (now - lastfalltime > DEBOUNCE_DELAY) { // register a press and do something lastfalltime = now; lastreading = reading; A3: The above code will debounce the user pressing the button, but it will also register spurious presses at another time. When, and why? This next code uses the display() function you wrote for the LED cube in lab 3b. 10

void loop() { static byte values[4][4][4]; static byte lastbuttonstate = HIGH; byte buttonstate1 = digitalread(button); delay(debounce_delay); byte buttonstate2 = digitalread(button); if (buttonstate1 == buttonstate2 && lastbuttonstate!= buttonstate2) { // register button state and do something to values[][][] display(values); // display a pattern on the LEDs lastbuttonstate = buttonstate2; A4: The above code will debounce the switch fine, but what is the problem with using the delay() statement like that? 11

8 Reflection Individually, answer the questions below. Two to four sentences for each question is sufficient. Answers that demonstrate little thought or effort will not receive credit! R1: What was the most valuable thing you learned, and why? R2: What skills or concepts are you still struggling with? What will you do to learn or practice these concepts? R3: If this lab took longer than the regular 3-hour allotment: what part of the lab took you the most time? What could you do in the future to improve this? 12