Reading and Graphing Light and Temperature Sensor Data With Raspberry Pi

Introduction: Reading and Graphing Light and Temperature Sensor Data With Raspberry Pi

About: I enjoy computer programming and making things with electronics. Contact me at JustMakeStuffMail@gmail.com. All pictures in my Instructables are taken on an iPhone 10.

In this Instructable you will learn how to read a light and temperature sensor with raspberry pi and ADS1115 analog to digital converter and graph it using matplotlib. Lets start with the needed materials.

Supplies

  1. Raspberry pi (any one will do, though I am using a 4)
  2. MicroSD card with Raspbian installed (good tutorial: https://www.raspberrypi.org/documentation/installa...)
  3. HDMI monitor and power source
  4. Micro USB cable
  5. Adafruit ADS 1115 analog to digital converter: https://www.adafruit.com/product/1085
  6. Jumper wires
  7. light sensor(LDR)
  8. temperature sensor
  9. potentiometer x2 (value will be the midpoint of the range of resistance of your temp and light sensors, which we will measure later)
  10. Breadboard

Step 1: Set Up Your Raspberry Pi

1. Follow this tutorial to set up you raspberry pi:
https://www.raspberrypi.org/help/noobs-setup/2/
2. Enable I2C: click the raspberry pi symbol in the upper left. Go to preferences > raspberry pi configuration > interfaces > and check the box "enable" on I2C. Then click OK.
3. Now open up a terminal window. On the command line type:

sudo apt-get upgrade
sudo pip3 install adafruit-circuitpython-ads1x15
sudo apt-get install python-matplotlib

Step 2: Measure Your Light and Temperature Sensors

Now we are going to need to measure the light and temperature sensors resistance. Take a volt meter on resistance measurement setting and measure across the leads of your light sensor in the light and the dark. Record the values. Now take your volt meter on the leads of your temperature sensor in hot and the cold(I used water). Record the values. We will use them later in our circuit.

Step 3: Wire Up Your Circuit

1. Gather the materials listed in the supplies list. For the potentiometers, use a value that is the average of the highs and lows (light and dark, hot and cold).

  • (high- low) / 2

2. Follow the circuit diagram above:

  1. Connect SDA on the analog to digital converter to SDA on the pi
  2. Connect SCL on the analog to digital converter to SCL on the pi
  3. Connect VDD on the analog to digital converter to 3.3v on the pi
  4. Connect GND on the analog to digital converter to ground on the pi
  5. Connect the rest of the components according to the circuit diagram.

Step 4: Code

1. Type in terminal:

nano digital.py

2. Paste the code I have below or on Github into the text editor that should appear.

import matplotlib.pyplot as plt
import numpy as np
import board
import busio
import time
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn

i2c = busio.I2C(board.SCL, board.SDA)
ads = ADS.ADS1115(i2c)
x = 0
light = AnalogIn(ads, ADS.P0)
temp = AnalogIn(ads, ADS.P1)
X1 = []
X2 = []
Y1 = []
Y2 = []
plt.ylim(-50, 1000)
plt.plot(X1, Y1, label = "light", color = '#0069af')
plt.plot(X2, Y2, label = "Temp", color = '#ff8000')
plt.xlabel('Time(minutes)')
plt.ylabel('Level')
plt.title('Light and temp over time')
plt.legend()
while True:
  x += 5
  Y1.append(light.value/30)
  X1.append(x)
  Y2.append(temp.value/3)
  X2.append(x)
  plt.plot(X1, Y1, label = "light", color = '#0069af')
  plt.plot(X2, Y2, label = "Temp", color = '#ff8000')
  plt.pause(300) 

3. Now press CTRL+X to exit, press y to save, then press enter.

Run your program by typing in terminal:

sudo python3 digital.py

4. Adjust the potentiometers so the graph shows a wide range of values. Try shining a light at the sensor and turning off the lights in the room to ensure that the graph shows a wide range of values.

If either of the values dip below the bottom, try lowering the corresponding divisor (line 29 and 31).

If either of the values go above the top, try increasing the corresponding divisor (line 29 and 31).

Step 5: Troubleshooting

1. Double check all connections against the circuit diagram

2. I2C detect - Will show you all devices connected through i2c:

  • Type in terminal:
sudo apt-get install i2c-tools
sudo i2cdetect - y 1
  • If no devices show up, double check that the connections to SDA and SCL are correct and that i2c is enabled (see step 1)

Be the First to Share

    Recommendations

    • Make it Fly Challenge

      Make it Fly Challenge
    • Maps Challenge

      Maps Challenge
    • First Time Author Contest

      First Time Author Contest

    Comments