# Demo: APA102 RGB LED

## Introduction

This tutorial describes how to connect and operate [APA102](https://www.advateklights.com/knowledge-base/apa102) LEDs on [MR-CANHUBK3](https://nxp.gitbook.io/mr-canhubk344/) board and control them using Zephyr RTOS.

## Items needed

<table><thead><tr><th width="103.33333333333331">S. No.</th><th width="470">Name</th><th>Quantity</th></tr></thead><tbody><tr><td>1</td><td><a href="https://nxp.gitbook.io/mr-canhubk344/">MR-CANHUBK3</a></td><td>1</td></tr><tr><td>2</td><td>USB-A (Male) to 2-Wire (Female) Power Cable</td><td>1</td></tr><tr><td>3</td><td><a href="https://nxp.gitbook.io/hovergames/rddrone-fmuk66/connectors/debug-interface-dcd-lz/dcd-lz-breakout-board">DCD-LZ Breakout Board</a></td><td>1</td></tr><tr><td>4</td><td><a href="https://probots.co.in/usb-to-serial-converter-adapter-module-with-ttl-ftdi-cable-6-pins.html">USB-A (Male) to TTL 6-Pin Serial Adaptor Cable (Female)</a></td><td>1</td></tr><tr><td>5</td><td><a href="https://www.adafruit.com/product/5754">JST GH 1.25mm Pitch 7 Pin Cable</a> (Female to Female)</td><td>1</td></tr><tr><td>6</td><td><a href="https://www.segger.com/products/debug-probes/j-link/models/j-link-edu-mini/">J-Link EDU Mini</a></td><td>1</td></tr><tr><td>7</td><td><a href="https://www.adafruit.com/product/1675">.05" 10-Pin Ribbon Cable</a></td><td>1</td></tr><tr><td>8</td><td><a href="https://robu.in/product/10-inch-micro-usb-cable-a-to-micro-b/">Micro-USB (Male) to USB-A (Male) Cable</a></td><td>1</td></tr><tr><td>9</td><td>3-LED (daisy chained) APA102 Strip</td><td>4</td></tr><tr><td>10</td><td><a href="https://www.adafruit.com/product/4401">STEMMA QT / Qwiic JST SH 4-Pin Cable</a></td><td>4</td></tr></tbody></table>

{% hint style="info" %}
Items #9 and #10 could be replaced with any similar setup of 12 APA102 LEDs daisy chained together.
{% endhint %}

## Hardware procedure

You will connect a SPI port (Zephyr lpspi4) on the CANHUBK344 Pin headers to drive the clock and data signal going to the APA102 LEDs. Note that in application, it may be desireable to use a different SPI connection and provide 5V power independently to the LEDs.)

<figure><img src="https://4107432906-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtwBzyvivLuBKj9SMDwV9%2Fuploads%2FkFk2ba9u5j40zHWEufLT%2Fblock_diagram.svg?alt=media&#x26;token=79467ec2-a500-4d35-9081-0988dbc137ac" alt=""><figcaption><p>Block diagram of CANHUBK344 connecting to string of APA102 LEDs, plus debugger and serial consolde.</p></figcaption></figure>

### Connections from APA102 to P8B (Connector that houses LPSPI4)

<table><thead><tr><th width="137" align="center">APA102 Pin</th><th width="154">Name</th><th width="99">Color</th><th>PIN # on connector P8B on CANHUBK344</th></tr></thead><tbody><tr><td align="center">1</td><td>Voltage</td><td>Red</td><td>2</td></tr><tr><td align="center">2</td><td>Ground</td><td>Black</td><td>1</td></tr><tr><td align="center">3</td><td>SPI Clock</td><td>Yellow</td><td>17 (LPSPI4 Clock)</td></tr><tr><td align="center">4</td><td>SPI MOSI</td><td>Blue</td><td>18 (LPSPI4 MOSI)</td></tr></tbody></table>

### Create your own SPI to STEMMA QT cable

{% hint style="success" %}
You will need to convert one end of a **STEMMA QT / Qwiic JST SH 4-Pin Cables** to female connectors in order to plug into the male pins of the P8B port.&#x20;
{% endhint %}

### Other SPI interfaces with JST-GH

