# Madison's NeoClock - A PIC + KiCAD + NeoPixel adventure

## The Backstory

My name is David Littell. &nbsp;I'm an embedded systems engineer with about 35 years experience with microprocessors, computer hardware, software, and systems of all sizes.&nbsp; I was one of "those" kids that was constantly taking apart (and sometimes even fixing!) all things electronic.&nbsp; I grew up spending my allowance at Radio Shack for kits, books, and parts and my nights with fingers glued to shortwave radios.&nbsp; At age 15 I built a Processor Technology Sol-20 from the kit - one of the only kids in my home state to have a computer (in 1978).&nbsp; It worked at first power-up.&nbsp; (I still have it and it (probably) still works - resurrection is yet another item on the bucket list.)

Fast-forward a few decades and I now have a daughter that loves when Daddy builds things for her.&nbsp; I had noticed the NeoPixels at Adafruit and thought they'd be interesting to work with, so I ordered a NeoPixel Ring-12 and started studying the Data Sheet.&nbsp; Slippery slope, that...

One idea led to another led to an avalanche of "Hey! This might actually work!&nbsp; What about _this_…" and the next thing I knew I was learning KiCad to design my first board to drive NeoPixel rings to make a desk clock for my daughter.&nbsp; Enter Madison's NeoClock:

https://www.youtube.com/watch?v=2_uGvP6pOAg&amp;feature=youtu.be

Well, these _are_ the famed NeoPixels so I wanted to use color in a sane but interesting way.

