APA102 + Pico (problem on write data)

Posted on
  • I have some trouble writing data to APA102 LED strip.
    In my example I just do the same as the documentation said.

    1. 4 bytes of 0x00 as a start frame.
    2. For each LED, a brightness byte (0xE0 + value), a blue value byte, a green value byte, and a red value byte.
    3. (N/2)/8 bytes of 0xFF as an end frame, where N is the number of LEDs.

    result is following source:

    SPI2.setup({mosi:B15, sck:B13, baud:4000000});
    
    var NumLEDs = 150;
    var leds = new Uint8Array(NumLEDs*4);
    
    function draw() {
      var millis = getTime();
    
      var r = Math.random()*255;
      var g = Math.random()*255;
      var b = Math.random()*255;
      for (var pixel = 0; pixel < leds.length; pixel+=4)
      {
        leds[pixel+0] = 0xE0 | 5;
        leds[pixel+1] = b;
        leds[pixel+2] = g;
        leds[pixel+3] = r;
      }
      write_leds(leds, NumLEDs);
    }
    
    function write_leds(leds, numLeds) {
      var halfLed = numLed/16 + 1; // compute end frame length
      var maxIndex = numLed * 4; // maximum leds row index;
      var index; 
      SPI2.write(0x00);
      SPI2.write(0x00);
      SPI2.write(0x00);
      SPI2.write(0x00);
      
      // LED Frame
      for (index = 0; index < maxIndex; index+=4) {
        SPI2.write(leds[index+0]);  // global brigthness 
        SPI2.write(leds[index+1]);  // B
        SPI2.write(leds[index+2]);  // G
        SPI2.write(leds[index+3]);  // R
      } // for all leds
    
      // End Frame
      for (index = 0; index < halfLed; index++) {
        SPI2.write(0xFF);
      } // for end frame
    } // write_leds()
    
    function onInit() {
      setInterval(draw, 50);
    }
    
    onInit();
    

    But it seems that the data I send is not the data I see.
    Also if I change the pixel data to show only red pixels; there is always a kind of random colors to see :/

    Hope someone can help me here to fix the problem.

  • It's probably that the data is getting sent out too slowly so the lights think its the start of a frame - in Espruino you can just supply the whole array of data at once, like this:

    SPI2.setup({mosi:B15, sck:B13, baud:4000000});
    var NumLEDs = 150;
    var leds = new Uint8Array(8 + NumLEDs*4);
    function draw() {
      var millis = getTime();
      var r = Math.random()*255;
      var g = Math.random()*255;
      var b = Math.random()*255;
      for (var pixel = 4; pixel < leds.length-4; pixel+=4)
      {
        leds[pixel+0] = 0xE0 | 5;
        leds[pixel+1] = b;
        leds[pixel+2] = g;
        leds[pixel+3] = r;
      }
      write_leds(leds, NumLEDs);
    }
    
    function write_leds(leds, numLeds) {
      // set start frame
      leds[0] = 0;
      leds[1] = 0;
      leds[2] = 0;
      leds[3] = 0;
      // set end frame
      leds[leds.length-4] = 0xff;
      leds[leds.length-3] = 0xff;
      leds[leds.length-2] = 0xff;
      leds[leds.length-1] = 0xff;
      // Send the data
      SPI2.write(leds);
    } // write_leds()
    
    function onInit() {
      setInterval(draw, 50);
    }
    onInit();
    

    You can use ArrayBufferViews so that you can get a 0-based index for the LEDs, but for now it's probably simpler to just use the single array, and start your LED indices from 4.

  • Thx @Gordon - this is working.
    But I have now another problem...
    After some tries to update the code it seems to hang.
    Only unplug and replug again helps to flash new code.
    But after some reconnects/replugs - the pico is dead :/
    If I plug it into USB it only shows short red light, but did not appear in the device list.
    On Reset+plugin - red + green light is on (no flashing like on firmware flash mode)

    Do you know, what this could be and how to solve it?

  • Have you checked your USB cable? Maybe try plugging in direct?

    That kind of thing usually happens if power is making it to the Pico, but one or both of the USB data pins aren't making contact.

  • Ah interesting...
    I will try this later. Already try some other Picos and they worked on same cable and USB port.
    It could be that the "usbport" of Pico is to scratchy and did not work correct anymore.

  • I do have one (Apple?) USB extension cable with a shitty non-standard socket that scratches away the silkscreen from the back of a Pico. See the image below:

    If it's like that, it would have shorted a USB data pin to GND, which would cause your problems.


    1 Attachment

    • usb_extensions.jpg
  • Damn! this could be the problem. I also use the apple extension cable.
    What can I do - if it is scratched?

  • The fix is easy though. Just cut into the back of the Pico where I drew the red line, cutting through the traces that are there.

    Or use another USB extension. Any sensible one will have little sprung bits of metal in it, spaced either side of the centre... and those won't gouge into the PCB and won't short out the data lines even on the board where the Apple extension went through.

    I shouldn't have added a Micro USB socket outline. I wonder how many people have even used it? Maybe it's something I should just remove on the next revision... It looks like Mini USB wouldn't cause any problems, it's just having the Micro one that sucks.


    1 Attachment

    • pico_cut_usb.jpg
  • I will try this - just another extension cable - if not working, than shorten the traces.
    Thank Gordon for your help and the pictures. I will report as soon as possible

  • Update:
    *default cable extension > not working
    *cutting micro-usb lanes > not working

    it seems that the board is dead :(
    it has power (short red flash), but no communication (device not in USB-device list)

  • I'd ohm it out to verify that no data lines are shorted to eachother or to ground/vcc... since it sounds like that was the cause of the problem. It can be incredibly hard to separate two traces, because the metal gets "smeared" along the cut.

    And yeah - I don't think many people are using the micro, because those surface mount micro connectors are crap. I don't know about anyone else, but I absolutely use the mini ones because I don't like the bulky extension connectors, and the average quality of extension cables seems to be terrible despite higher prices. 4 ohm resistance along a 1 meter cable's V+ line - and 3 of the 5 I had were like that (well, the other brand was 3.x). Most mini and micro cables are usable for their intended purpose for a while at least (I have a friend who's gone through two micro-USB connectors on her phone - I just ordered her second replacement - and has gone through dozens of micro usb cables. Micro-USB was supposed to have longer operation life, but I can't reconcile that with my experience nor hers - the mini connector seems much sturdier)

  • @DrAzzy So do you think I should try to solder on a mini connector?
    Or what can I try to see if my pico is dead or not?

  • @Jorgen wow, strange. As @DrAzzy says, can you check that the data traces aren't shorted? Also, where you did the cut should be above the area of PCB that got scraped off by the Apple connector.

    The other thing to watch out for is whether the resonator (tin can) on the other end of the board has got damaged somehow. Maybe you were soldering to the 0.05" pins and accidentally shorted out to it? Without the crystal Espruino will still bravely soldier on and boot, but won't be able to get USB to work :)

    On these chips, the IOs (especially USB) are pretty heavy duty. It's quite hard to blow them up so I'd have thought we could find a way to get it going again.

    @DrAzzy yeah, I'll try and get it taken off the rev 1.4 then. It'd stop this happening in the future. Frustrating really - just because someone asks for it on KickStarter doesn't mean they're representative of real users :)

  • @Jorgen Did you use a separate power supply for the leds? If not you probably burnt something on the Pico. This would also explain the random colours from post #1.
    @Gordon What burns normally first on Pico? Regulator? Capacitor?

  • @Spocki yes I do use a external power supply (5V 1A)
    I used a similar setting as shown on these picture:

    1. 5V connected to 5V power supply
    2. GND connected to 5V power supply + Pico GND (without connection GND also to Pico, everything is random and not stable)
    3. Data connected to Pico MOSI
    4. Clock connected to Pico SCK
  • @Gordon I will check this on weekend.
    But I decided to order two more Picos on Tindie.

    • one I have given to a friend
    • one with shim (ESP-01)
    • one with shim (ESP-12)
    • one with shim (HM-10)
    • the only one without any shim on, is the "broken" one...
  • Thanks! Hopefully you will be able to get the original working though - I can't believe it's that badly broken.

  • I take it you did check that none of the USB pins are shorted now?

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

APA102 + Pico (problem on write data)

Posted by Avatar for Jorgen @Jorgen

Actions