Syncing a strobe to a fan, fun, Swift and easy!

Syncing a strobe to a fan, fun, Swift and easy!


Have you ever really looked at a PC fan?  I must admit I hadn't really until recently. I knew that from linux boxes you could often control the speed of at least some of the fans and even measure the speed of the fans sometimes.  I also knew you could get little side fans that just plugged into the same, regular 4-pin power cables that snake all over the inside of your typical desktop, powering CD-ROM drives (haha, just kidding), HDs, etc. all over the inside. Yet the more controlled types of fans seemed to have their own special connectors on a motherboard.

It turns out that fans come in (at least) three varieties...

Simple "two wire" fans that have a power and ground rail wire - these usually cannot be speed controlled or measured.

Three wire fans - it turns out these can have their speed measured and possibly controlled.

Four wire fans - these have easy speed control and speed measure.

I've not done an audit of fans but my guess would be the four wire ones are the most common.

How is speed control achieved? It's pretty much what you might expect, either the voltage to the fan power is altered (not very common) or the power supply is subject to PWM to control power. The latter can have some slight annoyances due to "ringing" (articles all over the internet will explain the details) but it's easy to implement using S4A with a simple analogWrite statement and a transistor to deliver the fan power.

So how is speed measured? It turns out the third wire on a three wire fan is connected to a "tachometer" on the fan. This is a simple hall sensor, shorted to the fan ground wire that measures the fixed magnets passing as the fan rotates (PC fans are always brushless motors). Typically it will cut on and off twice per revolution of the fan.

In this video https://www.youtube.com/watch?v=NX8GGZ6C978 you can see what happened when I hooked this signal up to my oscilloscope. I raised the voltage of the tacho wire using a pull up resistor then measured the resulting voltage, which cut to zero twice per revolution as the hall sensor was activated by the magnets.

How can we use this?

Well the obvious first answer is to measure the pulses coming from the fan and divide over time to figure out the fan RPM.  I'll do that in a later project/post but for now I had something rather fun in mind. :)


What if I detect a rising edge on the pulse and (briefly) flash an LED as this happens?



I think you can guess. It worked very well! :)

Turning down the lights in the room and activating this project, the LED flashes at exactly the same points in the fan cycle, meaning you see the fan blades "frozen" as if unmoving! It looks cool and of course in S4A this is so much easier to do safely and easily than the equivalent C code would be...

setupPin2InterruptCallback(edgeType: RISING_EDGE) {
    digitalWrite(pin: ledPin, value: HIGH)
    delay(microseconds: 300)
    digitalWrite(pin: ledPin, value: LOW)
}

That's it!


Next I looked at the logo in the centre of the fan, and put a little sticker on the fan expecting to see the logo frozen in place but it looked a bit odd.

I quickly realised the problem, we have two pulses per revolution so we see the logo upside and right way up at the same time.  Easily fixed! We create a Boolean variable called firstPulse to alternate on and off and divide the signal by two. We could do it in a more complex way but that will work...

let ledPin = 4
var firstPulse = true

pinMode(pin: ledPin, mode: OUTPUT)
digitalWrite(pin: ledPin, value: LOW)

setupPin2InterruptCallback(edgeType: RISING_EDGE) {

  firstPulse = !firstPulse

  if firstPulse {

    digitalWrite(pin: ledPin, value: HIGH)
    delay(microseconds: 300)
    digitalWrite(pin: ledPin, value: LOW)
  }
}

And that's it!  The fan is seemingly frozen in place by the strobe.  Watch this night time video for a perfect demonstration of the effect...

https://www.youtube.com/watch?v=voY-VMM3nrg

See our next blog piece for how you can use a rotary encoder to "turn" the angle the fan is frozen at for an even more cool effect!

Comments

Popular posts from this blog

Halloween LED lights on a plastic trick or treat cauldron

All about bootloaders

code signing, entitlements, bundles, sandboxes, hardened runtime, notarisation, app store security