Raspberry Pi - Data Logging

189,872

635

32

Introduction: Raspberry Pi - Data Logging

About: A candle loses nothing of its light when lighting another

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!

Be the First to Share

    Recommendations

    • Barbecue Speed Challenge

      Barbecue Speed Challenge
    • Box Challenge

      Box Challenge
    • Colors of the Rainbow Contest

      Colors of the Rainbow Contest

    32 Comments

    0
    EugeneL18
    EugeneL18

    5 weeks ago

    Brilliant
    A spreadsheet work definitely easier as collecting data with a SQL database.
    It is also much easier to work with and to process the data (for me)
    Will add this with MQTT for distribution via the broker
    Just love it

    0
    jfd139
    jfd139

    Question 1 year ago

    What would be the direction I need to go to use the data received from my solar inverters to push the data to a webpage within the pi or external site?

    0
    michaeleisele.rof
    michaeleisele.rof

    Question 1 year ago on Step 5

    What is the USB stick for if you save the files on pi/home (SD Card)?

    0
    bunglesmate
    bunglesmate

    Question 2 years ago on Step 3

    Are there any comments for each line of this code for us newbies?

    0
    TimK171
    TimK171

    Question 3 years ago

    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?

    0
    tomlubinsk
    tomlubinsk

    5 years ago

    What would be some real life applications of this project?

    0
    GoobyPles
    GoobyPles

    Reply 5 years ago

    Data logging for example =D

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

    0
    TimK171
    TimK171

    Reply 3 years ago

    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

    0
    TimK171
    TimK171

    Reply 3 years ago

    Sorry should be distance sensors

    0
    Danjo77
    Danjo77

    3 years ago

    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

    0
    mabtan
    mabtan

    3 years ago

    like

    1
    Hejhooo
    Hejhooo

    3 years ago

    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

    0
    ginojenssen13
    ginojenssen13

    3 years ago

    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.

    0
    ginojenssen13
    ginojenssen13

    Reply 3 years ago

    you can also email me at ginojenssen13@gmail.com

    0
    tangoWorld12
    tangoWorld12

    3 years ago

    hi, hope you can help.. i get a error message for .. "time.sleep(5)

    file.close()"

    it says that the file name is not defined , why is this ??

    also, ls /media brings up "pi" and doesn't bring up the name of my usb, any ideas of how to resolve this ?

    0
    yoyojacky
    yoyojacky

    Reply 3 years ago

    you need to import time module in your code.

    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()
    it seems like that.....

    0
    fbooth
    fbooth

    4 years ago

    The data logging gave me ideas on saving data collecting from RasPi.

    0
    44389
    44389

    4 years ago

    Can you use this to log data off of a blood glucose monitor?

    0
    FilipaP2
    FilipaP2

    4 years ago

    Hello,
    I'm having a really hard time understanding your guide and how to use it in my project... maybe its me, since im a total newbie with python... will read the other two guides you shared tho, and try to understand how this data logger works.

    But.... is it possible to explain what does the "file.write(str(now+","+....." actually do?
    Thank you for your time!