How To Interface LDR To Raspberry Pi | LDR Sensor #raspberry #iot #python #ldr

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi there in this session let's start discussing how to interface sensors with Raspberry Pi and before doing that we must know what is exactly a sensor so let's start discussion with what is exactly a sensor sensor is basically a physical component which can sense changes into some kind of physical quantity or some kind of physical parameter like there is a sensor for temperature then for pressure humidity there is also sensors for LPG leakage as well as detection of alcohol then you know about the sensors present inside your phone which includes a compass as well as a proximity sensor which turns off the display when the call is on and so on there are hundreds of different types of sensors which are available and all of them can be used in electronic projects depending upon what type of output they produce these sensors are broadly classified as analog and digital sensor for example let's see there is a small sensor available which is called as ldr now it's simply a resistor which displays the changes or which shows the changes in resistance whenever the light falling onto it changes so let's see in detail about the sensors so as said depending upon the type broadly sensors are classified as analog or digital sensor analog sensor means some sensors which are fabricated to give a calibrated analog output now what I mean to say by calibrated analog output means at the time of manufacturing the sensor itself the outputs of the sensor are calibrated to the physical parameter that has been sensing to give you an example lm35 is a very recent temperature sensor which gives analog output and the analog output it gives is 10 millivolts per degree celsius of temperature so for example if right now the room temperature is 25 degree celsius the 1135 will give 25 multiplied by 10 it means 250 millivolts as output so this is a kind of sensor which is or which we can call as an analog sensor and then there are some sensors which are fabricated to give a digital output as well the digital sensors are the ones which give digital output give data onto some kind of digital communication mechanism or something that we technically call as protocols there are a number of different type of protocols like one wire protocol i2c protocol SBI protocol for communicating Digital Data with something like microprocessor or microcontroller with Raspberry Pi we have to understand one fact that you cannot directly interface an analog sensor like ln35 to it directly and Raspberry Pi natively only supports digital sensor because on Raspberry Pi board itself there is no analog to digital converter but if you have to connect an analog sensor like lm35 to Raspberry Pi what you need to do is you need to use an external a to d converter now in this session what we are going to do is we are going to see some of the sensors which are analog as well as digital so let's see the very first one that we can discuss is an ldr or light dependent resistor now ldr is by its nature you cannot call it as an analog sensor because it cannot directly give you an output it's just a resistor and we have to create a conditioning circuit around it so that it can give an analog or digital feedback to us the working of ldr is very simple it's basically a resistance this is the circuit symbol of ldr and the resistance of ldr changes with respect to the Fallen light onto it if there is very low or no light falling onto the ldr it means there is a dark condition in that case the resistance of ldr is very high and when there is sufficient light or when there is enough light onto the ldr in that case the resistance offered by it is low if the light is much brighter falling onto it the resistance is lowest and if the darkness or if the light is very dark there is no light falling onto the ldr in that case the resistance it offers is highest now let's see how we can interface the ldr with Raspberry Pi even without having an analog to digital converter so let's see the simple Hands-On in next exercise now let's see how we can interface the ldr with Raspberry Pi as said more light means less resistance offered by it and less light means more resistance offered by ldr and therefore whatever signal conditioning is to be done onto the ldr we have to do it in our interfacing circuit now let's see the interfacing circuit for ldr now as you can see here we have to create an interfacing circuit like this where we have ldr interfaced with a capacitor like this now the working of the circuit would be very simple one point of the ldr goes to 3.3 volt second part of ldr goes to a junction whose one point goes to the GPI pin of Raspberry Pi and the second Point goes to the capacitor's positive pin the negative pane of the capacitor goes to ground this is supposed to be an electrolytic capacitor and therefore it has its own positive and negative terminals now let's see how it will work now as you can see one point of the ldr is going directly to 3.3 volts over here and this pin is going to Raspberry Pi gpio so the algorithm would be something like this initially what we will do is we will make the Raspberry Pi pin act as input so very first thing is Raspberry Pi pin will act as input pin and what we are going to do is we are going to write down an algorithm where we can sense the voltage onto this pin where it goes from 0 to 3.3 volt as soon as it becomes 3.3 volt we will read it as logic 1 and when it is 0 volt we will read it as logic 0. this is how the interfacing is done so let's first understand the logic now to be precise let's see how the exact algorithm should be now let me clear it whatever was previously done now what we will do first is C one point of the capacitor is going to ground and the second point is going to Raspberry Pi so the very first step in our program should be this particular gpio pin will be made output okay we are going to read it as input but first let's do it output and in the Second Step what we do is we make the particular pin as false or write a false value onto the output which will write a zero volt onto the output now what happens because of the zero value this pin of Raspberry Pi this pin will become zero volt this pin is already ground means zero volt so in this condition what will happen the capacitor will be completely discharged so there is no charge on capacitor at all now what we will do is we will be making the pin work as input so now the gpio will work as input pin but we won't enable any pull up or pull down so it becomes or it stays a Tri-State pin now what happens because of Tri-State nature of the pin let's see now what will happen is depending upon the light falling on to the ldr the capacitor will receive some amount of current and based upon the current received through this resistor the capacitor will start charging right now the capacitor is completely discharged so the potential at this point which can be read is 0 volts as the capacitor starts building charge its voltage will rise slowly something like this so it's zero volt and based upon the ldr it will rise something like this and after some point it will reach to a value let's say 3.3 volt if this is the 3.3 volt threshold bar that we want and it's sure to take some amount of time in reaching to that point now if there is a dark light completely dark room in that case the capacitor will take full time to charge because the resistance of ldr would be highest and because of the highest because of the highest resistor because of the highest resistance what will happen is the current flowing through the capacitor would be very slow or very low and therefore it will take a lot of time to charge completely whereas if it is a well-lit room or if you are holding a light source onto the ldr then it will charge rapidly because in that case what will happen is the resistance offered by it will be too small and therefore the current flowing would be large enough to charge the capacitor faster now what we will do in our program is we will try to measure the time we will try to measure time needed for this pin to become 1 or to become logic one now let's see this is the interfacing diagram of ldr and capacitor with Raspberry Pi so this is exactly like this you can use you can use a one microfarad capacitor or a 10 microfarad capacitor but don't use more than 10 microfarad capacitor 1 to 10 any value would be okay this is the interfacing diagram one point of the ldr is going to 3.3 volt over here second point of ldr is going to capacitor and the same point is also going to pin number 21 gpio 21 that we have been using and the second pin of capacitor is going to ground exactly as shown over here now let's try to understand the program for it now this is the program and this is the algorithm if you can see so what we've said first was we'll make the GPI pin work as output pin so here we are setting it as BCM we are specifying pin number and in while true what is the first thing that we did we first made the particular pin here to work as output pin okay next what we did we write the PIN to 0 volts that will ensure that the capacitor gets completely discharged we also wait here for one second so that the capacitor is discharged completely and then what we do is we make the particular pin work as input pin translated input pin you can see and then the line is while gpro.input pin is equal to equal to low reading is equal to Reading Plus 1. it means basically this program will continue as long as the pin is low and as long as the pin is low what will happen a variable called reading will be incremented by one continuously now when that pin will become one or whenever that pin becomes one this particular while loop will be broken till then it will keep making the readings increment by one and when the pin becomes one or when or when the pin will become one whenever the ldr is fully charged so after this point at this point when the ldr when the sorry the capacitor charge becomes 3.3 volt at this point this particular while loop will be broken and then what we do is we print the reading onto the terminal and then we again wait for one second before repeating the entire Loop so as you see first of all make the PIN output then what we do is we make the particular pin 0 volt then we wait for discharge then we make it input and then we again wait now what we wait for to become true or one and while we wait it what we do is we increment a variable now when you print the value of this variable it will directly indicate or it will be directly proportional to the amount of light falling onto the ldr if the amount of light falling onto the ldr is very low or if it is very dark then the reading will be high and when the amount of light is sufficient enough the reading would be low in this sense I can say that the print value would be inversely proportional to light falling so if the light is high the reading we are going to suppose is reading we are supposed to get is low and when the light is low the reading we are supposed to get is high now enough of theory let us try to say it practically when we interface the capacitor and ldr with Raspberry Pi let's see that in next video and let's see the output now as you can see I have an ldr over here and this is how it looks it's basically a two pin component and it can come in smaller size also this one here is a mediate size ldr that I have now let's try to check it so this is the multimeter I have over here let's put the multimeter onto the resistance or this ohometer mode and let's check the ldr now the multimeter is in the resistance measurement mode and now what I'll do is I will connect this two points of ldr to the multimeter now as you can see it's calculating the resistance and it's about 1.4 k if I give Darkness onto the ldr you can see it's 6K 7K 8K if I make it completely dark at something around 48k you can see that's the resistance the ldr is offering now if I keep it as it is then it is 1.3 kilo ohm and what I have here is a torch light in the battery Bank so let me just keep it like this foreign so if I take the ldr in front of full light I'll just hold it like this over here now you can see the resistance is so reduced that it's about 100 ohm and something 70 of them this is natural room light on natural room light you can see the resistance coming out to be something like 1.3 K and when it is mid Target it's something like 50 kilo ohm or so so that's how you can check the ldr now let's proceed further now I have the Raspberry Pi working over here and I have a breadboard this is a one microfarad capacitor that I have now the negative capacitor is easily identified by this kind of white patch onto it which indicates a negative symbol now what we need to do is we need to connect the ldr just as shown onto the schematic so one point of ldr will go into the breadboards one pin other point will find another suitable hole and then a resist capacitor from that second point to another Point onto the breadboard like this there isn't much to the connections now let's connect the Raspberry Pi to it so this is where I'll connect 3.3 volts anywhere here and this is the ground pin and then what we need is the data pin data pin is the middle one which is here as you can see the combination of ldrn capacitor is the data pin now my data pin will go to pin number gpio 21 then this one will go to 3.3 volt and this one will go to ground now it's good now my connections are done now it's time to write down program onto the Raspberry Pi and let's see the output now let's connect to the Raspberry Pi through VLC viewer and let's try to write down the same program over there let me do the remember password check mark this is my Raspberry Pi now what I want is I want to open the python editor here so let's open Terminal and let's type sudothani so sudo gives us permissions to deal with the GP ions that's why sudo now let's try to create a new file and let's save it so I will call it ldr test in the pi folder I'll save it as ldr test now all we have to do is we have to copy this code over here that's the code that's about it let's try to run this program the output should be shown in this window over here so the print reading should be in parenthesis that's it no other changes needed now you can see a number is zero three nicely printed now if I take my hand there to earlier and if I bring some Darkness onto it you can see the number is increasing if I reduce it and keep it in normal room light it is again close to one two eight seven one two nine nine if I again take the hand above it you can see the reading increasing once again now if I take the thought slide and put onto it then you can see the reading will drastically reduce 200 250 300 and so on 131 185 so this can be a very simple experiment but it can also act as a clear indicator of how much is the Light present in your room so this is normal like you can say this is Darkness you can say something greater than 4000 is darkness something greater than 10 000 it's complete darkness so now I have you can see I have put my finger onto the ldr and the reading against more than fifteen thousand eighteen thousand or something and so on Now using this simple program you can create any light related mini project I will repeat that this is not a perfect indication of lumens of light that is present in the room but it's a definitive indicator whether the light is sufficient it is dark or it is brightly lit or it is super brightly in it now you can perform this experiment and see the output for yourself and you can also perform some tasks based on it thank you for watching this video
Info
Channel: Kitflix
Views: 3,606
Rating: undefined out of 5
Keywords: LDR, light dependent resistor, light sensor, internet of things, interfacing of ldr sensor, raspberry pi sensor interacing, iot based project on ldr sensor, raspberry pi mini project, python learning, micropython language
Id: AdxLGEnu-Qs
Channel Id: undefined
Length: 22min 0sec (1320 seconds)
Published: Fri Jun 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.