Buon giorno, Arduino!

I stopped by Micro Center today, and was pleasantly surprised to see that they stock some SparkFun kits and components. Their SparkFun inventory is very Arduino-centric; they carry inventor kits, Arduino Uno boards, and Arduino Pro Mini boards (some assembly required.)

Sparkfun gear at Micro Center!

I picked up an Arduino Uno (it looked like the typical Arduino board I’ve seen online), as well as an SD card “shield” accessory for it. I wanted to see what the difference was between developing for Arduino vs. developing for a stock microcontroller.

 

The Arduino Uno -- a basic dev board

It’s insanely easy! To give an idea of just how easy take the typical blink-a-single-LED-on-and-off-once-per-second test program (the embedded designer’s equivalent of “Hello, World.”) Here’s what’s involved (for those using Windows 7):

  • Buy an Arduino Uno board (or make one, if you want — the plans are open source!)
  • Find a USB A-to-B cable, since the board doesn’t come with one.
  • Download the Arduino software (Free, as in speech) here, and unpack it somewhere. (No need to install; it’s portable software!)
  • Connect the board to your PC (it gets its power through the USB cable.) The driver installation process will fail — wait for it to finish.
  • Install the driver manually (it’s in the drivers subfolder wherever you unpacked the Arduino software.) Note which COM port number it chooses (or move it as desired).
  • Run the Arduino software.
  • Click File->Examples->Basic->Blink. This will load the “Blink” example.
  • Click the “Upload” button to upload the “sketch” (program). The yellow LED on the board will start blinking. You’re up and running!

Opening, reading, and writing files stored on a MicroSD card is nearly as easy, if you have the MicroSD card “shield” (accessory board). This assembled in about ten minutes (a question of soldering on four rows of header pins), and plugged right in to the main Arduino Uno board, piggyback-style. After loading a provided SD card example, the board was able to read and write a “test.txt” file. Modifying this to write a list of Fibonacci numbers to a file called fibonacc.txt (apparently filenames have to conform to the old 8×3 format) was very straightforward, too. Suddenly, megabytes or gigabytes of storage are available for embedded projects!

 

A MicroSD card "shield" on the Arduino Uno

So, in conclusion, working with Arduino boards is pretty cool! About the only drawback I’ve found so far is an increased appetite. For whatever reason, I tend to associate the Italian language with good food: tortellini; calzone; pizza; formaggio parmigiana, mozzarella è provolone etc. Linguistically, for me, it’s a short hop from “Arduino duemilanove” (“Arduino 2009”) to imagining all kinds of culinary delights. The Italians certainly know how to cook. Apparently, they’re also pretty good at designing dev board systems!

 

Posted in Arduino, C, Coding, Digital, Electronics, HOW-TO, Toys | Leave a comment

Epic 4G driver fix (for Windows 7 64-bit)

Use a good USB cable. Apparently, it really *is* that simple!

The original cable, shipped with the phone, works great for charging but does NOT work for data transfer — at least on every computer running Windows 7 that I’ve tried. It causes the phone to not be recognized when plugged in to the computer as a USB device. I have had good luck with the following cables — and there are probably others out there that work well, too.

Here, step by step, is how I got the driver to recognize the phone. I have tried this on my work laptop (a Dell Latitude D630), my home desktop (a homebuilt Core i7 running a Gigabyte X58 chipset motherboard), and several other PCs.

  • Find a good USB cable. Here are the results from various cables that I’ve tried:
    • Stock USB cable shipped with the phone: NO GOOD
    • (nice) Belkin USB extension cable / adapter kit: NO GOOD
    • Belkin USB-to-micro adapter, plugged directly in: UNRELIABLE
    • Radio Shack micro USB cable:EXPENSIVE BUT RELIABLE!
      (I disagree with the reviews on the Radio Shack site; this cable looks to be very solid and reliable, and works well with the Epic — but is way more expensive than it needs to be. For $25, it had better work well!)
  • Turn the phone on, and unlock it if needed, so it is at the home screen.
  • Plug the phone in to the USB cable. This should bring up a dialog box.
  • Choose “Mass Storage” when prompted
  • Click on the “Connect” button
  • Click “OK”. (Google, WHY is Froyo so much more of a pain to connect? Do we really need to click through three confirmations for this??)
  • Wait for Windows to detect the phone and load the mass-storage driver. The phone should appear as removable storage under “Computer.”
  • Remember to unmount the phone (using the taskbar icon) when done.

