Apr 292013
 

In late 2006 I wrote “arduino-serial“, mostly for myself, to help with stuff I was working on at the time. It was a very simple & small, cross-platform tool written in basic C for reading/writing serial ports.

Now nearly seven years later I still get regular questions and frustrations about it. Part of this is due to how Arduinos have changed over time. You used to have to hand-reset an Arduino board, now the act of opening the serial port resets it. This has its plusses and minuses, but it really made my original use-case of arduino-serial fail. Then there were just all the minor deficiencies of the program.

To address some of these issues, but still keep things small & light, I’ve done a bit of fix-up of arduino-serial. It’s now hosted on Github at:
https://github.com/todbot/arduino-serial/

Changes & Improvements

Some changes that I recently made to arduino-serial:

  • Separation of the application (arduino-serial.c) from the library (arduino-serial-lib.{c,h})
  • Fixed probable --read bug
  • Fixed --port open to allow re-opens
  • Added --sendline command to send a string followed by a newline
  • Added --flush command to clear out receive buffer
  • Added --eolchar option to let you specify your own end-of-line character if ‘\n’ isn’t appropriate
  • Added --timeout option to specify a read timeout (reads no longer block infinitely)
  • Added --quiet flag to make output more terse/machine-readable

Here’s what the new usage help screen looks like:

laptop% ./arduino-serial
Usage: arduino-serial -b <bps> -p <serialport> [OPTIONS]

Options:
  -h, --help                 Print this help message
  -b, --baud=baudrate        Baudrate (bps) of Arduino (default 9600)
  -p, --port=serialport      Serial port Arduino is connected to
  -s, --send=string          Send string to Arduino
  -S, --sendline=string      Send string with newline to Arduino
  -r, --receive              Receive string from Arduino & print it out
  -n  --num=num              Send a number as a single byte
  -F  --flush                Flush serial port buffers for fresh reading
  -d  --delay=millis         Delay for specified milliseconds
  -e  --eolchar=char         Specify EOL char for reads (default '\n')
  -t  --timeout=millis       Timeout for reads in millisecs (default 5000)
  -q  --quiet                Don't print out as much info

Note: Order is important. Set '-b' baudrate before opening port'-p'.
      Used to make series of actions: '-d 2000 -s hello -d 100 -r'
      means 'wait 2secs, send 'hello', wait 100msec, get reply'

Using arduino-serial

arduino-serial has always been designed so you can “pipeline” commands/options, but it wasn’t implemented very consistently. It’s a bit better now. You can do multiple send/read pairs, even use multiple serial ports, all from a single command-line invocation.

For example, if you have the “SerialCallResponseASCII” sketch from the Communications examples loaded onto your Arduino, you can run commands to take multiple data readings. In the example below, the order of operations are:

  1. serial port is opened (at 9600)
  2. the string “A” is sent (a single-byte)
  3. the first line is read
  4. sleep for 1000 milliseconds
  5. send “A” again
  6. read second data line
  7. flush read buffer (just to show we can)
  8. send “A” a third time
  9. and take a final reading
laptop% ./arduino-serial -b 9600 -p /dev/tty.usbmodemfd131 \
            -s "A" -r  -d 1000  -s "A" -r  -F  -s "A" -r
send string:A
read string:465,396,0

sleep 1000 millisecs
send string:A
read string:358,352,0

flushing receive buffer
send string:A
read string:307,305,0

The flush was put in there to demonstrate that you could flush the receive buffer mid-command if you wanted. It’s not required, but might help some situations.

To use multiple serial ports, you can do something like the below, which opens up one serial port at 9600 bps, does a send & receive, then opens another at 57600 bps and does a send & receive on it:

laptop% ./arduino-serial -b 9600 -p /dev/tty.usbmodemfd131 \
        -s "A" -r \
        -b 57600 -p /dev/tty.usbserial-A800f8ib \
        -s "hello" -r 
 Posted by at 5:43 pm
Jun 242012
 

BlinkM coin cell holder

