Hey guys, Christian here from core electronics. Today, we're going to be undertaking circuit 1C, from 'Project Set 1" using the Sparkfun Inventors Kit version 4.0. In our previous video and circuit, we introduced our first analog component, the potentiometer and in this circuit, we’re going to be introducing another component known as an Photoresistor.
Now a Photoresistor, is an analog component, that has two pins like a normal resistor and the special part about this component, is that it's resistance changes in accordance the amount of light present. So, if there's more light present, it has a lower resistance than if it's dark and there's less light present.
So, we're going to be using it, in a series connection with a 10k resistor and we're going to be measuring the voltage divided from that, to give an adequate reading of the amount of light intensity in the room. So, what we're essentially making in this circuit is a Nightlight, if the light sensitivity or the light intensity in the room is below a certain threshold, the LED will turn on and if it's above a certain threshold the LED will stay off.
So, to connect the Photoresistor up, so that we can read the light intensity in the room;
We are going to connect A21 to D22, with the photo resistor we're going to use the 10k ohm, which is the brown black orange resistor and we're going to connect A22 to D25.
Then we're going to connect the red lead, the power, to E21, to the positive power rail.
We're going to connect the blue wire to A0 and to where the crossover happens which is at E22.
Then we're going to connect the ground to E25 to the negative power rail and we're simply going to assemble the LED the same way that we have in the previous two videos with the positive leg going from A2 and the negative leg connecting to A1.
Then E2 the F2 with the 330 resistor.
The negative connection going from E1 to the negative power rail.
The positive connection going from J2 to pin 13.
And the positive 5 volts going to the positive power rail and ground going to the negative rail.
Now that connections established, we can write the code that will let us activate this Nightlight. So, we'll connect it up and open the Arduino IDE.
So, open a new sketch what we're going to do at the start is we're going to declare a global variable or a global constant. So, we're going to hit #define, I'm going to call it "threshold". So, at this "threshold", we're going to use 750. So, what this essentially is it a reading from zero to a hundred and twenty-three, or 0 to 5 volts so once it is below this threshold, we will be activating the light and once it is above this threshold, we will be making sure that the LED is off. So, since we're defining it outside of our functions this constant will be visible throughout any function we choose to use. If instead of defining it, we want to call it an "int" and we want to keep it there, we can do that too. We just need to close it. But we're going to be using the "define" for this circuit.
So, we're defining the threshold to be 750, we're going to set our "pinMode(13,OUTPUT)" and in the main loop we're going to make another integer and that integer is going to be the value at A0 or the value of the voltage divided. So, we're going to say "int" photoVal = analogRead, once again it's like the previous reading that we use for the potentiometer because they're both analog components and they're both at A0.
So, we're analog reading A0, then what we're going to do is, we're going to use our first "if" or conditional statement, so an "if" statement means if this then "that". So, we're going to say if (photoVal < threshold) { digitalWrite(13, HIGH);} else{digitalWrite( 13, LOW); }, then we will compile that and upload it. We're going to save it once again, as circuit 1C so it will compile that, and it will overwrite our previous statement.
It's done uploading and if we cover this the light should turn on.
So, there we go that is how we use a Photoresistor, in a voltage divider, to activate a light or whatever we want to activate using Arduino.
So, I hope you enjoyed circuit 1C, you can join us in the next video for circuit 1D, which will bring together all of what we've learned in the previous three circuits and we'll be using an RGB LED, for the first time to. I'll see you there.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.