Making a Data Logger With the Raspberry Pi

by ComponentsPlus in Circuits > Raspberry Pi

17994 Views, 52 Favorites, 0 Comments

Making a Data Logger With the Raspberry Pi

FH5AQBNJ62G60G1.jpeg

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.

What You Will Need

1000pcs-Monolithic-Ceramic-Chip-Capacitor-1uF-105-50V-5-08MM-NEW.jpg
light-dependent-resistor-gl5516.jpg
raspberry-pi-logo.png
1-Pcs-New-400-Tie-Points-Solderless-PCB-Breadboard-Mini-Universal-Test-Protoboard-DIY-Bread-Board.jpg

- 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

Build Your Circuit

FH5AQBNJ62G60G1.jpeg
Cover Final.jpg

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.

Create a Python Script to Read and Log Your Data

python-logo.png

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

import RPi.GPIO as GPIO
import time
import datetime
loginterval=60 #log interval in seconds
savefilename="lightlevels.txt"
SensorPin=17
TriggerPin=27</p><p>GPIO.setmode(GPIO.BCM)
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)
    time.sleep(0.2)
    GPIO.setup(mpin, GPIO.IN)
    time.sleep(0.2)
    GPIO.output(tpin, True)
    starttime=time.time()
    endtime=time.time()
    while (GPIO.input(mpin) == GPIO.LOW):
        endtime=time.time()
    return endtime-starttime
    def writeline(txt,fn):
    f = open(fn,'a')
    f.write(txt+'\n')
    f.close()
    
i=0
t=0
while True:
    stime=time.time()
    for a in range(1,11):
        res=(measureresistance(SensorPin,TriggerPin)/cap)*adj
        i=i+1
        t=t+res
        if a==10:
                t=t/i
                print(t)
                writeline(str(datetime.datetime.now())+","+str(t),savefilename)
                i=0
                t=0
    while stime+loginterval>time.time(): #wait until logtime has passed
        time.sleep(0.0001)

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

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

python datalogger.py

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.