Did you know you can power BlinkMs or BlinkM MinMs off two 3V coin cells? Here is a tiny 3d-printable coin cell and header socket holder for BlinkMs and BlinkM MinMs. It snaps together and has mounting holes to turn it into a pendant or mounting additional diffusors for the LEDs.

And you don’t need many parts to build it:
- one each of plastic pieces here (see the STL files on Thingiverse)
- two CR2032 3V coin cells
- one 4-pos female header socket (Digikey S7037-ND or equivalent)
- two short pieces of solid wire (old resistor leads, 26 gauge wire stripped, etc.)

BlinkM coin cell holder

Steps:
1. Push the 4-pin socket into the hole until it stops. It should friction-fit in without any need for glue.
2. On the bottom, feed the two wire pieces in as shown in the photos, and solder them to the two pins on the header socket. These wires become the battery terminals.
3. Bend the wire battery terminals so they make good contact with the cells.
4. Insert coin cells, snap together top piece.
5. Insert BlinkM and watch it play its light sequence.
6. To protect the back, put a small piece of gaffers tape or similar over the terminals.

For more info and pix, see this project’s Thingiverse page or the Flickr pictures.

 Posted by at 9:48 pm
Mar 022012
 

Did you know each Arduino has a unique serial number in its USB interface that you can use to distinguish one Arduino from another? If you deal with multiple Arduinos, knowing exactly which one is plugged into your computer can be a real time-saver. But actually getting at this serial number and mapping it to COM ports can be challenging.

For Windows computers, here’s “listComPorts”, implemented both in GCC C code and in VBScript, both available from my usbSearch github repository.

It gives the COM port number, the manufacturer name, the USB Vendor ID and Product ID (VID & PID) and the serial number. The result is a concatenation of the VID, PID, and serial number that Windows calls a “PnPDeviceID”. You can use the whole thing, or pick off the parts you need using simple string processing libraries. In the example above, the Arduino Diecimila has a VID/PID pair of 0403/6001 and the Teensy has a VID/PID of 16C0/0483.

Arduino Serial Numbers

In the above example, the Arduino Diecimila (which has an FTDI chip) has the serial number with a “+”-sign after the PID. So, the serial number is “A6004CCF”. The Teensy can have a programmable serial number and by default it’s set to “12345″.

With Arduino UNOs and future Arduinos, the Arduino team has their own VID/PID pair, which let’s them assign different PIDs for different classes of Arduinos. The UNO also has a much longer unique serial number than older Arduinos using the FTDI chip.

Here’s an example with two Arduino UNOs:

The serial numbers are separated by the VID/PID pair with a backslash. So my two UNOs have the serial numbers “64936333936351408161″ and “6493234373835191F1F1″. Using this info, I can be sure exactly which Arduino I’m dealing with.

How it works

The C and the VBS versions both utilize the WMI infrastructure that’s been around since Windows 2000 to query the machine about its configured PnP devices. The WMI is a huge data structure of just about any information in Windows. Except, it seems, good information about COM ports. While there is a “Win32_SerialPort” table in WMI, that only contains information about hardware serial ports, not USB-to-serial adapters. Instead, these two tools look at the “Win32_PnPEntity” table. While this table does list USB-to-serial adapters, it does not contain a proper mapping of the adapter’s USB or PnP ID to COM port. Instead, these tools do a string search on the “Caption” field for the string “(COMn)” where “n” is a number. It’s an incredible hack but seems to work.

Other operating systems

On Mac OS X and pre-UNO Arduinos, knowing exactly which Arduino you were dealing with was easy because the FTDI driver put its serial number as part of the name of the serial port. For example, the port name created by the FTDI driver was “/dev/tty.usbserial-A6004ccf” and the serial number was “A6004ccf”.

With the UNO, the Arduino team used a reprogrammable ATmega8/16u USB chip using standard CDC USB-to-serial interface, which all OSes support natively. Unfortunately, on Mac OS X, the OS’s CDC driver creates a serial port for the UNO based on USB port location, not Arduino serial number.

