IR signal recorder with Arduino Uno

I’ve been tinkering with IR and the TSOP38238 IR receiver modules I got from Adafruit and Sparkfun. That’s right, plural, as I burned the first one — be REALLY sure not to mix ground and VCC with this one! I ordered 10 more from AliExpress just to make sure I have spares in case I burn my second one as well…

There are IR libraries for Arduino already, but they were a bit complex to my taste, as I’m first planning just to record one IR code from my bulky Sony projector remote and make a small trinket to send that on button press. The TSOP382 already demodulates the signal, so I just want to record the times the remote IR led is on, off, on, off, and so on. So I made an Arduino sketch to do just that: Count loop cycles, detect when signal goes from high (no IR signal detected) to low and vice versa:
Continue reading IR signal recorder with Arduino Uno

BeagleBone Black GPIO Benchmark

Look what the mailman brought: It’s a shiny (or maybe matte?) BeagleBone Black, freshly arrived (actually it’s been over a month, but time sure flies…) from Newark element14! I’ve been doing Raspberry Pi related hacking for a while, but especially when the Pi was still fresh and new, I did from time to time consider if the grass would be greener on other side of the fence. Or blacker, in this case, as I mean BeagleBone Black.

BeagleBone was long very much more powerful than Raspberry Pi, but now that Pi2 has come out, price and specification-wise they are closer than ever. A quick personal comparison chart:

  BeagleBone Black Raspberry Pi 2 (B)
Price 46 € (Element14) 32 € (Element14)
Processor 1GHz single-core Cortex-A8 0.9GHz quad-core Cortex-A7
Memory 512MB DDR3 1GB
Connections USB host, USB device, micro-HDMI 4x USB, HDMI, 3.5mm Audio/analog video
GPIO 2x 46 pin headers (65 digital I/O) 40 GPIO pins (26 digital I/O)
Other 4GB integrated flash, works as USB device camera and display interface on board

When Pi1 was out, the BeagleBone Black with the more modern Cortex-A8 chip and higher clockrate was definitely the more powerful, but now with 4-core Pi2, the tables have somewhat turned. Still, the clockrate is higher and there’s more GPIO. And speaking of GPIO, my Raspberry Pi vs. Pi2 GPIO benchmark has gotten a lot of interest, so I thought the best way to take this black beauty for a test drive would be to benchmark BeagleBone Black GPIO in a similar way.

Test setup

Test bench

The test subject is the most recent revision C of BeagleBone Black. I followed the (a bit lacking in detail and readability) Getting Started guide and downloaded the latest Debian Jessie image (8.3, 2016-01-24), flashed it to card and ran apt-get update and apt-get dist-upgrade (2016-04-14).
Continue reading BeagleBone Black GPIO Benchmark

Bottle.py PyMySQL Plugin

I’ve been playing around today with BottlePy, an excellent mini-framework for Python web development. However, getting Python MySQL support is always a hassle, I never know if MySQLdb has died or not, so I thought I’d try PyMySQL for a change.

In addition to being just generally awesome, Bottle has a nice plugin syntax, and even a great 60-line sample of a SQLite plugin. So I adapted it for PyMySQL, turns out this was quite easy:

import pymysql.cursors
import inspect

