Experiment 2: RGB LED Light Experiment
From Diustou Wiki
Contents
Arduino
Experimental Phenomenon
- Implementation of Cool Lighting Effects with RGB Module
Experimental Principle
- By adjusting the intensity of the three primary colors (red/blue/green) through PWM voltage input on the R, G, and B pins, a full-color mixing effect can be achieved.
Circuit Connection
Reference Program
int red = 3; //select the pin for the red LED int green = 5; // select the pin for the blue LED int blue = 6; // select the pin for the green LED int val; void setup() { pinMode(red,OUTPUT); pinMode(green,OUTPUT); pinMode(blue,OUTPUT); } void loop() { for(val=255; val>0; val--) { analogWrite(red,val); analogWrite(blue,128-val); analogWrite(green,255-val); delay(1); } for(val=0; val<255; val++) { analogWrite(red,val); analogWrite(blue,128-val); analogWrite(green,255-val); delay(1); } }
Raspberry Pi
Circuit Connection
Program Execution
Python
- Install the gpiozero library
- You can use the following commands to install the library:
sudo apt update sudo apt install python3-gpiozero
- For other systems on the Raspberry Pi, you can use the following command to install the library:
sudo pip3 install gpiozero
- Run the following command to view the GPIO pin definitions on the Raspberry Pi:
pinout
- Download the Raspberry Pi reference examples, unzip the files, copy them to the user's home directory, and run:
cd /home/pi/raspberrypi/2/python_gpiozero python RGB.py
- You should see the Raspberry Pi running the RGB program correctly. To interrupt the program, press ctrl+C.
- Command description: gpiozero.RGBLED(red, green, blue, active_high, initial_value, pwm)
- Main parameters:
- red: The GPIO pin that controls the red component of the RGB LED
- green: The GPIO pin that controls the green component of the RGB LED
- blue: The GPIO pin that controls the blue component of the RGB LED
- active_high:
- Set to True (default) for common cathode RGB LEDs.
- Set to False for common anode RGB LEDs.
- initial_value:
- The initial color of the RGB LED. Default is black (0,0,0).
- pwm:
- If True (default), constructs PWMLED instances for each component of the RGBLED.
- If False, constructs regular LED instances. This prevents smooth color transitions.
- For more commands, please refer to the gpiozero documentation