How to Write Data to a File on the Raspberry Pi - Circuit Basics % (2024)

The Raspberry Pi is perfect for data logging. It allows you to connect a sensor, record thedata, and store the data for later analysis.

How to Write Data to a File on the Raspberry Pi - Circuit Basics % (1)

In this tutorial, we will see how to save sensor readings to a file on an external storage device using a Raspberry Pi. Using the DHT22 humidity and temperature sensor, we will build an example project that writes the temperature and humidity values to text files and CSV files.

How to Make a New File

There are two ways of creating and working with files on the Raspberry Pi. The first method uses the Raspberry Pi terminal to open a command-line text editor (CLI). The common CLI text editors that can be accessed through this method are GNU nano, Vim, vi, and EMACS.

The second method uses graphical user interface (GUI) text editors. Examples of GUI text editors are Geany, Leafpad, Bluej, Adafruit, WebIDE, and Code::Blocks IDE. If you have a monitor connected to your Raspberry Pi, you can create and edit files using GUI-based text editors. Alternatively, we can use CLI based text editors if we are accessing the Raspberry Pi remotely. To create a new file using the Raspberry Pi terminal, we do the following:

  1. Open the terminal.
  2. Navigate to the directory where you want to store the file cd /directory.
  3. If you are using GNU Nano text editor, use the command sudo nano my_file.txt.
  4. Here, we are creating a new text file called "my_file" and has a (.txt) extension. If we want a Python extension, we simply use sudo nano my_file.py. We can create files with different extensions this way.

On the other hand, using GUI-based text editors is pretty straightforward. To create a new file, double click on the Leafpad icon to launch the program. Click on “save as” and enter the file name and extension you want. Click “OK” when complete.

How to Write to a File Using Python

In certain circ*mstances, we may need to create and write to files using a high-level programming language. We can use other languages such as C/C++, JAVA, or C#, but in this tutorial, we will use Python to create a text file and write to it. To open a file in Python, we need to use the "open" function:

file_Object = open("file_Name","mode")

To write data to files, we declare the variable file_Object to open a file with the name "file_Name". We use the built-in open function, and then Python opens the file with the permissions specified by the "mode" parameter. Different modes exist for the open function, and these are:

  • ‘r’ for reading files.
  • ‘w’ if we want to open the file for writing. If the specified file_Name is not available, Python creates a new file with the same name. However, if the file is available in that directory, Python opens it for writing.
  • ‘a’ for appending data to files.
  • ‘w+’ for reading and writing data to a file

The full implementation in this context will be f = open("myFile.txt","w+"). We will open a text file called myFile. If it is not available in that directory, we will create it. The file will have both read and write permissions, as defined by "w+".

How to Write to Text Files

In this first example project, we will write the humidity and temperature values from a DHT22 sensor to a text file. We will use the Raspberry Pi’s system time to add a date and time stamp for each sensor read.

Components

How to Write Data to a File on the Raspberry Pi - Circuit Basics % (2)

Code

import Adafruit_DHTfrom datetime import datetimeDHT_SENSOR = Adafruit_DHT.DHT22DHT_PIN = 4humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)if humidity is not None and temperature is not None:file = open("log.txt","a")file.write("Temp={0:0.1f}*C Humidity={1:0.1f}%".format(temperature, humidity))else:file = open("log.txt","a")file.write("NAN ")file.write(datetime.today().strftime('%Y-%m-%d %H:%M:%S')+"\n")file.close()

Code Description

The code in the section above reads the temperature and humidity values from a DHT22 sensor. The code is configured to use the Adafruit DHT22 sensor library, naming pin 4 as the input pin for reading the values. We import the datetime module because this will enable us to extract the date and time parameters from the data logging system.

Next, we use the If-else loop to check if we have a signal from the sensor. If we cannot connect to the sensor (for various reasons), we write the value "NAN" and the corresponding date and time stamp. This means that we failed to record the sensor readings at that time, which can be used to record sensor downtime.

Once the sensor is connected, we use the attribute "a" to append the sensor readings to a new line of a text file called "log.txt". The line file.write(datetime.today().strftime('%Y-%m-%d %H:%M:%S')+"\n") is used for extracting the date and time values. Lastly, we close the file with file.close(). The result is shown below.

How to Format the Data in the Text File

Plain text is one of the most efficient ways to store text. In most circ*mstances, we may need to enhance the visual appearance of our text. Additional characters such as headers, alignment, or indentation are often added to our original text for formatting the data. To do that, we use escape sequences in our code. These are characters that do not represent themselves when used inside string literals in our Python code. Here is a list of the most common escape characters:

  • "\t" horizontal tab
  • "\b" backspace
  • "\n" new line
  • "\r" carriage return
  • "\\" backslash character

We can also use conversion specifiers to format the data in our text files.

  • "%d" prints an integer as a signed decimal number
  • "%u" unsigned integer
  • "%e" lower case floating-point number
  • "%E" upper case floating-point number
  • "%f" prints number as a floating-point

