![]() ![]() To create the yellow/orange of a candle, we’ll start with lots of red. Our LED consists of red, green and blue, so we need the RGB wheel. Grab some cookies and milk and join me on the floor for a lesson on the color wheel. Now let’s talk about how we’re going to get the color we’re looking for out of the RGB LED. It’s also initializing the duty cycle for red and green to 100 and 0, respectively, which turns the red LED fully on and the green LED off. #Very flickery cabdle code#Assuming red is connected to pin 37, and green to pin 33, the above code is telling the Pi to show red 75% of the time and green 25% of the time, resulting in a color that’s a mixture of red and green, but more red than green. The PWM for each color is set separately. #Very flickery cabdle full#A setting of 100 is full on, as if you weren’t using PWM, and 0 is off. Changing that setting to 25 will cause it to be dimmer. That’s going to result in a relatively bright LED. ![]() ![]() By specifying 75, we’re telling it to keep the red LED on for 75% of each pulse, then off for 25%. If you set it too low (try setting it to 10), the pulsing will be slow enough for your eye to pick up on, and the effect is that it looks choppy. I don’t know what the practical limit is… I honestly didn’t see much difference between 100 and 300, and definitely didn’t see any difference above 300. So here, we’ll say channel 37 is connected to one of the colors on our RGB LED, and it’ll be pulsed on and off 300 times a second. The first thing you have to do is set the channel and frequency. And here’s a short snippet of my own: p = GPIO.PWM(37, 300) Here’s a sample implementation from their documentation. The RPi.GPIO library can simulate PWM with any of the GPIO pins you’d normally use to power an LED. That’s called pulse-width modulation, or PWM. But you can make it appear to be dimmer or brighter, by telling the Pi to quickly flash it on and off many times a second (frequency), along with telling it how long to keep it on and off each time it flashes (duty cycle). You can’t dim it or brighten it without changing the resistor. Once you have an LED wired up with a resistor, it’s either on at its current brightness, or it’s off. There’s a few concepts to cover before getting to the circuit and Python code. It’s a little pricey, but then you’ve got everything you need when you start playing around. If you don’t even have a Pi yet, you may want to purchase one of the "ultimate" starter kits from CanaKit or Vilros, which should include the Pi itself, a case, adapter and memory card, and the components I mentioned above that let you start experimenting with the GPIO pins. The only thing I missed out on was a wide array of resistors, but 220Ω works fine with LEDs. Personally, after I bought the Pi by itself, I purchased a kit by CanaKit on Amazon for $20, and haven’t had any problems with it. If you don’t have the above, you can buy fairly inexpensive kits on Amazon. A few jumper wires (male-to-male if using a T-cobbler, otherwise male-to-female).A T-cobbler (optional, but makes life easier when wiring up to GPIO pins).In order to test this out, you’ll need a few things. I’m not sure why – maybe it was because we lost power in our neighborhood recently – but I thought a flickering candle could be an interesting little challenge… Materials ![]() After getting PWM (pulse-width modulation) to work with an RGB LED last week, I was trying to think of what else I could do with an LED that demonstrated changes in color as well as intensity. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |