2 Pin RGB LED on an Arduino - arduino

I'm having some trouble setting up my RGB led. It's not common anode, so it only has 2 pins. Not 4. I can't seem to set the led up to change color. It will only blink red. I want the led to cycle through colors. Thanks for Helping!
int led1 = 2; //Don't worry about the other led variables, they work
int led2 = 7;
int led3 = 9;
int led4 = 12;
int led5 = 13;
int redPin = 5;int greenPin = 4;
int bluePin = 3;
//#define COMMON_ANODE
void setup(){
pinMode(led1, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
}
void loop(){
setColor(0, 255, 0); // green
delay(1000);
setColor(0, 0, 255); // blue
delay(1000);
setColor(255, 255, 0); // yellow
delay(1000);
setColor(80, 0, 80); // purple
delay(1000);
setColor(0, 255, 255); // aqua
delay(1000);
}
void setColor(int red, int green, int blue){
#ifdef COMMON_ANODE
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
#endif
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}

First of all this is not a software question.
When it comes to your problem, 2 pinned LEDs don't work the way 4 pinned ones work. If you are using an LED with 2 pins, you give them a voltage and wait for them to change their color with time.
However, you can change their 'Color changing time' applying PWM to it's Vcc pin. But on the other hand it makes it illuminate less.

Since you have a slow color changer you cannot control the timing through PWM. PWM is going to turn the LED on then off at a high rate. The color changing circuit in those 2 pin slow/fast color changer LEDs turns on when you apply voltage. As long as power is applied the program runs. When you turn it off and then back on (unless it is storing internal data) it will reset and start from the beginning of its programmed cycle.
Try this little experiment: Turn the LED on and wait 5 seconds. Take note of the color pattern. Turn it off and wait 2 seconds. Then turn it on again.
If it the color pattern starts over, then that LED "forgets" where it was and is reset every time power is applied.
If it continues where it left off (which I highly doubt), then it has internal NV memory and you might be able to control the changing speed but only in making it slower.
If after 5 seconds that LED never changes, I would have to say that it is a plain single color LED. To see if it is a Bi-Color type, try reversing the polarity. If you get another color, you have I Forward-Reverse Bi-Color LED. If not, you have a plain LED.
Hope that helps!

Related

Running WS2812B strip on arduino

I purchased a 5 meter strip of WS2812B LEDs to be used in conjunction with a motion detector (WS2812B 5 Pins RGBW RGBWW 4 IN 1 LED Strip Light Non-Waterproof DC5V).
The strips are hooked up to a 5V power supply (USB powerbank) and GND/5V/signal on pin 6 on an arduino UNO.
I should note that I so far have not cut the LED strip, so all 5 meters are intact.
I've tried getting the LEDs to emit simple colors using the FASTLED library using the code below. The Blue/blue/blue combination results in the colors Blue/Red/Green on LEDs 0-2
Changing to Red/Red/Red produces Yellow-ish/blue/off
Changing to Green/Green/Green produces the colors Red/lightgreen-ish/off
I've tried shifting from RGB to RBG color scheme to no avail
I don't have much information on the LED strip apart from what I have already provided you with
Can you give me any ideas on how to proceed?
#include "FastLED.h"
#define NUM_LEDS 5
#define DATA_PIN 6
// Define the array of leds
CRGB leds[NUM_LEDS];
void setup()
{
//FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS); // for GRB LEDs
}
void loop()
{
leds[0] = CRGB::Blue;
leds[1] = CRGB::Blue;
leds[2] = CRGB::Blue;
FastLED.show();
delay(500);
This might not be the exact answer you are looking for, but I’d suggest using the Adafruit_Neopixel.h library for your LED’s. Just did a Projekt with that library and the exact LED strip you are using and it is working great so far.
#include "Adafruit_NeoPixel.h"`
#define LED_PIN 6
#define LED_COUNT 60
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
int red = 100;
int green = 0;
int blue = 0;
void setup() {
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP}
}
void loop() {
for (i=0; i<LED_COUNT; i++){
strip.setPixelColor(i, strip.Color(Red, Green, Blue));
strip.show();
}
}
This should make 60 LED’s red. I’ve also got an LED Project on my GitHub page if you want to look that up. If the code above still doesn’t work I assume your wiring is wrong. I power my chip and LEDs off a power supple and also use the ground of the power supply.

Arduino is blinking Red LED; before taking input for Yellow LED

Input for Red LED Blinking would be anything e.g. 4. Once I give an input for the Red LED:
1- Red LED blinks 4 times
2- Displays the message to give the input for Yellow
3- Before entering the Input, the Red LED starts blinking
And the program is skipping Yellow LED.
int redLED;
int yellowLED;
int redLEDpin = 8;
int yellowLEDpin = 4;
void setup() {
// put your setup code here, to run once:
pinMode(redLEDpin ,OUTPUT);
pinMode(yellowLEDpin, OUTPUT);
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print("how many times would you like to blink red LED? ");
while (Serial.available()==0){}
redLED = Serial.parseInt();
Serial.println(""); //Produce line gap between the Prompts
Serial.print("how many times would you like to blink yellow LED? ");
while (Serial.available()==0){}
yellowLED = Serial.parseInt();
for(int counter=1; counter<=redLED ; counter=counter+1){
digitalWrite(redLEDpin, HIGH);
delay(1000);
digitalWrite(redLEDpin,LOW);
delay(1000);
}
for(int countery=1; countery<=yellowLED ; countery=countery+1){
digitalWrite(yellowLEDpin, HIGH);
delay(750);
digitalWrite(yellowLEDpin,LOW);
delay(750);
}
}
I believe your Serial Monitor's line ending setting is set to Both NL & CR. When you enter 4, 4 + CR triggers redLED = Serial.parseInt(); and NL triggers yellowLED = Serial.parseInt();. And the second parseInt() always returns 0 as newline only (or carriage return only) is not valid digits. Try other line ending settings.

LED strip not working

we are still having trouble with our LED strip. We fixed the library issue, and are now trying to program the LED's but have run into a few issues.
1) First of all, our LED strip does not consistently light up. We are using a 12v battery and everything is wired correctly, but how the LED's appear is very inconsistent. Not all of them light up, they are all different colors, and are all of varying brightness. We have tried to resolve this by just powering it with the battery, using our arduino as a power supply, and added a 1000uf capacitor to make sure the strip doesnt get a surge of power and short the strip. Our code is this:
/* Arduino Tutorial - How to use an RGB LED Strip
Dev: Michalis Vasilakis // Date 3/6/2016 // Ver 1.0
Info: www.ardumotive.com */
//Library
#include <Adafruit_NeoPixel.h>
//Constants
const int dinPin = 4; // Din pin to Arduino pin 4
const int numOfLeds = 10; // Number of leds
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(numOfLeds, dinPin, NEO_GRB + NEO_KHZ800);
// Color takes RGB values, from 0,0,0 up to 255,255,255
// e.g. White = (255,255,255), Red = (255,0,0);
int red = 255; //Value from 0(led-off) to 255().
int green = 0;
int blue = 0;
void setup() {
pixels.begin(); // Initializes the NeoPixel library
pixels.setBrightness(100); // Value from 0 to 100%
}
void loop() {
// For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
for(int i=0;i<numOfLeds;i++){
pixels.setPixelColor(i, pixels.Color(red,green,blue));
pixels.show(); // This sends the updated pixel color to the hardware.
delay(10); // Delay for a period of time to change the next led
}
}
We are trying to set all the LED's to red just to test the code, but nothing is working. Has anyone experienced this before or has any idea of what is not working?
99% of the time this happens when you haven't tied the ground of the LED strip to the ground of the Arduino and the ground of the power supply.

