Interaction between the SD and Servo libraries

Hi, I'm working on a self-sailing boat, and I want to be able to operate servos and log the boat position on an SD drive. The problem I am having is that the action of writing to the SD card appears to intefere with the servo timers, causing servo jitter - the more I log, the worse it is. My understanding is that writing to the SD card is quite slow and during the write interrupts are disabled (is this true?) so that the timers no longer work and it messes up the PWM modulation signal to the servos.

I could buffer the writes and only do them occasionally, but I will still always get the problem, and I suspect (because ultimately I only have one power source) that thie jitter is causing spikes which cause infrequent crashes (about every 30 minutes). I know that if I increase the frequency of logging the crashes increase, and vice-versa.

Attached is a simple program which demonstrates the problem, based on the examples from the two libraries, using "sweep" as the basis. I can see a small "hiccup" every time the logging takes place - doing multiple writes in a loop here makes it more obvious, but even a single write is visible. I've also attached a circuit diagram.

I'm at the point of moving the driving of servos to a separate servo controller board, but just wanted to check that I need to do this, and there isn't some obvious software fix that will circumvent this problem.

#include <SPI.h>
#include <SD.h>
#include <Servo.h>

#define SERVO_PIN 5
#define CHIP_SELECT 4

Servo servo;
int pos = 0;    // variable to store the servo position
File dataFile;
char dataString[20] = "some data";

void setup() {

  // see if the card is present and can be initialized:
  if (!SD.begin(CHIP_SELECT)) {
    // don't do anything more:
    while (1);


void loop() {
  for (pos = 35; pos <= 125; pos += 1) {

  for (pos = 125; pos >= 35; pos -= 1) {

  for (int i=0; i < 10; i++) {
    dataFile ="datalog.txt", FILE_WRITE);
    if (dataFile) {


Not by default. If you use the SPI library (default behavior) interrupts should be always enabled and there shouldn't be any reason for the timer interrupts to fire too late.

If the sketch actually crashes you have to fix the hardware. Ensure you get enough power to drive the Arduino continuously, add capacitors to avoid the voltage drops.

OK thanks pylon. So if the interrupts are not being disabled, do you have any clues as to why writing to the SD card would cause servo jitter? I'm currently powering the SD card module from the Arduino 5v rail (there's nothing else coming off the rail) - could that be the problem?

I don't think so.
Do you have access to a scope? I would check the PWM output signal. That might give you a hint to the cause of the problem. Maybe changing the SPI frequency might soften the problem (given that calling the SPI interrupt too often is the cause of it).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.