Posts

Grove PIR sensor

Image
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: https://github.com/swiftforarduino/community/tree/master/work%20in%20progress/Grove%20Examples/Neopixel%20person%20sensor.swift4a

But this is an easy project to write.

Grove Temperature and Humidity sensor

Image
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

Image
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.

https://www.youtube.com/watch?v=e3nqyxCACKA&feature=youtu.be


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)
    }
    iLEDFastWriteBuffer()
}

Halloween LED lights on a plastic trick or treat cauldron

Image
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

Image
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.

https://github.com/swiftforarduino/community/blob/master/contributed%20examples/Fan%20RPM%20speed.swift4a/main.swift

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…

The internal Arduino temperature sensor...

Image
OK so I've probably already surprised many of you.  Didn't know there's a temperature sensor built into an arduino?

Regular readers will be familiar with the chip in the Arduino UNO, the basis of the Swift for Arduino product, being the Atmel Atmega328p. This contains an internal temperature sensor that's hooked up to the same MUX that feeds the analog to digital converter, that allows you to read analog voltages on pins A0-A5.  In practical terms, that means you can read the temperature from this sensor and by extension roughly the temperature of the arduino by programming it correctly.

On some platforms this would be a complex process, on S4A... not so much...

Here's the code...

let temperature = slowReadTemperatureCentigrade(tsOffset: 335, tsGain: 1.15)

Yup.  That's it!

What are the magic numbers for? What are the caveats? Why can't I do this on other Arduino platforms or why is it so difficult??

Well, the magic numbers and the caveats... I'll come t…