# DEMO: APA102 RGB LED control via UCANS32K146 with PX4-Autopilot

## Introduction

\
This tutorial explains how to operate 3-LED APA102 RGB board via UCANS32K146 over SPI.

## Items Needed

* An APA102 based RGB LED board&#x20;
* UCANS32K146 or UCANS32K1SIC
* Jumper wires
* STEMMA QT / Qwiic JST SH 4-pin Cable
* Segger J-Link debugger

## APA102 LED Board

#### Connections

The input connector of 3-LED APA102 board has 4 wired connections: SDI, CKI, PWR, GND. To connect the LED board to the UCAN board, refer the configuration of 'stemma qt / qwiic jst sh cable'. The connections would be as follows:

<table><thead><tr><th width="268">3-LED APA102 Board Input</th><th width="124.33333333333331">Name</th><th>SPI Port (P1) Pin Number on UCAN</th></tr></thead><tbody><tr><td>Red</td><td>PWR (5V)</td><td>1</td></tr><tr><td>Yellow</td><td>CLK</td><td>2</td></tr><tr><td>Blue</td><td>MOSI</td><td>4</td></tr><tr><td>Black</td><td>GND</td><td>7</td></tr></tbody></table>

#### ![](https://2042189127-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M7FJ_hQKd8L0MNgduui%2Fuploads%2FjK7Y5LebkuEWZW8Sc70W%2F2023-04-19%2015.27.15.jpg?alt=media\&token=ecc64342-f43b-4ac9-a05b-bfb52e999bd5)![](https://2042189127-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M7FJ_hQKd8L0MNgduui%2Fuploads%2FW49mM7Q2RRTdAcOK2lBK%2F2023-04-19%2015.27.46.jpg?alt=media\&token=5a97dab8-646e-4b4a-9c51-8c7b4f853ba7)

#### Network

All the LEDs in the board are daisy chained together. Note: These ***boards*** can also be daisy chained from one board to the next. From the software point of view these are just considered one long string of LEDs daisy chained together.

<figure><img src="https://2042189127-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M7FJ_hQKd8L0MNgduui%2Fuploads%2FLvq1RS8GgcAlGA22fYFw%2FAPA102-RGB-LED-Circuit-Diagram.jpg?alt=media&#x26;token=f5fb1a63-dab5-4120-9b73-028640441bc8" alt=""><figcaption></figcaption></figure>

## Software Procedure

Note: You may refer the steps for building and flashing PX4 on this gitbook [link](https://nxp.gitbook.io/ucans32k1/px4-autopilot/building-and-flashing-px4).

#### Software Setup

```
git clone -b UCANS32K146 https://github.com/NXPHoverGames/PX4-Autopilot.git --recursive
cd PX4-Autopilot
```

Change the value of 'NUM\_LEDS' in '/platforms/nuttx/NuttX/apps/examples/apa102/apa102\_main.c' to the number of LEDs in your APA102 board. For our case, it will be '3'.

#### Building PX4

```
make nxp_ucans32k146_canbootloader
  // output: build/nxp_ucans32k146_canbootloader/nxp_ucans32k146_canbootloader.bin
make nxp_ucans32k146
  // output: build/nxp_ucans32k146_default/deploy/34.bin
```

After compilation is done, the new firmware is available under: *build/nxp\_ucans32k146\_default/deploy/34.bin*

#### Flashing PX4

Flash the firmware and bootloader, if the board already doesn't have one, using 'jlink.exe'.&#x20;

{% hint style="info" %}
You may flash the attached files (configured for a 3-LED APA102 board), to skip the above steps.
{% endhint %}

{% file src="<https://2042189127-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M7FJ_hQKd8L0MNgduui%2Fuploads%2F6no8qV4OYRmjbu3QPe6X%2Fnxp_ucans32k146_canbootloader.bin?alt=media&token=d49828fc-54da-4bd6-ab15-97edded1723b>" %}
PX4-Autopilot Bootloader
{% endfile %}

{% file src="<https://2042189127-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M7FJ_hQKd8L0MNgduui%2Fuploads%2F5Tioc3XNF29L7QKSZfmv%2F34.bin?alt=media&token=3e659e72-e91d-402d-a232-90dcaa759229>" %}
PX4-Autopilot Firmware
{% endfile %}

```
connect
// specify the following:
  // device: s32k146;
  // target interface: s (which stands for swd);
  // interface speed: 1000 khz (recommended);
loadbin <path to nxp_ucans32k146.bin> 0x0
loadbin <path to 34.bin> 0x6000
// quit jlink tools by typing 'q'.
```

### Operating the LEDs

Connect your board and access to the terminal using MobaXterm, minicom,...

Type the following command:&#x20;

```
apa102
```

<figure><img src="https://2042189127-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M7FJ_hQKd8L0MNgduui%2Fuploads%2FWOdzzw4lWLMW2VyoJJsd%2Fdemonstration.gif?alt=media&#x26;token=cb003638-99f9-40b8-a767-5cfc2226520e" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nxp.gitbook.io/ucans32k1/ucans32k1sic-demo-application/demo-apa102-rgb-led-control-via-ucans32k146-with-px4-autopilot.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
