Building a Clock With Nerves

Does anybody really know what time it is?

Decoding binary digits to tell time

Building a Clock With Nerves

Does anybody really know what time it is?

Them: “Hey, what time is it?”
Me: “Let me check this clock. Hmm, let’s see… that plus that plus 4…”
Them: “Just glance at the clock and tell me.”
Me: “Ok, just a second, the digits changed, let me redo this.”
Them: “Seriously, how hard can this… Oh.”


my binary clock

Nerves To the Rescue

Several years ago I heard about the Nerves project and was captivated by the idea of failsafe firmware upgrades. How often do we see “Do not turn off the power until this upgrade completes” and shudder? Having lived in Florida, where the power can flicker often due to lightning strikes, this promise resonated.

The other important value Nerves brings to embedded systems is the reliability of Elixir and OTP. I’ve built embedded instruments with C. It’ll never happen to you, but when the stack gets corrupted from some amazing pointer arithmetic you really hope the reset button is within easy reach. Elixir prevents a lot of these problems and OTP is there to hit reset for you. “Have you tried turning it off and on again?”

The Book

After looking around a bit I settled on Build a Binary Clock With Elixir and Nerves by Frank Hunleth and Bruce Tate. The premise of building something useful – maybe not useful, but certainly enjoyable to look at – was exciting. The hardware requirements seemed trivial enough, the soldering and woodworking wouldn’t be a challenge, so it was time to get started.

build a binary clock book

Did I mention the hardware “seemed trivial enough”? Oh how naive! In 2022 those cheap Pi Zero boards were still unobtanium. At this point the prudent course was to bravely run away and await a better day.

Turning It Around

It was at Gig City Elixir this year that I met Frank Hunleth. After introducing myself and explaining my interest in Nerves he asked if I’d been able to acquire a Pi board to develop with. He certainly wasn’t surprised to hear of my difficulty getting a board, but I was blown away when he reached into a box and handed me a Mango Pi MQ Pro. You can see this board in the picture at the top of this post.

Thank you, Frank!

Returning home from the conference, I used Nerves Livebook to bring up the board and look around. Hey, I could connect across the usb cable and blink the leds on the board. Success! It was enough for me to order some more parts and get ready to work on the project. Frank had explained that support for the Mango Pi was still a work in progress but that it should work to do the project in the book. Fortunately, between May and August, when I started on the book, he had already commited a lot of changes to Nerves so that I didn’t run into any issues with it not being an actual Pi Zero.

The Project Design

Look again at the subhead for the book: Use Layering to Produce Better Embedded Systems. That actually made this a much more valuable book than just “here’s how to twiddle some leds”. The layering allows for much easier development, testing, and deployment to hardware by the use of adapter modules for each phase of development.

When developing you want pretty output to understand how things are working. For testing, dependable results help verify correctness. Then it’s much easier to deploy and know that the underlying code is good and you only need to work out the interface to hardware. Each of these adapter modules are interchangeable inside the server module that is used with OTP to run the clock.

Challenges

Working through the initial stages of the book, you build a small led circuit and attach it to a GPIO pin on the Pi board. This allows understanding of the circuit, how to use the GPIO library to control the pin, and get a small win. In my case, the pin numbers for the Pi hardware didn’t match up with the Mango Pi. Fortunately, GPIO on the MangoPi MQ-Pro and the excellent Raspberry Pi Pinout website were excellent resources that enabled me to continue.

Without a lot of hardware knowledge I struggled to understand exactly what search terms I needed so that I could find some of the parts. I never did find the ribbon cables Frank was using in the book, but I did finally learn that “Dupont Connector” did what I needed. As he explains in the book, the ribbon cable helps keep the wires under control, but I fixed this with zip ties and colored electrical tape. In the future I’d love to learn what cable to get; when I do I’ll feel silly that it was so simple.

If you’ve never soldered before, it may seem daunting, but there are plenty of howtos on YouTube. Honestly, the only challenge with the soldering was how much there was to do – an endless, mind numbing amount. I thought I’d never finish, but once you get started you get into a rhythm and eventually find yourself on the other end of the task.

Conclusion

I’m extremely grateful to Frank Hunleth for making this possible. Without his gift of the Mango Pi I’d still be waiting on hardware. The book was a good introduction to Nerves and “use layering to produce better embedded systems” is perfect advice and a great software architecture.


If you’ve ever viewed any of Bruce’s recent training videos and wondered what was on the bookshelf in the background, now you know. And now I have one, too!