0

I have a nice sketch running which monitors a few things and displays output on an OLED display. Now I want to save the data to an SD card, but with or without the card module attached I'm getting constant reboots when I add 1 single line of code:

#include <SD.h>

Any idea what may be happening?

Stephen York
  • 121
  • 5
  • 2
    Uno? you run out of SRAM. what is the memory usage report before Upload? – Juraj Apr 15 '19 at 11:54
  • 1
    On an Uno or similar small board just including the SD library consumes a quarter of your memory. – Majenko Apr 15 '19 at 11:58
  • Yep that's it, far out :) I'm also running an SSD1306 display. TIme to optimise. Thanks – Stephen York Apr 15 '19 at 12:23
  • 1
    Ah. Common situation. There's loads of questions here about "SSD1306 and SD unable to allocate buffer" – Majenko Apr 15 '19 at 12:48
  • There is a "text only" SSD1306 library somewhere that doesn't need an internal buffer to save memory. Can't do graphics, of course... – Majenko Apr 15 '19 at 12:49

1 Answers1

2

The following libraries are probably used:

  • SD Card, using a buffer of 512 bytes (see also the comment of DataFiddler below).
  • SSD1306 OLED library, using a buffer of 1,024 bytes
  • Serial, using a buffer of 128 bytes (64 for RX, 64 for TX, see comment of Majenko below)

Since there is only 2,048 bytes of SRAM in an Arduino, this leaves 384 bytes. If you actually READ a file you are already above the maximum.

Your own global variables also will consume a considerable amount of SRAM.

Than, if you execute your sketch, parameters and local variables are stored on the stack (in SRAM), so even if your program starts well, it might/will run in problems when calling functions.

Michel Keijzers
  • 12,759
  • 7
  • 37
  • 56
  • 1
    Don't forget 128 bytes for Serial (64 TX, 64 RX) if Serial is being used... – Majenko Apr 15 '19 at 14:32
  • @Majenko Thanks ... I updated my answer. – Michel Keijzers Apr 15 '19 at 14:41
  • 1
    SD needs 512 bytes for the directory structure, and another 512 bytes later, when it opens a file. – DataFiddler Apr 15 '19 at 15:22
  • 1
    Ta. I've been doing a lot of reading around memory usage and restructured a little to reduce the number and length of string literals. What I find surprising is that the Uno only has 2k but the Nano has 8k. That's a little counter intuitive. Unfortunately my brand new Nano has a broken Micro SD port and overloads my PC and bus shuts down. Gotta get a replacement under warranty so I can finish this project off. Will still optimise the code though. – Stephen York Apr 16 '19 at 05:51
  • 1
    What was really doing my head in about it is I'm using Visual Micro add for Visual Studio to do my coding and it's not as obvious about the warnings. I switched to Arduino IDE and the warnings were far more obvious. – Stephen York Apr 16 '19 at 05:53
  • The percentage is only the percentage BEFORE running, while running more memory is needed. – Michel Keijzers Apr 16 '19 at 09:41
  • Btw, also consider using an STM32F103C8T6 (ARM) development board, they are dead cheap, they run Arduino Uno (check if your needed libraries are supported); they have 20 KB SRAM. Also you can consider using external SRAM but you need to either rewrite your own code using that SRAM or rewrite libraries (probably even more time consuming). – Michel Keijzers Apr 16 '19 at 09:42