Johan_Ha
Published © CC0

KY-016 Straight Onto UNO

Got a KY-016? Stick it straight onto your UNO and start attracting unicorns! No soldering. No schematics. No freaking Fritzing.

BeginnerFull instructions provided3,766
KY-016 Straight Onto UNO

Things used in this project

Hardware components

KY-016
×1
Arduino UNO
Arduino UNO
×1

Software apps and online services

Arduino IDE
Arduino IDE
Remember to install the SoftPWM library. In the Arduino IDE, click to the "Sketch" menu and then Include Library > Manage Libraries. Search for SoftPWM.

Story

Read more

Schematics

The components

I didn't draw any schematics, since it's all about just plugging one component with 4 male pins into another component with 4 female pins. Locate the GND or "-" pin of the KY-016. This goes into the GND pin of the UNO, next to digital pin 13. Don't bother if the red, green and blue pins of your KY-016 are in another order, as long as they go to pins 11 to 13 and the GND or "-" pin goes to the GND of the UNO.

Code

coloursweeper

C/C++
The code will have a RGB LED connected to pins 11, 12 and 13 sweep through all colours in the spectrum. One cycle lasts 6144 milliseconds.
/*
 *    COLOURSWEEPER
 *    by Johan Halmen
 *    credits to Brett Hagman
 *    
 *    Make sure you have installed 
 *    the SoftPWM library by Brett Hagman
 *    
 */
 
#include "SoftPWM.h"

int bPin = 13;
int gPin = 12;
int rPin = 11;

void setup() {
  SoftPWMBegin();
  SoftPWMSet(rPin, 0);
  SoftPWMSet(gPin, 0);
  SoftPWMSet(bPin, 0);
  SoftPWMSetFadeTime(rPin, 0, 0);
  SoftPWMSetFadeTime(gPin, 0, 0);
  SoftPWMSetFadeTime(bPin, 0, 0);
  delay(1000);
}

void loop() {
  int total, phase, delta;
  long int now, then = 0;
  now = millis();
  total = now % 6144;           // The cycle lasts 6144 milliseconds (6 * 1024)
  phase = total / 1024;         // Divide the cycle into 6 phases, think of each
                                // phase as sliding one slide of three slides on 
                                // a light board representing the red, green and blue
                                // channels of your light.
                                
  delta = (total % 1024) >> 2;  // Have the "slide" value vary from 0 to 255,
                                // the delta value increments every four milliseconds
  
  if (delta != then)            // Update the slides only if the millisecond clock 
                                // has ticked four times                               
  {
    switch (phase)
    {
      // Every second phase one slide goes up, 
      // every other second one slide goes down
      case 0 :
        SoftPWMSet(gPin, delta); // green goes up
      break;
      case 1 :
        SoftPWMSet(rPin, 255 - delta); // red goes down
      break;
      case 2 :
        SoftPWMSet(bPin, delta); // etc, you get the idea
      break;
      case 3 :
        SoftPWMSet(gPin, 255 - delta);
      break;
      case 4 :
        SoftPWMSet(rPin, delta);
      break;
      case 5 :
        SoftPWMSet(bPin, 255 - delta);
      break;
    }
    then = delta;               // Save the delta time for this last
                                // update of the slides
  }
}

Credits

Johan_Ha
17 projects • 26 followers
Music teacher Composer Coding for fun
Contact

Comments

Please log in or sign up to comment.