Wednesday, July 31, 2013

MSP430 Serial communication with LInux

In addition to hacking on Arduino, I am interested in other hobbyist microcontroller boards. One of the more promising ones is the MSP430 Launchpad by Texas Instruments. It is a tiny kit with interchangeable microcontrollers, and it runs great on Linux with an IDE called Energia.  Energia is a fork of the Arduino IDE so it should be familiar to most microcontroller hackers.

One downside to the Launchpad is that serial communication for the board don't work on Linux. The problem seems to be that the USB-serial emulation on the launchpad has a bug.  A person on the forums found that the Launchpad device is requesting a disconnect shortly after connecting. Gerald Stanje has an updated cdc-acm kernel module that ignores these disconnects.  That option works in case you don't mind installing a new kernel module. Also, remember to use the normal cdc-acm kernel when using devices other than the Launchpad.

External USB-serial device

A second solution is to purchase an external USB-serial translation device. There is a glut of cheap USB-serial converters available on online sites. Here is one such device based on the PL2303 chipset that costs less than $3, including shipping. If you buy such a device, you can bypass the USB-serial translation on the launchpad and use the PL2303 (or FTDI) based chip.

To use this method, disconnect the jumpers RXD and TXD jumpers that cross the Emulation - MSP-EXP430 line. Then, connect the lines as follows:

  1. GND on USB device to GND on Launchpad
  2. TXD on the USB device to RXD on the Launchpad
  3. RXD on the USB device to TXD on the Launchpad
  4. 5v on the USB device to a solder pad TP1 on the Launchpad near the Launchpad USB connector. This will power on the entire Launchpad device.
Using this, you should be able to communicate with the launchpad. To program it, connect the USB connector to your desktop as before.

This method doesn't require a new kernel module. In addition, this method works with any microcontroller board that can do serial communication: either through hardware UARTs, or through software.

Sunday, July 28, 2013

Tip: External editor for Arduino

I love Arduino: it is the perfect microcontroller platform. It is easy to program on Linux, cheaply available, there is a library for most anything you can think of. And the community is great for when you get stuck.

My only grudge: The Arduino editor doesn't match up to Emacs.

Yes, the IDE is great, but for large programs I find myself yearning for a C mode, proper indenting, incremental search, buffers. You know, a real editor.

Makefile for Arduino

So I experimented with using Emacs and compiling from the console. After experimenting with a few Makefiles, OpenXC works great for me. Clone their project, and set up environment variables according to your setup. I had to change exactly zero variables for their examples to work out of the box on Ubuntu. The Makefile is well documented. You can upload the program to an arduino by doing 'make upload'. This works with simple sketches and for doing everything through a shell script or cron.

It has limitations. It won't do pre-processing: so it will claim that methods or data structures are not defined. It won't work with multiple .ino files. Also, the list of boards is limited, and it doesn't have an easy way to specify Arduino Mini with 8Mhz, which is the device I'm currently hacking on.

External editor for Arduino

Navigate to:
File -> Preferences -> Use external editor.

This should gray out your editor window. Now, your Arduino IDE is only for compiling, uploading, and for the serial monitor, which it excels at. To save screen real estate, you can make the window short, though it wants to be a minimum height. Now edit it in your favorite editor.

When you are ready to compile, switch to Arduino (Alt-Tab), and then compile (Ctrl-R) or upload (Ctrl-U).

I'm waiting for Arduino to tie in with inotify on Linux and automatically compile when the code changes.  But in a pinch, this workflow gets over most of the annoyances of the Arduino IDE while retaining most of its advantages.

Makefile + External Editor

The best approach might be to combine the two above. Switch to an external editor in Arduino, and use a Makefile for automated builds on the command-line. The automated builds on a console will quickly let you know if your incremental edits introduced a compilation bug. And when you want to upload it to your boards, switch over to Arduino IDE.

Saturday, July 27, 2013

The joys of parenting: #1

This morning, walking with my son. We saw some people jogging.

Me: What are these people doing?
Son: (with characteristic enthusiasm) They are all running!
Me: Where are they going?
Son: They are going to papa office!
Me: Why are they running?
Son: <thinks for a minute>
Son: Because they are out of bicycles!

