DIY Useless Box

 Objective

This article provides a step-by-step guide on how to make your own useless box using an ATmega4809 Curiosity Nano. This includes source code, files for laser cutting, circuit diagrams, and assembly instructions.

GIF_box_2.gif
Figure 1: Useless Box

 Materials

Hardware Tools

Tool About Purchase
board-50px.png
Curiosity ATmega4809 Nano
Evaluation kit
board-50px.png
Curiosity Nano Base for Click boards
Hardware extension platform

In addition, you need:

  1. Li-Po battery 3.7V with a 2-pin JST B2B-PH connector
  2. Toggle switch On-Off-On
  3. Capacitor 1uF (electrolytic)
  4. Micro servo (analog)
  5. Jump wires
  6. Hinges
  7. Wood glue
  8. Super glue
  9. Rubber bands
  10. Soldering iron
  11. Laser cutter and a 4 mm thick material
materials_correct.jpg
Figure 2: Materials

Tip: Click on an image to open in a larger format.

Software Tools

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
swtool-28px.png
Atmel® Studio
Integrated Development Environment

Exercise Files

To download the code files, go to Atmel® START and click on Browse Examples. Find the DIY useless box example in the list, select it, and then click Download Selected Example.
A more thorough explanation of how to download and compile the code can be found in "Task 3" under the section called "Procedure".

Additional Files

Files

 Connection Diagram

baseboard_circuit_diagram_larger_font.png
Figure 3: Circuit Diagram

 Procedure

This section provides a step-by-step guide on how to make your own useless box. It starts by explaining the laser cutting and assembly of the box itself before it goes on to explain the circuitry and code.

1

Task 1: Build the Box

a

Task 1a

Download the laser cut files linked above in the section Additional Files.
The design of the box was done through the use of a tabbed box maker extension in Inkscape and some drawing on top afterward. The files in the Additional Files section are both in .svg and .dxf format. In order to use it for laser cutting, it must be stored in a .dxf format. This was done in Inkscape by clicking File > Save As… and then choosing .dxf from the drop-down menu. The base unit was chosen to be mm in the pop-up window that appears after clicking Save.

If you want to add some additional design to the box, you can open the .svg file in Inkscape and start drawing.

When laser cutting, it is important that you use a 4mm thick material. This is because the tabbed joints that connect the pieces together are made for this specific thickness. Using another thickness will result in the pieces not fitting together. The material used for the box in the pictures in this article is plywood.
The finished assembled box is 16.8cm long and 12.8cm wide. The height is 5.8cm.

You should also make sure that the laser cutter is engraving and cutting out the small holes before cutting the large box shape. This is because the pieces might "wiggle" when they are cut out, so doing this ensures larger precision.

laser_cut.jpg
Figure 4: Finished Laser Cut

Tip: Remember that you can click on an image to see it in better resolution.

b

Task 1b

It is now time to glue the pieces together in order to make the box. If you cut the pieces out of wood, make sure to use wood glue. Use a small brush to apply the glue onto the square tabs, starting with the bottom piece and the side walls. Do not glue the top onto the rest of the box. It is important that the short-side wall with the logo and the flat top goes on the side closest to the holes, like in the picture below.

box_assembly_2.jpg
Figure 5: Box Assembly

While waiting for the glue to dry, you can use rubber bands to hold the pieces together.

box_assembly_1.jpg
Figure 6: Letting the Glue Dry

c

Task 1c

The next step is to assemble the small platform that the servo will lie on. First, glue the two small legs into the hole in the bottom part of the box. To ensure that the legs will attach in the right position, you can put the platform top on while the glue is drying.
Once the glue has dried, you can glue the platform top. The finished result should look like this:

box_assembly_3.jpg
Figure 7: Servo Platform

Wait with attaching the arm to the servo for now.

d

Task 1d

Do not glue the upper part onto the rest of the box. Doing so will make it impossible to put in and take out the microcontroller, baseboard, and the rest of the circuit. For a more "reusable" solution, the top can just be clicked onto the rest of the box. If it sits on too loose, you can, for example, use small magnets to keep it on.

box_top_part_2.jpg
Figure 8: Top Part on the Box

e

Task 1e

Attach the lid using hinges. The hinges used in this example were bought at a hardware store. Each of the hinges are approximately 2.5 cm wide.
If you are unsure of how to attach the hinges, you can try taping them on first and see if they fit, like in the picture below.

hinges_tape.jpg
Figure 9: Taping the Hinges

Put superglue on one side of the hinges and glue them onto the short-side wall of the box that has a flat top (attach them the same way as in the picture above). You can also see Figure 11 for a reference to how it should look like when finished. When the glue has dried and the hinges are attached to the box, you can glue the lid onto the hinges. Make sure that you attach the lid such that it will close and not crash into the top part of the box. This can be ensured by taping one of the hinges to the lid in the correct position and then glue the other hinge to the lid. Once the glue on one hinge has dried, the tape can be removed and the other hinge can also be glued onto the lid.

