Appendix F. Arduino Pin and Timer Usage

The tables in this section show the pin and timer resources used by the projects in this book. You can use the same pin assignments for the Leonardo boards or the standard ATmega328 boards such as the Uno. However, there are subtle low level differences between these boards, so if you are adding capabilities that use additional pins or resources beyond those described in this book, then check the documentation on pin and resource usage for your board.

Handling Resource Conflicts

The Arduino chip has a rich collection of hardware resources, but you can run up against a conflict if a feature you are adding requires a hardware resource that some other feature is already using. A resource conflict occurs when a function reconfigures or requires exclusive access to some hardware capability. Running out of analog or digital pins is one kind of resource conflict, usually easy to spot.

More subtle is a conflict caused by a library that requires a resource such a hardware timer that is already used by some other function. For example, a motor shield uses PWM to control motor speed and each motor requires a timer component. Arduino tries to hide the underlying hardware (one of the things that makes it easy to use) but this can result in things going wrong when a resource conflict does occur. Sometimes the compiler will report a problem with an error message about a resource conflict. But sometimes the sketch will compile without an error message even though a resource conflict is preventing the code from functioning as expected.

For example, the infrared remote control library uses a timer to decode pulses in the background. If this is the same timer used by some other function, say the Arduino Servo library, one or both of these libraries will malfunction. The solution is to either reassign one of the libraries to use a different timer, or to find an alternative way to perform one of the functions without a timer. Both of these approaches will be discussed in this appendix.

Modifying a Library to Change Timer Allocation

Modifying a library is not a task for a beginner, but some libraries are designed to allow configuration. For example, the irRemote library used in Chapter 11, Remote Control has a file named irRemoteInt.h that can be edited to change the timer used by this library. Here are fragments of this file that determines the timer used by the library:

// Leonardo or Teensy 2.0
#elif defined(__AVR_ATmega32U4__)
  //#define IR_USE_TIMER1   // tx = pin 14
 // #define IR_USE_TIMER3   // tx = pin 9   
  #define IR_USE_TIMER4_HS  // tx = pin 10

And further down the file:

// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, etc
#else
  //#define IR_USE_TIMER1   // tx = pin 9
  #define IR_USE_TIMER2     // tx = pin 3
#endif

The first code fragment determines the timer to be used with a Leonardo board (the Arduino build process will use the code in this fragment if the chip is an ATmega32U4). The uncommented line contains: #define IR_USE_TIMER4_HS which results in the library using Timer 4. However, Timer 4 is also used to control one of the motors in the 4WD robot. If you have the 4WD robot and want to use the infrared remote control library, you need to find a free timer to use. You can't easily change the motor library because the pin for Timer 4 is hard wired to the motor controller chip. But you can change the remote timer by commenting out the line for Timer 4 and uncommenting a line that enables a free timer. The Leonardo has 5 timers but as shown in Table F-2, only Timer 1 is available. The code to disable Timer 4 and enable Timer 1 is as follows:

// Leonardo or Teensy 2.0
#elif defined(__AVR_ATmega32U4__)
  #define IR_USE_TIMER1   // tx = pin 14
 // #define IR_USE_TIMER3   // tx = pin 9   
 // #define IR_USE_TIMER4_HS  // tx = pin 10

Using your text editor to make and save that change in irRemoteInt.h will eliminate the conflict by using Timer 1 instead of Timer 4.

If your 4WD robot uses an Arduino Uno, then the change is to remove the // comment characters before the IR_USE_TIMER1 line and add the comment characters before IR_USE_TIMER2

// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, etc
#else
  #define IR_USE_TIMER1   // tx = pin 9
  //#define IR_USE_TIMER2     // tx = pin 3
#endif

Writing Code That Avoids the Use of a Timer

Sometimes there is a conflict but no alternative resource to use. An example of this is if your infrared remote library is using Timer 1 (see previous section) and you also want to use the Servo library, which also uses Timer 1. If you have the 2WD robot with a Uno, then you could use Timer 2 for the remote library so the Servo library can remain on timer 1. There are no free timers available if you have a 4WD robot or the 2WD robot with a Leonardo board, but you can solve this conflict by adding some code that controls the servo without using a timer. See Adding Scanning for an example of how this can be done.

Pin and Timer Tables

The best way to handle hardware conflicts is to plan in advance by familiarizing yourself with the resources currently in use and the resources needed by the function you are adding. The tables in this appendix show the chip pins and timers used by the projects in this book. Although you will have some pins free after connecting up all the projects presented in this book, there may not be enough pins to connect all the optional sensors mentioned in Chapter 8, Tutorial: Introduction to Sensors along with some of the suggestions in the appendices. Use Table F-1 and Table F-2 to keep track of your pin and timer allocations.

Table F-1. Pin Usage

PinUsageComment
Digital 0 Serial Receive
Digital 1 Serial Transmit
Digital 2UnusedLeonardo can use this for I2C
Digital 3 Motor 2 PWMTimer 2b on Uno, Timer 0b on Leo (Leo uses this for I2C)
Digital 4Motor control 
Digital 5Motor 4 PWMTimer 0b on Uno, Timer 3a on Leo
Digital 6Motor 3 PWMTimer 0a on Uno, Timer 4d on Leo
Digital 7Motor control 
Digital 8Motor control 
Digital 9Scan Servoused in Chapter 10, Autonomous Movement
Digital 10Distance Sensorused in Chapter 10, Autonomous Movement
Digital 11Motor 1 PWMTimer 2a on Uno, Timer 0a or 1c on Leo
Digital 12Motor control 
Digital 13On-board LEDThis can be used as a digital pin if LED not needed
  

Analog 0Left Reflectance Sensor 
Analog 1Right Reflectance Sensor 
Analog 2Center Reflectance Sensor 
Analog 3IR Remote Decoderused in Chapter 11, Remote Control
Analog 4Optional Battery MonitorUno can use this for I2C
Analog 5Optional sound or proximity sensorUno can use this for I2C

Table F-2. Timer Usage

TimerUno 2WDUno 4WDLeo 2WDLeo 4WD
Timer 0 PWM for motors 3 & 4 PWM for motor 1 & 2PWM for motor 1 & 2
Timer1 IR RemoteIR RemoteIR RemoteIR Remote
Timer2PWM for motors 1 &2PWM for motors 1 &2Not availableNot available
Timer3Not availableNot available PWM for motor 4
Timer4Not availableNot available PWM for motor 3
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.179.193