Thursday, February 15, 2018

OpenSCAD: A Curved 3d Printed Lithophane


How can I wrap a flat surface around a cylinder in OpenSCAD. This was a question I asked myself. OpenSCAD doesn’t provide a ready made method so we need to write our own. Luckily we don’t have to invent the wheel here, others have done that already. Justin Lin has written a very handy tutorial on his website how to wrap a text around a cylinder.  In this project I took it a step further and wrapped a png image over part of a cylinder resulting in a curved image.

3D printed curved lithophane

Curved lithophane

It seemed like fun to combine this technique of wrapping with the 3D print of a lithophane. A lithophane is an engraved image on translucent material. Many 3D printers have already produced lithophanes sometimes with stunning results. For this experiment I’ll use white PLA because it  has excellent translucent properties.

Now first let me explain the basics on how to wrap a 2D image around a cylinder. First we create a circle out of triangles. These triangles are then extruded to segments to form a cylinder.  Next all segments of the cylinder are lined up in a row and the image is place on the far side but just within the limits of the segments. When we intersect the image with every individual segment and return the segments to their original position in the cylinder, a curved image emerges (see images below).

A circle created out of triangles
The extruded triangles form a cylinder
The segments are lined up in a row

Image placed on the far side of the triangles
The triangles returned to their original positions
The image intersected with the triangles and returned to their original position.

If you want to know more please watch my video. In this video the OpenSCAD script is explained and demonstrated. Also is shown how to import the stl file in Cura and what settings are used.

Thursday, December 14, 2017

OpenSCAD: Generating Triangles and Poyhedron

Polyhedron, vertices and faces

OpenSCAD offers three methods to make 3D model. First is Constructive Solid Geometry where primitive objects are combined using boolean operations. The second method uses two dimensional shapes that are extruded either in the z direction (linear_extrude)) or around the the z-axis (rotate_extrude). In the third method vertices (or points) and faces are defined to shape the surface of a 3D model (polygon mesh). This, at least in theory, provides us with a method to create the most complex 3D shapes.

