Chocolate Box – Arduino

The less complicated part of the whole project is the one that brings the WOW factor!

We are all so used to the wonders of the Internet, interconnected devices, screens, and even voice-activated assistants like Alexa that as soon as things move, we all stare in awe.

To achieve such an effect, with the box that self-opens and closes, only three pieces of hardware are strictly required: a microcontroller, and two servos.

Hardware List

  • Arduino MKR1000.
  • 2# Servo.
  • Wires.
  • Breadboard.
  • Micro-USB Cable.

I went for an MKR1000 because it has integrated WiFi capabilities, and it was sitting in a drawer in my studio. Any WiFi-powered Arduino would actually do the job.

The servos are super basic too. I used two SG90 I had spare, but anything similar would do.

Wires and breadboard made the circuit easy to build and easy to tear down without any soldering or permanent connection.

The whole circuit is powered via a Micro-USB cable connected to a phone charger. This gives a handy 5V power supply for the servos without a dedicated power circuit.



The sketch for the MKR1000 is available on GitHub, with all the other resources.

The code initializes the Wi-Fi connection and subscribes a topic on the MQTT Broker. It then moves the servos according to the message it receives.

Important to notice, in the code, how the two servos are mounted facing each other, so they move in opposite angles. Not a big deal, but it might look cryptic in the code.

In the next posts, I’ll show how the cardboard box is built and how to make the Alexa interface to sound nice when it receives the command. Stay tuned!

Previous posts: Intro, Overview, Infrastructure/Logic.

Chocolate Box – Infrastructure / Logic

As mentioned previously, the central part of the project is represented by the Infrastructure and Logic, both performed on a Raspberry Pi 3, powered by Raspbian.

The two pieces of software that I’m using are Node-RED for the automation and Mosquitto for the signaling.

Let’s see them in details:


Node-RED is a programming tool for wiring together hardware devices, APIs and online services in new and interesting ways.

It allows representing every piece of your automation system with a node, configuring its behavior, and connecting it with all the other elements of your system. It also allows to program functions, manipulate the messages, set delays, and so on.

There are a ton of libraries to operate with many different devices and services. For this project I used just a handful of them:

Let’s see how they are interconnected and configured:

Alexa-local node

The configuration of this block is trivial. It’s just enough to give a name to the node, and it will be the name that Alexa will list in the discovered devices. After this node is deployed it’s necessary to give an “Alexa, discover devices” command to make it appear in the smart home section of your Alexa as a new device.
You can find the Alexa-local node on GitHub or in the install palette on Node-RED.

Function “Open” node

This function takes the message coming from Alexa and manipulates the payload to control the opening of the box.

The function is extremely simple:

Delay node and Close Template node

The first node introduces a 20-second delay before triggering the close template.

The close template is defined as follows:

Once again, this is nothing fancy.

MQTT-out node

As you might have understood, the only two messages that this node receives are true, when it’s time to open the box, and false, 20 seconds later, when it’s time to close it.

These messages are sent to an MQTT broker with the topic box_open as follows:

And that’s all it’s needed on Node-RED to make it work. Let’s now see how to tell the Arduino board to open and close the box.


To interface Arduino to Node-RED I went for the most appropriate protocol: MQTT. This protocol requires a broker, a piece of software that receives signals from the many different agents, and dispatches them to the appropriate recepients.

And this is all you need to know to make it work on our Raspberry Pi. You simply install Mosquitto and make sure it’s configured as your MQTT broker on Node-RED and on the Arduino sketch. Voila!

Downloads and resources

You can find the Node-Red Flow, and all the rest of the resources on this GitHub repository.

It’s all released under GPL!

That’s all for today, in the next posts I’ll show you how to configure Alexa, how to build the box, the circuit board, and configure Arduino.

Stay tuned, and feel free to contribute!

Chocolate Box – Overview

The Chocolate Box project is made of many different parts, which are summarised by this diagram:

Human Interaction

The interaction happens though Alexa, Amazon’s virtual assistant. In my case, any of the many Echo devices in my house can pick up the request and activate the whole flow.

Infrastructure / Logic

This part connects the signal coming from Alexa to the actuator device inside the box. The hardware for this part is a Raspberry Pi 3, and it’s powered by Node-Red for the automation system and Mosquitto as MQTT Broker.


The actuator device is based on Arduino MKR1000, and two servos. All embedded into a cardboard box, disquised as a Christmas present.

In the upcoming posts of this series I’ll break down in details every block, and all the configs will be made available too.

Stay tuned!

Chocolate Box – intro

I built an automated chocolate box that sits nicely under my Christmas tree, and it’s operated by Alexa. It took me a few hours to figure out all the bits and pieces, but instead of working on a massive post with all the details, I’m breaking down this project in several pieces and several posts.

Stay tuned and enjoy the ride!

Oh, by the way, this is the final product:

I started this blog back in 2006 as a tech blog, so I feel this project a little bit as a return to the origins.