Raspberry Pi - Data Logging

128,059

605

28

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

Intro: 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

    • Audio Contest 2018

      Audio Contest 2018
    • Furniture Contest 2018

      Furniture Contest 2018
    • Metalworking Contest

      Metalworking Contest

    28 Discussions

    0
    None
    TimK171

    Question 6 months 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
    None
    GoobyPlestomlubinsk

    Reply 2 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
    None
    TimK171GoobyPles

    Reply 6 months 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
    None
    TimK171TimK171

    Reply 6 months ago

    Sorry should be distance sensors

    0
    None
    Danjo77

    8 months 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
    None
    mabtan

    8 months ago

    like

    0
    None
    Hejhooo

    8 months 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
    None
    ginojenssen13

    9 months 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.

    2 replies
    0
    None
    ginojenssen13

    Reply 9 months ago

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

    0
    None
    tangoWorld12

    10 months 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 ?

    1 reply
    0
    None
    yoyojackytangoWorld12

    Reply 9 months 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
    None
    fbooth

    1 year ago

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

    0
    None
    44389

    1 year ago

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

    0
    None
    FilipaP2

    2 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!

    0
    None
    ojohn12

    2 years ago

    Hi Adrie,

    How do I get more information on how to use the Raspberry Pi data logger

    0
    None
    AdrieSentosaRocking Arduinos

    Reply 2 years ago

    nope, sqlite for the database that could be access easily from server but .sdv datalog require further code that affect in its simplicity.

    0
    None
    kooth

    2 years ago

    Very cool! Thanks for sharing!