Category Archives: Arduino

Guide to Burning Arduino Bootloader using ZeptoProg II and AVR Studio

If you’re interested in working with Arduino microcontrollers, eventually you become hip to the idea that you don’t need to buy expensive Arduino UNOs for your projects. It’s relatively simple to wire up your own minimal Arduino, saving 80% under the cost of an UNO or Duemilanove. I develop on a Duemilanove, and when I need to commit the work to the final project, I can pop out the ATmega328 chip that’s in the Arduino and plug in a fresh ATmega328 to program it. Actually though, you can’t just use any ATmega328 chip — it needs to have a “bootloader” program flashed onto it before it can work with the Arduino system. You can buy ATmega328’s with the Arduino bootloader, but these go for around $4.00 each. You can buy the same chips without the bootloader for about a dollar less, or more than that if you buy in bulk.

To install the bootloader software, you need a device that hooks into the ISP plug on your UNO or Duemilanove. I had an AVRISP mkII that I bought a few years ago, but it stopped working so I needed a replacement. Although I could use my BusPirate as an AVR Programmer, the documentation for that uses the command-line AVRDUDE to communicate with the chip. I wanted to use a toolchain I was more familiar with, the free AVR Studio, version 4.

I bought the replacement a couple of months ago on ebay: ZeptoProg II is an inexpensive AVR programmer with USB interface, and works with AVR Studio. It also sports a 4-line logic analyser and frequency output and measurement, and a few other widgets that I probably won’t ever use.

Most of the time involved in setting things up relates to installing AVR Studio. The version I use is 4.18, so you need to download the core version and then upgrade it incrementally with several service packs. Although I’ve been a Mac person since 1984, I develop on a Win7 netbook partly because AVR Studio on Mac OS X has not been kind to me.

The Guide

I’ve prepared a PDF Guide to Burning Arduino Bootloader using ZeptoProgII and AVR Studio.

Once you’ve mastered this, you can go on to bypass the Arduino altogether: for example, if you are running out of space on your ATmega chip, you can erase the bootloader — gaining about 2k — and use the programmer to flash the HEX file that you created when you compiled the Arduino program onto the chip. An added bonus to removing the bootloader is that on power-up or reset, your project starts faster because it doesn’t have to run the bootloader code.

Note to self: Use Tellymate when Debugging Arduino!

Tellymate is an Arduino shield that outputs all Serial.print() commands to a television (NTSC or PAL composite video). I’ve had one kicking around for a couple of years now. I bought it thinking that I’d like to build a “new” kind of interface/display. I even bought an inexpensive portable TV/DVD player for the purpose. That project is still on my ‘to-do’ list, but I had another problem—that Tellymate solved simply and elegantly.

I’ve been picking at the Media Circus project for about year, making improvements, letting it run to see if any bugs cropped up. Media Circus is a scrolling marquee that displays amusing headline news mashups. It receives updates from Twitter once a day, early in the morning. To do this, it needs to self-reset. This is not simply a matter of running a wire from a digital output pin to the Reset pin on the Arduino. A dependable self-reset requires a delay and ‘safety switch’ circuit.

The Problem…

Because the unit resets only once a day, I needed several days to test it. It seemed to work as designed with more frequent resets using a test program, but when I incorporated my reset code into the Media Circus sketch and let it run, I discovered that it would reset the first time, but never again. I needed to know what was going on in the program. Using the Arduino IDE, if you want Arduino to tell you what state it’s in at various points, you drop in Serial.print() statements and then open Arduino’s Serial Monitor. For short runs, this works, but Serial Monitor sometimes crashes if you give it lots of data to display over a long period of time. Additionally, when the system resets, it closes the Serial Monitor.

My next thought was to bypass Serial Monitor and attach a small LCD screen to show the various values. I tried the Arduino built-in LiquidCrystal library, but it didn’t work for me with the particular arrangement of hardware and software—I wasn’t able to figure out precisely why. Next, I tried a different library, arduinoshiftreglcd, which worked with the MEGA and the Ethernet Shield, but failed when in the presence of the Ethernet or EthernetUDP libraries. I was about to look at the New LiquidCrystal library, when I remembered the Tellymate.

The Solution…

Tellymate doesn’t require any additional code: if you can print to the Serial Monitor, it will show up on the TV. But there are two very simple functions that Batsocks provides that were useful to me: screen_clear and cursor_move. My TV display shows the number of seconds since midnight, the rough geographic location (Beijing/Moscow/London/Halifax/Vancouver) and Daylight Savings Time/Standard Time, and the epoch time at reset. This has finally allowed me to find—and solve—the problem!

And now, for today’s headline (June 8, 2012):

Small plane crashes on who’s giving it all away

You can look at the other headlines that are running on Media Circus, or follow General Eccentric.

“Shy Light” uses Dual ATMEGA 168’s and X10 Control

Here’s the elevator pitch:

An electric lamp doesn’t like anyone to see it when it’s turned on. It hides under a bushel basket and when it’s exposed, it fades out. When the basket is replaced, it lights back up.

This is another project in the series “The Technology of Good Intentions”: these works are intended to focus the viewer on the role of failure in design.

X-10 Modules: Lamp Control (left) and Power Line Interface (right)

