Krafter
Posts: 22
Joined: Wed Jan 27, 2016 11:36 pm

SPI and APA102

Wed Jan 27, 2016 11:49 pm

Hi. I new at Raspberry Pis and SPI in general. I'm trying to get some APA102 LEDs working with my Pi. I've had great success at making them work with a PICAXE microcontroller but I can not seem to get them to work with my Pi.

I've been do some reading and it seems that some APA102s work just fine with the Pi alone but some only work with a Level Converter. I bought one of these after my first failed attempt. I wired it in to my circuit trying to get only one LED working but I haven't had any luck. I'm pretty sure that my circuit is correct and pretty sure that I have the SPI enabled on the Pi. The only thing I'm not sure about is the code. Here is the code.

Code: Select all

import spidev
import time

spi = spidev.SpiDev()
spi.open(0,1) 'tried spi.open(0,0) as well

resp = spi.xfer(000, 000, 000, 000)
resp = spi.xfer(255, 255, 000, 000)
resp = spi.xfer(255, 255, 255, 255)
I'm attemting to turn the green LED on. I'm hoping that someone here has used APA102s with a Pi before and might point me in the right direction.

Here's the datasheet for the LEDs

Thanks

User avatar
DougieLawson
Posts: 36322
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: SPI and APA102

Thu Jan 28, 2016 10:45 am

Try this: https://github.com/tinue/APA102_Pi he's got an APA102 running on a PiZero.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Krafter
Posts: 22
Joined: Wed Jan 27, 2016 11:36 pm

Re: SPI and APA102

Thu Jan 28, 2016 8:42 pm

That's where I started at but I couldn't get it to work. I couldn't even tell how to setup the SPI with his code and library so that's when I tried something more basic.

I know how the APA102s work. You send it four bytes of 0x0s as an opening and then one byte of something for a global brightness and after that you send it a GBR to chose a color. The first LED you don't need an end but after the second LED four bytes of 0x1s are required.

I'd like to start with something basic such as the code I posted above and grow from there. Can someone verify for me that I'm:
  • Sending - 1111, 1111, 1111, 1111
  • Sending - 1111, 1111, 0000, 0000
This is how I'd do it with a PICAXE

Code: Select all

hspisetup spimode00, spifast 'setup SPI

hspiout ($00, $00, $00, $00) 'send four bytes of zeros
hspiout ($FF, $FF, $00, $00) 'send blue
This would result in the first LED turning blue.

edit: Sorry, I misspoke. I was atempting to turn on the blue LED. These LEDs are a little confusing because it's not RGB but instead it's BGR.

Krafter
Posts: 22
Joined: Wed Jan 27, 2016 11:36 pm

Re: SPI and APA102

Thu Jan 28, 2016 10:09 pm

New info:

I just proved my level converter by wiring it up to an LED and flashing it with my Pi. It has to be in the code at this point. Any help with the basic setup for SPI and sending basic data would be great.

Thanks

User avatar
DougieLawson
Posts: 36322
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: SPI and APA102

Thu Jan 28, 2016 10:16 pm

Get a photo of your wiring, post it on http://imgur.com post a link to the photo on here.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Krafter
Posts: 22
Joined: Wed Jan 27, 2016 11:36 pm

Re: SPI and APA102

Thu Jan 28, 2016 11:38 pm

Image folder

Wire colors on the Pi
  • Orange = 3.3v
    Red = 5v
    Black = Gnd
    Blue = MOSI
    Green = SCLK
Wire colors on the Level Converter
  • Yellow to LV = 3.3v
    Red to HV = 5v
    1st Green to LV3 = SCLK on the Pi
    1st Blue to LV4 = MOSI on the Pi
    2nd Green to HV3 = SCLK on the LED
    2nd Blue to HV4 = MOSI on the LED
    Blacks = Gnd
Wire colors on the LED
  • Blue = 5v
    White/Blue = Gnd
    Brown = MOSI
    White/Brown = SCLK

