The photoresistor and the raspberry pi
Folgende Teile braucht ihr für die Anleitung
The photoresistor and the Pi
At this point you are knowledgeable of how to properly connect the MCP3008 to the Raspberry Pi. We can utilize this same connection to read data from a photoresistor. A photoresistor is just as the name suggests, a resistor. There is however a significant difference between a photoresistor and the resistor most are accustomed to. The resistance value of the photoresistor changes according to the light conditions of its surrounding e.g. with a normal room lighting the resistance is about 10KΩ but in the darkness the resistance value is around 2MegaΩ. With the MCP3008 we can measure the resistance and are then able to detect different lighting situations.
|Connections Pi||Connections Sensor|
|3,3V + 10KΩ Resistor||Side 1 Photoresistor|
|CH0 + Side 1 Photoresistor|
|GND||Side 2 Photoresistor|
import spidev import time import os import RPi.GPIO as gpio # Start SPI connection spi = spidev.SpiDev() spi.open(0,0) # Read data from MCP3008 def analogInput(channel): adc = spi.xfer2([1,(8+channel)<<4,0]) data = ((adc&3) << 8) + adc return data while True: print("0: "+str(analogInput(0))) time.sleep(0.1)
Step for Step
The code shouldn’t be totally new to you as we used it in the MCP3008 example. You may refer to the previous MCP3008 chapter for a general explanation of the code above. We will only read the analog input of the first input channel also called CH0. In order to help you gain a better understanding of this circuit, we have prepared the diagram below:
The interesting part of this circuit begins with the 10KΩ resistor. This resistor has a connection from the power source as well as to a row on the breadboard which connects it to CH0 and the photoresistor. It is important to know that electricity always takes the connection with the least resistance. In the case above, if the resistance gets higher and higher, more electricity will go to the analog input pin. For us this means that we will get a higher reading if there is less light around the sensor. We can accomplish this by putting our finger above the sensor. Conversely the value increases with a brighter light which can be tested using your smartphone flashlight. With this functionality of the photoresistor, we can use the measured value to get the brightness.
We measured a value around 60 using our smartphone flashlight, a value around 300 under normal room lighting conditions, and with near darkness we measured a value around 800. With this kind of sensor it is totally normal if your measured values are only somewhat similar. To test the accuracy or calibrate readings you may measure the values yourself.