Thus where you plug in the UNO into determines its serial port name. For instance, on my Mac, plugging in an UNO with serial number “64936333936351408161″ gives a serial port with the name of either “/dev/tty.usbmodemfd131” or “/dev/tty.usbmodemfa141” depending on which USB jack is used. Even if you plug in a different UNO in the same jack, you’ll get the same serial port name. This is useful for some applications, but not if you need to know exactly which UNO you’re dealing with.

For Mac OS X, I use a tool I wrote called listArduinos.pl. It tries to determine the serial port names of every Arduino Uno. It’s out looks like:

bokbok% ./listArduinos.pl         
Finding Arduino UNOs...
/dev/tty.usbmodemfd131 - 0x2341/0x0001 - 6493234373835191F1F1
/dev/tty.usbmodemfa141 - 0x2341/0x0001 - 64936333936351400000
Found 2 Arduino UNOs
bokbok% 
 Posted by at 1:55 am
Jul 252011
 

Sometimes you just want a little extra light in a room. With RGB LED tape, you can put light anywhere. But controlling its brightness and making it the color you want is a bit harder. A BlinkM MaxM can easily control LED tape, either as a stand-alone device, with an Arduino, or your computer via a LinkM. Stand-alone mode is great if you want a specific color or color pattern (the BlinkMSequencer makes this really easy) For this installation, I added a FreeM to the MaxM to let you control the light with a standard infrared TV remote control.

Accent Light with BlinkM MaxM & FreeM

Another nice thing about the LED tape is that when it’s off, you can’t see where it’s installed.
Here’s a video showing how it all works.

How to make this

The parts you’ll need are:
- BlinkM MaxM
- FreeM
- RGB LED flexible tape
- 12V DC @ 1A power supply, you probably have this laying around
- Any universal TV remote, like this one

First up is to wire up the RGB LED tape with extension wires to go from the tape to the MaxM. This is so you can hide the MaxM where ever you feel like. Cut the wires to the length you need and solder them to the LED tape and a 4-pin male header like in the photo below. Note because the LED tape switches the Red and Green lines you’ll need to switch them because MaxM’s lines go V+,R,G,B.

Accent light with BlinkM MaxM FreeM

With that done, you should be able to hook everything up and have the MaxM drive the LED tape. The LED tape takes +12VDC. The amount of current it needs depends on the length of tape your driving. Chances are you have a 12VDC @ 1000 mA wall wart power supply from an old piece of computer gear. That should work fine.

If you want your accent light to play a constant color or color pattern, you’re now done and can place the LED tape where ever you like, like on top of a window sill. Peel off the sticker backing and stick the LED tape where it should go.
Accent light with BlinkM MaxM FreeM

Adding FreeM

If you want to go the step further and add a FreeM to give your light a remote control, then follow the steps on the Using FreeM with BlinkM MaxM page.

But it’s really not much more than plugging the FreeM into the bottom of the MaxM.

Accent light with BlinkM MaxM FreeM
FreeM with MaxM

 Posted by at 10:22 pm
Jun 062011
 

SlateV had a feature on 3D printing and Makerbots entitled “How to Print a Bicycle”. Bre of course was the spokesman. About halfway through it though, my friend Jon saw my face. Whoop, I’m famous! Okay, so maybe not famous exactly. But they did feature one of my creations.

While I think it’s cool they used my laser-cut iPhone stand as the representative of what Thingiverse is about, they probably should have used something 3d printed instead of laser cut. :)

 Posted by at 6:39 pm
Jun 062011
 


Eagle is a great cross-platform, free-for-non-commercial-use tool for many of us designing own own circuit boards. But it has a pretty glaring omission: the ability to import vector artwork to use as board outlines, logos, etc. You can import bitmaps via the finicky “import_bmp.ulp” ULP add-on, but bitmaps can’t work for board outlines. So we struggle with Eagle’s rudimentary vector drawing tools or have boring rectangular or circular boards. I think I’ve found a way to robustly transfer vector artwork from Illustrator to Eagle. Below I show an “amoeba” vector shape in Illustrator being converted and used as a board outline in Eagle.

The short version of the steps are:
1. In Illustrator, add many extra anchor points
2. In Illustrator, export artwork as R13 DXF format
3. In Eagle, run the “import_dxf_polygons_v4.ulp” on exported file
4. Use resulting shape in your circuit

