Making a Data Logger With the Raspberry Pi





Introduction: Making a Data Logger With the Raspberry Pi

About: Components Plus specialise in electronic components for hobbyists and makers alike. We are passionate about anything Arduino, RaspberryPi and robotics. We provide a wide range of cheap components aimed at in...

This simple data logger takes regular light measurements with an analogue LDR (Photoresistor) and stores them in a text file on your Raspberry Pi. This data logger will measure and record the light level every 60 seconds, enabling you to monitor how the brightness changes over a length of time.

If we want to use analogue sensors with the Raspberry Pi, we would need to be able to measure the resistance of the sensor. Unlike the Arduino, the Raspberry Pi's GPIO pins are unable to measure resistance and can only sense if the voltage supplied to them is above a certain voltage (approximately 2 volts). To overcome this issue, you could use an Analogue to Digital Converter (ADC), or you could use a relatively cheap capacitor instead.

Step 1: What You Will Need

- A RaspberryPi with Raspbian already installed. You will also need to be able to access the Pi using a Monitor, Mouse and Keyboard or via Remote Desktop. You can use any model of Raspberry Pi. If you have one of the Pi Zero models, you may want to solder some header pins to the GPIO port.

- A Light Dependant Resistor (Also known as an LDR or Photoresistor)

- A 1 uF Ceramic Capacitor

- A Solderless Prototyping Breadboard

- Some Male to Female jumper wires

Step 2: Build Your Circuit

Build the above circuit on your breadboard making sure that none of the components leads are touching. The Light Dependent Resistor and Ceramic Capacitor have no polarity which means that a negative and positive current can be connected to either lead. Therefore you do not need to worry about which way these components have been connected in your circuit.

Once you have checked your circuit, carefully connect the jumper cables to your Raspberry Pi's GPIO pins by following the above diagram.

Step 3: Create a Python Script to Read and Log Your Data

Open IDLE on your Raspberry Pi (Menu > Programming > Python 2 (IDLE)) and open a new project (File > New File). Then type the following:

<p>import RPi.GPIO as GPIO<br>import time
import datetime</p><p>loginterval=60 #log interval in seconds
cap=0.000001 #1uf
adj=2.130620985</p><p>def measureresistance(mpin,tpin):
    GPIO.setup(mpin, GPIO.OUT)
    GPIO.setup(tpin, GPIO.OUT)
    GPIO.output(mpin, False)
    GPIO.output(tpin, False)
    GPIO.setup(mpin, GPIO.IN)
    GPIO.output(tpin, True)
    while (GPIO.input(mpin) == GPIO.LOW):
    return endtime-starttime</p><p>def writeline(txt,fn):
    f = open(fn,'a')
t=0</p><p>while True:
    for a in range(1,11):
        if a==10:
    while stime+loginterval>time.time(): #wait until logtime has passed

Save your project as (File > Save As) in your Documents folder.

Now open Terminal (Menu > Accessories > Terminal) and type the following command:


The script will create a text file named "lightlevels.txt" and update it every 60 seconds. You can change this filename on line 6. You can also adjust how often the datalogger updates by changing line 5.



    • Water Contest

      Water Contest
    • Oil Contest

      Oil Contest
    • Clocks Contest

      Clocks Contest

    4 Discussions

    May I ask, why not simply measure resistance, but measure discharge time for the capacitor?
    I suppose that the final expression must be relatively complicated (exponent voltage fall, taking into account False voltage for GPIO pins...). What are the units of the result? Luxes?

    1 more answer

    8 months ago

    There are a few HTML tags scattered throughout your program. A <br> on the first line and </p> <p> in a few lines. This is a common problem with the Instructables code editor.

    1 reply

    Thanks for letting me know. I've gone through the code and removed them. Some of the indents had also disappeared (not good when writing in Python!), which I've now fixed.