hinges_glue.jpg
Figure 10: Glued Hinges and Lid

When everything is attached and glued, it should look like this:

hinges_finished.jpg
Figure 11: Glued Box

2

Task 2: Make the Circuit

This part will show you how to connect the switch, capacitor, servo, and battery as shown in Figure 3.

a

Task 2a

If the Curiosity Nano Baseboard does not already have pin headers on, start by soldering these. The female headers goes on the inner rows on the baseboard and the male headers on the outside of these, like in Figure 12. See section 3.4 in the "Curiosity Nano Base Hardware User Guide" for more info on how to mount the Curiosity Nano Base board.
Solder male pin headers to the ATmega4809 Curiosity Nano if this is not already done. However, the Curiosity Nano does have staggered pins, so it might not be necessary to solder the pin headers since they will stick anyways.

pin_headers_3.jpg
Figure 12: Soldering Pin Headers

b

Task 2b

The next step is to solder two jump wires on the switch as seen in the picture below. Solder one wire to the middle pin of the switch (Off pin) and one wire to either side (one of the On pins). Then, solder the 1uF capacitor on the same pins as the jump wires. Be careful to connect the negative lead of the capacitor to the middle pin of the switch. This is the wire that should be connected to GND.

Make sure to connect the negative lead of the capacitor to GND on the baseboard. Electrolytic capacitors can be destroyed and even explode if they are applied to a reverse polarity voltage. The negative lead on an electrolytic capacitor is typically marked with a colored stripe with a minus sign-on (see Figure 13). The negative leg might also be shorter.

capacitor_close_up.jpg
Figure 13: Polarity of Capacitor (white stripe symbolizes negative lead)
switch.jpg
Figure 14: Soldering the Switch

c

Task 2c

Click the Curiosity Nano onto the baseboard. Then connect the cables from the servo to PA0, VCC and GND as shown in the diagram. The switch is connected to PB4 (green cable with the anode of a capacitor) and GND (blue cable with the cathode of a capacitor). The battery goes on the baseboard via the 2-pin JST B2B-PH connector.
The finished circuit should look like the picture below:

finished_circuit_correct.jpg
Figure 15: Finished Circuit

3

Task 3: Upload the Code

