Raspberry Pi Light Sensor: A Simple LDR Tutorial

Опубликовано: 19 Март 2026
на канале: Pi My Life Up
69,337
304

In this video I go through the process of setting up a Raspberry Pi light sensor and go through the code required to get the correct data from it.

Full Tutorial: https://pimylifeup.com/raspberry-pi-l...

If you need a full equipment list of all the parts in this tutorial, then simply head over to the website linked directly above. It will have a list and a link to where you can buy similar parts to what I am using.

The sensor we’re using to detect light is called a photocell resistor or also known as a light dependent resistor. This small little device is super sensitive to light and changes its resistance accordingly. The darker it is the higher the resistance.

Unfortunately, the Raspberry Pi doesn’t have any analogue pins so we aren’t easily able to get the value from the light sensor. Instead we charge up a capacitor and count the time it takes for it to send our input pin high. Since the light sensor is a resistor the darker it is the higher the resistance meaning our capacitor will take longer to charge.

The code for this tutorial is super simple and there isn’t a huge amount you will need to know about it. Basically we have a counting function that returns a number representing how light or dark it is. The higher the number the less light there is on the photoresistor.

There are a lot of things you can incorporate the light sensor into. My personal favourite ideas include a light activated alarm and a garden shade sensor.

I hope you haven’t had any trouble with setting up this Raspberry Pi light sensor. If you have had any trouble, noticed I have missed something important or there is anything else you would like to say then please drop a comment below or over at pimylifeup.com.