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:
#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