raspberry pi

Photo Resistor with Raspberry pi Using MCP3008 Converter

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.

Figure 1: The typical appearance of photo resistors

Amazon Purchase Links:

Raspberry Pi


raspberry pi 4 4gb kit

Wireless Keyboard and Mouse for raspberry pi:

Night vision Camera for Raspberry Pi:

Oled HDMI touch display for raspberry pi:

TOP10 Best Raspberry Pi Kits

Other Tools and Components:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*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).

Figure 2: Voltage divider with photo resistor on the MCP3008

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.

MCP3008 Features:

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.

MCP3008 Application:

  • Sensor Interface • Process Control • Data Acquisition • Battery Operated Systems.

Absolute Maximum Ratings:

VDD…………….. 7.0V

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
import spidev
import time
spi = spidev.SpiDev ()
spi. open (0, 1)
while True:
answer = spi.xfer ([1, 128, 0])
if 0 <= answer [1] <= 3:
value = ((answer [1] * 256) + answer [2]) * 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.


Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button