Photo resistor with MCP3008 and Raspberry Pi :
MCP3008 Converter with Raspberry pi:- A photo resistor is a resistor whose value depends on the Light changes. The component can be ideally used as a twilight or Day/night sensor. Photo resistors with a wide variety of light/dark values can be found in every electronics store.
Amazon Purchase Links:
Other Tools and Components:
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
When using the photoresistor, however, the Raspberry Pi does not allow any direct connection to. So that the resistance value can be processed, need an external analog / digital converter. We use the here again MCP3008 and have adapted the circuit diagram to the photo resistor (see Figure 2).
Before I am going to explain the Python code, first let’s take a look at the MCP3008.
The Microchip Technology Inc. MCP3004/3008 devices are successive approximation 10-bit Analogto-Digital (A/D) converters with on-board sample and hold circuitry. The MCP3004 is programmable to provide two pseudo-differential input pairs or four single-ended inputs. The MCP3008 is programmable to provide four pseudo-differential input pairs or eight single-ended inputs. Differential Nonlinearity (DNL) and Integral Nonlinearity (INL) are specified at ±1 LSB. Communication with the devices is accomplished using a simple serial interface compatible with the SPI protocol. The devices are capable of conversion rates of up to 200 ksps. The MCP3004/3008 devices operate over a broad voltage range (2.7V – 5.5V). Low-current design permits operation with typical standby currents of only 5 nA and typical active currents of 320 µA. The MCP3004 is offered in 14-pin PDIP, 150 mil SOIC and TSSOP packages, while the MCP3008 is offered in 16- pin PDIP and SOIC packages.
Features • 10-bit resolution • ± 1 LSB max DNL • ± 1 LSB max INL • 4 (MCP3004) or 8 (MCP3008) input channels • Analog inputs programmable as single-ended or pseudo-differential pairs • On-chip sample and hold • SPI serial interface (modes 0,0 and 1,1) • Single supply operation: 2.7V – 5.5V • 200 ksps max. sampling rate at VDD = 5V • 75 ksps max. sampling rate at VDD = 2.7V • Low power CMOS technology • 5 nA typical standby current, 2 µA max. • 500 µA max. active current at 5V • Industrial temp range: -40°C to +85°C • Available in PDIP, SOIC and TSSOP packages.
- Sensor Interface • Process Control • Data Acquisition • Battery Operated Systems.
Absolute Maximum Ratings:
All Inputs and Outputs w.r.t. VSS …………– 0.6V to VDD + 0.6V
Storage Temperature ………………–65°C to +150°C
Ambient temperature with power applied…….–65°C to +150°C
Soldering temperature of leads (10 seconds) ………….+300°C
ESD Protection On All Pins (HBM)……………………………..≥ 4 kV
To test the photo resistor under Python, use the following code:
#! / usr / bin / python3
# File photoresistor.py
spi = spidev.SpiDev ()
spi. open (0, 1)
answer = spi.xfer ([1, 128, 0])
if 0 <= answer  <= 3:
value = ((answer  * 256) + answer ) * 0.00322
print (value, "V")
time. sleep (1)
The program will now show you a voltage value at intervals of one second. This results from the ratio of the resistances of the voltage divider. Depending on the resistance value of your photo resistor adjust the fixed resistance value. Since the photo resistor is now the flexible resistor in the voltage divider, results Depending on the lighting conditions, a different voltage value, but never 3.3 V will exceed. Cover the resistor with your finger and watch the read out values. Now you can e.g. Define areas (for example in if queries) that differentiate between day, twilight and night.
Note: Temperature instead of light Just like photo resistors, there are temperature-dependent resistors: NTC resp. PTC resistors (Negative or Positive Temperature Coefficient) can be used like photo resistors are used.