Arduino Project - LDR and ServoMotor Sync and Rotate

Im doing a home project just for fun and Im pretty new to the Arduino, but I do know the basics.
Im creating a automatic venetian blind where it will open when dark and close when bright.
So the slats of the venetian blinds will rotate to a open position when the light sensor detects darkness and rotate to a closed position when the light sensor detects light.
Im using:
arduino uno r3/
continuous servo motor /
led/
LDR (light sensor)/
10k resistor/
This code works for a standard servo motor. I wanted it to work with a continuous servo motor because you can "control it better" and its the one I have.
I guess I will need a if statement something like this:
if light sensor detects dark then rotate the servomotor a to a certain degree and then stop
and if light sensor detects light then rotate the servomotor to a certain degree and then stop.
#include <Servo.h>
Servo servo1;
int sensorPin = A0; // select the input pin for the ldr
int ledPin = 13;
unsigned int sensorValue = 0;
int servoPin = 9;
int pos = 0;
void setup()
{
//Start Serial port
Serial.begin(9600); // start serial for output - for testing
servo1.attach(9);
pinMode(ledPin, OUTPUT);
}
void loop()
{
// For DEBUGGING - Print out our data, uncomment the lines below
Serial.print("Cell = "); // print the value (0 to 1024)
Serial.println(analogRead(sensorPin)); // print carriage return
pos = analogRead(sensorPin);
pos = constrain (pos, 0, 1023);
int servoPos = map(pos, 0, 1023, 255, 0);
int servoDegree = map(servoPos, 255, 0, 0, 179);
servo1.write(servoDegree);
Serial.print("Servo Degree = ");
Serial.println(servoDegree);
int val = analogRead(sensorPin);
val = constrain (val, 0, 1023);
int ledLevel = map(val, 0, 1023, 255, 0);
analogWrite (ledPin, ledLevel);
delay(50);
}
With continuous servo motors, you give up position information. (See this Polou page for details.) This means you won't know when the blinds have reached their open/closed positions unless you also add limit switches. If you go with the switches, then a continuous servo would work. A better solution might be a small gearhead stepper motor like this one from AdaFruit. They have torque but they are much slower than continuous servos.
Key thing is that you don't want to be energizing the motor continuously (which is how standard servos maintain position). That is wasteful and will burn out the motor in something like a blinds application, day in, day out. You want it to do the task and then in loop() wait until the state (light level in your app) has changed. So you would need to keep track of the last light level, then in loop() check if the current light level is different (and greater than some threshold you will have to determine through testing), then change the state of the blinds and store that last level.

