Problem writing to SD card

Hello,

i am building something like a temperature data logger.

Every minute i want to write temperature on SD card. I am using millis function as a timer. I also have LCD to display temperature.

My problem is a little bit strange. If i power circuit over USB cable and with LCD backlight turned on, it works. If i put SD card into card reader i can see temperature in txt file. But if i unplug LCD back light or if i power the circuit over stable outside power supply, the txt file stays empty so temperature is no longer writing on SD card.

I am using Arduino Nano, DS18B20 sensor, 1 GB SD card and 2x16 LCD display. I tried to put some delay in function but result was the same.

Any ideas, what could be wrong?

Here is part of the code:

// SD CARD
           
      if ((long)(millis() - waitUntil) >= 0) 
      {
        
      myFile = SD.open("test.txt", FILE_WRITE);  // OPEN
      
      myFile.println(temp);  // WRITE
      
      myFile.close();  // CLOSE
      
      waitUntil = waitUntil + interval;  // WAIT UNTIL ANOTHER INTERVAL
      }

The symptoms seem to point towards a problem on the power supply - maybe the voltage is too high, too low, or not stable.

Can you use a blinking LED or similar to confirm that the sketch is continuing to run and has not been reset?

If i put LED and make it blink in this function (instead of part for writing on SD card) it blinks every interval time without problems (for a few milliseconds but it blinks constantly). Is that what you mean?

I will make outside power supply with LM7805 and try to power the whole circuit with it.

Not quite what I meant.

I was thinking that you would add a startup sequence that blinked the LEDs in some recognisable way at startup, and some different recognisable way while it was running normally, and in yet another recognisable way if it detected an error (such as SD initialisation or write failure).

This would let you find out whether the Arduino is successfully starting up and running without error and without being reset subsequently.

How is your SD card hooked up?
Diagram and picture.

I am using this SD module:

http://www.ebay.com/itm/SD-Card-Module-Slot-Socket-Reader-For-Arduino-ARM-MCU-Read-And-Write-/170817952203?pt=LH_DefaultDomain_0&hash=item27c58b19cb

With Arduino Nano it is connected on port 10, 11, 12 and 13.

MOSI - pin 11
MISO - pin 12
CLK - pin 13
CS - pin 10

It is powered up with 5V. I am using classic 1 GB Transcend SD (i formated it with official SD formatting tool).

I don't believe that there is a problem in program code because otherwise i can't understand how sometimes it is working.

Unbelievable just $2 free shipping. Unfortunately there is no schematic so I don’t know if the 3.3V logic is properly translated to 5V. Is there anything on the underside of the board? If not, there is no proper logic shifting. I only see a power supply-ish part and a few resistors.

I have the exact same SD card reader and it works fine as follows with a nano.
Dont forget you have to drive the SD card signals at 3.3V, so the best way to do this is run the whole system at 3.3v
I do this by connecting my input power >6V into the 5V input of the card reader, the card has its own 3.3 reg,
use this 3.3V output to drive the power to your Arduino. I connect it to the regulated 5V side (pin 27 nano) of the Arduino that way the system then runs at 3.3V. There are resistors on the board but they are just pullups to get a good edge and not level dividers so this card is designed to run at 3.3V signal levels.
For development I use one of those USB-TTL $4 devices that provides the 3.3v and not the onboard usb as this would drive the system at 5V, only problem is you have to hit the reset to do the download, no big deal.

I have the schematic some where but it's here for now SD Card reader: No Voltage required! - Practical UsagePractical Usage
This guy also isn't sure what he's doing powering it correctly.

Hope that helps.
I will try and help more if you dont understand.

... Simon

Simon,

You are technically overclocking your chip. At 3.3V the chip is not stable running 16MHz.

Liudr

Why would you say that, can you provide a link to further detail or explain it.

Many good authors Arduino Hacks - 3.3Vizing an Arduino have documented Arduinos running at 3.3V
Several clones come with a jumper so you can select the system voltage and the first page of the Atmel atmega328 http://www.atmel.com/devices/atmega328.aspx lists the operating voltage as being 1.8 to 5.5V

I think this is a good option rather than using level shifters and especially voltage dividers given that the original posters problems appears to be in this area.

... Simon

I will try this on monday because right now i am not in my workshop.. I will try to power the whole circuit on 3,3V, like you said, or use a few resistors.

I will reply as soon as i will try.

simoncastle:
Why would you say that

He's read the datasheet?

simoncastle:
, can you provide a link to further detail or explain it.

Datasheet section 28.3, figure 28.1

Thanks Fungus

Could you provide a more detailed reference or at least state the section heading

when I look in http://www.atmel.com/Images/doc8271.pdf

Section 28.3 is titled Signature Bytes

As you went to the effort to look it up once, its a shame you didn’t provide the information so that people can find it.

… Simon

Ok I guess you mean Section 29.3 Speed Grades.

According to the graph and a quick calculation you need 3.78V for 16Mhz

So whats the consequence of running at 3.3V

simoncastle:
According to the graph and a quick calculation you need 3.78V for 16Mhz

So whats the consequence of running at 3.3V

If you don't supply the necessary voltage, you may encounter logic errors - essentially, you can no longer rely on correct operation.

Can't you easily switch to the internal 8 MHz speed, even if the resonator and caps are physically in place ?
Just change fuses ?
Is there a predefined suitable Board in the IDE, or is it necessary to add one ?

simoncastle:
Ok I guess you mean Section 29.3 Speed Grades.

According to the graph and a quick calculation you need 3.78V for 16Mhz

So whats the consequence of running at 3.3V

It's 28.3 on my copy of the datasheet. They must have moved things around.

It's in the section "Speed grades" under "Electrical characteristics", near the bottom.

Respect the spec sheet and don’t go beyond the specs. If you go beyond the specs and have questions, no one including the manufacturer is interested in answering them. You can use internal oscillators yes but I heard you need to calibrate it so it may be good enough for loading sketches. You may also need to flash an older bootloader that runs at lower serial baud rate since the internal oscillators are more depending on things like temperature etc. If you want 3.3V, get a 3.3V arduino board. External crystals are 8MHz and bootloader is ready.

So, after all i read it is better to run on 5V.. Now i don't know if this SD card module that i am using can be connected directly to Arduino or it need some extra outside resistors or something?

What you can get away with depends on many factors. Ultimately the best approach is to run a 16Mhz system on 5V and use a bi-directional level shifter for 3.3V devices, I dont think anyone denies that, the problem is all these lego type blocks that people are trying to glue together. Many people manage to use a pair of resistors to divide down but this has its problems, depending on the speed of your SD card and its sensitivity to edge detection etc, the physical length of your bus, the number of devices on your bus etc etc.
This card reader has intended pull ups on it as I described, that will affect any divider you try and implement, what I offered in my original reply was a way of using the card in a stable way for myself at least. Unfortunately the discussion quickly degenerated into the suitability of running a 16M device with 3V. What I can tell you is, it works fine for me and many others, presumably Ladyada's moded board worked fine see her last statement about the overclocking at the end, and the Sparkfun's openlog http://www.sparkfun.com/datasheets/DevTools/Arduino/OpenLog-v11.pdf works OK despite being another 16M 3V setup doing exactly what you are trying to.
These problems are sometimes very sensitive as you found out in your original post, or even robust and work for the wrong reasons, like the guy in my post who had a working system without any power to the reader, which just happened to work. All I can say is have a go and learn something along the way, I will try and post some pics or something to show how my setup is with this card working.