# LPC824 NeoPixel IR Distance Sensor

## Overview

This advanced project shows you how you can drive a [16 NeoPixel ring](https://www.adafruit.com/product/1463) and [Sharp IR distance sensor](https://www.adafruit.com/product/1031) using an inexpensive ARM Cortex M0+&nbsp;[LPC824](http://www.nxp.com/products/microcontrollers-and-processors/arm-processors/lpc-cortex-m-mcus/lpc800-series-cortex-m0-plus-mcus/low-cost-32-bit-microcontroller-based-on-arm-cortex-m0-plus:LPC824M201JHI33). The LPC824 was used&nbsp;due to it's low cost, and&nbsp;an interesting peripheral it has called the&nbsp; **State Configurable Timer** , which allows us to offload the tight NeoPixel timing requirements to a HW peripheral.

Danger: 

This was largely a personal project, wanting to keep up to date with recent members of the LPC800 device family from NXP, but it's being published here in the hope that it proves useful to other people interested in driving NeoPixels with a small, but powerful and flexible MCU like the LPC824!

# Hardware Requirements

- [NeoPixel Ring - 16 x 5050 LEDs](https://www.adafruit.com/product/1463)&nbsp;(RGB)
- [Sharp IR Distance Sensor - GP2YOA02YK](https://www.adafruit.com/product/1031)
- [LPCXpresso824-Max Development Board](https://octopart.com/search?q=OM13071): This board isn't available from Adafruit, but can be purchased from a large number of distributors (click the link to see a list on Octopart).

# End Result

The final project will give you something resembling this:

![](https://cdn-learn.adafruit.com/assets/assets/000/040/961/medium800thumb/proximity_LPC824DistanceAnimated.jpg?1492706599)

# LPC824 NeoPixel IR Distance Sensor

## Software

Danger: 

The project was written entirely in C using the free cross-platform [LPCXpresso IDE](http://www.nxp.com/products/microcontrollers-and-processors/arm-processors/lpc-cortex-m-mcus/lpc1100-cortex-m0-plus-m0/lpcxpresso-ide-v8.2.2:LPCXPRESSO) (version 8.2.2 specifically). To add support for the LPC824 MCU, the [LPC824 Code Bundle for LPCXpresso](http://www.nxp.com/assets/downloads/data/en/software/LPC824_Example_Code_Bundle_LPCXpresso_r1.0.zip) (version 1) was also downloaded and installed.

You can perform the following steps to make the software available locally on your development machine:

1. [Download](http://www.nxp.com/products/microcontrollers-and-processors/arm-processors/lpc-cortex-m-mcus/lpc1100-cortex-m0-plus-m0/lpcxpresso-ide-v8.2.2:LPCXPRESSO) and install the LPCXpresso 8.2.2 IDE for your operating system
2. Download the [LPC824 Code Bundle for LPCXpresso](http://www.nxp.com/assets/downloads/data/en/software/LPC824_Example_Code_Bundle_LPCXpresso_r1.0.zip) (version 1)
3. Download the Project Code Bundle, available as a [.zip file](https://github.com/microbuilder/LPC824_NeoPixel_DistanceSensor/raw/master/CodeBundle/NeoPixelDistanceSensor_13April2017.zip) in the Github repo.

## Importing the LPC824 Code Bundle

Before you can import the project files, you need to import the LPC824 Code Bundle, which includes all the low level drivers for this project:

- Click the&nbsp; **Import Projects** button in the QuickStart menu in the bottom right-hand corner of the IDE:

![](https://cdn-learn.adafruit.com/assets/assets/000/040/942/medium800/proximity_Screen_Shot_2017-04-20_at_17.21.31.png?1492701767)

- In the dialogue box that pops up, select the appropriate .zip file in the&nbsp; **Archive** field:

![](https://cdn-learn.adafruit.com/assets/assets/000/040/943/medium800/proximity_Screen_Shot_2017-04-20_at_17.21.59.png?1492701815)

- Click the&nbsp; **Finish** button to import the files, which should results in your Project Explorer looking like this:

![](https://cdn-learn.adafruit.com/assets/assets/000/040/944/medium800/proximity_Screen_Shot_2017-04-20_at_17.24.20.png?1492701877)

## Importing the Project Code Bundle

Next, you need to import the project code bundle, which is provided as a .zip file for easy import.

- Select the&nbsp; **Import project** menu option again in the Quickstart window:

![](https://cdn-learn.adafruit.com/assets/assets/000/040/945/medium800/proximity_Screen_Shot_2017-04-20_at_17.26.08.png?1492702006)

- Now point to the project Code Bundle in the&nbsp; **Archive** field:

![](https://cdn-learn.adafruit.com/assets/assets/000/040/946/medium800/proximity_Screen_Shot_2017-04-20_at_17.27.08.png?1492702069)

- Click the&nbsp; **Finish** button and you should have a new project in the Project Explorer called&nbsp; **lpc824\_neopixel** :

![](https://cdn-learn.adafruit.com/assets/assets/000/040/947/medium800/proximity_Screen_Shot_2017-04-20_at_17.28.21.png?1492702123)

You can expand this menu option to see the source code for this project, double-clicking on&nbsp; **main.c** to see the project entry point:

![](https://cdn-learn.adafruit.com/assets/assets/000/040/948/medium800/proximity_Screen_Shot_2017-04-20_at_17.28.58.png?1492702177)

- At this point, you can access and modify the source code if required:

![](https://cdn-learn.adafruit.com/assets/assets/000/040/949/medium800/proximity_Screen_Shot_2017-04-20_at_17.29.49.png?1492702241)

# NeoPixel Setup

The included project supports both&nbsp; **RGB** and&nbsp; **RGBW** NeoPixels, and you can adjust the number of NeoPixels present if you aren't using an identical NeoPixel layout.

Before proceeding any further, make sure the following two values are set to match the NeoPixel setup you are using (these can be found in&nbsp; **main.c** ):

Info: 

```auto
#define	WS2812_LED_CNT	(16)
#define WS2812_LED_CLRS	(3)					// 3 = RGB, 4 = RGBW

```

![](https://cdn-learn.adafruit.com/assets/assets/000/040/958/medium800/proximity_Screen_Shot_2017-04-20_at_18.01.12.png?1492704092)

## Compiling the Project

To make sure everything is installed correctly, you can try to compile the project as follows:

- Make sure that the correct project is selected in the&nbsp; **Project Explorer** window on the left-hand side of the IDE
- Connect your LPCXpresso824-Max board to your development machine via the USB cable (the board includes an on board SWD debugger that will be used to flash and debug the LPC824 MCU)
- Select the **Project \> Build Project** menu item to build the project as well as any project depencies (peripherals and utilities libraries in this case):

![](https://cdn-learn.adafruit.com/assets/assets/000/040/952/medium800/proximity_Screen_Shot_2017-04-20_at_17.38.39.png?1492702744)

- Optionally, you can select the&nbsp;**Build 'lpc824\_neopixel' (Debug)** QuickStart menu item:

![](https://cdn-learn.adafruit.com/assets/assets/000/040/953/medium800/proximity_Screen_Shot_2017-04-20_at_17.39.58.png?1492702857)

- If you get a popup dialogue box (this should only appear once) select the&nbsp; **lpc824\_neopixel Debug** profile, as shown below, then click the&nbsp; **Run&nbsp;** button:

![](https://cdn-learn.adafruit.com/assets/assets/000/040/951/medium800/proximity_Screen_Shot_2017-04-20_at_17.35.32.png?1492702608)

- If everything worked correctly, you should see the compilation results in the Console window at the bottom of the IDE:

![](https://cdn-learn.adafruit.com/assets/assets/000/040/954/medium800/proximity_Screen_Shot_2017-04-20_at_17.41.30.png?1492702933)

- To flash and run the compiled software, click the&nbsp;**Debug 'lpc824\_neopixel' (Debug)** QuickStart menu item with the board connected to your machine:

![](https://cdn-learn.adafruit.com/assets/assets/000/040/955/medium800/proximity_Screen_Shot_2017-04-20_at_17.43.07.png?1492702999)

- This will cause the IDE to connect to the debugger, flash the sketch, and stop code execution at the project entry point, which is&nbsp;**main(void)**:

![](https://cdn-learn.adafruit.com/assets/assets/000/040/956/medium800/proximity_Screen_Shot_2017-04-20_at_17.43.39.png?1492703074)

- To continue code execution use the&nbsp;**Resume (F8)** or other toolbar icons or keyboard shortcuts:

![](https://cdn-learn.adafruit.com/assets/assets/000/040/957/medium800/proximity_Screen_Shot_2017-04-20_at_17.44.46.png?1492703132)

# LPC824 NeoPixel IR Distance Sensor

## Hardware

## Pinout

To run this demo, connect the NeoPixel ring to the LPC824Max development board as follows:

- Connect the **DATA/DIN** pin on the Neopixels to **P0.22** (A3 on the LPC824MAX Arduino header)
- Connect&nbsp; **Power 5V DC** on the NeoPixel to the&nbsp; **+5V** pin on the LPC824Max
- Connect&nbsp; **GND** on the NeoPixel to&nbsp; **GND** on the LPC824Max

Then connect the Sharp IR distance sensor as follows:

- Connect the **yellow cable** (analog output) to&nbsp; **P0.6&nbsp;** (A0 on the LPC824Max Arduino header)
- Connect the&nbsp; **red cable** &nbsp;to the&nbsp; **+5V** &nbsp;pin on the LPC824 Max
- Connect the&nbsp; **black cable** to&nbsp; **GND** on the LPC824Max

## Physical Wiring Details

There are numerous ways to solder this project together, depending on the project requirements, but for convenience sake&nbsp;the NeoPixel ring and Sharp IR Sensor were connected to the LPC824MAX development board with&nbsp;[silicon wiring](https://www.adafruit.com/product/2003)&nbsp;since it's heat resistant, flexible, and is easy to work through the back of the enclosure.

The ends of the two sets of three cables were soldered to two sets of 3-pin wide [0.1" male headers pins](https://www.adafruit.com/product/3002), which were then inserted into a [tiny breadboard](https://www.adafruit.com/product/65):

Danger: 

![](https://cdn-learn.adafruit.com/assets/assets/000/040/960/medium800/proximity_HeaderSoldering.jpg?1492704429)

# 3D Printed Enclosure

To get the project off the table and into a usable form factor, a 3D printed enclosure was designed for the NeoPixel ring and the Sharp IR distance sensor.

The STL files are available with the [source code on Github](https://github.com/microbuilder/LPC824_NeoPixel_DistanceSensor/tree/master/DesignFiles), and consist of two mandatory parts, and an optional third part:

- [Standv1.stl](https://github.com/microbuilder/LPC824_NeoPixel_DistanceSensor/blob/master/DesignFiles/Standv1.stl): A stand to support the NeoPixel ring and Sharp IR sensor
- [Neopixelv5.stl](https://github.com/microbuilder/LPC824_NeoPixel_DistanceSensor/blob/master/DesignFiles/Neopixelv5.stl): A circular enclosure for the NeoPixel ring and Sharp IR sensor that can be screwed onto the base
- [Neopixelv5\_diffuserv4.stl](https://github.com/microbuilder/LPC824_NeoPixel_DistanceSensor/blob/master/DesignFiles/Neopixelv5_diffuserv4.stl): An optional third diffusion ring that can be printed in a semi-opaque material and snapped on top of the circular enclosure if desired

## Preview

A rendered image of the various parts can be seen below. The development board and wires aren't shown.

![](https://cdn-learn.adafruit.com/assets/assets/000/040/941/medium800/proximity_Neopixel_demo_final_v2.3.png?1492700320)

# LPC824 NeoPixel IR Distance Sensor

## Downloads

## Software &nbsp;Development

- [LPC824\_NeoPixel\_DistanceSensor](https://github.com/microbuilder/LPC824_NeoPixel_DistanceSensor)&nbsp;- Project and 3D Files
- [LPCXpresso 8.2.2 IDE](http://www.nxp.com/products/microcontrollers-and-processors/arm-processors/lpc-cortex-m-mcus/lpc1100-cortex-m0-plus-m0/lpcxpresso-ide-v8.2.2:LPCXPRESSO)
- [LPC824 Code Bundle for LPCXpresso (Rev 1)](http://www.nxp.com/assets/downloads/data/en/software/LPC824_Example_Code_Bundle_LPCXpresso_r1.0.zip)

## Documentation

- [LPC824 Product Summary](http://www.nxp.com/products/microcontrollers-and-processors/arm-processors/lpc-cortex-m-mcus/lpc800-series-cortex-m0-plus-mcus/low-cost-32-bit-microcontroller-based-on-arm-cortex-m0-plus:LPC824M201JHI33)
- [LPC824 Datasheet](http://www.nxp.com/documents/data_sheet/LPC82X.pdf) - Key technical details
- [LPC824 User Manual](http://www.nxp.com/documents/user_manual/UM10800.pdf) - Programming manual for the MCU

## Hardware

- [LPCXpresso824-MAX Development Board](http://www.nxp.com/products/microcontrollers-and-processors/arm-processors/lpc-cortex-m-mcus/software-tools/lpcxpresso-boards/lpcxpresso824-max-board-for-lpc82x-family-mcus:OM13071)
- [NeoPixel Ring - 16 x 5050 LEDs](https://www.adafruit.com/product/1463)&nbsp;(RGB)
- [Sharp IR Distance Sensor - GP2YOA02YK](https://www.adafruit.com/product/1031)
- Optionally: [NeoPixel Ring - 16 x 5050 RGBW LEDs](https://www.adafruit.com/product/2855)


## Related Guides

- [Thermal Camera with Display](https://learn.adafruit.com/thermal-camera-with-display.md)
- [Superhero Power Gauntlet](https://learn.adafruit.com/superhero-power-gauntlet.md)
- [Distance Measurement with Ultrasound](https://learn.adafruit.com/distance-measurement-ultrasound-hcsr04.md)
- [NeoPixel LED Necklace Insert with USB Charging](https://learn.adafruit.com/neopixel-led-necklace-insert-with-usb-charging.md)
- [Boomy Pi Airplay Boombox](https://learn.adafruit.com/boomy-pi-airplay.md)
- [CircuitPython Trombone Champ Controller](https://learn.adafruit.com/circuitpython-trombone-champ-controller.md)
- [Guardian Robot – Zelda BOTW](https://learn.adafruit.com/guardian-robot-zelda-botw.md)
- [reef-pi Guide 2: Power Controller](https://learn.adafruit.com/reef-pi-power-controller.md)
- [MUNNY Glowing Friend with Bluetooth Control!](https://learn.adafruit.com/munny-lamp.md)
- [Raspberry Pi Halloween Costume Detector](https://learn.adafruit.com/raspberry-pi-halloween-costume-detector.md)
- [Adafruit QT Py and NeoPixel LEDs](https://learn.adafruit.com/qt-py-and-neopixel-leds.md)
- [Severance Portable Macrodata Refinement Terminal](https://learn.adafruit.com/portable-macrodata-refinement-terminal.md)
- [Four Channel Audio Passive Stereo Mixer](https://learn.adafruit.com/four-channel-audio-passive-stereo-mixer.md)
- [Minecraft GPS Compass](https://learn.adafruit.com/minecraft-gps-compass.md)
- [Owl IR TV Remote](https://learn.adafruit.com/owl-ir-tv-remote.md)
