# "Read Write" Example writes twice

**URL:** <https://forum.arduino.cc/t/read-write-example-writes-twice/1429609>\
**Category:** Storage\
**Created:** [February 7, 2026, 5:30pm UTC](https://forum.arduino.cc/t/read-write-example-writes-twice/1429609 "2026-02-07T17:30:18Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![kooner](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kooner/32/1126861_2.png) [@kooner](https://forum.arduino.cc/u/kooner)\
**Post date:** [February 7, 2026, 5:30pm UTC](https://forum.arduino.cc/t/read-write-example-writes-twice/1429609/1 "2026-02-07T17:30:18Z")

</div>

Hello,

I purchased a P254 MicroSD card from Adafruit and got it working with my UNO R3.

I ran the Card Info example and it worked.

However, when I tried the Read Write example the “testing 1,2,3.” text was written twice. I put the SD card in my laptop just to be sure.

I though it may have something to do with the Serial Monitor Window being opened or closed during upload, maybe the program ran twice but only “testing 1,2,3,3.” was written twice. The other status messages were displayed once.

Line 46 to 56 of the Read Write example (from the Documentation –\>Programing section) is shown here:

```arduino
// if the file opened okay, write to it:

```

` ` **`if`** ` (myFile) {`

`Serial.print("Writing to test.txt...");`

` myFile.println("testing 1, 2, 3.");`

`// close the file:`

` myFile.close();`

`Serial.println("done.");`

`} ` **`else`** ` {`

`// if the file didn't open, print an error:`

`Serial.println("error opening test.txt");`

`}`

I do have a second UNO R3 and P254 to try but though I would post here first.

-kooner

---

<div class="post-metadata">

**Author:** ![van\_der\_decken](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/van_der_decken/32/1006976_2.png) [@van\_der\_decken](https://forum.arduino.cc/u/van_der_decken)\
**Post date:** [February 7, 2026, 5:53pm UTC](https://forum.arduino.cc/t/read-write-example-writes-twice/1429609/2 "2026-02-07T17:53:09Z")

</div>

Snippets are generally unhelpful, as the problem usually occurs in the code that isn't shown, which is true here.

Read the [fantastic documentation](https://docs.arduino.cc/libraries/sd/).

> - In `setup()`, create a new file with `SD.open()` named "test.txt". `FILE_WRITE` enables read and write access to the file, starting at the end. **If a file `"test.txt"` was already on the card, that file would be opened.**

> `mode` (optional): the mode in which to open the file. Mode can be `FILE_READ` (open the file for reading, starting at the beginning of the file) or `FILE_WRITE` (open the file for **reading and writing, starting at the end of the file** ).

Nowhere does it say that opening a file with FILE\_WRITE truncates an existing file.

Which you could have discovered youself by looking in the `SD.h` header file.

```arduino
#define FILE_WRITE (O_READ | O_WRITE | O_CREAT | O_APPEND)

```

No O\_TRUNC. O\_APPEND instead.

Although I haven't run the sketch in question, I would expect it to add to the file each time it is run.

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [February 7, 2026, 7:05pm UTC](https://forum.arduino.cc/t/read-write-example-writes-twice/1429609/3 "2026-02-07T19:05:46Z")

</div>

You have posted modified code (your line 46 is not **the** line 46). Use the original... [SD/examples/ReadWrite/ReadWrite.ino at master · arduino-libraries/SD · GitHub](https://github.com/arduino-libraries/SD/blob/master/examples/ReadWrite/ReadWrite.ino)

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [February 7, 2026, 7:49pm UTC](https://forum.arduino.cc/t/read-write-example-writes-twice/1429609/4 "2026-02-07T19:49:36Z")

</div>

> [@kooner](#):
>
> I though it may have something to do with the Serial Monitor Window being opened or closed during upload, maybe the program ran twice

When you open the Serial Monitor the Uno reboots. Could this be the cause of what you are seeing ?

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [February 8, 2026, 4:29am UTC](https://forum.arduino.cc/t/read-write-example-writes-twice/1429609/5 "2026-02-08T04:29:24Z")

</div>

> [@van\_der\_decken](#):
>
> Which you could have discovered youself by looking in the `SD.h` header file.

Digging into libraries is not quite the way that beginners expect to work with Arduino 😉  
Some eventually will learn to do so to try to understand what is happening.

---

<div class="post-metadata">

**Author:** ![van\_der\_decken](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/van_der_decken/32/1006976_2.png) [@van\_der\_decken](https://forum.arduino.cc/u/van_der_decken)\
**Post date:** [February 8, 2026, 4:52am UTC](https://forum.arduino.cc/t/read-write-example-writes-twice/1429609/6 "2026-02-08T04:52:33Z")

</div>

And the sooner someone tells them that, the sooner they find out that it can be a good idea. 🙂

---

<div class="post-metadata">

**Author:** ![kooner](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kooner/32/1126861_2.png) [@kooner](https://forum.arduino.cc/u/kooner)\
**Post date:** [February 8, 2026, 8:42pm UTC](https://forum.arduino.cc/t/read-write-example-writes-twice/1429609/7 "2026-02-08T20:42:25Z")

</div>

> [@van\_der\_decken](#):
>
> ```arduino
> #define FILE_WRITE (O_READ | O_WRITE | O_CREAT | O_APPEND)
> 
> ```
> 
> No O\_TRUNC. O\_APPEND instead.

I’m definitely a novice on this stuff, from what I could research the define statement is saying to allow reading and writing to the file, create it if it doesn’t exist and append to the existing file instead of over writing.

I’m lost on “No O\_TRUNC. O\_APPEND instead.” Are you saying to replace APPEND with TRUNC?

> [@xfpd](#):
>
> You have posted modified code (your line 46 is not **the** line 46). Use the original... [SD/examples/ReadWrite/ReadWrite.ino at master · arduino-libraries/SD · GitHub](https://github.com/arduino-libraries/SD/blob/master/examples/ReadWrite/ReadWrite.ino)

Yes, I agree snippets are bad but I could not find the code example in the Arduino IDE or my hard drive. I will use GitHub next time.

> [@UKHeliBob](#):
>
> When you open the Serial Monitor the Uno reboots. Could this be the cause of what you are seeing ?

Yes I think this is the problem but I don’t fully understand what’s going on.

As a test I ejected the SD card from my Arduino setup and inserted it into my Windows 10 laptop to delete the file. So now the SD card in empty.

Then I inserted it back into my setup. I clicked on the Serial Monitor icon in the IDE (top right). The code ran and displayed “testing 1,2,3.” one time as it should.

I also noticed the red led on the P254C SD board flashed one time.

I ejected the card, erased it and repeated the test, this time using the “Upload” button. The Serial Monitor is still open at this time.

The red led flashes TWICE and “testing 1,2,3.” is displayed twice on in the Serial Monitor window. I put the SD card into my laptop to verify the file was written to twice.

So I’m a little confused because if I make a change to the code and click “Upload” the red led will flash twice when the Serial Monitor is open and I need it open to see what’s going on.

The only way I can see to get one flash on the red led it to close the Serial Monitor window hit “Upload” then eject the card and read it in my computer but there must be another way.

-kooner

---

<div class="post-metadata">

**Author:** ![van\_der\_decken](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/van_der_decken/32/1006976_2.png) [@van\_der\_decken](https://forum.arduino.cc/u/van_der_decken)\
**Post date:** [February 8, 2026, 8:45pm UTC](https://forum.arduino.cc/t/read-write-example-writes-twice/1429609/8 "2026-02-08T20:45:50Z")

</div>

You're going to see "testing 1,2,3" on the serial monitor every time the Uno reboots.

Which it will do when the upload finishes.

Which is will also do when power is applied.

And it will do every time a terminal is opened. Like when the Serial Monitor is opened.

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [February 8, 2026, 8:46pm UTC](https://forum.arduino.cc/t/read-write-example-writes-twice/1429609/9 "2026-02-08T20:46:46Z")

</div>

> [@kooner](#):
>
> click “Upload” the red led will flash twice when the Serial Monitor is open and I need it open to see what’s going on.

One monitor is ~~"Console"~~"Output" monitor and shows the compiling, and uploading... the other monitor is "Serial Monitor" and shows what your program is transmitting after the upload is complete.

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/8/e/7/8e74f7c179a2ad172b9be44dee2d1db3aa247a4d.png)

---

<div class="post-metadata">

**Author:** ![kooner](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kooner/32/1126861_2.png) [@kooner](https://forum.arduino.cc/u/kooner)\
**Post date:** [February 11, 2026, 5:39pm UTC](https://forum.arduino.cc/t/read-write-example-writes-twice/1429609/10 "2026-02-11T17:39:08Z")

</div>

Thanks for all your responses. I was able to get the SD card / ADC working with my TMP36 temp sensor for my project. Now I can log data over the next few weeks to see what’s happening in my cold box.

I wanted to ask about making something similar to a toggle flip flop.

My cold box has a primary fan that runs all the time. I’ve added a second fan as a backup incase the primary fan fails., so most of time the secondary fan will not run.

I would like to change this setup so that that fans alternate at power up so both fans get run time.

So if the primary fan is running and the secondary is off, the next time the Uno power cycles the secondary fan will run and the primary fan will be off. On the next power cycle only the primary fan runs etc…

I’m assuming I need a flag of some soft that is stored in memory but sure how to implement this. The only coding experience I’ve had is BASIC in the 1980s and a tiny bit of C with a pic about 10 years ago.

-kooner

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [February 12, 2026, 4:18am UTC](https://forum.arduino.cc/t/read-write-example-writes-twice/1429609/11 "2026-02-12T04:18:03Z")

</div>

> [@kooner](#):
>
> I’m assuming I need a flag of some soft that is stored in memory

You can store the flag in the EEPROM of the Uno and at every reboot read the flag in setup(), change the value and write it back to EEPROM.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 11, 2026, 4:18am UTC](https://forum.arduino.cc/t/read-write-example-writes-twice/1429609/12 "2026-08-11T04:18:51Z")

</div>

Closed on August 11, 2026, 4:18am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