{% hint style="info" %}
Note there are also [two other SPI interface on CANHUBK344](https://nxp.gitbook.io/mr-canhubk344/hardware/connectors-and-user-i-o/spi) which have a 7 pin JST-GH plug. Alternatively a cable could be prepared to connect to one of these interefaces. Teh Zephyr overlay would need to also be modified to match the SPI port chosen.
{% endhint %}

### Alternative sources for 5V LED power

{% hint style="warning" %}
NOTE the power available from the 5V supply on MR-CANHUBK344 is limited. It is quite likely that too many LEDs fully illuminated will draw too much power from the board. In this case 5V power should be supplied from somewhere else. \
Good options are:\
\- A "BEC" (RC Hobby DC-DC converter) that is typically used to supply 5V to servos.\
\- The 5V power output from PDB board on the MR-B3RB (NXP Buggy3 RevB)
{% endhint %}

### SPI and Power connection on MR-CANHUBK344 header for LEDs in this Demo

<figure><img src="https://4107432906-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtwBzyvivLuBKj9SMDwV9%2Fuploads%2FI5lMfALTIbUWpUv6tSVD%2FScreenshot%202023-10-06%20101512.png?alt=media&#x26;token=d035704c-8960-43b6-a6be-3b3a0f40dff2" alt=""><figcaption><p>P8B connector on CANHUBK3 for connecting APA102 LED to LPSPI4 in Zephyr</p></figcaption></figure>

<figure><img src="https://4107432906-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtwBzyvivLuBKj9SMDwV9%2Fuploads%2FzVFGGILOyEP4X3bk94MB%2Fconnections_left_view.jpg?alt=media&#x26;token=d9e6282d-9e80-4f55-a18b-1dda374a2fb2" alt=""><figcaption><p>Wiring connections from APA102 to P8B (side view)</p></figcaption></figure>

## Software procedure

### Initialize west workspace

```
mkdir apa102_canhubk3
west init -m git@github.com:zephyrproject-rtos/zephyr.git apa102_canhubk3
cd apa102_canhubk3
west update
cd zephyr
git checkout a04a1d8991e312ece8530f0ab35b444010113fb9
```

{% file src="<https://4107432906-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtwBzyvivLuBKj9SMDwV9%2Fuploads%2Fqq1O5saoWQoAwcDwiAvb%2Fapa102_canhubk3_v3.0.patch?alt=media&token=283ba546-6ea1-42c6-ba0f-6f49f1ae75b5>" %}
APA102 LED Patch
{% endfile %}

```
git apply apa102_canhubk3_v3.0.patch
```

### Building & flashing "led\_apa102" application

This application displays a 3-colour pattern (red - green - blue) that walks the strip until it reaches the end, then starts at the beginning again; by default, the delay between each message is 40 ms.

{% hint style="info" %}
By default, this application is configured for 20 APA102 LED daisy chained together. Hence, you will see a delay in motion if you have less that 20 LEDs.

Optionally, you may change the value of the "STRIP\_NUM\_LEDS" macro in "zephyr/samples/drivers/led\_apa102/src/main.c" to the number of LEDs in your setup.
{% endhint %}

```
west build -b mr_canhubk3 samples/drivers/led_apa102/ -p
west flash
```

### Video demo 1

{% file src="<https://4107432906-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtwBzyvivLuBKj9SMDwV9%2Fuploads%2F9koXZJl1GvScr0S9uEoN%2Fled_apa102_demonstration.mp4?alt=media&token=a63f8806-931a-40dc-9161-c9ed19f1aa35>" %}
Demo video for "led\_apa102" application
{% endfile %}

### Building & flashing "led\_apa102\_indicators" application

This application demonstrates the lighting of **turn indicators** for vehicles. It cycles between the four turn indicators - front left, front right, back left and back right. These turn indicators have LED patterns respectively: 2 → 1 → 0, 3 → 4 → 5, 8 → 7 → 6 and 9 → 10 → 11.

```
west build -b mr_canhubk3 samples/drivers/led_apa102_indicators/ -p
west flash
```

### Video Demo 2

{% file src="<https://4107432906-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtwBzyvivLuBKj9SMDwV9%2Fuploads%2FPRubJ5sqwtPQl9X1YnPQ%2Fled_apa102_indicator_demonstration.mp4?alt=media&token=005d3d21-dd83-410b-8b75-f72787bf312e>" %}
Demo video for "led\_apa102\_indicators" application
{% endfile %}

### LED Daisy Chain configuration

{% hint style="danger" %}
This application requires 12 APA102 LED daisy chained together as explained in the diagram below, but due to hardware constraints videos above show only the first 9 LED.
{% endhint %}

<figure><img src="https://4107432906-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtwBzyvivLuBKj9SMDwV9%2Fuploads%2FOWJBxuaL43vaZ6xuLeh5%2Fapa102_indicator_application_connections_1.jpg?alt=media&#x26;token=7ea1901c-3f6f-4305-908c-584d7bf38f1a" alt=""><figcaption><p>APA102 LED connection diagram for "led_apa102_indicators"</p></figcaption></figure>

<figure><img src="https://4107432906-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtwBzyvivLuBKj9SMDwV9%2Fuploads%2FASrEyfR1rm8tbvTRj9RX%2Fapa102_indicator_application_connections_2.jpg?alt=media&#x26;token=00a07ba4-0410-43bf-9fee-6f1a18434345" alt=""><figcaption><p>APA102 LED connection demonstration for "led_apa102_indicators"</p></figcaption></figure>
