Monday, November 14, 2011

"Sempliduino", My jaunt into Arduinoland.

I've always thought the Arduino was a really cool idea, it lowers the barrier to entry for people without an engineering and/or software background to get their nerd on. Code jockey that I am, I always thought the Ardiuno seemed a little to "beginner" for me. On the other hand, I'm always trying to think of ways to help get my kids interested coding and electronics (eg, see Scratch, more about that later).

Hardware wise, the Arduino at its core is not much more than an ATmega microcontroller, with some convenience hardware(USB serial, voltage regulator, reset button, etc), and a configuration of headers for Arduino add on boards called "shields".

As usual, I dont know exactly what I'm going to get my kids to do with them yet, but I decided that I would order some Mega 28s and some 16 mhz crystals and slap together my own 'duinos on the cheap. Since there are already Adruino minis, and nanos, picos,and femtos, I decided call mine "Sempliduino" which comes from "semplice" and  "Adrduino", the former being Italian for "simple". Yeah, I'm sure that makes no sense to an Italian speaker, but I Googled it and it wasn't taken, so it sticks.

Sempliduino 1.0

The real value of Arduino comes in the fantastic software and libraries and the amazing projects that have been inspired by it. A bootloader handles uploading new code, and the IDE and the Arduino "language" wrap C/C++ and hides some of the mundane details of setting up a C projects and initializing a chip. One can just rapidly jump into a the fun.

There are some really cool community projects that add additional cool factor. My kids love Scratch, and there is S4A (Scratch for Arduino) that is a modified version of Scratch that has additional logic blocks that talk to a special "Sketch"(user program) running on the chip. The blocks let you read sensors, set ditigal/"analog(PWM'ed) values. The Adruino acts as a "slave" device, carrying out commands issued by Scratch. Another really neat project that uses visual programming is Minibloq. Minbloq is pretty new, but it is very exciting, it converts a graphical program you design into a sketch that is then compiled and uploaded to the Adruino, so in concept it's  like a scratch that can run independently on the chip.

I had this blog in mind when working on this, so I tried to keep notes. Here we go.

I noticed that Adafruit was selling stickers to put on your AVR chip for Arduino-in-breadboard setups. Since I had already blown my hobby allowance for the month, and since I'm a DIY kind of guy, I made my own. I used LibreCad to create an AVR(I actually took a few measurements of the DIP 28 chip before I realized that it was all in  the datasheet, d'oh!). Anyways, here is the resulting cad file, not as pretty as the Adafruit one, but it gets the job done. I printed to regular paper and then glued it to the chip.

The next step was getting the Arduino bootloader onto the chip. Once you have a bootloader, you can upload new code with only a serial connection to the AVR. I have an AVR Dragon, which can do just about anything you want with 8 bit AVR's, isp, JTAG, debugWire, High Voltage programming, you name it. I also have a "Dragon Rider" which makes flashing a chip really easy.

Dragon Rider. The Dragon device itself sits underneath.
I assembled this thing a couple years ago,
it's a great way to  bone up your soldering skills!

I loaded up the flash/programming utility in AVRStudio and selected the "ATmegaBOOT_168_atmega328.hex" file that came with the Arduino software. The version I used was 023, the latest as of this writing. The one thing I had a little trouble figuring out was the appropriate fuse settings. You can find a lot of different fuse settings in the make file for various Arduino variants, and I couln't find documentation describing what Arduino software and libraries expect from AVR fuses. In the end I found that these fuse settings worked:

“Extended”: 0xFD
“High Fuses”: 0xDA
“Low Fuses”: 0xFF

I also set the lock bits to 0xCF, which I think is the way that the arduino bootloader get protected from accidently overwriting itself.

I then used a cheapo usb serial board to connect rx and tx on the arduino a loaded up the Arduino IDE. Success! The "Blink" sketch ran right off the bat!

My next step was to get it working on my kid's laptop with Ubuntu on it:

The astute may notices I'm missing a lot of things you should have, like crystal and decoupling caps.
For my simple setup it seems to work without them. As my circuit gets more complex,
the lack of proper conditioning would start to cause bizarre behavior.

Then I ran into a problem, the IDE would not talk to the board. It was running the previous sketch I had uploaded almost immediately. Older Arduinos required you to press the reset, and then the bootloader waited 10 seconds before running the resident sketch. Newer Arduinos can reset the chip themselves, via the DTR signal on a the serial connection. My cheapo serial board lacks a DTR output, so I had to rely on manual resetting. The problem was that the firmware that came with the binary lacked the 10 second delay, so I had to be much more precise with the reset and hitting upload at just the right time. No fun.

So I edited the make file (in 'arduino-0023\hardware\arduino\bootloaders\atmega', copied the atmega328 make target, and changed the "MAX_TIME_COUNT" macro value to "F_CPU>>1", and "NUM_LED_FLASHES" to 3. I rebuilt the firmware (make, lives in arduino-0023\hardware\tools\avr\utils\bin). Worked flawlessly, and Arduino pin13 flashes rapidly 3 times on reset.

In case you want your own Sempliduino, here is that slightly modified firmware.

After that I was able to upload sketches at will. S4A also worked great. Currently, minibloqs only runs under Wine on Linux, so as a proof of concept I tested it on a Windows machine. works.

Now maybe I'll let my kids take a turn.


No comments:

Post a Comment

I welcome you're thoughts. Keep it classy, think of the children.