This is a fundamentally cross-platform technique: no special binaries needed, like some Windows-only DXF converters. Also, it should work for any other vector program that can export to a DXF format. And since the Eagle importer is public domain and freely inspectable, it can be improved over time. The importer is currently limited in many ways, but seems pretty usable, given appropriate input.

Step 1: Add Extra Anchor Points to Shape

The import command in Eagle produces only straight lines as output – any curves in your shape will be approximated by straight lines. To get around this, add extra anchor points/nodes to your shape. Add enough until you feel comfortable that the number of extra points sufficiently describes your shape. In Illustrator, this means running the “Add Anchor Points” repeatedly. You can find the command under the Object/Path menu:

Zoom into your shape when adding anchor points to see the points being added. Here’s a before-and-after of part of the amoeba shape:

If you don’t see the anchor points on your shape, be sure “Hide Edges” is turned off under the “View” menu.

Step 2: Export Shape as DXF

Illustrator supports exporting to DXF files natively. For importing into Eagle, we want the older R13 version of DXF. Here’s the settings I use in the DXF export dialog:

Be sure to place your shape as close to the lower left of the page in Illustrator, otherwise the shape will be far away from the origin in Eagle. (which is at the lower left)

Step 3: Import into Eagle

The DXF import is performed by the ULP “import_dxf_polygons_v4.ulp“. It is an updated version of the nice “import_dxf_polygons.ulp” extension written by Tim Ruetz and originally available from the Cadsoft Eagle userfiles misc area. I modified it slightly to let you select between WIRE (outline shape) and POLYGON (filled shape). Also, with WIRE, you can import unclosed objects, which you can’t do with POLYGON. Download the updated version here: “import_dxf_polygons_v4.ulp” and remember where you saved it.

In Eagle, from the Board window of your design, click the “ULP” button on the toolbar. Eagle will open up a “Run” file dialog. Navigate to where you downloaded the “import_dxf_polygons_v4.ulp” file and open it. You will be presented with a second file open dialog for “DXF file to import”. Navigate to your exported DXF file and open it. Then you’ll get the main import dialog:

From here you can rescale and mirror the shape if need be. The most important option is arguably the “Import to layer” one. For board outlines, use the settings:
- Layer: “Dimension”
- Type: WIRE
- Pen width: 0.0

When you click the “Execute” button, the shape will be placed into your board.

If you can’t see your shape, click the “Zoom Fit” button to fit all objects in the window. (the one to the right of the ULP button in the Zoom area of the toolbar) Now place your shape using a Group move.

Be careful using the POLYGON type. For shapes with interior holes, like the amoeba in this example, a POLYGON will fill in those holes, probably not what you want:

In most cases, use the WIRE type instead. It will also let you use open shapes like a spiral:

This isn’t useful for board outlines (which need to be closed), but could be useful for custom traces.

Let me know if you use this technique, or improve upon it. I think I’m going to try making this crazy little amoeba board that has an ATtiny on it, a bunch of LEDs, some I/O pins and a battery pack. :-)

(thanks to Carlyn for the inspiration and help in figuring this out, particularly in how to wrangle Illustrator)

 Posted by at 12:30 am
May 292011
 

For Maker Faire this year I made a second version of my BlinkM Cylon: BlinkM Cylon mkII. This is not a very cost-effective way of getting a Cylon effect. It however is a good way of showing how to wire up multiple BlinkMs with a long cable, using our new WireM cabling kit for BlinkM. And unlike normal Cylon circuits, this has full RGB color effects and gradual fading.

BlinkM Cylon mkII

Here’s a quick video showing it in action.

BlinkM Cylon mkII consists of:
- 13 BlinkMs (one for each of the tribes of Kobol)
- one WireM cabling kit for BlinkM, consisting of IDC connectors and ribbon cable
- an Arduino
- two 4.7k resistors
- a single push-button
- 9VDC wall wart to power it all.
- laser cut acrylic enclosure

Below are all the files needed to recreate your own BlinkM Cylon. Click any of the images for larger versions.

Continue reading »

 Posted by at 11:32 pm