Hardware Reference
In-Depth Information
Acquisition Accuracy
For certain fast acquisition tasks, interrupting is an absolute necessity. For
example, suppose that you are using a rotary encoder. Rotary encoders are
commonly mounted on direct current (DC) motors and send a pulse to the
microcontroller every time some percentage of a revolution is completed. You
can use them to create a feedback system for DC motors that allows you to keep
track of their position, instead of just their speed. This enables you dynamically
adjust speed based on torque requirements or to keep track of how much a DC
motor has moved. However, you need to be absolutely sure that every pulse is
captured by the Arduino. These pulses are fairly short (much shorter than a
pulse created by you manually pushing a button) and can potentially be missed
if you check for them by polling within loop() . In the case of a rotary encoder
that triggers only once per revolution, missing a pulse causes your program to
believe that the motor is moving at half of its actual speed! To ensure that you
capture timing for important events like this, using a hardware input is a must.
If you are using a slowly changing input (like a button), polling might suffice.
UnderstandingtheArduino'sHardwareInterrupt
Capabilities
With most Arduino boards, you can use only certain pins as interrupts. Interrupts
are referred to by an ID number that corresponds to a particular pin. The excep-
tion is the Due, on which all the pins can act as interrupts, and you reference
them by pin number. If you are not using the Due, consult TableĀ 12-1 to determine
what pins on your Arduino can act as interrupts and what ID number they are.
Table 12-1: Available Hardware Interrupts on Various Arduinos
BOARD
INT 0
INT 1
INT 2
INT 3
INT 4
INT 5
Uno, Ethernet
Pin 2
Pin 3
-
-
-
-
Mega2560
Pin 2
Pin 3
Pin 21
Pin 20
Pin 19
Pin 18
Leonardo
Pin 3
Pin 2
Pin 0
Pin 1
-
-
These IDs are used in conjunction with attachInterrupt() . The first argu-
ment is the ID (in the case of the boards in TableĀ 12-1) or the pin number (in the
case of the Due). If, on the Uno, you want to attach an interrupt to physical pin
2 on the board, the first argument of attachInterrupt() would be 0 because
pin 2 is attached to interrupt 0 on the Uno. The Uno (and other ATMega328-
based boards) support just two external interrupts, whereas the Mega and the
Leonardo support more external interrupts.
 
Search WWH ::




Custom Search