Opening a txt file.

Hi!, I’m working on Arduino Mega (Atmega1280). This is my first time with Arduino and in the forum.

I want to put a .txt file into Arduino and make the program read it and depending of his information (only 0 and 1) turn on/off a led.

Is possible to put a .txt file in the arduino memory?

Now I’m working with proteus, and the arduino programmer. I would know why the “fopen” function does not work because I read that in C it is ok.
I’ve included this libraries:
#include <stdio.h>
#include <stdlib.h>

and nothing…

Wich function I have to use?
Wich libraries I would include

Thank you.

Hi Nacho,

A better way would be to put the pattern of 1's and 0's into an array within your program and then read it from there. Here's the list of functions you have access to natively on the arduino:

Unfortunately, you can't just put a text file on the arduino and open it, it's not that kind of platform. It may be possible (with some difficulty), but I think as a beginner user you'll find it much easier to use an array.

Thanks for your answer.

The .txt is an image converted in 0 and 1. And it is very big, like 1000 chars of height and 150 of width.

I must find the way, it is so difficult to work with an array with so many chars.

Thank you again

If you have an SD-card shield you can use the SD library to read text files:

Nop, i am not using any sd-card

No, as in, if you were to purchase an SD-card shield (for example: then you can do it, otherwise there isn't really any way to interface with a text file since the board doesn't have built in formatted storage space. The key part is that you're attaching formatted storage to the arduino. You can achieve the same result with a pcb-mount SD card socket, some hookup wire and a soldering iron too, which would be even cheaper.

Or as an alternative, stream it over serial to the device?

Thanks, I will continue searching solutions. Install and SD reader seems so advance to me but I will check it.

If someone have a different idea to make it work please help me.


If the Arduino is connected to a computer you could send the information though serial and process it on the fly.


And it is very big, like 1000 chars of height and 150 of width.

1000 * 150 = 150,000. That’s 150k of single bits. If it is ascii 1 and 0s, then it is 150,000 * 8 = 1,200,000 bits. The Arduino Mega only has 8k of RAM and only 256k of Flash memory.

So you don’t have any method of storing that much data on the Arduino, which is why SD Card has been given as a suggestion. Keep in mind, that even once you store that much data somewhere, you only have 8k of RAM to work with the file. That is going to be very, very difficult on the Arduino.

Daneel: It may be possible (with some difficulty), but I think as a beginner user you'll find it much easier to use an array.

It can never be like this so as it's "processor" not a memory to store something on an retrieve because in a Processor if anything goes in is Simply machine code the Binary and nothing else and binary is the instruction set not a repository that you will save something on it and retrieve at time's.

If he uses the progmem macros the data will get only stored in flash memory. Thus 18k is still in the range of the Uno.

That would be too much for a Duemilanove/Uno, etc., but Mega has enough memory for this.

True only as long as the data is stored in PROGMEM. Even then, the Mega still only has 8k of SRAM to work with. Any kind of processing of the file's data is going to be tricky.

Indeed, as mentioned above, the only board capable of handling it would be a Mega… Perhaps break it into subsets and have multiple arrays so that you don’t go over the 8k ram limit? If you did it in 2K blocks you could preload the next block while still using the current one.

It all comes down to what he's doing with the data. If it's simply being read in a serial fashion then using PROGMEM could potentially work on a Uno class device. The pgm_read_byte/word etc functions are easy to work with. I'm using them currently on smaller sets of data for some games programming using TVout on a 328 (2K SRAM).