The final circuit to build in the light-projects set combines everything learned in the first 3 circuits and more with the introduction of an RGB LED. As mentioned previously, the RGB LED has a different connection convention due to its four legs and, hence, 3 inputs (Red, Green, Blue). Different input combinations to the LEDâs pins will allow for a wide spectrum of colours to be produced; this will be the focus of this circuit.
Hey guys, it's Christian here from Core Electronics. Today we're going to be building the final circuit for our Project Set 1, Circuit 1D. Now, what we're going to be doing in this circuit is a combination of everything that we did in our previous circuits, plus we're going to be using an RGB LED, which is a red, green, blue light emitting diode. So as opposed to just being single colored like the red one that we've been using in the previous tutorials, we're going to be able to control the amount of red, green, and blue that appears at that diode. So it's slightly different to a normal diode. As you can tell in the guide, it has a red, a common or ground, a green, and a blue pin. So it's got a total of four pins instead of two. So for this, we've got to make sure that we connect it the right way because although it's got a different polarity, polarity is still polarity. So with that said, let's start building our circuit and get it underway.
So as they demonstrate in the book, it's probably a good idea since we have red, blue, and green jumper wires to use them appropriately to determine which colour is which. So we're going to plug the RGB LED in first. We're going to connect using one of the red wires through a resistor, 330 ohm resistor. So we're going to bridge E6 to F6 with one of those. We're going to bridge E4 to F4 with another one. And once again, we're going to bridge E3 to F3 with the other one. That way all of the resistors are connecting through there to limit the current through each of the different colour segments.
Now that that's done, we will connect the red jumper from J6 to pin number nine. We will connect the ground from E5 to the negative power rail. We will connect J4 to pin 10 using the green jumper, and we'll use a blue jumper to connect J3 to pin 11. Now the reason that we're using 9, 10, and 11 is so that we can output an analog write signal, which will become apparent later.
Okay, we're going to connect the potentiometer. Once again, we want the flat side facing the centre column. We're going to connect the centre column and the top pin. We're going to connect to the positive rail of the power bus. We're going to connect the bottom one to the ground bus, and we're going to connect the middle pin to A1. Then we're going to have the same voltage divide that we did in the last project set with the photoresistor and the normal resistor. So we're going to connect 24A to 26C using the photoresistor, and we're going to connect 26A to 30C, or whatever really works for you guys, using the normal resistor. Then same convention, we're going to connect the positive part of the photoresistor to the power rail, to the positive power rail. We're going to connect the other side of the normal 10k resistor to the negative power rail, and we're going to connect where they overlap on row 26 for me to A0. Then once that's done, the only thing left is to connect the ground and power rails to ground and power.
So we're going to connect that up there, so ground to ground, and then we're so ground to the negative rail, and plus five volts to the positive rail. Now that it's all connected, we can connect up the red board to the computer, and I'll run the program that was previously on there. Now it's at this point a good time to mention that down here on the tutorial page, we have the Sparkfun Inventors Kit code that's linked to their website. I would highly recommend doing that for this project set here, just for this circuit, because it is a hefty amount of code as you're about to see. So it's all commented too, which makes it really easy to read and understand, but essentially what's happening is we're getting a global variable up here called photoresistor. It's saying that the photoresistor is connected to A0, it's saying that the potentiometer is connected to A1, and it's saying that the threshold that they are setting for this is 700. So it's also defining pin assignments, so in the event where we need to change something later, they've said that red pin is pin number nine, and if we wanted to make red pin pin number eight along the way, instead of changing everywhere it says red pin, what we can do is we can just call it red pin everywhere, and then change it up here to something else. So 9, 10, and 11 is connected here. They're starting using the serial port, so you're actually able to read in, or print data or print values to the serial port to actually read what they're outputting, and we'll refer to this in a second. So they've defined the pin modes for the red, the green, and the blue pin, which are 9, 10, and 11 respectively. Then they've read in the value of the photoresistor, and they've read in the value of the potentiometer. This is the looping section, so it'll do that continually, and essentially we're going to print out to the serial port the value of the photoresistor and the value of the potentiometer. This is more for debugging purposes, and to kind of see if your code's doing what you want it to do. Then here they've got a conditional if statement, and they've got a few of them actually that will run different functions. So they've got a function called red, a function called orange, yellow, green, cyan, blue, and magenta, and essentially all those are is different combinations of colours using that pulse width modulation that was discussed earlier. So pulse width modulation is a little digital trick where a square wave with a different duty cycle, so if it's putting out a, instead of putting out 5 volts, if we want to put out 2.5 volts, what we can do is we can digital write a value of 511 or 512, or analog write a value of 511 or 512 to the pin specified, and what that's going to do is instead of having a constant 5 volts, it's going to have 5 volts for half the time and 0 volts for half the time, and digitally that will be averaged to 2.5 volts. So here, oh it's important to note that these are only 8-bit, it's an 8-bit microcontroller, so we go from 0 to 255 here. So a value of 100 for red is what it's going to output, and then it's going to put 0 green and 0 blue for the red. Similarly in orange, it's going to put out 100 in the red, but now it's going to use 50 in green and 0 in blue, and it's similar for all of these, it's just different colour combinations to create different colours. The only change is where we use the turn off function. Now the turn off function happens if the light in the room, the ambient light, is above the threshold. So here these run if it's below the threshold, if the light is dark enough in the room, but if it's too light then it will run the turn off function which is essentially just writes a zero value or an average of zero, so no current, no voltage to each of these pins, which means that it doesn't see any light happen, no light appears at the LED. So we'll just hit compile, sketch folders disappeared, cool. So it's compiling the sketch, and once it compiles the sketch it'll upload it to the board, and hopefully now that it's done uploading, if we cover this to a certain extent, the light should turn on. Currently it's not, so what we can actually do is we can use the serial port to see what the value is at the photoresistor. So it's at 800 and it's dropping down, so it should be turning on, but it's not. So since we see that uncovered it's about 800 and covered it's about 720, what we can do is we can change this threshold up here, and by changing this threshold up here what we can do is we can modify the code to work in this specific light setting. So what I'm going to do is I'm going to change it from 700 to 750 to be safe, and then we'll re-upload that and hopefully this should output a value, so it should light up a little easier.
And there we go, we have a red light. If we change this dial and cover it again we should get a green light, and so on and so forth as we go across the board. So now we've got a cyan light. If I were able to cover it with one hand and twist it, it would actually change colour as it's done. But what we can do here, the beauty of microcontrollers, is we can reverse this sign here to show you what it means. So now it'll only turn on if the light is above the threshold. So now it's not a night light, it's just an on light light. So uploading that, we get a cyan light, and as we scroll across it changes. So we have magenta, then we have blue, then cyan, then green, then yellow, then orange, and then red. That there guys is our final circuit for Project Set 1. If you have any questions or comments, please consult our forum and get in touch with us and I'll do my very best to help you guys out. I hope you enjoyed this video and we will see you guys in the next project set, which is sound. Catch you later.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.