I was working on a program and needed the user to be able to select from over 12 choices of pre-recorded patters.

I chose to use a absolute position rotary encoder that had 16 detents.

The particular one I chose was the Alps EC18AGB20401 Unfortunately the code that it returned was not quite binary so I had to use a look up table to go from 0-15.

int rotLUT[]={0,1,15,14,3,2,4,5,11,10,12,13,8,9,7,6};

Based on this table:

# #0 pin pin pin pin decodes as
4 3 2 1 dec
4 3 2 1 dec
1 0 0 0 0 0 0
2 1 0 0 0 1 1 clockwise
3 2 0 1 0 1 5
4 3 0 1 0 0 4
5 4 0 1 1 0 6
6 5 0 1 1 1 7
7 6 1 1 1 1 15
8 7 1 1 1 0 14
9 8 1 1 0 0 12
10 9 1 1 0 1 13
11 10 1 0 0 1 9
12 11 1 0 0 0 8
13 12 1 0 1 0 10
14 13 1 0 1 1 11
15 14 0 0 1 1 3
16 15 0 0 1 0 2



I am currently working on a project involving QR codes and a Raspberry Pi.

I have QR codes printed on business card size media. When the card is far enough away for the camera to see the whole code it was out of focus.

Luckily I had some cheap ($1.50) lenses that happen to have a 4.5″ focal length laying around from a previous project.  These worked perfectly for me as the distance from the card to the lens was ~4″. The nice thing about this mod is that it does not require modification of the original raspberry pi camera lens.

Before and After:



These worked perfectly. I am now able to reliably read the QR code using the zbarimg program from the repository.

sudo apt-get install zbar-tools

With this step out of the way I can get back to my project.

Now that we live in he age of the Internet-of-Things (IOT), I find that these ‘things’ have to communicate to a normal web server at some point.

The problem of course is how best to send data to the server and make sure what was sent is what was received. My preferred method is to use ‘Method GET’ which means that the data is encoded in the URL. It is easy to implement on just about any platform from Arduino UNOs to Arm Cortex processors. Every platform that supports 802.11 or 802.3 will have an example of using ‘Method GET’ to fetch a URL. Which means that the opportunity to send data is as easy as getting a URL.

The next problem is how to send binary data. Since the URL schema has special characters like, ‘”/?& that have meaning you cant just send binary data in the url. To be completely safe in a 7bit ASCII world, one needs to encode that data into some format.

For example if you wanted to send a number of 0x31EAA5 via a URL you could do easily enough. That would be converting the number to a integer represented by ASCII characters. This method becomes cumbersome when there are multiple values to send: ?q1=555&q2=444&q3=333&q2=222

My preferred method of late is to send the data as a byte stream. Doing this requires some sort of ASCII armor, such as BASE64 or UUENCODING. Originally I started using base64 which creates GET URLs like: ?q=SSBhbSBEQVRBCg== which decoded on the receiving server would be ‘I am DATA’. But for whatever reason the encoding uses special characters for some data. Namely / and + characters. So a few variants of base64 have sprung up to change these characters to more friendly to URL or other standards.

Not wanting to deal with the hassles of base64. I decided to give base 16 a try as the encoding. This has worked out rather well. Base16 encoding is easy to implement in any language. And many have built in encoders/decoders.

So in the above example of the data being ‘I am DATA’ it would render as


It uses exactly twice as many bytes as the original data, but is perfectly safe to transport binary data. So the sacrifice is size vs safety and ease of implementation. I will almost always chose ease of implementation over any other factor.

On to the code:
Full Examples

Arduino code snippet

char base16[16]={ '0','1','2','3','4', '5','6','7','8', '9','A','B','C', 'D','E','F' };
// this expects byte theData[len] and byte theEncodedData[len*2]
void base16encode()
  char b1,b2; // bytes 1 and 2 of the encoded data
  byte ev; // the value to encode
  for (int lop=0; lop<theDataLength; lop++)
    b1=base16[( (ev>>4) & 0x0F)];
    b2=base16[( (ev) & 0x0F)];

PHP snippet

function base16decode($data)
  for ($lop=0; $lop<strlen($data); $lop+=2)
    //echo "ob[$oneByte]tb[$twoByte] v1[$v1]v2[$v2] tv[$theValue] av[$av]| \n";
  return $retv;

These code examples are written with the goal of being easy to read and understand rather than being examples of a good coding style.

Over the last few days I have been Tweeting photos with location but I am unhappy with how the maps are displayed.  So I made a bookmarklet to extract the map links.

Grab the GPSCoords bookmarklet by following the link then dragging and dropping it from there.

This is the first project that I am putting up using the Teensy 2.0 from PJRC

I am quickly becoming a super fan of the Teensy.  What used to take me days of coding and many hours of reading I can now do in minutes thanks to the work that PJRC has done with Teensydunio.

