Advertisement
microrobotics

CJMCU-9813 Full Colour RGB LED Module - I2C Interface

Apr 4th, 2023
823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Here's the example code for controlling the CJMCU-9813 Full Colour RGB LED Module using the P9813 driver with an Arduino:
  3.  
  4. This code sets the RGB LED color to red, green, and blue in sequence, with a 1-second delay between each color change.
  5.  
  6. Connect the CJMCU-9813 Full Colour RGB LED Module to the Arduino as follows:
  7.  
  8. VCC pin to 5V (or the appropriate voltage for your module)
  9. GND pin to GND
  10. DATA pin to digital pin 5 on the Arduino
  11. CLK pin to digital pin 4 on the Arduino
  12. Please note that you may need to adjust the pin numbers in the code depending on the specific pins you choose to use for connecting the module to your Arduino.
  13. */
  14.  
  15. #include <Arduino.h>
  16.  
  17. const int clkPin = 4; // Connect this pin to the CLK pin of the module
  18. const int dataPin = 5; // Connect this pin to the DATA pin of the module
  19.  
  20. void setup() {
  21.   pinMode(clkPin, OUTPUT);
  22.   pinMode(dataPin, OUTPUT);
  23. }
  24.  
  25. void loop() {
  26.   setColor(255, 0, 0); // Set LED color to red
  27.   delay(1000);
  28.  
  29.   setColor(0, 255, 0); // Set LED color to green
  30.   delay(1000);
  31.  
  32.   setColor(0, 0, 255); // Set LED color to blue
  33.   delay(1000);
  34. }
  35.  
  36. void setColor(byte red, byte green, byte blue) {
  37.   sendFrame(0x00); // Start frame
  38.   sendFrame(((blue & 0xC0) >> 6) | ((green & 0xC0) >> 4) | ((red & 0xC0) >> 2));
  39.   sendFrame(blue);
  40.   sendFrame(green);
  41.   sendFrame(red);
  42. }
  43.  
  44. void sendFrame(byte value) {
  45.   for (byte bit = 0x80; bit != 0; bit >>= 1) {
  46.     digitalWrite(clkPin, LOW);
  47.     digitalWrite(dataPin, (value & bit) ? HIGH : LOW);
  48.     digitalWrite(clkPin, HIGH);
  49.   }
  50. }
  51.  
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement