Since the operation of setting the value is done on the HTML side, Python side only needs to describe initial and termination processing.
GPIO = webiopi.GPIO
RED = 22
GREEN = 17
BLUE = 27
# Set GPIO to PWM
GPIO.setFunction(RED , GPIO.PWM)
GPIO.setFunction(BLUE , GPIO.PWM)
# Light off
GPIO.pwmWrite(RED , 0)
GPIO.pwmWrite(BLUE , 0)
Since PWM is used this time, specify “GPIO.PWM” as the second argument of the setFunction in lines 10 to 12. When “GPIO.OUT” is specified, the pulse signal is not recognized, and every time you move the slide bar it will make a flashing flash for a sec.
Although the processing to turn off the LED at the end of the program is described, the “pwmWrite” function is used.
Write An Integer Value To The Given PWM Channel.
REST API: POST / Devices / Name / Pwm / Channel / Integer / Value
Name (Str): Device Name From Configuration File
Channel (Int): Analog Channel Number
value (int): integer value to output from 0 to pwmMaximum
In the first argument, specify the GPIO pin number and the value you want to set for the second argument. When the value is set to 0, it becomes OFF state and the LED is turned off.
Let’s run it!
sudo /etc/init.d/webiopi start
We tried to adjust the intensity of color development by moving the slide bar as shown in the video above. With red, green and blue monochrome, you can adjust the brightness like a gradation. In addition, by overlapping them, it became possible to express additive colors. Setting a very small value makes the lighting unstable with shimmer, so some degree of brightness is necessary.
It’s very convenient since Wi-Fi and WebIOPi‘s automatic startup settings can be used immediately just by turning on the power.
Installation is super easy, just insert the jumper wire directly into the LED pin as shown in the picture above. Sometimes they the wires come off, so it may be better solder them in.
I tried to put the LED inside a bell-shaped ornament. It looks pretty cool, doesn’t it?
Raspberry Pi’s GPIO alone was able to light up 2 full-color LEDs. If you want to connect more than one, it can be done using parts like a shift register.
Christmas is fast approaching! Why not get in the holiday mood with these cute led Christmas decorations? You can be even more creative, too! Try putting it in a transparent ornament or a candle. You can create handmade lighting using lots of LEDs for Christmas decorations!