class PyMySQLPlugin(object):
    ''' This plugin passes a pymysql database handle to route callbacks
    that accept a `db` keyword argument. If a callback does not expect
    such a parameter, no connection is made. You can override the database
    settings on a per-route basis. '''

    name = 'pymysql'
    api = 2

    def __init__(self, db, user, password, host='localhost', charset='utf8mb4', keyword='db'):
        self.host = host
        self.user = user
        self.password = password
        self.db = db
        self.charset = charset
        self.keyword = keyword

    def setup(self, app):
        ''' Make sure that other installed plugins do not affect the same
            keyword argument.'''
        for other in app.plugins:
            if not isinstance(other, PyMySQLPlugin): continue
            if other.keyword == self.keyword:
                raise PluginError("Found another %s plugin with "
                "conflicting settings (non-unique keyword)." % self.name)

    def apply(self, callback, context):
        # Override global configuration with route-specific values.
        #conf = context.config.get('sqlite') or {}
        #dbfile = conf.get('dbfile', self.dbfile)

        # Test if the original callback accepts a 'db' keyword.
        # Ignore it if it does not need a database handle.
        args = inspect.getargspec(context.callback)[0]
        if self.keyword not in args:
            return callback

        def wrapper(*args, **kwargs):
            # Connect to the database
            db = pymysql.connect(host=self.host,
                    user=self.user,
                    password=self.password,
                    db=self.db,
                    charset=self.charset,
                    cursorclass=pymysql.cursors.DictCursor)

            # Add the connection handle as a keyword argument.
            kwargs[self.keyword] = db

            try:
                rv = callback(*args, **kwargs)
                #if autocommit: db.commit()
            #except sqlite3.IntegrityError, e:
                #db.rollback()
                #raise HTTPError(500, "Database Error", e)
            finally:
                db.close()
            return rv

        # Replace the route callback with the wrapped one.
        return wrapper

As you may see, there is no autocommit support and graceful recovery from database errors is something that is missing as well, but it’s a great start. There were a couple of GitHub projects with similar aims, but those seemed a bit behind the times (one was adding Python 3.4 support, and I’m already on 3.5, for example). A little NIH syndrome for me, maybe. Using the above library is quite easy:

from bottle import install, route
from somefile import PyMySQLPlugin

pymysql = PyMySQLPlugin(user='dbuser',
        password='dbpass', db='dbname')
install(pymysql)

@route('/demo')
def demo(db):
    # This method has a "db" parameter, and the plugin activates
    with db.cursor() as cursor:
        sql = "SELECT `id`, `password` FROM `users` WHERE `username`=%s"
        cursor.execute(sql, ('johndoe',))
        result = cursor.fetchone()
        return str(result)

Hope you found this useful!

RedBear Duo First Impressions

Look what a little beauty the mailman brought! I participated the Red Bear Duo Kickstarter Campaign a while ago, and the folks at Redbear did a really professional job in delivering on the promises of that campaign.

Headline features include ARM Cortex M3 120 MHz microcontroller with plenty of RAM and flash, and of course dual WiFi and Bluetooth connectivity. Setting up the Duo was quite simple by following the instructions provided, once I realized I’ll need to use the Zadig tool as instructed in this sub-howto (Redbear guys: I think 4-5 separate pages to get one started on Windows is something that might be optimized), I got the firmware updated and everything set up. Basic outline of my installation was about this:

  1. Plug in the board and see LEDs light up
  2. Try to install the serial driver, only to realize I already had working serial (I probably have all usb serial drivers between heaven and earth installed due to encounters with various devboards)
  3. Connect to COM6, 9600 with Putty to verify it works as it should, note down device ID
  4. Install dfu-util, and wonder why I cannot connect to the device, even with the yellow LED (looked more like yellow-green though) was blinking as it should
  5. Use Zadig tool to install drivers when in DFU mode
  6. Successfully update firmware and stuff
  7. Reboot the board a couple of times and connect with Putty to COM6 to see the IP address
  8. Visit the IP address to see the LED demo is working as it should! Nice!

Particle.io Cloud Programming

Once I had the basics figured out, I wanted to try out the Particle.io cloud development platform. It seems like an Arduino on cloud steroids, meaning that instead of flashing the device over USB cable, you write the software in a web interface, and flashing the device will upload the sketch to your device using the active wifi connection on Redbear Duo (I believe the device is periodically polling Particle.io service to see if there is a new sketch to download).
Continue reading RedBear Duo First Impressions

USB Mouse with ATmega32U4 Pro Micro Clone and LUFA

