APA102 flickering madly in simplest animations

I’ve been working on trying to get Bibliopixel working for… well, at least 10-12 hours now!
I don’t know what’s causing it, but even the simplest animations seem to have a mad, epileptic flickering randomness.

I’m using an APA102 directly wired to a Raspberry Pi 4 (I don’t know if this has been tested with BP much?) so using the SPI driver.
I’m pretty certain there’s no issue with the APA102 strip, as using another driver I can make it behave as it should.

The code I’m currently using is a pair of yaml files that I borrowed from another thread here. The contents are:

init.yaml:

driver:
typename: SPI
c_order: BGR
ledtype: APA102
num: 60
shape: 60

strip.yaml:

animation:
  typename: fill.Fill
  palette: [0,0,0]

I adjusted only slightly from the originals in the other thread, as they are hopefully working. Then started BiblioPixel with:
bp init.yaml + strip.yaml

This results in about a dozen red LEDs lighting up midway down the strip, moving up and down the strip slightly, and fringed by a few other colours. It seems, frankly, a bit random. If I change the palette, the red leds change colour accordingly.

I’ve also tried running one of the core projects - green.yaml from Bibliopixel github (can’t link as I’m a new user).
This results in green LEDs lighting as they should at the beginning of the strip, while about a dozen LEDs in the middle of the strip go a bit loopy, cycling really quickly between a load of colours, a bit like in the strip.yaml script. Nothing happens up at the end of the strip.

As I say, I don’t think there’s any problem with the APA102 strip, as it works perfectly smoothly using tinue’s driver (linked above)

I’ve tried uninstalling and reinstalling Bibliopixel a few times, but to no avail. Are there any other dependencies that might be causing problems? Really tearing my hair out with this!

Huge thanks in advance for any help - I can see that this could be a really, really useful tool that I have a great application for if I could get it working.

Edit: I’ve tried cutting down the ‘shape’ to 6, and that seems to light up the first pixels reliably. I then tried increasing the ‘shape’ length incrementally, and the last pixel starts flickering when I get up to 11, and then gets worse if I increase it further.

Having read more about this issue, I’m starting to think it might be a hardware problem - I might need to wire in a level shifter (74HCT245 seems common). I’ll order one of these.

I’m still very confused that the other driver works without any flickering though. I thought it might be to do with SPI Speed, so I reduced this, but it didn’t help.

A level shifter is generally required for all LEDs to work properly with the Pi. That’s why we sell the PiPixel
As to why that other library doesn’t flicker - that’s likely because the default SPI interface is the “FILE” interface which doesn’t respect the spi_speed option. Try setting interface: "PYDEV" on that driver (you may need to pip install spidev first).
The “FILE” interface just dumps the data to /dev/spi and is default because it doesn’t require anything special - but there’s no speed control (at least not on newer Raspbian, unfortunately).

Thanks very much Adam, that’s really helpful! While I’m waiting for my level shifter to arrive (PiPixel shipping to my country would be higher than its value!), is it possible to use the FILE interface for Bibliopixel?

Not while also controlling the SPI speed - APA102 really needs the speed to be 12Mhz or under and unless you really need it you should run at 1Mhz

Hmm - what happens when the speed isn’t controlled? Only wondering out of curiosity as to how the other library works.

It runs at whatever the system default SPI speed is - on older PIs it was 20Mhz but I think it’s higher on the Pi4 (I only just got one and haven’t tested yet).
I looked at the code for the other driver and it’s using an SPI library that, itself, uses the spidev library under the hood and defaults to 8Mhz
We default to the FILE mode because no dependencies are needed… but I guess that’s problematic on the Pi4

Ahh, so that’s why it seems to run well without a level shifter?
I can understand the desire not to have other dependencies. I don’t suppose converting Bibliopixel over would be something I could do easily?? :grin:

You don’t need to as I already noted. Just set interface: PYDEV and install spidev

Ahh sorry, I misunderstood. I thought you meant to set interface: PYDEV on the other library.

1 Like

Ooh, that works much better! Alright, that’s a working solution to play with until the level shifter arrives (74HCT245 should be right I think?) and I figure out how to wire it up.
Thank you very much.

(in case this is useful to someone else, I already had spidev installed, but had to uninstall and reinstall it as sudo for bibliopixel to recognise on my installation)

That’s what the PiPixel uses :wink:

1 Like

Perfect, thanks! One more unrelated question now I’ve gotten going playing around with the animations - I want to use different colours with Twinkle.py from BPA, but when I add the line colors = COLORS.White, COLORS.Azure it doesn’t seem to do anything, and the animation uses the defaults. Should I be putting this somewhere else? (I’m adding by altering the simple example from the BPA readme)

Hey @adammhaile - sorry to bother you, but I’m still struggling with trying to get colors to change in Twinkle.py as above. Am I using the Colors= argument correctly?

Could you include the full code?
But I think that it’s just that you didn’t make it a list…
colors = [COLORS.White, COLORS.Azure]

Thanks - I’ve tried that now, but it still seems to ignore it and default to the regular twinkle animation colours. Here’s the .py code in full:

from bibliopixel.drivers.SPI import *
from bibliopixel import LEDStrip
from bibliopixel.colors import COLORS

#import the module you'd like to use
from BiblioPixelAnimations.strip import Twinkle

#init driver with the type and count of LEDs you're using
driver = SPI(ledtype='APA102', num=60, interface= 'PYDEV')

#init controller
led = LEDStrip(driver)

#init animation; replace with whichever animation you'd like to use

COLORS = [COLORS.White, COLORS.Azure, COLORS.LightSkyBlue, COLORS.Yellow]
anim = Twinkle.Twinkle(led)



try:
    #run the animation
    anim.run()
except KeyboardInterrupt:
    #Ctrl+C will exit the animation and turn the LEDs offs
    led.all_off()
    led.update()

And running the other way (as a .yaml file) results in:

ERROR - failed -
Unknown attribute for animation Sequence: "colors"
^CINFO - signal_handler - Received signal SIGINT

That file is:

aliases:
  bpa: BiblioPixelAnimations.strip

layout:
  typename: strip
  brightness: 50

animation:
  typename: sequence
  run:
    seconds: 20
    fps: 10
  length: 60
  colors: [COLORS.White, COLORS.Azure]
  animations:
     - $bpa.Twinkle

Oh - all you are doing there is overriding the global colors array. Do this:

mycolors= [COLORS.White, COLORS.Azure, COLORS.LightSkyBlue, COLORS.Yellow]
anim = Twinkle.Twinkle(led, colors=mycolors)

Amazing, it works! Thanks so much - been trying all sorts of variables, but never would’ve thought of that.

And, out of curiousity (and because it might be very useful!) - how do you pass that variable in a .yaml file please?