Difference between revisions of "Digital RGB LED"

From CQRobot-Wiki
Jump to: navigation, search
Line 14: Line 14:
 
=='''Connection Diagram'''==
 
=='''Connection Diagram'''==
 
{|-
 
{|-
|[[File:Digital_RGB_LED-4.jpg |left|850px]]
+
|[[File:Digital_RGB_LED-4.jpg |left|1000px]]
 
|}
 
|}
 
----
 
----

Revision as of 06:48, 25 September 2020

Crash Sensor

Introduction

This is a cascadable RGB full-color single lamp bead module. only needs one signal pin, and the RGB LED strip constructed by cascading it has extremely low power consumption. This function can cascade more than 100 modules, the total length can reach tens of meters, but the power requirement does not exceed 5V 2A. This makes it easier and more flexible to configure the length of the RGB LED strip. This module uses high-quality and high-brightness WS2812 RGB LED, which is fully compatible with the driving circuit or program used by the light strip, while maintaining an excellent display effect. For existing procedures from RGB LED strips to RGB LED module strings, this hardly needs to be changed.

The module comes with a JST PA SMT 3-Pin buckle socket. Also, a JST PA SMT 3-Pin to DuPont female single-head wire is offered to facilitate wiring. Length: 210mm, buckle attached.


Product Size

Digital RGB LED-2.jpg
PA 3Pin Wire-3.jpg

Connection Diagram

Digital RGB LED-4.jpg

Specifications

Module

  • Operating Voltage: DC 3.3V-5.3V
  • Maximum Current: 48 mA
  • Quiescent Current: 0.7 mA
  • Communication Interface: JST PA SMT 3-Pin interface
  • Dimensions: 30mm * 22mm
  • Fixing Hole Size: 3mm

Wire

  • Withstand Voltage: <50V
  • Withstand Current: <1000MA
  • Length: 21CM
  • Wire Sequence: Black-Negative power supply, Red-Positive power supply, Green-Signal input

Demo for Arduino

1. Before burning the program, put the SPI and SD folders in the compiler installation directory\Arduino\libraries. For example mine: C:\Program Files\Arduino\libraries. Otherwise it will fail to compile.

2. When setting the cascade, we can change the number of RGB LEDs in #define NUM_LED 1, The number of RGB LEDs set in the demo code is 1.

 #include <Adafruit_NeoPixel.h>

    #define PIN_LED 3     // Control signal, connect to DI of the LED
    #define NUM_LED 1     // Number of LEDs in a strip

    // Custom colour1: Yellow
    #define RED_VAL_1       255
    #define GREEN_VAL_1     255
    #define BLUE_VAL_1      0

    // Custom colour2: Purple
    #define RED_VAL_2       255
    #define GREEN_VAL_2     0
    #define BLUE_VAL_2      255

    // Custom colour3: Cyan
    #define RED_VAL_3       0
    #define GREEN_VAL_3     255
    #define BLUE_VAL_3      255

    // Custom colour4: White
    #define RED_VAL_4       255
    #define GREEN_VAL_4     255
    #define BLUE_VAL_4      255

    Adafruit_NeoPixel RGB_Strip = Adafruit_NeoPixel(NUM_LED, PIN_LED, NEO_GRB + NEO_KHZ800);

    void setup() {
      RGB_Strip.begin();
      RGB_Strip.show();
      RGB_Strip.setBrightness(128);    // Set brightness, 0-255 (darkest - brightest)
    }

    void loop() {

      colorWipe(RGB_Strip.Color(255, 0, 0), 1000);  // Red
      colorWipe(RGB_Strip.Color(0, 255, 0), 1000);  // Green
      colorWipe(RGB_Strip.Color(0, 0, 255), 1000);  // Blue

      colorWipe(RGB_Strip.Color(RED_VAL_1, GREEN_VAL_1, BLUE_VAL_1), 1000);   // Custom colour1: Yellow
      colorWipe(RGB_Strip.Color(RED_VAL_2, GREEN_VAL_2, BLUE_VAL_2), 1000);   // Custom colour2: Purple
      colorWipe(RGB_Strip.Color(RED_VAL_3, GREEN_VAL_3, BLUE_VAL_3), 1000);   // Custom colour3: Cyan
      colorWipe(RGB_Strip.Color(RED_VAL_4, GREEN_VAL_4, BLUE_VAL_4), 1000);   // Custom colour4: White

      rainbow(20);  // Rainbow
    }

    // Fill the dots one after the other with a color
    void colorWipe(uint32_t c, uint16_t wait) {
      for (uint16_t i = 0; i < RGB_Strip.numPixels(); i++) {
        RGB_Strip.setPixelColor(i, c);
        RGB_Strip.show();
        delay(wait);
      }
    }

    void rainbow(uint8_t wait) {
      uint16_t i, j;

      for (j = 0; j < 256; j++) {
        for (i = 0; i < RGB_Strip.numPixels(); i++) {
          RGB_Strip.setPixelColor(i, Wheel((i + j) & 255));
        }
        RGB_Strip.show();
        delay(wait);
      }
    }

    // Input a value 0 to 255 to get a color value.
    // The colours are a transition r - g - b - back to r.
    uint32_t Wheel(byte WheelPos) {
      if (WheelPos < 85) {
        return RGB_Strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
      } else if (WheelPos < 170) {
        WheelPos -= 85;
        return RGB_Strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
      } else {
        WheelPos -= 170;
        return RGB_Strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
      }
}