I have spent a fair amount of time with 8-bit AVR microcontrollers and one of the cooler things has been the V-USB library which implements low-speed USB with clever (and very time-critical) bit-banging. The popularity of my USB tutorials is a testament to its usefulness, and I’ve gotten lots of mileage out of that.

There are, however, some limitations to software USB with such a low spec microcontroller. USB communication hogs up the MCU completely during USB communication, which means you lose dozens of microseconds in random (or in many cases 8 ms) intervals. This rules out things like software UART at reasonable speeds (which I discovered when trying to implement MIDI on Adafruit Trinket). And more powerful ATmega328-based dev boards like Pro Trinket start to get quite large.

ProMicro on a breadboard

Not so with this tiny beauty shown in the image. It’s a ATmega32U4 based board, where the U4 means it has hardware USB support. The form factor is extremely compact 12 pin header length, which leaves 5 rows free on the smallest prototyping breadboards. That means you can have a DIP8 component with a few resistors on the same breadboard (such as a 6N137 optocoupler which is nice for MIDI… ;).

And the best part is, that because the chip is flashed with same firmware used in Arduino Leonardo (and a largely matching pinout), you can use Arduino for programming, and avrdude supports it out of the box.

Actually, scratch the above statement. The best part is the price. The board is based on Sparkfun Pro Micro 16 MHz, but it’s actually a Chinese clone, which you can get for $4 via DealExtreme and from quite many places in AliExpress: Just search for ATmega32U4 and they will come up. This means you can just order five and solder them into whatever project you’ll make permanently. And unlike Arduino Micro (for which clones exist as well), this has the micro-USB port already in place.

Using Pro Micro without Arduino IDE

Now you can just follow SparkFun’s instructions on how to use that thing on Arduino (short version: select Leonardo as board type, and look up the schematic if you are unsure which pins are connected to LEDs, etc.). But if you’re like me and want to get to raw metal, avr-gcc and avrdude is the way to go. Here’s a simple blinky demo:
Continue reading USB Mouse with ATmega32U4 Pro Micro Clone and LUFA

MIDI to USB Adapter with Teensy LC

Among my recent electronics purchase spree was the amazing Teensy LC from PJRC. It has a nice ARM Cortex-M0+ processor, real hardware USB, and what’s the nicest part, an Arduino add-on called Teensyduino which enables easy programming with Arduino, but with support for many of the hardware features.

Now I started playing piano a while ago, and just a few weeks ago bought a Pianoteq license to send notes via MIDI to my computer, and render high quality piano sound to speakers. However, it turns out my $8 USB-MIDI adapter from DealExtreme had a less than perfect implementation, essentially changing pedal events into “note on” events!

Thankfully, I had a MIDI connector and a high-speed optocoupler at hand, and with these I could implement a MIDI in rather easily. After some investigation with Arduino Uno, it seemed quite simple to receive the serial MIDI bytes and dump them over Arduino serial (I’ll write another post about this later).

However, Arduino cannot become a USB MIDI device very easily, so here comes the really nice part: Teensy LC can, and the Teensyduino add-on included a working USB MIDI and also serial MIDI libraries!

The Hardware

The Wikipedia page for MIDI essentially shows the required circuitry for receiving MIDI data – wire the DIN cable pins 4 and 5 through the receiving side of optocoupler and put a 200 ohm resistor in series with it. A diode is also suggested for reverse current (ESD) protection, but I skipped that. You can start with a LED instead of optocoupler to see it lights up if you’re unsure you have pins 4 and 5 the right way. Or just put the LED (with a resistor) on the other side of the optocoupler first.

The HCPL-2531 I had at hand requires an additional VCC connection on the sending side. After some experimentation, a 4k7 ohm pullup resistor between VCC and VO1 (NPN-transistor base?) gave the cleanest signal out. The wiring diagram (thanks Diptrace!) is shown below:

Teensy LC MIDI schematic
Continue reading MIDI to USB Adapter with Teensy LC

Adafruit Trinket USB keyboard without Arduino