According to my son, everyone is going to my office all the time. Since I bicycle to work, to him everyone bicycles to work. And anyone bicycling is going to my office.

In the evening, we were using a rotary cutter for the first time. My son was watching from inside the house through large windows as I was using the rotary cutter outside. The sound was a loud grinding noise, which is his favorite sound. But the rotary cutter is more shrill than the other appliances, so I saw him back away from the window slowly.  I stopped the cutter, and looked inside.

Me: Are you ok?
Son: Yes.
Me: Are you scared?
Son: No-o.
Me: Are you happy?
Son: (after some hesitation) Yes.
 Then he makes the sign for happy.

But I can tell he is a bit shaken, so I take off my protective gear and go inside.

Me: Were you scared?
Son: No-o.
Me: Were you a little bit scared?
Son: (hugs me warmly) Yes.

I love parenting.

Monday, July 22, 2013

SoundSleep source code

A while ago, I had developed an application to continuously play music on my phone.  The program is called Sound Sleep and is available for free on the Android store. I use this application every night to play sleep-time music for my child.

I have now put the source code to Sound Sleep online. The entire Android source code for Sound Sleep is available at Github under the Apache License version 2.

To clone the entire repository:
$ git clone

This source is great for learning how to program with Android. It demonstrates:
  1. Running on a wide variety of Android devices.  The minimum SDK version is 3, and I use this on an ancient phone with Android 1.6
  2. Using SD card storage to read music
  3. Setting up a new service and communicating with it using Local Broadcasts
  4. Playing music from Android applications

I'm happy to accept patches and suggestions. I have used it daily since March, and it has been rock solid and indispensable.  It has exactly the feature-set I need. Here is a screenshot of the application.  There are two controls: the top half plays music randomly from SDCARD/music/sleeping. The bottom half plays white noise.

Thursday, July 11, 2013

Hacking with your child

Kids want to be involved in adult activities.  This is so fortunate!  My son has started taking interest in my microcontroller devices.  If I could involve him in building some circuits, I get more time to play with electronics. As a bonus, this would give my wife some free time and distract my son from his usual business of hiding valuables and destroying property.

So we set out to build a simple circuit: we chose a blinking lights circuit.  The core of the circuit is the NE 555 chip in astable mode: with a single LED blinking. If you want to do this, follow along. The point of this post is not what circuit to build, rather it is how to build this circuit to keep the interest of a very young child.

Some overall guidance:

  1. Simple circuits: A friend at work suggested this idea. Select a trivial project that you can get done in no more than 15 minutes. Everything takes longer with a child, and they have shorter concentration spans. Aim to finish in 30 minutes with the child.
  2. Prepare in advance: Depending on how old your child is, you might want to prepare most of the circuit in advance. Remember, you don't want to debug things with a potentially cranky child. One trick I use is to wire everything with long wires and tall leads on the components and get it working. Once it is working, get your kid along and switch all the ugly long wires for short ones, trim the leads, and have the child put the most critical component in, which gets everything going.
  3. Involve the child: The idea is to get them to participate and get a feeling of accomplishment.
  4. Avoid theory: At this age, the child is too young to understand what a resister does. Get them to love the process.
  5. Light-based circuits: Light-based circuits work well. Sound-based circuits get annoying for parents.
  6. Real pieces: The more this looks like a real grown-up thing, the more some kids will be interested. Avoid tacky childish substitutes.
  7. Future hacking: Keep some room for future hacking. Extra room on the breadboard, potential to add a switch for some simple interaction. This will come in handy when the child starts to lose interest, or asks to make another circuit.