Test Methods and Results

Connect according to the wiring diagram and upload the code to the arduino board. After power-on, the RGB LED on the module will cycle to display different colors of lights.


Demo for Raspberry Pi

Note: When setting the cascade, we can change the number of RGB LEDs in LED_COUNT = 1 # Number of LEDs. The number of RGB LEDs set in the following demo code is 1 .

#coding:utf-8
import time
from rpi_ws281x import PixelStrip, Color
import argparse

LED_COUNT = 1 # Number of LEDs
LED_PIN = 18 # DI port connection GPIO18

# The following content can be used without modification
LED_FREQ_HZ = 800000  # LED signal frequency in hertz (usually 800khz)
LED_DMA = 10          # DMA channel to use for generating signal (try 10)
LED_BRIGHTNESS = 255  # Set to 0 for darkest and 255 for brightest
LED_INVERT = False    # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL = 0       # set to '1' for GPIOs 13, 19, 41, 45 or 53

# The following are various functions of LED mode conversion
def colorWipe(strip, color, wait_ms=20):
    """ Erase the color of the displayed pixels at once."""
    for i in range(strip.numPixels()):
        strip.setPixelColor(i, color)
        strip.show()
        time.sleep(wait_ms / 1000.0)

def theaterChase(strip, color, wait_ms=50, iterations=10):
    """ Cinema lighting style Chase animation."""
    for j in range(iterations):
        for q in range(3):
            for i in range(0, strip.numPixels(), 3):
                strip.setPixelColor(i + q, color)
            strip.show()
            time.sleep(wait_ms / 1000.0)
            for i in range(0, strip.numPixels(), 3):
                strip.setPixelColor(i + q, 0)

def wheel(pos):
    """ Generate rainbow colors spanning 0-255 positions."""
    if pos < 85:
        return Color(pos * 3, 255 - pos * 3, 0)
    elif pos < 170:
        pos -= 85
        return Color(255 - pos * 3, 0, pos * 3)
    else:
        pos -= 170
        return Color(0, pos * 3, 255 - pos * 3)

def rainbow(strip, wait_ms=20, iterations=1):
    """ Draw a rainbow and fade all pixels at once."""
    for j in range(256 * iterations):
        for i in range(strip.numPixels()):
            strip.setPixelColor(i, wheel((i + j) & 255))
        strip.show()
        time.sleep(wait_ms / 1000.0)

def rainbowCycle(strip, wait_ms=10, iterations=5):
    """ Draw a rainbow evenly distributed over all pixels."""
    for j in range(256 * iterations):
        for i in range(strip.numPixels()):
            strip.setPixelColor(i, wheel(
                (int(i * 256 / strip.numPixels()) + j) & 255))
        strip.show()
        time.sleep(wait_ms / 1000.0)

def theaterChaseRainbow(strip, wait_ms=50):
    """ Rotating colorful lighting effects."""
    for j in range(256):
        for q in range(3):
            for i in range(0, strip.numPixels(), 3):
                strip.setPixelColor(i + q, wheel((i + j) % 255))
            strip.show()
            time.sleep(wait_ms / 1000.0)
            for i in range(0, strip.numPixels(), 3):
                strip.setPixelColor(i + q, 0)

# Main program logic follows:
if __name__ == '__main__':
    # Process arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('-c', '--clear', action='store_true', help='clear the display on exit')
    args = parser.parse_args()

    # Create NeoPixel object with appropriate configuration.
    strip = PixelStrip(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
    # Intialize the library (must be called once before other functions).
    strip.begin()

    print('Press Ctrl-C to quit.')
    if not args.clear:
        print('Use "-c" argument to clear LEDs on exit')

    try:
        while True:
            print('Color wipe animations.')
            colorWipe(strip, Color(255, 255, 0))  # Red wipe
            colorWipe(strip, Color(0, 0, 0), 30)
            colorWipe(strip, Color(0, 255, 255))  # Blue wipe
            colorWipe(strip, Color(0, 0, 0), 30)
            colorWipe(strip, Color(255, 0, 255))  # Green wipe
            colorWipe(strip, Color(0, 0, 0), 30)
            
            print('Theater chase animations.')
            print('Rainbow animations.')
            rainbow(strip)
            colorWipe(strip, Color(0, 0, 0), 50)
            rainbowCycle(strip)
            colorWipe(strip, Color(0, 0, 0), 40)
            break
        while True:
            rainbowCycle(strip)
            #print('***********************')
            colorWipe(strip, Color(0, 0, 0), 100)

    except:
        colorWipe(strip, Color(0, 0, 0), 100)

Test Methods and Results

1. Connect the sensor to the Raspberry Pi 4B, and put the test code in the Raspberry Pi system in the form of a folder.

2. If you have not installed pip, you must install pip first: (If you have installed it, skip this step)

sudo apt-get install python-pip

3. Execute the following commands to install related libraries.

sudo pip3 install rpi-ws281x

4. Execute the following commands in the terminal to run the program. We can see that the RGB LED on the module displays lights of different colors in cycles. Press Ctrl+C to stop the program.

cd TS1730
ls
sudo python3 TS1730.py
Digital RGB LED-5.jpg