Happy New Year 2016! After a long hiatus in electronics, I have been quite busy in the last month or so. I have a bigger (USB MIDI related) project posting coming up, but just wanted share a small nugget already.

I ordered a big chunk of things recently from Adafruit shop and Sparkfun. Among them was the lovely Adafruit Trinket (which actually came as a free bonus because I spent way too much on black friday :).

Now I am planning a project which involves transforming the very compact, but already USB-enabled Trinket into a USB MIDI device. However, there are two problems:

  1. Adafruit examples for USB come in Arduino form
  2. There are no USB MIDI examples

I somewhat dislike the high-level Arduino environment in cases where low-level performance is needed (and the V-USB implementation is one of those places), and for my later MIDI part, I will need fine-grained control to juggle serial communication and USB. Also, all USB MIDI examples are on “bare metal”, so the Adafruit example Arduino code would require deeper knowledge of Arduino inner workings than I have.

Time to do some chopping!

Slimming Down the TrinketKeyboard Example

I decided to adapt the excellent base code in Adafruit Trinket USB GitHub repository, but trim the keyboard example to bare essentials (my next step will be to transform it into a USB MIDI device, so the less code I have to adapt, the better). Turns out this was quite simple to do:
Continue reading Adafruit Trinket USB keyboard without Arduino

Topre Realforce vs. Matias Mini Shootout

Topre Realforce vs. Matias Mini

It’s been ten months since I got and reviewed the Topre Realforce 88UB. I’ve been very satisfied with the keyboard, the only real issue being that for some reason my brain is having strange difficulties with adjusting to the new microlayout since my time with HHKB — for some reason I still hit adjacent keys quite a lot when coding, getting # instead of % or ‘k’ instead of ‘j’ (doubly frustrating with Vim!).

However, the thirst for new experiences never really leaves you, so I decided to try out the Matias switches that have caused quite a lot of discussion at Geekhack. Since I’ve liked the low thud of Topre keys and loved the compact layout of RF 88UB, I decided I’d see how the Matias Mini Quiet Pro compares to my Realforce.

After using the Matias keyboard extensively for several weeks, I think I have enough experience to write a bit about this new entrant to the rather established Cherry/Topre/Unicomp triopoly of mechanical niche keyboards. To make it more interesting, it’ll be a shootout against the reigning king, Topre Realforce 88UB. Fight is on!

Warm-up Round: Specifications and Price

thekeyboardco
I got my Realforce through The Keyboard Company in UK, and they were kind enough to provide the Matias Mini review unit for this shootout. As a thanks I’m including their banner here, and based on several years of personal experience I can really recommend them, especially if you’re within EU as there will be no customs fees.

Specification-wise the Realforce and Matias are quite similar with reduced tenkeyless layout sporting function and cursor keys. The Topre has standard pageup/pagedown etc. column whereas Matias a bit more compact but requires a function key to access insert, home and end and the more esoteric print screen, scroll lock and pause/break keys. Price-wise, the Matias sits in the not-quite-inexpensive $160 price range, but the Topre almost doubles this with its hefty $295 price tag.

  Topre Realforce 88UB Matias Mini Quiet Pro
Price ca. $295 ca. $160
Layout Tenkeyless Compact with cursors
Mechanism Topre 45g Matias quiet
Weight Heavy (1200g) Sturdy (950g)
Connectivity USB USB
Keys Dye sublimation (black on black) Laser etched (white on black)
Extras Yellow WASD keys and keycap tool 3 USB 2.0 ports and two cables (long & short)

Continue reading Topre Realforce vs. Matias Mini Shootout

Raspberry Pi 2 vs. 1 GPIO Benchmark

Raspberry Pi 2 GPIO Benchmark

It’s battle time! Some of you may have heard that Raspberry Pi 2 is out with more punch than ever. Just how much more? Well, apt-get dist-upgrade went about 5 times faster with the new Pi. With 1 GB of RAM and four cores, this will definitely be a boost for my home SSH box ergonomics over the previous version.

