Problem using LED and SD Card - Why?

Hello,

I have just got a W5100 Ethernet Shield. The shield comes with SD card etc.

So just playing with it I noticed a problem of the pins being set high and not able to be used when trying to initialise the SD card.

OK, I have a simple setup, an LED (with resistor) off Pin 11 and then to Gnd

I am using the following code.

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
                  
int LEDPin = 11;
const int chipSelect = 4;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Initializing SD card...");
  if (!SD.begin(chipSelect)) {
  return;
  }
  Serial.println("card initialized.");
}

void loop() {
    analogWrite(LEDPin, 0);                     
}

When the code has been uploaded the LED immediate lights up with full brightness. Why? when I have set the LED pin to 0

If I comment out the following line and upload again, then it works fine.

if (!SD.begin(chipSelect)) {
  return;
  }

It appears to be the cmd SD.Begin that sets the others pins high..

Can anyone shed a light on why. this happens.

#include <SPI.h>which pins are used by SPI ? (hint look for MOSI)

I have had read and understand a bit about SPI. So when using the SD card I do not have access to the pins 13,12,11 and 4 for other tasks like controlling my LED

I am using data from a sensor to control the brightness of an LED and storing that sensor value on the SD Card in a file.

So I have tried using other pins for controlling my LED but none other them allow me to control the brightness. Using digital just allows high/low.

Any ideas or is it best to try something else.

Have you tried analogWrite(pinNumber, brightness);
with pinNumber = any of 3,5,6,9,10,11 that aren't in use
and brightness = 0 to 255?

SniffTheGlove:
I have had read and understand a bit about SPI. So when using the SD card I do not have access to the pins 13,12,11 and 4 for other tasks like controlling my LED

So you know pin 11 is useful for SPI, you know SPI will do things in the background and thus you do

int LEDPin = [color=red]11[/color];
...

void loop() {
    analogWrite(LEDPin, 0);                     
}

Puzzling....


I have just got a W5100 Ethernet Shield. The shield comes with SD card etc.

I would suggest that at the very beginning of setup()

you set pin 10 as an output and set it HIGH to disable the ethernet part of the module. That will make a difference