Using the INT0 interrupt to count external pulses (also hardware debounce)

back to main tutorial page

Dr Nathan Scott & Dr Hiroyuki Kagawa · July 2002

Microcontrollers can sense the voltage levels on their pins and software can be written to respond to these levels. In this tutorial we will set up an AVR to respond to an external digital signal using an interrupt method.


Hardware

Set up your AVR and breadboard as for the TMR0 tutorial.

  • The only hardware addition needed at this point is a wire from PORTD, PIN2, which you can plug into either the GND or VCC lines. This will be our "digital input". Refer to the pinout diagram.
  • Compile the project int0.prj and download it to the AVR.

Figure 1 Example program that uses the INT0 interrupt

  • Now try moving the wire on INT0 (PORTD, PIN2). What do you observe?
  • Try touching the INT0 wire. What happens?

It is very likely that you will find that your LED flashes in an apparently random way. Sometimes it will turn on, sometimes it won't, every time you connect or disconnect the "digital input" wire. Why?


The problem of floating inputs

When a pin of the AVR is configured as an input pin, it has very high impedance which means it can easily pick up stray voltages from the surroundings, even radio signals. If an input pin is not connected to GND and it is not connected to VCC we say it is floating, and the signal that we see on the pin is called electrical noise.

It is thus very important to tie input pins to either GND or VCC so that there is a definite voltage on them.

Figure 2: example of an input which is tied low and another which is tied high.

Figure 3: switch noise and floating input noise when the INT0 wire is moved (apologies, figure shows AT90S8535, an older AVR.)


Digital noise

When a switch opens or closes, there is some switching noise, it is an unwanted electrical signal caused by the roughness and vibration of the metal surfaces inside a mechanical switch. The digital electronics is so fast that it can count hundreds or thousands of "HIGH" digital signals from a short burst of noise. The solution is to install a debounce circuit.

Figure 4: noise caused by switching or from a floating input is converted to a very fast digital noise signal.

Here is a simple circuit which greatly reduces switching noise:

Figure 5: a debounce circuit (tied low version) 

The circuit of Figure 5 is supposed to have the effect shown in figure 6:

Figure 6: desired effect of the capacitor debounce circuit of Figure 4

  • You should implement the circuit of Figure 5 and test it. I found that it did not work! The LED was unreliable, and sometimes the AVR would be reset! What was happening?

I had to go to the internet and do some research. I learned that most digital logic circuits do not like slowly changing inputs - this can cause rapid electrical oscillations. To reliably sense switch events we must condition the input signal using a device called a schmitt trigger. A convenient device that contains these circuits is the 74LS14 hex inverter with schmitt trigger input stages. This device converts a slowly changing input to a sudden, sharp voltage change at a certain voltage level, very much as shown in Figure 6. The device also inverts the signal which means that a low input signal causes a high output signal, and a high input causes a low output. This means that we must now ground the input of the 74LS14 (pin 1) to cause a low-to-high transition as shown in Figure 6.

Figure 7: practical debounce circuit i.e. should actually work

Here is what your breadboard would look like to implement the circuit of Fig. 7:

Figure 8: a reliable debounce circuit using a capacitor, resistors and 74LS14

The circuit of Figs.


When not to debounce

The debounce circuit has a capacitor which can be charged or discharged. The circuit tends to smooth the noise signal into a single gradually increasing or decreasing voltage. However the smoothing process tends to make the input slow to respond. It will mean that a debounced input cannot count a high frequency signal. The size of the capacitor will determine the maximum countable frequency.


Exercises

  • Set up a second debounce circuit and write a program which uses both (debounced) inputs for control. When one input is "pressed", the LED should flash at a rate of 5 times per second; when the other input is pressed it should stop.
  • Now connect up a motor (with MOSFET driver as in the power tutorial) and use your "go" and "stop" buttons to control it.
 

Dr Nathan Scott · nscott@mech.uwa.edu.au