The goal: get a 2-3 year-old child to see how a circuit board is built and get the child familiar with some components.
  1. This is the circuit we built. Open the page and print it out.
  2. Obtain twice the parts: a breadboard, a 9V battery and clip, a 555 chip, a capacitor, three resisters and one LED.   You are getting twice the parts because young kids love holding on to some parts. You want a spare sets of parts both to account for loss and for a child getting very affectionate towards a 1uF capacitor.
  3. Prepare the board and make sure you know what you are doing. Your electronics chops might be weak, check if you can get the circuit working.
  4. When your child is free, motivate the idea. "Do you want to make a NEW blinky-blinky?" "Oooh, shall we make a NEW blinky-blinky?". Stress on creation, stress on the fact that you are doing it together. If the child is not interested, wait for an opportune time. Consider involving your spouse in motivating the idea: the child might be going through a mommy-phase or a daddy-phase. It could be that your child just doesn't care about circuits. Respect your child's interests.
  5. Get the child to sit down on the dining table high chair. Consider strapping the child for everyone's protection.
  6. Show the child all the components, name them. You'd be surprised what they can remember. 
  7. Start putting the components in, carefully clipping leads and wires to a precise fit. You want all the components to fit tightly on the board. With any luck, this will be your child's favorite toy for a few weeks. That means high G-forces, and being carried everywhere. You don't want to drop components all over town.
  8. If your child wants to put in components, allow it. Allow incorrect component placement. Don't chastise. Instead, gently correct the component while saying, "Hmm.  That's good, but how about we put it this way..."  Any negative words might turn your child away from this entire affair. And success here can justify that $1000 oscilloscope to your spouse. So be patient, and avoid evil thoughts.
  9. Keep an eye on your child's interest level. If they look bored, stop. Return another day.
  10. Leave the 555 IC for the very end, and perhaps get your child to put it in.
  11. Hopefully, you get the project done. Congratulations! If it helps, call the breadboard something special to enforce that something new was created: "Wow, this is a 555-blinky-blinky!"
  12. Allow your child to hold the breadboard, toss it around. Don't disapprove of rough handling. This is your child's toy, not yours.
  13. Ideas for future hacking:
    A second LED that turns on when output on pin3 is low. You'll need a pull-up resister and an LED. Try a different color if possible.
    You could add another 555 for two extra blinking LEDs. Choose a different period of oscillation.
    You could include a push-button switch to force Pin3 high to keep the LED on as long as the button is pressed.

Things you might do differently depending the age and personality of your child:
  1. Solder: I used a breadboard rather than soldering because the end-product should be non-sharp, and should be child-friendly. Depending on the age of your child, you could use this as an opportunity to teach some soldering.
  2. Explanation: If your child has more background, perhaps from electronic toys, consider explaining what the components do.

Credit: Many people at work for suggesting projects and giving guidance before I started out. You know who you are. Thanks!

Wednesday, July 10, 2013

Camera shutter control for Arduino

I'm building a remote shutter release for my Nikon DSLR. I'm using Arduino to light up an Infra-Red LED.  There are many libraries that provide the correct LED pulse for Nikon cameras. Unfortunately, they don't often work, leading to time-consuming debugging. So here is the current state of affairs when it comes to Nikon IR shutter release through Arduino.

The library that works

The Arduino Multi Camera IR control by Sebastian Setz. I have version 1-91 of the library. My current environment is Arduino 1.0 that is pre-packaged for Ubuntu (I obtained it through apt-get install). I'm using this on an x86 laptop. Currently I'm using an Arduino Mega for this, though I'll switch to an Atmega328 for the real project. The IR LED is a 940nm component I bought many years ago.

I mention this in painful detail to illustrate everything that was under suspicion when the project didn't work out. The sample code involved is trivial.  But when things don't work out, debugging can get quite involved as you check every thing methodically.

The library above is the Swiss-Army knife of shutter control. Not only does it work with Nikon, but it also works with other, lesser known brands. Minolta, Sony, Pentax, Olympus, even obscure brands like Canon.

The libraries that don't work

Von Roth's Nikon library:  I had used this in the past with one of my Arduino boards and it had worked on the first try. So this was the first library I used. I was disappointed when it didn't trigger my shutter. I thought the culprit was the IR LED, since it had sat in the bottom of a parts-box for a few years, and looked rebellious at this mistreatment.

Lady Ada's intervalometer code: Given the considerable reputation of Lady Ada, I was certain that this code would work. But I couldn't get it to work, again, leading to more unjustified suspicion heaped on the IR LED.

The debugging was fun, though only in retrospect. To test whether the LED was working, I pulled out my baby monitor and verified that the LED was indeed emitting some Infra-red. A few Arduino boards were swapped, I switched from x86_64 to x86, I switched to the old installations of Arduino.

Hope this saves you some effort.

Credits for the image and the library: Sebastian Setz