 Hi, today we'll be learning about light dependent resistors. So have you ever wondered how automatic street lights work or perhaps how light seeking rescue robots work? They all use this, the light dependent resistor, to differentiate between varying degrees of brightness. Light dependent resistors are basically semiconductors. Impurities are added to the semiconductors to make it more efficient at conducting electricity when light hits it. Just like us humans can only see certain wavelengths of light. The same way light dependent resistors like these are sensitive to only certain wavelengths of light. If these certain wavelengths of light go ahead and hit the light dependent resistor, what happens is that the resistor becomes a lot more conductive with just a little bit of light energies. To understand how conductivity actually works, we must understand that if electrons can flow freely, it's a good conductor. If electrons cannot flow freely, it's a poor conductor. In materials such as this, when light actually hits a sensor, electrons can physically move from a valence band where they're not free to move, right, so bad conductor, to a conduction band. So they actually physically jump and when they're in the conduction band, they can actually move freely making it a good conductor of electricity. A good analogy for this is actually car parks. When a car is parked on the side of the road, it can't really move, but as soon as it signals and turns out into the main road, the car can speed up and move freely. The same way when there's no light hitting the sensor, the electrons are kind of stuck in this car park-like situation, but when light hits it, the cars are free to move out of the car park into the main road. These brightness sensors are considered analog sensors. So unlike the digital sensor counterparts, which just give zero to one, these sensors can give a value that can be read as zero to 1,023. Wiring up this sensor is actually super simple. It just has two pins and better yet, because the sensor acts like a resistor, the order of the pins as in the way you connect them doesn't matter. All you need to know is that the first pin will connect to a power source. Typically, this is a five volt power source in a microcontroller. The second pin actually has two connections. The first connection is actually to a resistor that will connect it to ground. The resistor helps normalize the values that your analog read pin measures. The second electrical connection is to an analog read compatible pin. Make sure it's an analog read compatible pin and not a digital read compatible pin. Now that we've actually wired up a sensor, let's write some code and actually test it out. But before we do that, if you want to see more tutorials like this, do consider subscribing. Now let's actually write some code. So here I've got two sets of code. This one is in Python for people who code Raspberry Pi. This one is in Arduino, so C++. You can use similar codes such as this on your Raspberry Pi as well because Raspberry Pis can compile and run C++ code. Let's start with the Python code here because it's very simple. First of all, I've imported a very simple library that allows us to use the GPIO pins of the Raspberry Pi. What I've created is an object here, an object LDR, which is the light sensor object attached to pin four. Next, we have a forever loop, which is while true. In this forever loop, that means it's going to run forever, we're going to print print reads show on screen the value of the LDR object, which is attached to pin four. So basically it's going to read the value at pin four and consists and just forever show that on the screen. On the Arduino side, I've done very something very similar. I've created an integer to hold the sensor pin. So the sensor is attached to pin A0. I've initially set the value of the sensor to zero. This is just, again, I'm just setting it initially. Once we start getting readings, this is going to change from zero. Then I've started a serial monitor, which is basically the same thing as printing it on the screen. And in our loop, basically we set sensor value to analog read the sensor pin. So we're going to basically read the pin A0 and we're going to print that. So the code's actually really straightforward. Let's actually flash it and test it out and see what results we get. Okay, so I've got everything wired up and the code flashed. We're going to use an Arduino for this demo, but you can also use the Raspberry Pi. That's the difference. And as you can see, we're already getting a live number from the brightness sensor. So let's go ahead and actually test our sensor out. As I bring my hand closer and cover up the sensor, the number goes down, down, down, down, down. And as I still let more light in, the number goes back up. Again, if you're using a different microcontroller such as the Raspberry Pi or Microbit, the range of values might differ. Instead of getting something like zero to 1,023, you might be getting a range of decimal values from 0 to 1 or maybe 0 to 100. That doesn't change how the brightness sensor behaves. It's just that the range might be shifted one way or the other. Anyway, that's a tutorial. If you have any questions, you can leave them down in the comments below where I'll be answering them. And if you want to watch future tutorials like this, do subscribe. Thanks.