Raspberry Pi - Data Logging

118,548

602

40

Posted

Introduction: Raspberry Pi - Data Logging

In this project, we will learn how to logging data from Raspberry Pi using Python and Excel which collect data and save the output of the collection or analysis.

It’s a quite quick project and can be used either on its own or part of something bigger. Check it out >> Integrated Weather Station(IWS).

Step 1: List of Material

For this project, we will be using:

- Raspberry Pi (mine Raspberry Pi 2)

- Flashdrive cost: ~5.00 USD

Step 2: Hardware Connections

In general, the connections are fairly easy. Just connect Flash Drive to Raspberry PI with USB slot and check the name of Flash Drive in Raspberry pi by type "ls /media" in Raspberry Pi terminal, the result should be content Flash Drive's name and you are good to go.

Step 3: Raspberry Pi Programming

Below is the Python code. Edit the code in any way you prefer, run the module , and you should have no problems.

import os
import time 
from time import sleep
from datetime import datetime

file = open("/home/pi/data_log.csv", "a")
i=0
if os.stat("/home/pi/data_log.csv").st_size == 0:
file.write("Time,Sensor1,Sensor2,Sensor3,Sensor4,Sensor5\n")
while True:
i=i+1
now = datetime.now()
file.write(str(now)+","+str(i)+","+str(-i)+","+str(i-10)+","+str(i+5)+","+str(i*i)+"\n")
file.flush()
time.sleep(5)<br>file.close()

Step 4: Read Your Data Log

Your file that located in your /home/pi/ or any location you prefer is a comma-separated value(CVS) file. Comma-separated values (CVS) file is a formatted file that stores tabular data (numbers and text) which separated by comma. To open and modify this file, simply double click on it, or open it with Microsoft Excel. You can plot your data with Chart tools that available in Microsoft Excel.

Step 5: Enjoy!

After making sure that everything works correctly, you can take this project into bigger project or integrated it with Real Time Clock (RTC) for accurate logging. Check it out >> Raspberry Pi - Real Time Clock (RTC) !

Power through power bank or a plug and you’re good to go!

Share

    Recommendations

    • Microcontroller Contest

      Microcontroller Contest
    • Science of Cooking

      Science of Cooking
    • Pocket-Sized Contest

      Pocket-Sized Contest
    user

    We have a be nice policy.
    Please be positive and constructive.

    Tips

    Questions

    0

    Hi Adrie, this is a great piece of code, thank you, just wanted to know, how do I connect 7 x distance sensors to this code so I can get the data sent to the csv file? The code for the sensors work perfectly and I can see the data when running the code, but don't know how to connect it to your cool piece of code, can you help please?

    27 Comments

    What would be some real life applications of this project?

    Data logging for example =D

    It logs data from sensors, and that's the real life application. Weather monitoring, movement tracker, use your imagination ^^

    Hi GoodyPles, I'm new to RPi, I have 7 x directional sensors connected to RPi 3 all working perfectly, I just need to download the data to csv file, above code is very useful and works very well. How to I connect the sensor data to this code? Please help really struggling. Cheers

    Sorry should be distance sensors

    Hi ArdieSentosa,

    I like this project!

    @Hejhooo:

    1) try ls /media/*

    2) got it running by re- opening file before closing again in the while true loop ; dunno why it closes after header line - I'm also a newbie

    3) if running well you will not see it opening; in folder you will see file size raising

    Help!!!

    there is 3 things that is totally black for me

    1) ls /media ? the responses i got is - pi?? do i tell my RB it have USB ports?

    2) in the code i got a fault on i=i+1

    Code:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    import os
    import time
    from datetime import datetime
    file = open("/home/pi/data_log.csv", "a")
    i=0
    if os.stat("/home/pi/data_log.csv").st_size == 0: file.write("Time,Sensor1,Sensor2,Sensor3,Sensor4,Sensor5\n")
    while True:
    i=i+1
    now = datetime.now()
    file.write(str(now)+","+str(i)+","+str(-i)+","+str(i-10)+","+str(i+5)+","+str(i*i)+"\n")
    file.flush()
    time.sleep(5)
    file.close()

    3) It don't open the file data_log.csv

    best regards Henrik

    sir can you help me how to put input pin on this data logger?. example how can i code input in sensor 1. im new in raspberry pi and i really want to explore more. please help me for my project thanks and best regards.

    you can also email me at ginojenssen13@gmail.com

    example in sensor 1 how can i code input?. can i have example?. much better in push button for easy use.