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!

Comments

author
fbooth (author)2016-11-17

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

author
44389 (author)2016-11-08

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

author
FilipaP2 (author)2016-08-17

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!

author
ojohn12 (author)2016-07-01

Hi Adrie,

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

author
Triteron (author)2015-11-25

Will this be more useful than sqlite if you want a webinterface?

author
AdrieSentosa (author)Triteron2016-05-31

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

author
kooth (author)2015-11-24

Very cool! Thanks for sharing!

author
AdrieSentosa (author)kooth2016-05-31

thanks kooth, your welcome!

author
perfo (author)2015-12-01

Hello, I've been looking for a simple data logger for the PI, thanks.

I know I must be missing something here but what is the Arduino code for ? have we got to translate it to PI code ? also how do you get the data in to the pi for reading? what pins is the logger reading etc...

Sorry to have the questions I must be having a dumb moment...

Thanks

author
mist42nz (author)perfo2015-12-01

looking at code, "arduino" looks like a typo

author
perfo (author)mist42nz2015-12-04

Thanks for the reply. I still can't see where the data comes from to log .. Ot how this interfaces with something to get the data. That's what I thought maybe the Arduino would be for...

author
AdrieSentosa (author)perfo2016-05-22

Sorry for long time to reply. Arduino in this case as the data source which give raspberry pi data to be logged. You could use another microcontroller or even use your module in raspberry pi (module sensor, adc reading, etc. etc.). Good luck dude! Inform me if you did well.

author
AdrieSentosa (author)mist42nz2016-05-22

yup, i just copy the AWS instructables step and edit it for efficiency Sir! Btw i already edit the instructables. Thanks dude!

author
tomlubinsk (author)2015-12-01

What would be some real life applications of this project?

author
GoobyPles (author)tomlubinsk2015-12-03

Data logging for example =D

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

author
power000 (author)2015-12-01

i like statistics like this , very nice well done!

author
MikeD11 (author)2015-12-01

Wow, I like it!

About This Instructable

102,967views

579favorites

License:

Bio: A candle loses nothing of its light when lighting another
More by AdrieSentosa:LED Acrylic Trophy / PlacardRaspberry Pi - Remote Desktop ConnectionIoT Service Robot
Add instructable to: