The Arduino cannot naturally create files on your host system, so you will need to
- run an independent program on the host system which watches the serial connection and logs the messages produced by the Arduino (my recommendation)
- attach some storage to the Arduino (such as an SD card shield)
- have the Arduino pretend to be a keyboard and output as if it were being typed upon
Independent Program
This is the route I would recommend
- easy to use and test (just check your file has the contents you want)
- will not "mess up" your Arduino
- can do other work and tests on system
- do not need to deal with storage problems
This simple Python script may work for your needs: Serial data logging with python
This post suggests you can do it with a 1-liner under both Linux (may be the same for Mac) and Windows, but you may run into trouble with the Serial Port's baud rate. If this answer is dated or only gets some of the output (ie. a single line and then exits), you could run it in a loop or search further. You'll need to pick the right serial port as there may be a few (or just one with a different name).
Attached Storage
Many vendors will sell you a Shield for this
Beware that Flash Storage can be annoying to deal with
- need to eject (perhaps swap out) the card and look at it periodically to both see the results and if the results are correct
- filesystems hoopla (should I use FAT, exFAT, ext2..)
- ensuring the Arduino can write the filesystem (though modern shields probably do this for you, at least the Adafruit one suggested above does)
Keyboard Emulation
To start, I do not recommend doing this for the following reasons, though it's pretty neat and doesn't require any more hardware than you have.
- BEWARE may make your Arduino unusable without some start gate such as waiting for a switch to be enabled (haywire inputs to computer: cannot program it)
- requires total access to the computer while running (computer cannot be otherwise used)
- not easier to configure than an independent logger (annoying trial and error / waiting / haywire inputs)
Official docs: https://www.arduino.cc/reference/en/language/functions/usb/keyboard/
They have the same warning I would give
A word of caution on using the Mouse and Keyboard libraries: if the Mouse or Keyboard library is constantly running, it will be difficult to program your board. Functions such as Mouse.move()
and Keyboard.print()
will move your cursor or send keystrokes to a connected computer and should only be called when you are ready to handle them. It is recommended to use a control system to turn this functionality on, like a physical switch or only responding to specific input you can control. Refer to the Mouse and Keyboard examples for some ways to handle this.