In our code for writing temperature and humidity values to a text file, we used "Temp={0:0.1f}*C Humidity={1:0.1f}%".format(temperature, humidity). This formats the temperature and humidity values to floating-point numbers with one decimal place. You can also replace “f” with “e” to change the output of the values or add additional decimal places as you please.

How to Write to CSV Files

For our second project, we are going to write the temperature and humidity values from the DHT22 humidity and temperature sensor to a CSV file. We are also going to add a date and time stamp for each sensor read.

Here is the code for creating and writing sensor readings to a standard CSV file:

import Adafruit_DHTfrom datetime import datetimeDHT_SENSOR = Adafruit_DHT.DHT22DHT_PIN = 4humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)if humidity is not None and temperature is not None:file = open("log.csv","a")file.write("{0:0.2f}".format(temperature)+","+"{0:0.2f}".format(humidity)+",")else:file = open("log.csv","a")file.write("NAN "+",")file.write(datetime.today().strftime('%Y-%m-%d'+"," '%H:%M:%S')+"\n")file.close()

The functionality of this code has not changed. However, this time, we separated the temperature and humidity readings with a comma. We also removed the "Temp" and "Humidity" headings from the output string. Finally, our sensor readings now have two decimal places. Running the code above will produce the output below.

How to Write Data to a File on the Raspberry Pi - Circuit Basics % (4)

How to Label the Columns

To make a new column and label it, we are going to open our CSV file and add a new column to the left. This will be the first column of the output CSV file and it will record the log index (LOG ID) of each sensor reading. Here is the code:

import csvnum = 1with open('log.csv','r') as file: with open('final.csv','w') as output: reader = csv.reader(file) output.write("Log ID"+","+"Temperature"+","+"Humidity"+","+"Date"+","+"Time"+"\n") writer = csv.writer(output, lineterminator='\n') for row in reader: writer.writerow([num]+row) num = num+1

Code Description

  1. with open('log.csv','r') as file: We create a new Python file with the name readData.py. In this file, we write code to open our CSV file'log.csv', which contains the temperature and humidity readings. We use the open function and pass the filename and the read attribute'r'as parameters to this function.
  2. Also, we create a new CSV file to store the results of our operation. As we have discussed before, we need to add a new column to an existing CSV file and label it. To do that, we create a new CSV file with the name 'final.csv', and we use the 'w' attribute to write to our new file. Here is the code: with open('final.csv','w') as output:
  3. output.write("Log ID"+","+"Temperature"+","+"Humidity"+","+"Date"+","+"Time"+"\n") We use this code to create the first row, which contains the column labels.
  4. num = 1, writer.writerow([num]+row), num=num+1 We create a variable with the name num. This variable will keep track of the number of rows in the CSV file, assigning each row a number that can be used to refer to the log index of the timestamped sensor readings.
How to Write Data to a File on the Raspberry Pi - Circuit Basics % (5)

How to Save the Text and CSV files

The data logging exercise’s final task is to store the values in a file for further processing. We may need to store the file either to a local drive or to an external storage device. If you are using the Raspbian desktop, simply plug in a USB stick and save the document as you would on your desktop computer or any computing platform.

To save the file to the USB drive from the command line, follow these steps:

  1. Plugin the USB stick to the Raspberry Pi.
  2. Use this command ls -l /dev/disk/by-uuid to check the ID which has been assigned to your USB stick. In my situation, I have been assigned AAD6-2A81. It is always next to ../../sda1, so you cannot miss it.
How to Write Data to a File on the Raspberry Pi - Circuit Basics % (6)

3. To create a mount point, we create a new directory by using the following command: sudo mkdir /media/usb. You can use any name to identify your new directory.
4. Next, we are going to give Pi the right to own this directory with this command: sudo chown -R pi:pi /media/usb.
5. Finally, we are going to use sudo mount /dev/sda1 /media/usb -o uid=pi,gid=pi to manually mount the drive.
6. After successfully mounting the drive, we can use the following command to check if our device is ready to use. We use the df -h command. Basically, this command displays the available space of file systems and we get the following:

How to Write Data to a File on the Raspberry Pi - Circuit Basics % (7)

We are now ready to copy or move files from our drive to the Raspberry Pi or vice versa. To copy the log.csv file from the Raspberry Pi to the USB stick, we can navigate to the directory of the file and use the following command: cp log.csv /media/usb.

Thanks for reading! Leave a comment below if you have questions about anything.


How to Write Data to a File on the Raspberry Pi - Circuit Basics % (2024)

FAQs

How do I write code on Raspberry Pi? ›

Programming a Raspberry Pi Pico
  1. Step 1: Connect to your computer. Connect your USB cord to the USB port on the device. ...
  2. Step 2: Install MicroPython on the Pico. Copy the MicroPython file you downloaded. ...
  3. Step 3: Set the interpreter for Thonny. Open the Thonny software. ...
  4. Step 4: Write your program. ...
  5. Step 5: Run program.

