Scripting a Box: Making Laser-Cut Battery Holders

Previously, I posted a tutorial about writing SVG files by hand to create simple patterns for a laser cutter to produce. But as I noted at the end of that post, writing that sort of file by hand is not a great solution. It is difficult to make a design that can be modified later, since SVG files don’t appear to have a good way to store variables yet.

So in this tutorial, I’ll go over the process of writing a simple Python script to create the same sort of “divided grid box” as in the previous SVG-writing tutorial across a wide range of dimensions. Then I’ll demonstrate how to use it to create a small 4-cell AAA battery holder out of something that isn’t plastic:

AAA battery case made of laser-cut wood

While “3D printing” a case allows you to add curves and small overhangs to hold the batteries in place, these simple laser-cut boxes can only have perpendicular edges. But I’ve found that using the sorts of spring contacts that you find in most commercial battery cases provides enough pressure to hold the batteries in place, at least until you hold the case upside down and knock it against something to pop them out.

Plus, 3D printing is comparatively slow; a battery case of this size would take between 30-60 minutes to print out on a Prusa i3 running at high speed, but a CO2 laser can cut out these parts in about 60 seconds. You do need to glue the pieces together, but if you adjusted the scripts to account for the “kerf” of your particular laser/material, you might be able to make them press-fit. Anyways, let’s get started!

Box Pattern Overview

This script will design boxes using the same approach described in the previous “writing SVG files” tutorial. But to make things easier on the user, it will produce a single pattern file with the correct number of each type of part placed appropriately:

One ‘base’ for the box. Each of the ‘wall’ parts will fit into the base perpendicularly, like puzzle pieces.

Two ‘vertical edge walls’, which spans the box’s entire length.

At least two ‘horizontal wall/dividers’ which span the box’s entire width, minus the width of the material taken up by the two ‘vertical edge walls’. This will also double as a ‘divider’ to separate ‘rows’ of grid cells within the box.

Any extra ‘vertical dividers’, which only span the height of a single grid cell to separate ‘columns’ between horizontal dividers.

If you’re okay with ignoring the laser’s kerf, we can make the edge wall dividers share edges with the box base in order to save material. Here’s what the generated pattern will look like, with each part labeled:

2×2 AAA battery box generated pattern

Finding Dimensions

Checking Parameters

The first thing our script will need to do is define the dimensions of the box that it will produce. I decided to accept the following parameters in this simple example script. For simplicity, I will assume that dimensions are specified in millimeters:

Interior “width” (X-axis) of a grid cell.

Interior “length” (Y-axis) of a grid cell.

Depth (Z-axis) of each grid cell.

Number of “horizontal” (X-axis) grid cells.

Number of “vertical” (Y-axis) grid cells.

Thickness of the material being cut.

That is all the information that we need to generate these patterns. There are better ways to handle arguments than this, but this is not a tutorial about command-line parsing, so I’m just going to grab the raw values passed in to the script and have it print a ‘Usage’ message if it gets the wrong number of arguments:

With that structure, I’ll use the following command to generate a box to fit 4 AAA batteries in a 2×2 grid – you can find the finished script in this Github repository, for reference:

python gen_grid_box.py 50 13 12 2 2 3

The box will have interior cells that are 12mm tall, 13mm long, and 50mm wide. The battery contacts that I bought are 12mm in both length and height, which seems pretty standard for AA/AAA contacts. Leaving an extra mm of length is useful because it is easy to glue the walls on at almost-but-not-quite 90 degree angles, and the wood may not be exactly 3mm thick anyways. As for the cell width, one AAA battery is 45mm long, but I added an extra 5mm to each cell because the negative battery contact has a spring that compresses to hold the battery in place, and that adds a bit of space. Adjusting this width value will change how tightly the batteries are held in place.

Calculating Box Dimensions

Now that we know how large each grid cell should be and how many cells should be placed in each direction, we need to calculate the values which our script will use to draw the actual paths and shapes in the SVG files. First, we need to find the total outer dimensions of the box:

It’s pretty simple; the box’s total size is equal to that of the requested grid, plus any extra space required by the walls and dividers. Next, we can find the number of interlocking ‘crenellations’ to place along the X/Y edges, and how long each one should be. These are what will let the base and walls slot together like puzzle pieces:

The small single-column ‘vertical dividers’ will also have their own notches cut along the base’s Y-axis; I calculated those differently from the ‘vertical wall’ notches so that all of the column dividers could be the same size:

And that’s all that we’ll need for dimensioning. The total size of the SVG pattern can be calculated by finding the width/height of the box’s base with its interlocking walls, and adding enough extra height for whichever group is taller – the horizontal or vertical dividers:

I made a typo on the ‘horizontal wall/dividers’ length – it should be box_w - (gaps_t * 2) instead of cell_w – but you get the idea.

Drawing the Box Pattern

To keep things simple, I used a consistent layout for the pattern. If you ask for a box with lots of columns and rows it might make a very tall image, but you can play around with the spacing if you want to. In a nutshell, the box’s base is placed near the bottom of the image, with its two horizontal and vertical walls interlocking along its edges. Rectangles are placed within the box base where holes should be cut out to fit the dividers into. Then the dividers themselves are stacked above the base image in a single column.