The entire assemblage uses two X-10 modules: an PL513 Power Line Interface (PDF) and an LM465-equivalent lamp module. The PL513 provides an interface from the microcontroller to the X-10 protocol, and the LM465 accepts commands (such as dim light and brighten light) to control the light.

Why X-10?

A primary project requirement was the ability to have the microcontroller dim and brighten the lamp on command. I investigated three ways of doing this:

  1. Making a light dimmer using a triac and opto-isolation. This is easier said than done. I found that there are complications, such as buzzing, radio-frequency interference, and of course, safety, since I was playing with 120v AC current. I experimented with triac control using low-voltage AC, and decided it wasn’t worth the effort in trying to overcome the drawbacks just to save a few dollars.
  2. Buying a hardware-store light dimmer switch and using an actuator—such as a servo or stepper motor—to engage the dimming and brightening. At an early stage of the design process, I had decided that the project was much more complicated than I first thought, and I wanted to avoid making it look like a Rube Goldberg device.
  3. Using the X-10 system, which transmits commands through household AC power. The Power Line Interface module uses 4 wires to connect to a 5-volt microcontroller, via a standard RJ11 telephone plug.

I had used the X-10 as an automatic light dimmer years ago, and though I found it sometimes quirky, it worked 95% of the time and was super-safe. Plus, no moving parts were involved. I chose door number three.

How’s It Work?

  1. The overturned basket is closed. The user activates the gadget using a foot-switch.
  2. The switch sends an electrical pulse to a box enclosing two interlinked ATMEGA168 microcontrollers. In this case, the “Master” controller receives the foot-switch signal.
  3. The Master ATMEGA signals the “Slave” controller to direct a servo motor to open the basket. The sole purpose of the Slave controller is to generate the pulse-width modulation commands to the servo motor.
  4. The Master controller also sends a “dim lamp” message to the X-10 power line interface, which is plugged into a 120-volt power wall socket. The interface translates the 5-volt pulse generated by the Master controller into an encoded series of pulses on a 120 kHz carrier wave, on top of the 60 Hz AC power signal.
  5. The commands generated by the X-10 power line interface are received by the X-10 lamp module.
  6. The lamp under the basket dims as the basket opens.

Why TWO Microcontrollers?

The Dual-Microcontroller Circuit: Master (right) Slave (left)

The Master Control microcontroller handles the foot-switch input, orchestrates the timing of the opening/closing of the basket (using commands to the Slave Control unit) and the lamp dimming. Initially, I had no idea that the project would require two; there are enough analog and digital pins on one microcontroller to do it all, but—and I’m not absolutely certain about this because I didn’t keep detailed notes about this—I think that I was forced into a dual processor arrangement because of the complexity of the program timing. As I designed the circuit, I came to the realization that the timing required by the various elements was going to be difficult for me to accomplish as an amateur C-coder if I tried to stuff it all into one controller. So I don’t want to say that “it can’t be done with one chip”. I just decided that it was simpler for me to compartmentalize the various tasks with hardware. So I added another ATMEGA168, at an added cost of about $5.00.

Physical Considerations

The Lamp

6W LED bulb

Before starting the design, I had ordered an LED light bulb from Dealextreme: this 128-LED bulb uses an E27 socket (the familiar screw-in type found in most incandescent and compact fluorescent bulbs) and draws 6 watts. It has a diffusion enclosure that makes it look somewhat like a typical light bulb. This LED bulb was preferable because I wanted the light to stay on under the basket when it was closed. It’s lower current draw resulted in less heating in a small enclosed space. There was only one problem: when combined with the X-10 dimmer, I discovered that this bulb wasn’t fully ‘dimmable’!

After I recovered from this mild disappointment, I elected to use a clear “BT” shaped halogen lamp. Halogens put out a lot of heat, so I had to add a routine to dim the lamp after a short delay whenever the basket covered the lamp. Additionally, although this dimming routine never failed while it was on the testbed, I was concerned that this X-10 ‘dim’ command might, at times, fail to dim the lamp, heating the inside of the basket, causing a fire hazard. So I added a second level of safety: a CDS cell monitors the light level when the basket is fully closed. After the basket closes and the unit goes into idle, the state of the light sensor is polled: if the light level is above a certain level, the X-10 issues an immediate “light off” command. This certainly doesn’t cover all contingencies—especially if the X-10 suddenly fails while the lamp is on—but I’m less concerned about the fire hazard issue now.

Construction Notes
The servo and linkages are bolted together using Meccano hardware. The main hinge that connects the base of the basket with the base is also Meccano: to secure the hinge, I used a dab of thread locker on the bolts. The servo is a heavy-duty unit (SG-5010) that has metal gears and a stall torque of 3kg. As I found in design and testing, this amount of torque is enough to wrap the 17 gauge steel linkage wire around the servo shaft!

The Next Iteration…

The next version, if there is one, will dispense with the X-10 control and use high-powered DC LED illumination. Rather than a light in the centre of the base, I would consider attaching the lighting to the inside of the basket.

View the Shy Light Schematic (PDF), and here’s the Arduino code for the Master and Slave microcontrollers (Github).

Shy Light Mechanical Drawing (PDF)