![](https://cdn-learn.adafruit.com/assets/assets/000/018/824/medium800/projects_1550_LowLight.jpg?1408068014)

https://www.youtube.com/watch?v=Q6PsgXI_lSs&amp;feature=youtu.be

And **wow** will they light up a room at night!&nbsp; Sleeping in sunglasses isn't all that comfortable, so what can be done about that? &nbsp;Should it throttle back based on only time or can it somehow know the brightness around itself?

![](https://cdn-learn.adafruit.com/assets/assets/000/018/826/medium800/adafruit_products_2125_LowLight.jpg?1408069037)

Where to begin? &nbsp;Where the rubber meets the road, I think: &nbsp;creating a&nbsp;bitstream for the NeoPixels. &nbsp;There are many options that work well but I already had a few Microchip PIC's that I'd been using in other projects. &nbsp;Can this do the job?&nbsp; Time to visit&nbsp;those WS2812 and PIC Data Sheets...

# Madison's NeoClock - A PIC + KiCAD + NeoPixel adventure

## Rubber, meet Road

The tough part about using NeoPixels is the 1-wire control protocol. Frankly, looking at the WS2812 Data Sheet for the first time gave me the creeps!

![](https://cdn-learn.adafruit.com/assets/assets/000/018/858/medium800/adafruit_products_WS2812_Timing.jpg?1408307305)

I like my protocols & interfacing simple. &nbsp;This ain't quite what I had in mind. &nbsp;So what now?

OK, if you stand right here and squint like this and the light is just right...there it is! &nbsp;There _is_ a way to push a little here, pull a little there, and get the "0-code" and "1-code" overall durations to match while still having reasonable T0H and T1H times. &nbsp;That's what I needed because I'm using one of the PIC CCP (Capture Compare PWM) modules in PWM mode to create 0's and an additional CCP in PWM mode to create 1's. &nbsp;The CCP's in PWM mode have some constraints and this (from the PIC's Data Sheet) is pretty much all I know:

![](https://cdn-learn.adafruit.com/assets/assets/000/018/859/medium800/adafruit_products_PIC_CCP_PWM.jpg?1408308326)

Ha! &nbsp;Now I have a constant stream of 0's and a constant stream of 1's. &nbsp;Great. &nbsp;Full on or full off - take your pick. However, I want this clock to be colorful which means I need 0's and 1's intermixed.&nbsp; To do that, there's a wicked-cool little spoonfull of voodoo in the PIC's bag of tricks - the Data Signal Modulator (DSM):

![](https://cdn-learn.adafruit.com/assets/assets/000/018/860/medium800/adafruit_products_PIC_DSM.jpg?1408310178)

Now _this_ is cool: one "carrier" is the PWM stream of 0 bits and the other "carrier" is the PWM stream of 1 bits. &nbsp;The "modulated data" will be the output of one of the MSSP's in SPI mode. &nbsp;This is coming together!

Then Reality comes stomping in and kicks your nice little Lego house all over the room: the MSSP in SPI mode simply will not be convinced to output bytes back-to-back. &nbsp;There's no way around this little 1 clock lapse between bytes transmitted by the SPI hardware. &nbsp;Well, that just throws a big ol' monkey wrench into my plan...

So instead, I'll try something else - pushing data through bit-by-bit via the handy MDBIT input to the DSM. &nbsp;

That's where this portion of the adventure ended up - with a little help from the interrupt-on-change inputs so the code can know exactly when to flip MDBIT to a 0 or a 1 to keep the output bitstream coherent. &nbsp;It's not as fire-and-forget as SPI but I did indeed learn a _lot_&nbsp;while writing (and rewriting and rewriting...) those 400 lines of PIC assembly. &nbsp;WooHoo FUN!

So now I can finally talk to this thing - what do I want to tell it?

# Madison's NeoClock - A PIC + KiCAD + NeoPixel adventure

## Eschew Pallor

NeoPixels are all about color - beautiful, vibrant, in some cases retina-scorching, color. &nbsp;But this project is supposed to be a clock, not a beacon for alien spacecraft entering orbit. &nbsp;I want a color palette that's interesting over a 24-hour period but not extreme or overwhelming.

It's like they always say: "There's a sinusoid for that!" &nbsp;(Well, maybe not _always_...) &nbsp;After way too much geekiness with a spreadsheet I settled on something like this:

![](https://cdn-learn.adafruit.com/assets/assets/000/018/861/medium800/adafruit_products_ColorCurvesSnapshot.jpg?1408317215)

One "special effect" I was interested in implementing was a tapering before and after the brightest point (which indicates the actual time on the clock). &nbsp;A number of false starts and bad ideas later I ended up with something like this:

![](https://cdn-learn.adafruit.com/assets/assets/000/018/862/medium800/adafruit_products_Taper.jpg?1408317679)

But not before having too much fun with the Mac's Grapher (which I now know I am nowhere near smart enough to actually _use_...):

![](https://cdn-learn.adafruit.com/assets/assets/000/018/863/medium800/adafruit_products_BrightnessTaper.jpg?1408317986)

Using the NeoPixel ring-12 to represent hours and the ring-24 to represent minutes was OK, but I wanted a little something extra (feature creep alert!) for seconds. &nbsp;It had to be different from both&nbsp;the hours and the minutes representation and still be (relatively) obvious. &nbsp;Enter the white chasing taper. &nbsp;A white dot makes the round of the ring-12 each second and is "chased" by a few more tapering-intensity white dots. &nbsp;All that doesn't disturb (too much) the ring-12's hour display and its own tapering.

# Madison's NeoClock - A PIC + KiCAD + NeoPixel adventure

## Connect the Dots

After adding the ability to sense ambient light so it can calm itself when the light is dim I finally realized that the clock's timing was drifting all over the place. &nbsp;The ultimate answer, of course, was more cowbell! &nbsp;Juggling pins on the PIC freed up the I2C signals for the newly added ChronoDot and Frankenstein's monster was (nearly) complete.

Sub-adventure 37a in this saga required learning KiCad to get the schematic onto something other than taped-together scribble sheets, a visit to Dave's Hack-a-matic Board Layout Shack, and my first shot at a getting multiple boards to fit together.

The overall circuitry is straightforward as most of the magic is in the software and the stitching together of the various PIC modules:

![](https://cdn-learn.adafruit.com/assets/assets/000/018/864/medium800/adafruit_products_Grab_PIC_Schematic.jpg?1408323564)

The power section was interesting as I tried to incorporate the recommendations from the ST LM317 Data Sheet and lessons learned from working with some really good hardware designers. &nbsp;Mistakes are mine alone - but it hasn't smoked yet!

&nbsp;
![](https://cdn-learn.adafruit.com/assets/assets/000/018/865/medium800/adafruit_products_Grab_PowerSection_Schematic.jpg?1408323986)

# Madison's NeoClock - A PIC + KiCAD + NeoPixel adventure

## Hard Times

After all that (and then some) I still needed to make a physical printed circuit board. That meant more fun with KiCad, which I do like pretty well.&nbsp; I spent a lot of time hunting down component parts and footprints and such - definitely on my faves list. &nbsp;But I somehow managed to produce something that OSH Park could manufacture:

![](https://cdn-learn.adafruit.com/assets/assets/000/018/866/medium800/adafruit_products_Grab_layout.jpg?1408324895)

I'm sure some layout guys are busily sharpening their knives and I know there's much, much, much I don't know. My defense:

I came,

I saw,

I hacked,

Deal. &nbsp;;-)

One particular challenge was placement of the connectors to which the NeoPixel rings attach. I had to bumble around in Eagle enough to understand how the power and data connectors were placed in its coordinate frame and then translate that into KiCad's. I still have bruises on my scalp from standing on my head (and in the end I had to pretty much just guess on the additional ring-24 power and grounds). I whipped up yet another spreadsheet to help me work out the translation:

![](https://cdn-learn.adafruit.com/assets/assets/000/018/867/medium800/adafruit_products_Rings_EagleToKiCad.jpg?1408325498)

In the end, I think it came together very well. Madison loves it and keeps it on her desk. &nbsp;I had a blast, learned a _lot_ along the way, and stretched myself more than a little. I lost count early on of how many hours I put into this but it's been like many other projects over the years - some little nugget I learned here is guaranteed to help in another project later on.&nbsp;

# Madison's NeoClock - A PIC + KiCAD + NeoPixel adventure

## Parting Shots

Here, then, are the guts of the finished article:

![](https://cdn-learn.adafruit.com/assets/assets/000/018/868/medium800/adafruit_products_BoardBack.jpg?1408326281)

Artistic lighting? &nbsp;Totally unintentional - I'm just as much of a hack with photography...

![](https://cdn-learn.adafruit.com/assets/assets/000/018/870/medium800/adafruit_products_BoardBackLeft.jpg?1408326643)

![](https://cdn-learn.adafruit.com/assets/assets/000/018/869/medium800/adafruit_products_BoardBackRight.jpg?1408326616)


## Featured Products

### NeoPixel Ring - 12 x 5050 RGB LED with Integrated Drivers

[NeoPixel Ring - 12 x 5050 RGB LED with Integrated Drivers](https://www.adafruit.com/product/1643)
Round and round and round they go! 12 ultra bright smart LED NeoPixels are arranged in a circle with 1.5" (37mm) outer diameter. The rings are 'chainable' - connect the output pin of one to the input pin of another. Use only one microcontroller pin to control as many as you can...

In Stock
[Buy Now](https://www.adafruit.com/product/1643)
[Related Guides to the Product](https://learn.adafruit.com/products/1643/guides)
### NeoPixel Ring - 24 x 5050 RGB LED with Integrated Drivers

[NeoPixel Ring - 24 x 5050 RGB LED with Integrated Drivers](https://www.adafruit.com/product/1586)
Round and round and round they go! 24 ultra bright smart LED NeoPixels are arranged in a circle with 2.6" (66mm) outer diameter. The rings are 'chainable' - connect the output pin of one to the input pin of another. Use only one microcontroller pin to control as many as you can...

In Stock
[Buy Now](https://www.adafruit.com/product/1586)
[Related Guides to the Product](https://learn.adafruit.com/products/1586/guides)
### ChronoDot - Ultra-precise Real Time Clock

[ChronoDot - Ultra-precise Real Time Clock](https://www.adafruit.com/product/255)
The **ChronoDot V3** is the latest version of macetech’s popular ChronoDot line of products. Designed during the Great Chip Shortage, it uses the newly-released MAX31328 temperature-compensated real-time clock chip. However, it remains pin- and code-compatible with the older...

In Stock
[Buy Now](https://www.adafruit.com/product/255)
[Related Guides to the Product](https://learn.adafruit.com/products/255/guides)
### Photo cell (CdS photoresistor)

[Photo cell (CdS photoresistor)](https://www.adafruit.com/product/161)
CdS cells are little light sensors. As the squiggly face is exposed to more light, the resistance goes down. When it's light, the resistance is about ~1KΩ, when dark it goes up to ~10KΩ.

To use, connect one side of the photocell (either one, it's symmetric) to power...

In Stock
[Buy Now](https://www.adafruit.com/product/161)
[Related Guides to the Product](https://learn.adafruit.com/products/161/guides)
### 5V 2A (2000mA) switching power supply - UL Listed

[5V 2A (2000mA) switching power supply - UL Listed](https://www.adafruit.com/product/276)
This is an FCC/CE certified and UL listed power supply. Need a lot of 5V power? This switching supply gives a clean regulated 5V output at up to 2000mA. 110 or 240 input, so it works in any country. The plugs are "US 2-prong" style so you may need a plug adapter, but you can pick one...

In Stock
[Buy Now](https://www.adafruit.com/product/276)
[Related Guides to the Product](https://learn.adafruit.com/products/276/guides)

## Related Guides

- [Burning Fire Wizard Staff](https://learn.adafruit.com/burning-fire-wizard-staff.md)
- [Bluefruit Luminary Lanterns with Capacitive Touch](https://learn.adafruit.com/bluefruit-luminary-lanterns-with-capacitive-touch.md)
- [Neopixel Crystal Chandelier with CircuitPython Animations and Speed Control](https://learn.adafruit.com/neopixel-crystal-chandelier-with-circuitpython-animations-and-speed-control.md)
- [Super Simple Sunrise Lamp](https://learn.adafruit.com/super-simple-sunrise-lamp.md)
- [LED Harness Bra](https://learn.adafruit.com/neopixel-led-harness-bra.md)
- [Sipping Power With NeoPixels](https://learn.adafruit.com/sipping-power-with-neopixels.md)
- [3D Printed Daft Punk Helmet with Bluetooth ](https://learn.adafruit.com/3d-printed-daft-punk-helmet-with-bluetooth.md)
- [The PICsellator](https://learn.adafruit.com/the-picsellator.md)
- [Sound Reactive Paper Lanterns with LedFx](https://learn.adafruit.com/sound-reactive-paper-lanterns-with-led-fx.md)
- [Your Pulse Displayed with NeoPixels](https://learn.adafruit.com/pulse-sensor-displayed-with-neopixels.md)
- [NeoPixels on Raspberry Pi](https://learn.adafruit.com/neopixels-on-raspberry-pi.md)
- [3D Printed LED Fire Horns](https://learn.adafruit.com/3d-printed-led-fire-horns.md)
- [Celebration Spectacles](https://learn.adafruit.com/celebration-spectacles.md)
- [UFO Drone](https://learn.adafruit.com/ufo-drone.md)
- [NeoPixel Ring Bangle Bracelet](https://learn.adafruit.com/neopixel-ring-bangle-bracelet.md)
