I've got an upcoming project that will require servos, PWM, and SPI on an Arduino Nano:
- four servo outputs (via Servo library)
- four PWM outputs (via AnalogWrite for 5v laser diodes)
- SPI (for Ethernet via ENC28J60 module and EtherCard library)
The Arduino page for AnalogWrite says that digital pins 9 and 10 cannot be used for PWM when the Servo library is in use. This leaves pins 3, 5, 6, and 11 for PWM. In my case, however, pin 11 is already taken by the SPI interface, leaving me short one PWM output.
Is there a way to get the Servo library to disable a different pair of PWM pins instead, such as pins 3 and 11? I suspect it has something to do with the internal oscillators (timers, right?) for the ATMega microcontroller. The pinout suggests that:
Digital pin 3 = OSC2B
Digital pin 5 = OSC0B
Digital pin 6 = OSC0A
Digital pin 9 = OSC1A
Digital pin 10 = OSC1B
Digital pin 11 = OSC2A / SPI MOSI
The pairings make sense, so I'm assuming there's a connection here with the Arduino timer functions, but this is diving just a little deeper into the internal workings of the ATMega chip than my experience has gone before. I poked around a little in the Servo library and found that it has a "#define _useTimer1" that seems relevant to the Nano's ATMega328P controller.
Is Timer1 the same as (or at least associated with) OSC1? If so, can the Servo library use a different timer, say OSC2? Are there pros/cons to each timer?
Worst-case scenario, I'll look into using something like a PCA9685 PWM controller via I2C, but I was hoping not to since the Nano already has enough output pins and space is at a premium.
I'll keep doing research and experimentation, but if someone would like to offer a nudge in the right direction, it would be appreciated!