Experiment 2: RGB LED Light Experiment

From Diustou Wiki
Revision as of 17:29, 8 February 2025 by Yousimaier17 (talk | contribs) (Created page with "*Basic Experiment Kits For Arduino *Basic Experiment Kits For Raspberry Pi == Arduino == === Experimental Phenomenon === *Implementation of Cool Lighting Effects with...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

  • 实验二:RGB彩灯实验 接线1.png

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

  • 实验二:RGB彩灯实验 接线2.png

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.

Example Download

Related Examples

Basic Experiment Kits and Examples for Arduino and Raspberry Pi
教程名2
  • 列表2
教程名3
  • 列表3
教程名4
  • 列表4
教程名5
  • 列表5
教程名6
  • 列表6