If these steps don’t work, here are some things to try:

  • Try another USB cable. For some reason, Windows 7 is very picky about USB cabling when connecting to an Android device like the Epic 4G. This is what finally solved the issue for me — even though I was already using the OEM cable supplied in the box with the phone.
  • Download the mass-storage drivers for the phone from Samsung’s site here. (Click on software, then on the file folder icon at the right.) Unpack it, unplug your phone from the PC, and run the installer.
  • Go into Device Manager and remove the USB driver and try the above again.

Good luck. Please feel free to post any questions, comments, or suggestions here.

 

Posted in Android, Digital, HOW-TO, System Administration, Toys | 2 Comments

Fundamentals: Ohm’s Law

Ohm’s Law is fundamental to understanding the three-way interaction between three of the most important concepts in electronics: Voltage, Current, and Resistance. Here is a quick overview of these concepts, from a DC-circuits viewpoint:

Voltage, measured in volts, is the difference in electrical potential between two points in a circuit. Voltage is always relative; giving the voltage at a certain point in the circuit is meaningless unless you specify what the reference point is. (Similarly, it would be meaningless to give your location as ten kilometers west, unless you also specified west of where.) The relativity of voltage is the reason that birds can safely land on power lines. The power line may be at hundreds or thousands of volts with respect to the ground, but if the birds don’t contact both points, the difference is meaningless.

Current, measured in amps, is the amount of electricity flowing through a certain point of the circuit (a connection, usually a wire) per second. Since current flow is specified at a certain point, it is absolute.

Resistance is a property of materials that relates current flow to the difference in voltage. The higher the resistance, the “harder” it is for current to flow through that material — and the higher the voltage will have to be in order to force a given amount of current through the material.

Intuitively, the three quantities can be thought of in terms of plumbing. This analogy works reasonably well, but don’t expect it to hold water (pardon the pun) for more complex circuits.

  • Voltage can be thought of in terms of pressure — which is always a difference between two given points.
  • Current can be thought of in terms of flow rate — how much electricity (or how much water) is going past a certain point in a given amount of time.
  • Resistance can be thought of in terms of restricting the flow (of electricity, or water): if you want to move water at a given flow rate through a narrow, high-resistance pipe, it will take more pressure than if the pipe were larger. Similarly, it takes a greater voltage to move a given current through a high-resistance circuit than a low-resistance one.

The basic equation for Ohm’s Law, E=IR, can be expressed in three basic ways:

  • E = IR :   Voltage (E, for Electromotive force) is equal to Current (I) times Resistance (R). This means that if a current of I amps is flowing through a resistance of R ohms, there will be a difference in voltage of E volts across that resistor.
  • I = E/R :   Current is equal to Voltage divided by Resistance. This means that if you apply a voltage of E volts across a resistance of R ohms, the current flow will be equal to I amps.
  • R = E/I :   Resistance is equal to Voltage divided by Current. This means that if you pass a current of I amps through a resistor, and the voltage drop is E volts across that resistor, its resistance is equal to R ohms.

By solving the right form of this equation for the variable you want to find, you can determine what resistor to use, what the voltage will be for a given current and resistance, or what the current will be for a given voltage and resistance.

Perhaps the easiest way to remember Ohm’s Law is graphically:

A memory aid for Ohm's Law. Cover up the variable to solve for.

Take the image above and cover the variable to solve for with your hand:

  • Cover E, and you get I*R: Voltage is Current times Resistance.
  • Cover I, and you get E/R: Current is Voltage over Resistance.
  • Cover R, and you get E/I: Resistance is Voltage over Current.

Resistors actually turn out to be extremely useful when making electronic circuits. Some of their many uses will be covered in another article.

Posted in Analog, EET201, Electronics, Fundamentals, Science | 1 Comment

Puzzle Box

For some reason, I’ve always really liked the idea of puzzle boxes. Being a Digital Design geek, I enjoy figuring out how systems work — and I think it’s just human nature to enjoy a good mystery.

Ever since reading about the (original) Reverse Geocache a few months ago, I had wanted to build one. Except for the box itself, I already had all of the pieces I needed (microcontroller, GPS, LCD, servo motor, battery) — and the microcontroller code to tie them together.

Traditional geocaching is a high-tech game of hide-and-seek, where you are given the latitude and longitude of a hidden container, and you use a GPS receiver to navigate there and find it. It’s a fun hobby, good exercise, and definitely recommended.

