Swift for Arduino newsletter - Saturday November 16th 2019

Hello to the Swift for Arduino community!

We are so excited to get in touch with you and let you know what's going on.
First and foremost... Black Friday/Cyber Monday!
We are giving away huge discounts!

*** The Swift for Arduino Starter Box... HALF PRICE!! *** (Contains a Seeed Studios Lotus board and connectors, you don't need any of your own hardware, get started right away!)
Note: to use this discount, you must enter the discount code BFCM50 in the Shopify store during checkout. This discount is limited in time and number - act now or lose out!
Offer closes 23.59 on Monday 18th November 2019
(For convenience, you can use this link:

*** The Swift for Arduino IDE - license only... 70% OFF!! *** (suitable for customers who already have an Arduino UNO or similar board) This discount is automatic for customers purchasing the IDE only product, just go to our Shopify store and checkout…

Grove PIR sensor

As you can see in this video, I've hooked up a simple program to change the colour of a strip of neopixels in response to detecting people passing by.

See example code here:

But this is an easy project to write.

Grove Temperature and Humidity sensor

Grove are a fun concept, lots of cheap, little sensors, buttons, etc. on little PCBs with a universal 4 wire connector system (e.g. gnd, vcc, rx, tx or gnd, vcc, sda, scl). Making projects becomes more like lego but it's a bit less heavyweight than the "shields" or "hats" ideas, and a lot cheaper!

I got a mixed bag recently, about £12 worth, plus a "seeduino", which is Seeed Studios' *duino clone. The price quite recently dropped from something like $19 to something like $6.90! To be honest, most retailers, like the one I went to, are still selling nearer the old price, but I guess you can't blame them if that's what they paid for the stock. I think I forked out £18 in the end, plus the 12 for bits, making about 30... plus postage, etc.

I got:

An IR distance interrupter - single bit detects if a person (or thing) gets close, obviously great for robotics, although many other applications

PIR (passive IR) motion detector - security lights, e…

Button press daemon

As you've seen from my other posts, I have a couple of lamps at home, controlled by homekit. I also have a daemon on the pi that does (slightly inaccurate) PWM to control LEDs for showing the current weather forecast (look around my various blogs or ask here for links if you want to see how each of these works).

I've added a couple of useful functions to the daemon and recently upgraded it to be a bit more of a packaged service (see the github page). It's also in it's own repo now.

The last feature I added was I wanted some way to capture inputs not just output voltages.

There are already good solutions on the pi, many of them written in python, that will read input pin values, and about 7 years ago, someone patched the kernel to allow interrupt drive, an addition to sysfs to indicate you want to write code that blocks on a device with a poll() or select(). Probably the best solution for most people will be to use this mechanism within their own C program or (even bett…

Neopixel rainbow

Thanks to great work by Mark Swanson, neopixel support is now available for S4A!
We needed a small upgrade to the S4A component library, AVR to give some timing functions, otherwise the work is all done in swift. You can see some great demo videos.

Spurred on by this I decided to add some optional extra low level support for many basic classes of neopixels, to give developers two options how to get started.

Here is an example of making a rainbow using the new "Fast iLED" components.

And all this in just a few lines of simple Swift code!

import AVR
typealias IntegerLiteralType = UInt8let ledPin = 4
let pixels = 60var buffer = iLED_Setup_Buffered(pin: ledPin, count: pixels, hasWhiteChip: true)func rainbow() {
    let hueDeltaPerPixel = 255 / pixels
    for i in 0..<pixels {
        buffer[Int(i)] = iLEDFastMakeColor(hue: i &* hueDeltaPerPixel, saturation: 255, value: 255, white: 0)

Halloween LED lights on a plastic trick or treat cauldron

Halloween has past but it wasn’t uneventful. My two year old daughter went trick or treating for the first time and it was all so exciting!

When i saw the plastic cauldron she was carrying as a trick or treat bag i had an idea. I could use an arduino to operate red LEDs and make the cauldron flash on and off like little evil eyes. I knew she would love it!

Once I drilled the holes and got all the pieces I needed:

2 red LEDs soldered in series with a resistor.

I connected those to a breadboard arduino I had spare. (See my blog post on making a breadboard arduino.) You can just use a normal Arduino UNO here instead if you prefer. My reasons are because they're cheaper, and also because a breadboard Arduino uses less power and I wanted this to be battery powered.

I'm powering the whole thing with 3 AA batteries in a battery case. Because Maplin has shut down in the UK :((( I will have to wait for the battery holder to be delivered... no more weekend fun popping down the shop for …

Measuring fan RPM

Going on from our earlier posts about how to synchronise a strobe light to a three wire fan, I've designed something a bit more practical, to measure the RPM of the fan.

The important trick is we need to turn one of the timers into a "counter" and count the number of pulses coming in from the fan tachometer.  Then if we monitor the count per unit time, we get the RPM simply by multiplication.

What is a timer really? It is actually really a counter.  In normal timer modes, the timer counts clock cycles. You may have noticed you can run it at different speeds, this is done by dividing the clock cycle. But alternatively, two of the atmega328p timers can alternatively count pulses from external sources.

How easy is it to do? Well on S4A it's pretty easy...

timer0SetAsCounter(edgeType: RISING_EDGE)

Timer 0 will now count the rising edge of pulses on pin 4 (a…