Arbitrary PWM frequency while using SPI?

Hi all,
I have again a rather general question, however it seems to me that it is not entirely easy to answer (hopefully somebody proves me wrong instantly :wink: )

Due to a research project of my university, I’m in search of a micro-processor which can deal with the following demands:

  • read out data from a compass via the serial interface
  • react on these data via a PWM signal (specified to have a time-resolution of at least 64 micro-seconds / i.e. a frequency of 15.625 kHz
  • logging of the compass-data to a MMC/SD-Card (about 10MB per day). This always seems to involve the SPI-protocol.

Now my question: Since SPI is used on pins 10-13 (at a minimum), and I don’t want to change the PWM frequency of timer0 (pins 5,6) since this is the “main clock”, controlling at least the delay() and/or millis() function, it seems only pin 3 is left for PWM at the requested frequency!

The problem I thought about might be that changing timer2, which controls pwm frequencies for pin3 and pin11 might lead to side-effects, so that SPI wouldn’t work properly anymore…

Is this valid thinking? Does somebody know for sure that setting the PWM frequency on pin 3 does not interfere with SPI-settings involving pin 11? It seems to me that this is completely decidable with Atmel’s datasheet, but I’m no programmer and might miss something important… so please help me!

PS: The threads I have my information from:
Regarding PWM frequencies: and

Regarding Logging to Cards via SPI:

so that SPI wouldn’t work properly anymore.

No SPI does not require a specific frequency to operate. The master supplies the clock so it can be virtually anything so don’t worry about changing the clock speed and messing up the SPI.

omg thank you - then I’ll just start coding! Finally ;D

It seems that I am working on a similar project. I am worried about pin count as well since we will be using two encoders which take up pins 2-5 (2 and 3 must be used since they allow for attachInterrupt function, 4 and 5 can be moved if needed), two motors which need PWM (so will prob use pins 6,9), and SPI digital pins 10-13 to read values from our digital compass. Somehow, we will also need to control up to 9 servos. I’m praying we won’t need a second microcontroller, but I can’t see this working out nicely. I know that the analog pins can be converted to digital output pins if needed, but will these be able to control motors or servos with PWM? Any suggestions on how to lay out my Arduino would be appreciated.

Also, I noticed that you were using a digital compass controlled by SPI communication. I’m new to SPI and have to write an Arduino sketch to read in heading values from our digital compass ( I was wondering if I could see a copy of your code (if you got it working) so that I could write my own program. The only SPI examples I’ve seen so far was for a pressure sensor ( which did not require any configuration. There are several functions for our compass to set configuration and get Data but I don’t know if there is a library that establishes these functions or if I’ll have to write them myself. Any help would be very much appreciated!
Thank you,

Hi Lauren,
I’m afraid I cannot help you much with your compass code… we only need SPI for logging our data - i.e. for communication with an SD-Card. The implementation of the SPI protocol would most probably be the same in your case though, so you might look it up at some of these (numerous!) projects using MMC/SD-Cards.

9 Servos sounds tough if they all need independent PWM signals - but I’m really not the person to judge this. All I know is that the 6 PWM-capable pins are called like this because it is a feature of the ATmega168 itself, so it is at least convenient to use PWM on these. I simply don’t know whether it is possible to emulate PWM signals with software on other pins… most probably not arbitrary many with really high frequencies… but maybe you don’t need high frequencies with servos/motors?

Sorry, that’s all I can say about this…