The first step is to create an SVG file and open a few starting tags, similar to what was used in the previous “SVG writing” tutorial:

The Box Base

The base of the box is the most complex part, since it has crenellations along each of its four edges, and rectangular cuts for the dividers. For the dividers, rect tags are used to place slots matching the appropriate crenellations. And for the outline, I just traced a series of horizontal and vertical lines. The c_sign variables are used to draw the alternating ‘in/out’ pattern of each edge, but in retrospect it would probably be better to multiply by a numerical value of 1 or -1 instead of using a string for the - sign:

Horizontal and Vertical Walls/Dividers

The horizontal and vertical walls basically follow the same pattern as the box edges. The horizontal ones are shortened by the thickness of the material, and the same pattern as the horizontal walls is used for the horizontal row dividers. I won’t copy a whole bunch of repetitive horizontal/vertical SVG lines again, but you can find that logic in the Github repository.

Assembling the Box

With the script ready to go, we can run it with the dimensions mentioned above, and use a laser cutter to cut the resulting pattern out of 3mm-thick material. I used birch plywood:

The individual parts of the case, as they are cut out

Pour a bunch of wood glue all over everything, and fit the pieces together. I also stuck the battery clips on while I was in a glue-ing state of mind:

The parts all glued together

Once the glue dries, you can flatten the battery clips down and then solder the wires/tabs where appropriate. It might be a good idea to use a different sort of adhesive for the metal contacts, but this is just an example. Four AAA batteries in series will usually deliver between about 4.5-6 Volts, depending on the type of battery and how charged they are.

Conclusions

This script should scale well; you can also use it to generate patterns for larger items like a parts tray or jewelry box. You may need to separate the dividers into multiple cuts for larger boxes depending on how large your laser cutter is, but it is easy to re-arrange the parts with a program like Inkscape or Illustrator because the script places each divider in its own path within the SVG file.

Comments (2):

Hi everyone! I’m quite new to 3d printing and I have quite a few questions on the subject, so I hope you won’t get mad at me for asking here at least couple of them. I think before I’ll get seriously into sculpting I should focus on the software I’m going to use, and that’s what I would like to ask you about. Mainly, should I begin with the most simple/crudest program I can find or would it be better to start on something more complex? I’m worried that I’ll get some undesirable habits while working on less complex software. The second question is about the CAD software as well: should I look for CAD software that would let me design and slice it in it, or should I use a different software for each? Will it even make a difference? Weirdly, I couldn’t find the answer to that, as it seems like most articles want to focus on the very basics (like what is 3d printing and so on), and while the answers to those questions are fine, it seems like no one wants to go into the details (it looks like some of them even plagiarise each other! I swear I’ve found the same answers to the same questions on at least 3 different blogs) but I’m getting off-topic… The last question is about 3d pens. Would it be possible to somehow convert whatever I draw with a 3d pen to a 3d model in a program? For example, if I’ll draw a horse with 3d pen, would it be possible to get its design in a program? I’m not sure how that could even work, but the very idea sounds interesting to me. Anyway, I think I’ll stop here just in case no one will answer me and all of this writing will be for nothing. I’m sorry that I’m using your content to ask questions, but I hope you’ll understand and advice a beginner like me. Anyway, thank you for posting. I learned something from this and that’s always appreciated. Thank you, and I hope to hear back from you very soon 🙂

Vivonomicon

August 22, 2019 at 10:37 am

Good luck with your projects, but I think that your questions are a little bit beyond the scope of my knowledge. I’m sorry that I don’t know very much about 3D printing, but for sculpting, it seems like you might be better served by subtractive manufacturing. From what I remember, 3D-printed parts usually require a lot of polishing and ‘post-processing’ to look nice, and it’s hard to make stable parts with 3D pens because the layers don’t usually fuse too well together. Maybe the pens have gotten better in recent years, though, and they look like a fun way to ‘doodle’ outside of CAD programs.

Good luck, and sorry again for my lack of knowledge. There are a lot of great resources for learning about 3D printing if you search, and those places will have much better information than I do.

Related posts:

Several years ago, a company called Future Technology Devices International (FTDI) sold what may have been the most popular USB / Serial converter on the market at the time, called the FT232R. But this post is not about the FT232R, because that chip is now known for its sordid history. Year after year, FTDI enjoyed their successful chip’s market position – some would say that they rested too long on their laurels without innovating or reducing prices. Eventually, small microcontrollers advanced to the point where it was possible to program a cheap MCU to identify itself as an FT232R chip and do the same work, so a number of manufacturers with questionable ethics did just that. FTDI took issue with the blatant counterfeiting, but they were unable to resolve their dispute through the legal system to their satisfaction, possibly because most of the counterfeiters were overseas and difficult to definitively trace down. Eventually, they had the bright idea of publishing a driver update which caused the counterfeit chips to stop working when they were plugged into a machine with the newest drivers.

