# ATmega328p driving RGB LED WS2801

I am using an ATmega328p to control this RGB LED module. The ATmega is on an Arduino Uno, but I am programming it in C, without using the Arduino libraries.

To power the RGB module I am also using a 12V power supply. To power the Arudino Uno I am using the USB power. I connected it the following way:

• RGB LED Module 12V -> 12V power supply 12V
• RGB LED Module GND -> 12V power supply GND
• RGB LED Module SDI -> ATmega PORTB0
• RGB LED Module SCK -> ATmega PORTB1
• RGB LED Module GND -> ATmega GND

Now if I send some data R=0xFF, G=0xFF, B=0xFF (Set SDI to 5V and make every second 24 SCK pulses) according to the datasheet nothing happens. The lights stay off. But randomly I found out, that if I disconnect the 12V power supply and connected it again randomly, sometimes the Lights turning on in a random color. So I don't have any clue whats going on.

What can cause this behavior, what can I do to find the error?

    #include <avr/io.h>

// 16ms delay.
static void delShort()
{
TCCR0B = 0;
TCCR0A = 0;
TIMSK0 = 0;
TCNT0 = 0;
GTCCR = 0;

TIFR0 = TIFR0 | (1 << TOV0);
TCCR0B = (1 << CS02) | (1 << CS00);

while(!(TIFR0 & (1 << TOV0)))
{

}

TCCR0B = 0;
}

// 1000ms delay.
static void delLong()
{
for(unsigned char lI = 0; lI < 61; ++lI)
{
delShort();
}
}

static void ledSetup()
{
PORTB &= ~(1 << PORTB0);
DDRB |= (1 << DDB0);

PORTB &= ~(1 << PORTB1);
DDRB |= (1 << DDB1);
}

static void ledWrite(uint32_t pData)
{
uint32_t lValue = pData;

for(uint8_t lIndex = 0; lIndex < 24; ++lIndex)
{
if(lValue & 0x1)
{
PORTB |= (1 << PORTB0);
}
else
{
PORTB &= ~(1 << PORTB0);
}

delShort();

PORTB |= (1 << PORTB1);

delShort();

PORTB &= ~(1 << PORTB1);

delShort();

lValue = lValue >> 1;
}
}

void main(void)
{
ledSetup();

delLong();

while (1)
{
delLong();
ledWrite(0xFFFFFFFF);
}
}

• Schematic and the code are needed. – Eugene Sh. Jun 28 '17 at 17:01
• @Eugene Sh. I added the code and schematic. – Michael Jun 28 '17 at 17:51
• @Michael Oh, wow! It's been some years since I was drawing ASCII schematics!! Cool. (I actually wrote a program to accept LTSpice schematics and then generate quite readable ASCII from that.) Just so you know, there is a schematic editor available here for you and you can construct arbitrary "black boxes" with arbitrary pin names, if you want to. It's not so bad. – jonk Jun 28 '17 at 18:42
• As you are developing for an ATmega in C without using the Arduino libraries or approach, I've edited your question to emphasize that, and de-emphasize the nearly irrelevant fact that your board happens to say "Arduino" on it. – Chris Stratton Jun 28 '17 at 20:11
• You should verify that your timing functions work for sure as a first step. – HaLailah HaZeh Jul 11 '17 at 3:20