This is only the first of many projects that I will share based on the Teensy.

USB MIDI Servo Control In 23 Lines Of Code

This is an update to the Das Blinken Board from N&V June 2009. Der MagnetFelder uses a hall effect sensor to determine the relative strength and polar orientation of magnets.

Get the whole story in Nuts & Volts November 2009 and check out the website for the code.

Since most of the projects I work on with Atmel are USB based the first thing I do is load AVRUSBBoot on a chip. Once the bootloader is on the chip you can re-program it by setting a jumper on your circuit and using the avrusbboot command line interface. (Example: avrusbboot ./filename.hex)

But how to get the bootloader on the chip in the first place? Easy. Buy a AVRISPMkII from an Atmel distributor. It comes with the necessary hardware and software to program chips. Then the question becomes: If I have a programmer why would I need a bootloader? I guess the answer is: convenience. Once a circuit is built with usb support, which is only 4 wires, it is easy enough to put a jumper on and use the bootloader. When I order a new batch of chips I could put them all on the programmer and load the bootloader all at once.

The downside to the usb bootloader is that it requires a 12Mhz crystal to be attached to the chip. (I primarily use Atmega8 chips.)

Although I have never used one there is a possibility of using a serial port or parallel port programmer built out of very few, cheap components.

There is a good overview of all the methods of programming an avr at

One of the greatest beginning tutorials I have found for Atmel programming is the I Make Projects article called “A Quickstart Tutorial for ATMEL AVR Microcontrollers.” If you are just starting buy a couple Attiny45 chips and follow that.

Warning: To get a bootloader to work with usb you have to change fuse settings. It is easy to “brick” a chip if you give it the wrong fuse settings. Especially changing the clock and re-assigning the reset line. (Reset is needed to activate the In Circuit Programming.) Read, re-read, and triple read before reprogramming fuses. Look at RSTDISBL, SPIEN, and CKSEL.

When I prep an Atmega8 for use with the avrusbboot program I use Hight fuse @ C0 Low Fuse @ 9F and locks set to EF. As soon as you hit the button to program these fuses it will absoulutely need that 12Mhz crystal on pins 9 and 10 with the 27pf capacitors. Or you wont be able to talk to the chip again.

An online fuse calculator for avr chips.

Now that I have my quick protoboard together it allows me to take other people’s projects and put them together really quickly. Today I built the USB-LED-Fader. Since the hard parts were already done it took about 5 minutes to add the 4 resistors and LEDs to a breadboard. Then another 5 minutes to compile and upload the firmware using the bootloader.

Then I spent about an hour playing with the different LED patterns. This is a really fun project. I can see tons of potential for different status lights. Tie a cpu meter to one of the lights… the faster it flashes the more cpu is being used. One LED for email status. One for network traffic. And finally one for server status.

(The photo from the atmega8 development board shows this project on the breadboard.)

Last night I stared building a atmega8 development board. Tonight I finished it.

I got tired of building the same circuit over and over; so I made a generic board that I could slap onto a breadboard and quickly try out projects. It also allows me to rapidly prototype something new.

I based the board on AVRUSBBoot. The only thing I changed was the programming pin. I moved it to PD7. This allowed me to put PB* and PC* on the plug side of the board.

On the right hand side of the board I brought all the PortC and PortB pins as well as + and GND to a row of header pins.

The important thing about this board to me is that it is USB powered and based. I don’t need to hook up a programmer, I just use the bootloader and usb to update the software on the chip.

My header looks like this:

  1. PC5
  2. PC4
  3. PC3
  4. PC2
  5. PC1
  6. PC0
  7. GND
  8. PB0
  9. VCC
  10. PB5
  11. PB4
  12. PB3
  13. PB2
  14. PB1

Last night I showed my “Hallucination Generation” device at the The Robot Group meeting and many people asked me how the device could cause visual hallucinations. I have a theory about that.

First in normal people speak. Sensory overload in your eyes makes you see stuff.

Now in geek speak. The phenomenon of pareidolia, which is a type of apophenia, is the brain’s (dis)ability to see patterns where none exist. When the UBHG device flashes lights in the eyes it causes retinal fatigue. As the duration of exposure and frequency changes, the amount of noise coming from the cones and rods increases. The brain does its best to interpret the noise. Depending on the viewer’s natural tendency of pareidolia, what the brain interprets can range from just a flashing light to religious icons. Most see patterns like checkerboards, spirals, and triangles.

The next logical question is: Why use Alpha, Beta, Delta, and Theta waves as the basis for the frequency of flashing? I guess the best answer is: Why not? They seem to work fairly well. I do want to make a future version where each of the four frequencies can be tuned to what works best for my body’s specific nature.

Next Page »