Image

Image

Image

Image

Image

Image

Image

Image folder

User avatar
DougieLawson
Posts: 36322
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: SPI and APA102

Thu Jan 28, 2016 11:47 pm

I'd start by replacing your Sparkfun level shifter with a 74AHCT125 or 74AHC125 level shifter.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Krafter
Posts: 22
Joined: Wed Jan 27, 2016 11:36 pm

Re: SPI and APA102

Thu Jan 28, 2016 11:51 pm

Okay. I can certainly do that. But your reply, I take it that my code is okay?

Thank you for your help. :D

User avatar
DougieLawson
Posts: 36322
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: SPI and APA102

Fri Jan 29, 2016 9:28 am

Try running the code from Github.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Tam
Posts: 1
Joined: Sat Jan 30, 2016 4:55 pm

Re: SPI and APA102

Sat Jan 30, 2016 5:03 pm

I have all the code from https://github.com/tinue/APA102_Pi running on my Raspberry Pi using APA102 type led string, so the code does work ok.

Krafter
Posts: 22
Joined: Wed Jan 27, 2016 11:36 pm

Re: SPI and APA102

Tue Feb 02, 2016 1:18 am

Tam wrote:I have all the code from https://github.com/tinue/APA102_Pi running on my Raspberry Pi using APA102 type led string, so the code does work ok.
Thanks for the input. I just ordered a couple of 74AHCT125. Hopefully that will cure my problem.

Tinue
Posts: 41
Joined: Fri Dec 05, 2014 12:48 pm

Re: SPI and APA102

Fri Feb 05, 2016 5:44 pm

Krafter wrote:Okay. I can certainly do that. But your reply, I take it that my code is okay?
Your code wouldn't work, there are some brackets missing. This code lights a single LED in green on my APA102 strip:

Code: Select all

import spidev
spi = spidev.SpiDev()
spi.open(0,1)
resp = spi.xfer([0x00, 0x00, 0x00, 0x00])
resp = spi.xfer([0xFF, 0xFF, 0x00, 0x00])
This is with a level shifter in place. From what I have seen on the SparkFun website, this level shifter should work fine as well. Maybe it's not wired correctly?

Krafter
Posts: 22
Joined: Wed Jan 27, 2016 11:36 pm

Re: SPI and APA102

Fri Feb 05, 2016 11:22 pm

Tinue wrote:
Krafter wrote:Okay. I can certainly do that. But your reply, I take it that my code is okay?
Your code wouldn't work, there are some brackets missing. This code lights a single LED in green on my APA102 strip:

Code: Select all

import spidev
spi = spidev.SpiDev()
spi.open(0,1)
resp = spi.xfer([0x00, 0x00, 0x00, 0x00])
resp = spi.xfer([0xFF, 0xFF, 0x00, 0x00])
This is with a level shifter in place. From what I have seen on the SparkFun website, this level shifter should work fine as well. Maybe it's not wired correctly?
Thank you x1000! I used your code and got an LED to light up blue. I can go from here with my own custom code. I still couldn't get the github code working though. I'm not sure why yet but I don't know if I'll even use it anyway.

Thanks again.

Tinue
Posts: 41
Joined: Fri Dec 05, 2014 12:48 pm

Re: SPI and APA102

Sat Feb 06, 2016 9:45 am

Krafter wrote: Thank you x1000! I used your code and got an LED to light up blue. I can go from here with my own custom code. I still couldn't get the github code working though. I'm not sure why yet but I don't know if I'll even use it anyway.

Thanks again.
Perfect! Apparently your LEDs are wired differently. Mine are GBR.

As for the library: Maybe it's a Python version mismatch. The library is written for Python 3. So you have to do two things:
  • Install the spidev with Python 3 (i.e. "python3 setup", with all the necessary "sudo" etc.)
  • Run the code with Python 3, i.e. "python3 runColorCycle"

Return to “Beginners”