But what about hacking? There has been a lot of interest in getting GPIO benchmarks for the Pi 2 similar to my earlier Raspberry Pi GPIO benchmark. Well here it is! Please refer to the earlier article for source code and nice screenshots of square waves, as I’ll concentrate on the performance difference only here. You can also get the code from Github:

https://github.com/jokkebk/rpi-gpio-benchmark

Summary of results

All the Pi 1 benchmarks were ran 14th and 15th February 2015 using latest versions of the libraries as stated in my updated benchmark post. Pi 2 benchmarks were all run 25th and 26th March 2015 with the latest versions. If you get significantly different results at a later date, please let me know and I’ll update the table!

Language Library Pi 1 Pi 2 Change
Shell /proc/mem access 2.8 kHz 7.0 kHz 2,5x
Shell / wiringPi WiringPi gpio utility 40 Hz 95 Hz 2,4x
Python RPi.GPIO 70 kHz 243 kHz 2,5x
Python wiringpi2 bindings 28 kHz 103 kHz 3,7x
Ruby wiringpi bindings 21 kHz N/A 3,7x
C Native library 22 MHz 41.7 MHz 1,9x
C BCM2835 5.4 MHz 7.2 MHz 1,3x
C WiringPi normal GPIO wiringPiSetup() 4.1 MHz 9.3 MHz 2,3x
C WiringPi GPIO wiringPiSetupGpio() 4.6 MHz 9.4 MHz 2x
C WiringPi sys wiringPiSetupSys() 120 kHz 185 kHz 1.5x
Perl BCM2835 48 kHz 154 kHz 3.2x

Continue reading Raspberry Pi 2 vs. 1 GPIO Benchmark

Topre Realforce 88UB – A Happier Hacking Keyboard

Topre Realforce 88UB review

This is my review of the Topre Realforce 88UB mechanical keyboard with evenly weighted 45g switches and UK layout. This is the ISO layout sister model of the Topre Realforce 87U (87UB / 87UW) so everything in this review will apply for that model, except a few layout details regarding the extra keys. Before diving into details, a short background of my previous journey:

Two years ago I got really interested in high quality mechanical keyboards. After all, the keyboard is the instrument I use several hours every day, so if I am spending close to 2000€ on a workstation and display, why should I compromise with a cheap 30€ keyboard, especially since the keyboard will likely last several workstation lifespans?

After heavy research I settled on Happy Hacking Keyboard 2 Professional due to its compact “hacker” layout which seemed great for vim, and Topre key switches, which many consider to have the very best tactile feel. See my review of the HHKB2 keyboard for details.

However, in two years of typing with the HHKB, I never fully learned to use function key bindings for cursor keys and Home/End fluently. Coding with Vim somewhat alleviated the problem, but every time with a command prompt or a shell this nagged me. Also, playing games required a separate keyboard, as most games make use of function keys, and many make use of both WASD and cursor keys. It was time for something else. Something better.

Topre Realforce 88UB – Best of both worlds?

After my experience with HHKB, I had the critical components for my dream keyboard well thought out:

  1. Tenkeyless model with dedicated cursor and function keys. I’ve also used CM Quickfire TK and I can say from experience it is a terrible idea.
  2. ISO layout, so I get the \ and | right next to my right pinky, and an additional key next to z to get < and > in Finnish keyboard layout (it doubles as another \| in US layout)
  3. Topre switches for the ultimate typing comfort. Having used rubber dome, scissor mechanisms and Cherry MX blues, and trying out reds, blacks and browns, there really is no competitor. Buckling spring is just too heavy and noisy for my taste.

The items 1 and 2 are quite easy, but the third one essentially meant that the Topre Realforce line would be the primary candidate to fill all these needs. I’ve used The Keyboard Company before and they have a superb selection of Topre models, so I quickly zoomed in on the black 88-key model with UK layout:

Topre Realforce 88UB UK layout
Continue reading Topre Realforce 88UB – A Happier Hacking Keyboard