Reverse Geocaches were recently invented by Mikal Hart. (He created the original one as a wedding present for a friend — the story is a great read.) The idea is nothing short of genius — instead of finding a hidden box, give the box to the recipient, with no instructions. “Reverse Geocache” is his trademark, so I guess mine is a “GPS Puzzle Box.” (Heck, this lets me expand the idea with accelerometers, gyros, light sensors, etc later, anyway.)

The box (which is locked, with no readily apparent unlocking mechanism) has an LCD display and a single button on its top. Pressing the button, once the box is where it can get a GPS fix, results in a distance (say, in km) being displayed on the LCD. The box “knows” where it wants to go, and will only open when it is at that particular location.

My wife’s birthday was coming up, so I decided to build one for her while they were still a unique gift. (Despite the best intentions of its inventor, I’d be surprised if we didn’t see these in Wal-Mart in a few years’ time. They’re just that cool.) My wife is inquisitive and very bright — but she can also be very impatient and is not by any means a “gamer” — so I wasn’t sure how things would turn out.

Here is a relatively detailed look at what is involved in making a project like this. (If you’re thinking of getting into microcontroller electronics yourself, you definitely should — it’s a lot of fun, not too hard to learn, and doesn’t cost anywhere near as much as you might think.)

First, I prototyped the design on solderless breadboard. This is a great way to start out a new design, since it allows changes in the circuit to be quickly made. It also makes diagnosis easy, since portions of the circuit can be easily disconnected and tested independently from the rest.

Here is the schematic for the device. A reed relay was used to control power to the peripherals; in standby mode, only the PIC draws power, waiting for the button to be pressed. In this mode, the I/O ports are tristated, and the onboard oscillator is run at 31kHz instead of the usual 8MHz.

The schematic. Most components are powered off in standby mode; accessory power is controlled via the reed relay. Click for larger.

Although due to the nature of the device, large pieces of the design certainly resemble Hart’s original, I designed the box from scratch, going just from Hart’s idea of a locked box with a GPS, an LCD and a pushbutton. The original used an Arduino, but since I’m pretty much a PIC partisan, I went with a PIC18F2525-I/P microcontroller. (Dev boards like the Arduino are cool, especially if they get more people interested in prototyping — but it just seems wasteful to use a whole dev board in a project when a bare MCU will do. Each part in the design does something, this way. Besides, it’s cheaper.)

Given that the box had to not only handle operating a LCD and servo, but do GPS position (distance) calculations, I decided to program the firmware in C. (My reasoning was that rewriting my LCD and servo code in C would be easier than doing a floating-point implementation of square root in assembly. Even I’m not that fanatic — yet.)

Writing the LCD code was fairly straightforward; there are some timing constraints, but all of these are of the easily-resolved “not less than” variety. You could send commands to the LCD at a rate of one bit per century, and it should work just fine.

Once the LCD code was sorted out (this way, I can use printf() messages for debugging), I moved on to the servo code. This was trickier, but still fairly straightforward; with the use of a ‘scope, I could tell if the pulses were more or less the right length, and could then tweak them to make the particular servo used turn to the right angles. (Servos do vary in timing among individual units, but each unit seems reasonably consistent over time, fortunately.)

The most difficult part of the project was getting the GPS and serial code working. Without the LCD to help debug, this would have been impossible. To make a long story short, since I was running the PIC on its internal 8MHz clock, the timing was only guaranteed to within 2% or so, which turned out to be too imprecise to run the 4800 baud serial timing at spec. Instead, I had to tweak the per-bit and per-half-bit timings to make everything come out as it should — and the settings for this vary with the individual PIC used. I really ought to have included a TTL oscillator, but the PIC’s onboard oscillator turned out to be stable enough that I could tweak it into working. (Digital storage ‘scopes which can grab a frame of NMEA output and let you zoom in, checking timings and output sequences, are a wonderful invention, by the way.)

Now I had to find a suitable box. A quick eBay search turned up a broken music box that looked perfect.

The box, prior to being repurposed

Once the box arrived, I looked it over; the mainspring in the music clockwork had broken, so I didn’t feel as bad about repurposing it. (I kept the music clockwork in my parts bin anyway — I could always drive it with a motor or something.)

