4-wire fan PWM control using a PIC18F4550


Once again I am playing around with PIC microcontrollers. Now I finished a complete demo that does this:

  • drive a 4-wire fan, in my case a Noctua NF-S12A using PWM
  • read the fan’s tachometer output to determine its speed
  • show the PWM duty cycle and fan speed on a 16×2 LCD

The demo application starts with a duty cycle of 0, where the fan doesn’t rotate and increases the duty cycle by 5% every 5 seconds, until it reaches 95%. Then it decreases the duty cycle by 5% every 5 seconds until it reaches 0. It keeps repeating this sequence forever.

At the same time it counts the pulses coming in from the fan’s tachometer output and displays the calculated speed every second.

The circuit

The circuit on my breadboard is made after this schematic (click to make it bigger):


The circuit to drive the fan and to read the tachometer is really simple. The microcontroller runs at 5 Volt and can directly drive the PWM input from a CCP output. The signal from the tachometer needs to be pulled up to 12V, after which simple resistor divider suffices to get that signal at the required TTL levels.

The firmware

The application is written in MPLAB X and uses Microchip’s xc8 compiler. The LCD module is borrowed and adapted from the “PC Case USB LCD” project by Simon Inns. The source is heavily commented so should be quite easy to understand.

The demo

You can see that the fan starts spinning when the system is powered up, but that it doesn’t move at duty cycles of 0 and 5%. It does start spinning at 10%.

The source


I sincerely hope that this helps someone.

Building TagLib on Windows – part 2

Last time I left of with a built Release version of TagLib, but no good way to also have a Debug build. Let’s see if I can change that. For the next commands we need the appropriate Visual Studio Command Prompt.

For Release:

> cmake .. -G"Visual Studio 11" -DCMAKE_INSTALL_PREFIX="e:\workspace\lib\installed" -DZLIB_INCLUDE_DIR="e:\workspace\lib\installed\include" -DZLIB_LIBRARY="e:\workspace\lib\installed\lib\zlib.lib" -DWITH_ASF=on -DWITH_MP4=on -DBUILD_EXAMPLES=on
> msbuild /P:Configuration=Release INSTALL.vcxproj

For Debug:

> cmake .. -G"Visual Studio 11" -DCMAKE_DEBUG_POSTFIX="d" -DCMAKE_INSTALL_PREFIX="e:\workspace\lib\installed" -DZLIB_INCLUDE_DIR="e:\workspace\lib\installed\include" -DZLIB_LIBRARY="e:\workspace\lib\installed\lib\zlibd.lib" -DWITH_ASF=on -DWITH_MP4=on -DBUILD_EXAMPLES=on
> msbuild /P:Configuration=Debug INSTALL.vcxproj

Note the difference in the ZLIB_LIBRARY: zlib.lib vs zlibd.lib. Also note the addition of -DCMAKE_DEBUG_POSTFIX=”d”. This results in a file tagd.dll for the debug build.

Normally you would like to generate the project once and then build multiple configurations. The trouble is that we need to specify the ZLIB library which is different for Debug and Release. The FindZLIB.cmake module apparently is unable to correctly find the Debug version.

So now we have a Debug and a Release version of the library. That’s nice, but to goal is to use TagLib, so that the application automatically finds the TagLib headers and links to the correct library.

In the next article I will present my FindTagLib.cmake module to do just that.

Building TagLib on Windows

In my hobby project MusicCollection I use TagLib to read tags from audio files. The repository contains an INSTALL file which attempts to detail the steps necessary to build the project both on Linux and on Windows. Building TagLib and using it in a CMake based project is absolutely trivial on Linux. On Windows a bit less so.

Lukáš Lalinský of TagLib and MusicBrainz Picard fame wrote a blog detailing the steps that were necessary to build TagLib on Windows. These steps certainly helped a lot, but were not complete for today. Here is what I needed to do.

The current version of zlib is 1.2.8, the current version of TagLib is 1.9.1. I build a 32 bit version on Windows 7 with Microsoft Visual Studio 2010 or 2012. An answer on stackoverflow.com was very helpful.

  1. Download the sources for both packages at here and here. (But do check if newer ones are released.)
  2. Unpack and unzip and rename them in the same directory, i.e. e:\workspace\lib. I use 7-Zip on Windows which handles everything I ever come across. Now I have this structure:
  3. Start a “Developer Command Prompt for VS2010” (or VS2012) and navigate to the zlib directory. In my case:

> e:
> cd \workspace\zlib-1.2.8

  1. Build zlib, by first having CMake create a Visual Studio solution file, then building this solution with Visual Studio:

> mkdir build && cd build
> cmake .. -G"Visual Studio 11" -DCMAKE_INSTALL_PREFIX="e:\workspace\lib\installed"
> msbuild /P:Configuration=Debug INSTALL.vcxproj
> msbuild /P:Configuration=Release INSTALL.vcxproj

The -G”Visual Studio 11″ part instructs CMake to generate a Visual Studio 2012 solution. For a 2010 solution use -G”Visual Studio 10″. This should build with 0 errors and 0 warnings. Look for the results in the installed directory. Now, on to TagLib.

  1. Build TagLib much in the same way:

> cd ..\..\taglib-1.9.1
> mkdir build && cd build
> cmake .. -G"Visual Studio 11" -DCMAKE_INSTALL_PREFIX="e:\workspace\lib\installed" -DZLIB_INCLUDE_DIR="e:\workspace\lib\installed\include" -DZLIB_LIBRARY="e:\workspace\lib\installed\lib\zlib.lib" -DWITH_ASF=on -DWITH_MP4=on -DBUILD_EXAMPLES=on
> msbuild /P:Configuration=Release INSTALL.vcxproj

TagLib builds with a number of mostly silly warnings, but no errors. Note that this last part builds a Release configuration; you can use /P:Configuration=Debug but this results in a dll with the same name (i.e. it overwrites the release dll) that links to zlib.dll instead of zlibd.dll. This I will have to address later.

DiskStation: fix ipkg and SSH after upgrade

For about half a year now I own a Synology DiskStation DS412+, which is a wonderful 4-bay NAS. I fitted it with 4 3TB Seagate drives for a grand total of 12 TB of storage. Later I will document how I store my data and especially what and how I backup.

Now I want to note the steps I have to take after updating the DSM software of the box to get things back to normal. Because the same two things always break:

  1. Lost access to ipkg packages
  2. Password authentication gets re-enabled for SSH access

Continue reading