Arduino RGB led-strip turns off at full intensity

I'm playing around with an Arduino Uno, and an RGB led-strip (Ikea Dioder 4 strips, only one is connected). Primary and secondary colors look fine when writing 255 to one or two colors. The problem is when I want white light (e.g. 255 on all three LEDs), then instead of becoming fully white, it just turns off.
I have an ethernet-shield connected to the Arduino, and a seperate 12V dc powersource. When I connect the power source to the Arduino, it works, but the regulator gets insanely hot (known issue). So I connect the LEDs to the 12V power source directly (they are rated at 12V, the Ikea one is also 12V). Only this causes the problem.
In the program below I can see it very clearly. The code should do the following: fade to red; fade to yellow; fade to white; repeat. The first two go fine, but when it's time to fade to white, it fades to black instead. It just turns off. And I have no idea why.
int redPin = 3;
int greenPin = 5;
int bluePin = 6;
int color[] = {3, 5, 6};
int i = 0;
int j = 0;
void setup(){
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop(){
for(i=0;i<=2;i++)
{
for(j=0;j<=255;j++)
{
analogWrite(color[i],j);
delay(5);
}
}
delay(1000);
analogWrite(redPin, 0);
analogWrite(bluePin, 0);
analogWrite(greenPin, 0);
}
Since it works fine then powered from Arduino, I guess this is hardware, not software problem. Arduino port providing around 40mA, while each colour of Dioder sync around 140mA. Do you use any amplification, like FET or ULN2003? Can you post your schematic?
The problem has been solved (I think) by connecting the GND for the IC and the (-) side of the adapter to the GND of the Arduino.

Resources