Which command is used to create a file in Raspberry Pi? ›

One of the easiest ways to create a file in the terminal is to use the "nano" editor. It is simple to use and has on-screen prompts on the key functions like Ctrl+O to save the file (write Out), Ctrl+X to eXit. To create a file called "vnc.sh" type "nano vnc.sh", then type the file contents, save and exit.

How to create a file in terminal? ›

In the Terminal app on your Mac, invoke a command-line editor by typing the name of the editor, followed by a space and then the name of the file you want to open. If you want to create a new file, type the editor name, followed by a space and the pathname of the file.

What is the file structure of Raspberry Pi? ›

Your Raspberry Pi's file system is arranged in a hierarchical directory structure. This means that the file system is structured as a series of directories branching off from a single directory. As a diagram, the system resembles a tree.

How to setup Raspberry Pi step by step? ›

  1. Getting started with your Raspberry Pi. Power supply. Boot Media. Keyboard. Mouse. Display. Audio. ...
  2. Install an operating system. Install using Imager. Install over the network.
  3. Set up your Raspberry Pi.
  4. Configuration on first boot. Bluetooth. Locale. User. Wi-Fi. Browser. Software updates. ...
  5. Recommended software. Tutorials. Support.

How do I use my Raspberry Pi for the first time? ›

There's no on button, so as soon as you connect power to the Raspberry Pi, it'll switch on and start booting up. Once booted up, the Raspberry Pi will guide you through connecting your keyboard and mouse (it usually involves pressing the pairing button in your keyboard or mouse and waiting a few seconds). Boot up!

How to write a Python script on Raspberry Pi? ›

To create and write a new Python script with Nano:
  1. Open a terminal.
  2. Use. nano filename.py. on an existing file or just give a new file name (you can save it later).
  3. Write your Python code.
  4. Press CTRL+S to save the file.
  5. press CTRL+X to exit Nano and come back to the terminal.

What coding language is Raspberry Pi in? ›

By default, C, C++, Python and a few others are installed IIRC, but it is very easy to install more. You would do well to start with Python. A Google search will find may tutorials. Principal Software Engineer at Raspberry Pi Ltd.

Is Raspberry Pi written in C? ›

The Raspberry Pi has traditionally been programmed using Python. Although this is a very powerful language, many programmers may not be familiar with it. C on the other hand is perhaps the most commonly used programming language and all embedded microcontrollers can be programmed using it.

What are the data types in Raspberry Pi? ›

Built-in Data Types

Numeric Types: int, float, complex. Sequence Types: list, tuple, range. Mapping Type: dict. Set Types: set, frozenset.

How do I use commands on Raspberry Pi? ›

Command Line for the Raspberry Pi
  1. pwd Print Working Directory. The shell command pwd displays the file path from the root directory to the current working directory. ...
  2. mkdir Make Directory. ...
  3. ls List. ...
  4. cd Change Directory. ...
  5. Filesystem Structure. ...
  6. The Command Line. ...
  7. cp Copy. ...
  8. Command Options.

How do I create a folder in Raspberry? ›

Method 1 - Using the GUI
  1. Open a File Manager window by clicking on the icon in the top left corner of the screen.
  2. In the window, right-click and select Create New… and then Folder from the context menu.
  3. In the dialogue box, type the name of your new directory and then click OK.

How do I create a new Python file on Raspberry Pi? ›

To create and write a new Python script with Nano:
  1. Open a terminal.
  2. Use. nano filename.py. on an existing file or just give a new file name (you can save it later).
  3. Write your Python code.
  4. Press CTRL+S to save the file.
  5. press CTRL+X to exit Nano and come back to the terminal.

How do I create a Python file on Raspberry Pi? ›

Open IDLE by selecting the Raspberry Pi logo in the top-left, and click Programming > Python 3 (IDLE). You should be presented with the Python interactive interpreter. To write a program, go to File > New File. Enter in your code.

How do I save files on my Raspberry Pi? ›

Choose 'File->Save As'. Thonny will ask whether you want to save the file on This computer or the Raspberry Pi Pico. Choose This computer to save your code to your computer. Choose a location on your computer such as your 'Documents' folder.

Top Articles
Latest Posts
Article information

Author: Rev. Porsche Oberbrunner

Last Updated:

Views: 6640

Rating: 4.2 / 5 (53 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Rev. Porsche Oberbrunner

Birthday: 1994-06-25

Address: Suite 153 582 Lubowitz Walks, Port Alfredoborough, IN 72879-2838

Phone: +128413562823324

Job: IT Strategist

Hobby: Video gaming, Basketball, Web surfing, Book restoration, Jogging, Shooting, Fishing

Introduction: My name is Rev. Porsche Oberbrunner, I am a zany, graceful, talented, witty, determined, shiny, enchanting person who loves writing and wants to share my knowledge and understanding with you.