Next, I had to decide on a layout for the components, which meant deciding what batteries to use. I measured the current for the whole setup, and came up with about 80mA running, and very little current when dormant. At 850mAh capacity, four rechargeable NiMH AAA batteries would provide about ten hours’ runtime, which would be plenty. With the GPS, LCD, and button in the recessed lid, I decided to put the batteries and circuit there, too (to avoid running a dozen or so wires to the base.) This left only a few wires to run to the base, for the servo and debug lines.

The batteries, LCD, and opening for the GPS. (The circuit board clips on to the bottom of the LCD.)

Once the LCD and GPS openings were in place, I went about designing a locking mechanism. The local Target had a picture-hanging kit, from which I was able to bend a few metal hooks into latches. Mikal Hart’s design uses a cut chopstick as a latching mechanism, but I figured I could get away with using a servo horn directly, if it was supported solidly enough.

The next step was to move the circuit from the breadboard to a circuit board. I cut a Radio Shack circuit board to fit, then figured out where the LCD header needed to go. From there, I laid out the sockets for the PIC microcontroller, as well as the relay and other various parts.

The circuit board. Not very pretty, but functional.

Once the circuit board was built, I tested it, keeping the lid open. Everything seemed to work correctly, but to help Murphy’s-law-proof the device, I created two backdoors. First, I brought out all five debugging pins (Vcc, Ground, Vpp, PGD, and PGC) to a wire-wrap header tucked discreetly underneath the box. Next, I put a check in the startup code to unlock the box if the button were held down at power-up.

With these precautions in place, I finally shut the box for full-scale field testing. Apart from some “urban canyon” GPS problems, it worked well on a quick walk to a nearby restaurant; the distances decreased as I walked towards the designated coordinates, and the box opened once I got there.

Next, I decided to test the practicality of using a map to determine the goal location. I picked a goal location near where I lived, then drove home from work, checking the location on occasion when stopped at traffic lights.

 

The GPS Puzzle Box in action!

The box dutifully opened once I got home — but how accurately would the various distances recorded allow me to determine the goal, if I didn’t know it in advance? I entered the corresponding circles into MapPoint, and got the result I was looking for — intersecting arcs well within half a block of the goal. The box really worked — and just in time: my wife’s birthday was that weekend. I prototyped a hollow block of ABS plastic with an inscription (it helps keep the wires under control), and covered up the electronics with a piece of green construction paper.

Inside the finished puzzle box. The latches are bent picture hangers, covered with shrink wrap. The servo horn can be seen at the bottom (it's in the closed position here "locking" the box open.)

Finally, it was showtime. I reset the box, loaded it with some nice opal-and-silver jewelry (I’m not totally clueless!), and set the destination for the parking lot of Longwood Gardens.

How did my wife react? Well, the “practical” / “impatient” side almost won out. Once she determined that the box wasn’t going to open right then and there, she discussed encouraging it to open using a screwdriver and/or a hammer. (Like I said, she’s very practical that way.) I convinced her to give solving the puzzle a try, though, and we set off (laptop and PICKIT2 debugger along just in case).

“Which way should we go,” she asked. I shrugged and said the box just seemed to give a distance, not a direction. She decided that it might be somewhere west (statistically, a good guess; we go that way a lot), so we set off down I-76. The distances started decreasing, so we were on the right track.

Her next question was “How do we know which way to turn?” I tried turning things around on her. With several degrees including an MD, she’s easily smart enough to know how to solve this puzzle, but needed to get past her impatience. “Where are all the places 48.2km from here,” I asked. “Well, they’re in a circle.” She had the right idea, but was still fiddling with the hinges and talking about cracking the box open.

We stopped for breakfast and I got out my laptop to show her the first two distance circle intersections on the map. One was to the northwest; the other to the southwest. She picked the first one (also statistically a good guess, which happened to be wrong this time), and asked how we would know if that was it. I showed her how a third circle would answer the question — or if the distance started increasing once we turned north, that would also mean we had gone the wrong way.

We turned north, and soon the distances did start going up. At the next exit, we stopped for fuel and turned around, following 476 south. Eventually, the distances started decreasing slowly; the intersection had indicated the goal was west of 476, so we turned west. A short while later, she guessed the destination (there were signs along the way.)

Once we got to the parking lot, the box opened. Seeing the jewelry definitely improved her opinion of the whole adventure — and we had a pleasant day walking around Longwood, as well.

 

Posted in C, Coding, Digital, Electronics, Games, GPS, HOW-TO, Level 4, Math, PIC Microcontrollers, Projects, Toys | Tagged , , , , | 3 Comments