Start by downloading the code from Atmel START. Select {

Browse Examples and find the DIY Useless Box example in the list. Choose the example by clicking on it and then click Download Selected Example. The download of .atzip file will then begin.

Open Atmel Studio, click File > Import > Atmel Start Project, and choose Browse. Then select the downloaded .atzip file and click OK.
Once everything has been opened, you can upload the code to the Curiosity Nano by clicking the green Start Without Debugging button. Remember to connect the Curiosity Nano to your computer before compiling.

After the code has been compiled and uploaded to the Curiosity Nano, you can try to remove the USB cable, connect the circuit as in the connection diagram and flick the switch to see the servo arm move. The "Battery On" switch next to the battery connector on the baseboard must be set to on.
A thorough explanation of how the code works is in the Code Explanation section.

4

Task 4: Combine the Circuit and the Box

a

Task 4a

Attach the switch by removing the nut, place the switch in the hole in the top part of the box from the inside and then put the nut back on from the outside. This ensures that the switch is secured.

switch_collage.JPG
Figure 16: Attaching the Switch

b

Task 4b

The next step is to attach the arm to the servo. Before you do so, find the "start position" of the shaft. This is done by simply turning on the circuit after the code has been uploaded. The servo's shaft will go directly to the start position at startup. Then, glue the arm onto the servo like in Figure 17 (the servo on the right-hand side of the arm when seen from the same angle as in the picture). Make sure to use super glue as the arm will experience a lot of movement and pressure and it is thus necessary that it is attached securely.

servo_arm.jpg
Figure 17: Servo Arm

Attaching the servo on the platform can be done using superglue. Before you glue it on, make sure that you know exactly where to place it. Hold the servo down with your hand and find the right position such that the arm flicks the switch nicely. Drawing around the servo once the right position is found makes it easier to glue it in the exact same place afterward. Make sure to press the servo down while the glue is drying such that it attaches firmly.

circuit_in_box_1.jpg
Figure 18: Servo Glued Onto the Platform

c

Task 4c

Then, place the ATmega4809 Curiosity Nano and the Curiosity Nano Adapter with the battery into the box like this:

circuit_in_box_2.jpg
Figure 19: circuit in box

d

Task 4d

The only thing that remains now is to put the top part on and then your DIY Useless Box is finished! Remember to flick the Battery On switch on the Curiosity Nano Adapter to turn your box on.

finished_box_2.jpg
Figure 20: Finished Box

5

Task 5: Charging the battery

When the battery is empty, simply connect the Curiosity Nano to a computer through a USB cable while leaving the "Battery On" switch in the on position. When charging, S1 and PS LEDs will be on. When the battery charge is complete, PS and S2 will light up. You can read more about this in the user guide for the Curiosity Nano Base Board.

 Code explanation

The code was made using Atmel START. If you want to see exactly how everything was set up, you can open Atmel START by clicking Project > Reconfigure Atmel Start Project in Atmel Studio.

System clock
The main clock source ($f_{\text{CLK_PER}}$) is set to be 10 MHz. This is done by setting CLKSEL to 20 MHz and the prescaler to 2 in the CLKCTRL section in Atmel START.

CLK_setup_start_squares.png
Figure 21: System Clock Configuration

PWM and Servo Control
The servo is driven by the PWM which outputs on pin PA0. The PWM signal determines the position of the shaft. A pulse width of 1 ms yields an angle of 0 degrees and a pulse width of 2 ms yields 180 degrees as shown in Figure 22 below.

servo_how.png
Figure 22: Servo Angle and Pulse Width

The servo expects to see a pulse every 20 ms. In other words, the period of the PWM signal should be 20 ms. This corresponds to a frequency $f_{\text{PWM_SS}} = 1/20\text{ ms} = 50\text{ Hz}$. To find what the TOP (also called PER) value must be, we use the following formula:

(1)
\begin{align} f_{\text{PWM_SS}} = \frac{f_{\text{CLK_PER}}}{N(PER+1)} \end{align}

Equation (1) can be found in the ATmega4808/4809 datasheet under the section Single-Slope PWM Generation. $N$ is the prescaler, which we have set to 16 in this example. Rearranging the formula above yields

(2)
\begin{align} PER = \frac{f_{\text{CLK_PER}}}{f_{\text{PWM_SS}}N}-1 = \frac{10·10^6}{50·16}-1 = 12499 \end{align}

The TOP value (or PER) is thus given by 12499 which yields a period of 20 ms as desired.
The total setup of the PWM can be seen below (red squares where settings have been changed):

pwm_setup_start_squares.png
Figure 23: Setup of PWM

The code for how to control the servo can be seen in servo.c. The servo is controlled via the Atmel START generated function PWM_0_load_duty_cycle_ch0(duty_cycle). 180 and 0 degrees is converted to maximum and minimum duty cycle through a simple mapping function deg2duty(deg).

The values for DUTYCYCLE_MIN and DUTYCYCLE_MAX were found through both calculation and trial-and-error. These values should correspond to pulse widths of 1 ms and a pulse width of 2 ms respectively.
By doing some math, we have that 1 ms is 5% of the whole period of 20 ms. We know that the maximum duty cycle value is 12499 from the calculations above. Thus, the corresponding duty cycle to 1 ms is 5% of 12499: DUTYCYCLE_MIN = 12499·0.05 = 625.
Doing the same to find the maximum value, we have that 2 ms is 10% of 20 ms. The duty cycle must then be DUTYCYCLE_MAX = 12499·0.1 = 1250.

However, by testing these values, it was seen that they only resulted in approximately 100 degrees difference. Servos often have a bit deviation from how we expect them to work. New values outside the ones calculated were tested and eventually, values that resulted in a 180 degree difference were found. They are DUTYCYCLE_MIN = 350 and DUTYCYCLE_MAX = 1400.

Interrupt and the Switch
The servo is activated through the interrupt on the switch. Every time the switch is flicked, a variable move_arm is toggled. When move_arm is true, the servo is set to 180 degrees. The movement of the servo arm will then flick the switch back, yielding a new interrupt which will toggle move_arm again, and the servo arm will move back down into the box. This code for this is in the file driver_isr.c.

In Atmel START, the configuration of the interrupt pin is done in the PINMUX section. Pin PB4 is configured as this:

switch_pin_setup_START.PNG
Figure 24: Setup of Switch Pin

The interrupt is enabled in the CPUINT section in Atmel START as shown in Figure 25.

interrupt_enable_start_squares.png
Figure 25: Enabling Interrupt on Switch Pin

Every time the interrupt is triggered, LED0 is toggled. LED0 is connected to pin PF5 which is configured as shown in Figure 26.

LED0_start_setup.png
Figure 26: Configuring LED0

Main
The main file is only initializing all the drivers. The while loop is empty because everything is interrupt-driven.

 Further development

If you want to, you can modify the box by adding new features like for instance LEDs, sound, wheels or sensors. The Curiosity Nano Base board supports many different ClickBoards.

 Conclusions

This article has shown you how to make your own useless box.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.