FTDI may have technically been within their rights to do that, but it turned out to be a mistake as far as the market was concerned – as a business case study, this shows why you should not target your customers in retaliation for the actions of a 3rd party. Not many of FTDI’s customers were aware that they had counterfeit chips in their supply lines – many companies don’t even do their own purchasing of individual components – so companies around the world started to get unexpected angry calls from customers whose toy/media device/etc mysteriously stopped working after being plugged into a Windows machine. You might say that this (and the ensuing returns) left a bad taste in their mouths, so while FTDI has since recanted, a large vacuum opened up in the USB / Serial converter market almost overnight.

Okay, that might be a bit of a dramatized and biased take, but I don’t like it when companies abuse their market positions. Chips like the CH340 and CH330 were already entering the low end of the market with ultra-affordable and easy-to-assemble solutions, but I haven’t seen them much outside of Chinese boards, possibly due to a lack of multilingual documentation or availability from Western distributors. So at least in the US, the most popular successor to the FT232R seems to have been Silicon Labs’ CP2102N.

It’s nice to have a cheap-and-cheerful way to put a USB plug which speaks UART onto your microcontroller boards, so in this post, I’ll review how to make a simple USB / UART converter using the CP2102N. The chip comes in 20-, 24-, and 28-pin variants – I’ll use the 24-pin one because it’s smaller than the 28-pin one and the 20-pin one looks like it has some weird corner pads that might be hard to solder. We’ll end up with a simple, small board that you can plug into a USB port to talk UART:

Drivers for the CP2102N are included in most popular OSes these days, including Linux distributions, so it’s mostly plug-and-play.

It’s worth noting that you can buy minimal CP2102N boards from AliExpress or TaoBao for about $1, but where’s the fun in that?

It has been about nine months since ST released their new STM32G0 line of microcontrollers to ordinary people like us, and recently they released some new chips in the same linup. It sounds like ST wants this new line of chips to compete with smaller 8-bit micros such as Microchip’s venerable AVR cores, and for that market, their first round of STM32G071xB chips might be too expensive and/or too difficult to assemble on circuit boards with low dimensional tolerances.

Previously, your best bet for an STM32 to run a low-cost / low-complexity application was probably one of the cheaper STM32F0 or STM32L0 chips, which are offered in 16- and 20-pin TSSOP packages with pins spaced 0.65mm apart. They work great, but they can be difficult to use for rapid prototyping. It’s hard to mill or etch your own circuit board with tight enough tolerances, and it’s not very easy to solder the chips by hand. Plus, the aging STM32F031F6 still costs $0.80 each at quantities of more than 10,000 or so, and that’s pretty expensive for the ultra-cheap microcontroller market.

Pinout and minimal circuit for an STM32G031J6 – you only really need one capacitor if you have a stable 3.3V source.

Enter the STM32G031J6: an STM32 chip which comes in a standard SOIC-8 package with 32KB Flash, 8KB RAM, a 64MHz speed limit, and a $0.60 bulk price tag (closer to $1.20-1.40 each if you’re only buying a few). That all compares favorably to small 8-pin AVR chips, and it looks like they might also use a bit less power at the same clock speeds. Power consumption is a tricky topic because it can vary a lot depending on things like how your application uses the chip’s peripherals or what voltage the chip runs off of. But the STM32G0 series claims to use less than 100uA/MHz, and that is significantly less than the 300uA/MHz indicated in the ATTiny datasheets. Also, these are 32-bit chips, so they have a larger address space and they can process more data per instruction than an 8-bit chip can.

Considering how easy STM32 chips are to work with, it seems like a no-brainer, right? So let’s see how easy it is to get set up with one of these chips and blink an LED.

As someone who likes both electronics and the outdoors, sometimes I get anxiety about a lack of electricity. It would be nice to go camping somewhere away from it all, and still be able to charge things and run some lights, a display, maybe a small cooler. I’m sure some of you are rolling your eyes at that, but I’ve also been wanting to play with adding aftermarket indicators to old cars, like backup sensors or blind spot warnings, and it’d be nice to run them off a separate battery to avoid the possibility of accidentally draining the car’s battery overnight.

Since low-power solar panels are fairly cheap these days, I figured that it might be worth buying a few to mount to my car’s roof. And since my car is technically a pickup, it was very easy to put the battery in the bed and run the wiring through the canopy’s front window:

I’ve secured the battery a bit more since taking these pictures, but this is the basic idea – it’s pretty simple.

If you have a different kind of car, I’d imagine that you could just as easily put the battery in your trunk, but you might need to drill a hole for the wires if you don’t want to leave one of your windows cracked open.

I guess that a lot of this guide won’t apply exactly to your situation, because you’ll have different dimensions to work with, different limitations, and probably different solar panels. But I hope that laying out each step that I took and what worked for me might be helpful – your basic approach could probably look very similar.

And before we go any further, please keep your expectations in check. These panels can only produce up to 100W in direct sunlight, which is nowhere near enough power for something like an electric vehicle. So read on if this sounds interesting, but the car still runs on gas. We’re not saving the world here.