Go Down

Topic: problem with SD library and PWM at the same time (Read 1 time) previous topic - next topic

admd

Good day! I am using the Arduino to output PWM to switch a MOSFET gate, to be used for a buck converter. I am also using the Arduino to monitor the voltage output from the buck converter, and I want to log that voltage in an SD card.

However, it seems that PWM does not work when I write data to the SD card using the SD library. What happens is that when the output voltage data is written to the SD card, the PWM (from pin 11) turns off. The PWM signal will alternately appear and disappear when viewed in an oscilloscope. It seems that the Arduino is alternating between writing to the SD card and then back to the PWM output.

Here is a test code:

Code: [Select]
#include <SD.h>
const int chipSelect = 4;

File dataFile;

void setup(){
  Serial.begin(9600); 
  pinMode(11,OUTPUT);
  pinMode(10,OUTPUT);
  pwmSetup();
  dataFile = SD.open("datalog.txt", FILE_WRITE);
}

void loop(){
//  delay(1000);
  analogWrite(11, 200);
  dataFile.println("Hello World");
//  delay(2000);
//  analogWrite(11, 50);
//  delay(1000);
//  analogWrite(11, 200);
//  delay(2000);
//  analogWrite(11, 50);
//  delay(5000);
}

void pwmSetup(){
  //set PWM freq to 31250 Hz
  TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM20);
  TCCR2B = _BV(CS20);
  OCR2A = 0;
}


Can anyone point out what am I doing incorrectly?

Thanks

SurferTim

If you are using an Uno, you can't use digital pins 11-13 for anything but the SPI bus. SD is a SPI device.
D11 = MOSI
D12 = MISO
D13 = SCK

admd

Thanks SurferTim for answering. So I should just try the other PWM pins (3,5,6,9, and 10)? I'll post back with results. Thanks

admd

I changed the PWM output pin to 3, but I still get the same problem.

SurferTim

Your code does not start the SD. Try something like this:
Code: [Select]
void setup(){
  Serial.begin(9600); 
  pinMode(10,OUTPUT);
  pwmSetup();

  // call SD.begin first
  Serial.print("Starting SD...");
  if(!SD.begin(chipSelect)) Serial.println("failed");
  else Serial.println("ok");

  dataFile = SD.open("datalog.txt", FILE_WRITE);
}


Go Up