Polygon mesh of a dolphin. In this example a triangle mesh is used. A public domain image from Wikipedia (

The third method can be achieved with the polyhedron function of OpenSCAD. The description polyhedron function in the OpenSCAD manual is more than adequate but it only provides the user with examples where the vertices and faces are manually entered in OpenSCAD. This can be a cumbersome process that is prone to bugs, leading to a non-valid solid. Having OpenSCAD generate these vertices and faces is a method that is more favorable (at least to me) for two reasons. First once the algorithm is correct the chance to generate non-valid solids is minimized but more importantly computer is so much faster in calculating the vertices needed.

Triangle mesh of a sphere

An important question is what kind of faces are needed for our polygon mesh. From OpenSCAD version 2014.03 onward a face description can have any number of vertex larger than two however I think that the triangle mesh is still the best choice. If the user chooses more than three vertices OpenSCAD subdivides the faces to triangles and this can cause problems as is demonstrated in an article of Justin Lin where a cube that consist of four vertex faces and one of the faces is not planar with the other three. So in conclusion the triangle mesh provides the best control over the 3D model in OpenSCAD.

In the following video tutorial I'll explain how to create such a 3D model of a sphere, cone, torus and cylinder in OpenSCAD.

Wednesday, November 1, 2017

Raspberry Pi Squeezebox server with 3d printed enclosure

The need for a enclosure

Back in August I created a Squeezebox server using a Raspberry Pi 2 and a USB harddrive I had both lying around. I quickly had the server up and running and I'm using it ever since but I didn't have a enclosure. A 3D printed enclosure would enable me to give the server a more permanent place and at the same time I'm getting rid of these components and cables lying around on my desk.

The Squeezebox server sitting nicely into the 3d printed enclosure that I designed for it.

Solvespace for the enclosure

As a 3D CAD tool to design the enclosure I choose Solvespace. Solvespace is free and open source available for Windows, OSX and Linux. I use Solvespace a lot and it has it's limitations but I felt it was fully capable for this job and indeed it didn't disappoint. Before starting I made a couple of simple drawings with pen and paper, I measured the size of the harddisk and the Raspberry Pi. I also used a template for the Raspberry Pi mounting holes that I found online.

With all this information I started drawing with Solvespace. The power of Solvespace (and other 3D CAD programs) is that's it's very easy to make changes to the 3D design. While working on the design I noticed that USB harddisk can become hot so I figured it needed to be cooled. In the drawing I elevated the harddisk a bit by adding four supports one on each corner of the disk and made four ventilation slots in the bottom of the enclosure thus allowed air to flow freely around the harddisk. The Solvespace files of this enclosure are here. Make sure to adjust the size to the USB HDD you're using.

The 3D design I came up. The printed parts, bottom and lid, are grey, the green part is the Raspberry Pi and the red part is the USB harddrive.

3d printing and assembly

I printed all the parts on my Hephestos 2 printer using black PLA filament and the result looks pretty good. Assembly of the parts is simple. First I used 2.5mm bolts and nuts to mount the Raspberry Pi upside down to the lid of the enclosure. Next I insert the USB HDD into the bottom placing it on the elevated supports.  The enclosure is fastened with four bolts 5x60mm and 5mm nuts. The nuts fall nicely into the hexagonal cavity of the feet. Finally the USB cable of the HDD is connected with the Raspberry PI and the ethernet cable is connected from my router to the Pi. The server has run for several weeks in the 3d printed enclosure without any problem. For those that want more information I made a YouTube video about this project.

Tuesday, October 3, 2017

OpenSCAD Essentials: Lists and list manipulation


This is the second part in a series on OpenSCAD (here is a link to the first). This series will not cover the very basics of OpenSCAD, there is plenty of good material on that, but it's for those who want to advance beyond these basics. This part is about Lists, sequences of OpenSCAD values. These are very important in OpenSCAD because they are the equivalent of arrays. They're also referred to as vectors. Lists can contain values numbers, booleans and strings. For most examples below version 2015.03 is required.

Quicksort example from the OpenSCAD User Manual demonstrates how to manipulate a list. The red row of cylinders represents the unsorted list while the green row represents the sorted list.

First let's create some lists by typing the following in the OpenSCAD editor.

lst = [1,2,3,4,5,6,7,8,9]; //a list of numbers
lst2 = ["red","yellow","blue"]; //a list of strings
lst3 = [false,true,undef]; //a list of booleans

In OpenSCAD the user is able to automatically generate lists with for, if and let. A couple of examples

lst4 = [for (i = [0:9]) i*i];

This generates a list of [0, 1, 4, 9, 16, 25, 36, 49, 64, 81], so for every i the results is i multiplied by i. OpenSCAD even allows nested loops

lst5 = [for (i = [0:2]) for (j =[0:2]) for (n=[0:2]) [i,j,n]];

This generates a list of a long list of vectors ECHO: [[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1, 0], [0, 1, 1], [0, 1, 2], [0, 2, 0], [0, 2, 1], [0, 2, 2], [1, 0, 0], [1, 0, 1], [1, 0, 2], [1, 1, 0], [1, 1, 1], [1, 1, 2], [1, 2, 0], [1, 2, 1], [1, 2, 2], [2, 0, 0], [2, 0, 1], [2, 0, 2], [2, 1, 0], [2, 1, 1], [2, 1, 2], [2, 2, 0], [2, 2, 1], [2, 2, 2]]

Manipulate a list

A list in OpenSCAD can be manipulated with a function. Manipulation is needed in order to make changes to the list. Since OpenSCAD is a Functional Language we can't just for example change value in the list. We need a function to do this. Lets assume we want to create a partial list from an existing list. We then create the following function:

function partial(list,start,end) = [for (i = [start:end]) list[i]];

In the Console of OpenSCAD the following is displayed ECHO: [2, 3, 9, 5, 6, 7, 8, 4]. The function has three parameters: a list, a start number and an end number. The function iterates through the list and then creates a new list with the items with indices 1 to 8. Note that the first item in the list with index 0 has been excluded from the new list. Another example.


This prompts ECHO: [5,6,7,8], a list with the 4th to the 7th item of lst. Remember that lst itself hasn't changed (you can check this yourself). Next we write a function to remove a number in a list.

function remove_item(list,position) = [for (i = [0:len(list)-1]) if (i != position) list[i]];

This prompt ECHO: [1,2,4,5,6,7,8,9], the third item in the list has been removed. The function, that has the parameters list and position, loops through the items of the list and if the index i is not equal to the parameter position it is added to the new list. In this example it means that the item with the index 2 is not part of the new list. The following function inserts a value instead of removing it.

function insert(list,value,position) = let (l1 = partial(list,0,position-1), l2 = partial(list,position,len(list)-1)) concat(add_value(l1,value),l2);

It cuts the list on the position 4 in two new lists, inserts a number of 10 between the two lists, using the function partial that was created above. The result in the console is: ECHO: [1, 2, 3, 4, 10, 5, 6, 7, 8, 9]. BTW: It's entirely possible that there's an easier way to do this.

The last and most complicated example is taken from the OpenSCAD User Manual and implements the quicksort algorithm. The quicksort function uses an unsorted list as input and produces a sorted list with a recursive method.

lst6 = [1,4,6,7,3,5,7,2]; //a list of numbers
function quicksort(list) = !(len(list)>0) ? [] : let(
    pivot   = list[floor(len(list)/2)],
    lesser  = [ for (i = list) if (i  < pivot) i ],
    equal   = [ for (i = list) if (i == pivot) i ],
    greater = [ for (i = list) if (i  > pivot) i ]
) concat(
    quicksort(lesser), equal, quicksort(greater)


It produces the following output ECHO: [1,2,3,4,5,6,7,7]. The function starts with a base case !(len(list)>0) ? [] meaning if a the length of list is 0 return an empty list (or the boolean value false) else continue. Next a pivot is determined being the number in the middle of the list, in this case the number 3. Next all numbers in the list smaller than 3 are placed in a list lesser, all numbers equal to 3 are placed in a list equal and all numbers larger than 3 are placed in a list greater. These three list are then concatenated to a new list however, and now we get to the recursive parts, the lesser and greater lists are the new parameters for quicksort. This iteration continues until the base case, the length of the list is equal to zero or less, is reached.

Understanding lists and list manipulation is essential to advance in OpenSCAD. Without it would for instance be near impossible to work with the all important vectors in both 2D and 3D. But also in other areas, such as iterating through a row, lists are essential.

Friday, September 15, 2017

OpenSCAD Essentials: Functional Programming

Functional programming

OpenSCAD is a 3D CAD program where the 3D model is created with a script. Although the basics are very simple there is a point on the learning curve where the OpenSCAD programming language can become confusing. OpenSCAD is a Functional Programming Language and behaves differently than most people, used to the Imperative Programming Paradigm, expect. With Imperative Programming a value can be assigned to a variable and later another value can be assigned to same variable later in the program. This changes the state of the program. This approach will not work in OpenSCAD.

Sierpinski triangle in OpenSCAD. A program I made earlier that demonstrates recursion in OpenSCAD.

An example

The easiest way to explain this is with a simple example. In this example we want the program to add the numbers 1 to 10 (1+2+3+4+5+6+7+8+9+10).

total = 0;

module count(number) {
    for (i = [1:number]) {


You might expect that the answer is 55 however to your horror you'll find in OpenSCAD that it actually is 0. What happened? Instead of assigning a new value to the variable total for every interation, total keeps it's initial value. The program written in OpenSCAD is a mathematical expression that is evaluated by pressing F5. OpenSCAD supports powerful functional constructs to support these expressions. In the case of the example above we write.

function sum(number) = (number == 0 ? 0 : number + sum(number-1));


The result of this program is 55 as intended but how does it work. We define a function (hence the name Functional Language) sum with a parameter number. On the right side of the function there is an evaluation, in pseudocode: if n == 0 then 0 else number + sum(number-1). Notice that the function is recursive so for example for number is 3 the function evaluates as follows

sum(3) = 3 + sum(2) = 3 + 2 + sum(1) = 3 + 2 + 1 = 6

Iteration in OpenSCAD is usually accomplished via recursion. If you are not used to recursion it's takes effort and practice to understand but it's often simple and elegant. Now let's do a somewhat more complicated example, the greatest common devisor (gcd). When we look at gcd algorithms on wikipedia we find the recursive Euclidian algorithm which is described as follows:

gcd(a,0) = a
gcd(a,b) = gcd(b, a mod b)

Even if we would not understand the algorithm it's easy to translate it to OpenSCAD

function gcd(a,b) = b == 0 ? a : gcd(b,a%b);

Where a%b means a modulo b (BTW: the syntax used in the functions above is similar to the programming language C). It's worthwhile as a training to find recursive algorithms and try to implement them in OpenSCAD.

Further reading

If you want to advance in OpenSCAD it is necessary to read the manual at some point. This discloses valuable information that is hard to find by just using the Cheatsheet or the watching YouTube videos. The user manual can be found here:

Tuesday, August 22, 2017

Creating a Raspberry Pi Squeezebox server

Frustration with Spotify

In my last blog post I wrote how Spotify remotely deleted their app from my Squeezeboxes. This pissed me off and I wondered who was in control of the Squeezebox devices, me or some third party. It forced me to choose between the service, that streams music from the internet, or set up the Squeezebox server on my network. The Squeezebox server software being free and open source software guarantees that I take control of my Squeezeboxes and the music that I'm streaming.

The're many hardware options for the Squeezebox server software (or Logitech Media Server) but I decided to use the Raspberry Pi 2 that I had lying around because of it's small footprint and massive support for the Pi. Within a couple of hours I had the Logitech Squeezebox Server up an running with my music playing.

Below I'll show the steps that need to be taken to set up the Logitech Squeezebox Server on the Raspberry Pi. I got some important steps from the Variax Firmation website but I tested everything.

Raspberry Pi 2 with a USB-harddrive connected to my network with an ethernet cable.

Step 1: Ripping CD's and writing them on an HDD (choosing an audio format)

Before setting up a server rip the CD's and write them to a HDD. For this project I used a small usb-harddrive that was also lying around. I formatted the HDD as FAT32. Next I collected all my music CD's and ripped them. As ripping software I used the Asunder software on my Tahrpup Linux system (6.0.5) but I guess that the're good alternatives. As audio format I choose mp3 for now. If I buy a larger drive I'll probably move to flac.

Step 2: Installing Raspbian Stretch Lite

Next I installed the just released Raspbian Strech Lite on the Raspberry Pi 2 by first downloading the image and writing the image to the micro SD card using these instructions.  I chose the lite version of Raspbian Stretch to make the server as lightweight as possible. I had no problems booting the Raspberry Pi and connecting it to my network using ethernet. To avoid potential problems connect the Pi to a monitor with an HDMI cable and make sure that it boots properly.

Step 3: Mount the HDD on the Raspberry Pi

Now with Raspbian Lite installed I mounted the HDD that we prepared in step 1 we need to login remotely and retrieve the name of the drive. To login remotely we need another computer (Windows, Linux or OSX). Open a terminal (or Putty in case of Windows) and type
sudo ssh pi@Raspberry_Pi_ip_address (in my case
The password is raspberry. Then type.
sudo fdisk -l
Then look for the HDD that is just connected. This was /dev/sda1 in my case (but this may differ in yours). To make the files on the HDD accesible to the Raspberry Pi we need mount the drive. But before that we need to make a mounting point.
sudo mkdir /media/usb-drive
and mount
sudo mount /dev/sda1 /media/usb-drive
You should hear some HDD activity. Now type
cd /media/usb-drive ls -l
and a list of your music should be visible.
To unmount the HDD do
sudo umount /media/usb-drive

The list of folders with albums that I ripped. In this stage I only ripped a few albums to test the system.

Step 4: Installing Logitech Media Center on the Raspberry Pi

Before we need to install a library to play audio files.  Since you want mp3 only do:
sudo apt-get install -y libsox-fmt-all
This installes SoX, a command line utility that, among other things, plays various types of audio files. If you want to add flac support you'll probably need to do this
sudo apt-get install -y libsox-fmt-all libflac-dev
Now with the audio libraries in place the Logitech Media Software can be downloaded. We want the latest version which is 7.9.0.
wget -O logitechmediaserver_all.deb $(wget -q -O - "")
Now install the server software with:
sudo dpkg -i logitechmediaserver_all.deb
With all the software installed we're ready to go.

Step 5: Working with Logitech Media Center

To enter the web interface of the Logitech Media Center on the Pi we need a computer and a browser. In the browser url we type:
<your_Raspberry_Pi_ip_address:9000> (in my case 

The Logitech Media Center is started for the first time and a script is started to set it up. Important is that the mounting point of the HDD (in my case /media/usb-drive) is entered. This way the Media Center is able to retrieve all the music files.
sudo mkdir /media/usb-drive
The Logitech Media Server is now ready and every time the Raspberry Pi boots the Logitech Media Server is automatically started however the HDD is not automatically mounted so we need to change that. This is done in the fstab system configuration file that can be found at /etc/fstab
sudo nano fstab
and add the following line
/dev/sda1    /media/usb-drive     vfat&    defaults     0
Save the file and exit nano. Reboot the Raspberry Pi. Now the installation is complete. The music can be played from the web interface (see above), with the remote controls that come with the Squeezebox devices or with a free app for Android. The Squeezebox Radio also has controls on the device to play the music. So, plenty of options.

Webinterface of the Logitech Media Server.

What's next

To give the media server a permanent place a case is needed. Luckily I have a 3D printer and some 3D CAD skills so I'll create my own. I'll show the finished product in a future blog post. I also wonder if I could make this work on the C.H.I.P which would make the project even cheaper. EDIT: I have created the 3D printed enclosure. If you're interested you'll find the details here.

Wednesday, August 16, 2017

Spotify silently and remotely deleted app from Squeezebox devices

Spotify removed

Spotify silently and remotely deleted the Spotify app from the Squeezebox devices I found out after returning from vacation (Yes, they did post an article on their help pages I found out but I wasn't notified). I own a Squeezebox Radio and a Squeezebox Touch that I both use heavily to listen to music with the Spotify app. I even have a Spotify Premium Family account that enables me to play Spotify on multiple devices. I understand that support for hardware, such as the Squeezebox, is limited and will end some day. However what outrages me is that Spotify remotely removes software on a device that I own. Spotify makes it very clear that they control the software that they provide for the device and not the user. With that they control the functionality of my devices.

The Logitech Squeezebox Radio, one of several types of Squeezebox devices, now without Spotify (photo from

The Squeezebox music player

The Squeezebox device is a network music player from Logitech. The devices are versatile and have excellent hardware. The Squeezebox can be used in two different ways; as a player that works with an online service,, or with a private server called Logitech Media Server. I preferred to use my Squeezebox devices with the online service mainly for its ease of use and because some streaming services such as Spotify and Deezer are available as an app. The apps are very convenient to stream music just with a remote control. The Squeezebox hardware was discontinued in 2012 but  but, to it's credit, Logitech maintains the online service to this very day.

I didn't mention the best thing about Squeezebox, both the server and player software are non-proprietary and can be installed on several different operating systems. This enables all users to make build their own Squeezebox clients and servers.


With the removal of the Spotify app I have a choice to make. Will I continue to be using the my Squeezebox devices with the online server using Deezer instead of Spotify or will I set up my own private server? The latter is of course more of a hassle since it involves installing and maintaining the Logitech Media Server, ripping CD's to mp3's and storing them on the server. Nevertheless I may choose to create my own private server. I recently decided to move the software on my iMac from proprietary to non-proprietary so creating the private server for my Squeezebox would also make sense. I happen to have a Raspberry Pi 2 lying around and in the coming weeks I'll try to set it up as a Logitech Media Server.