Monday, November 16, 2009

Pulses on pushbutton

A friend was asking after a circuit for making pulses from a pushbutton on push and release. Two ways immediately suggest themselves- using a PIC and using a differentiator.

The PIC is easy for me, at least- I have a small stock of 12F683s which are 8-pin devices. Add a bypass cap and you're in business- you could even reprogram it to change the delay, debounce the input, etc. Of course, not everyone has a PIC programmer, and I feel like it's a pretty trivial app for a PIC, so I'll not talk much more about it.

The differentiator circuit is a little more involved.
Apologies for the crappiness of the image- LTSpice doesn't generate great output images, for one, and this was part of a larger image (it started NPN based, and generated negative going pulses; that's where Q3, Q4, etc etc went).

V3 is the switch- in the application, it should be a 100-ohm pull-up to 5V and a switch that yanks C2 to ground when it's closed.

R4 pulls up the base of Q3 and Q4, keeping them turned off and providing a 0V output normally. In steady-state non-pushed, C2 will have about 0V across it. When the switch closes, the base of Q3 and the emitter of Q4 get yanked down (since the voltage across the cap can't change instantly). Q3 turns on, and the load sees a pulse that goes up to 5V. C2 charges to 5V (since one end is at 0V and the other is tied to 5V through R4), and as it charges, Q3 slowly turns off. Q4 has, as yet, never turned on, since the base has never been at a lower potential than the emitter. When you release the button, the (not shown in the drawing) 100-ohm resistor which is a pullup to 5V yanks the bottom of the capacitor up by 5V. Since the cap is already charged to 5V, the voltage across the capacitor is now 10V, which shoves the base of Q3 above the 5V rail by about 5V (turning it off even harder, for what it's worth)(which is not much) and, here's the magic part, shoves the emitter of Q4 5V above the 5V rail. Now, there's a 5V difference between the base and emitter, the transistor is biased, and it turns on, which again yanks the load up towards 5V. Again, C2 will discharge, and Q4 turns off as it does. The diode provides a clamp to the 5V rail, limiting the voltage on the output. It tends to pop up to 8V or so, although only briefly, so it may not be an issue.

This circuit is a real hack job- I know I could kill that >5V excursion if I spend a little more time on it. It's simple, though, and I like it.

The LTSpice file is available here.

EDIT: Adding a 270-ohm resistor between the base of Q3 and the emitter of Q4 keeps the excursion to a very reasonable .2V above 5V.

Tuesday, November 10, 2009


For those in the electronics world, SPICE simulation can be a great way to answer the "will my circuit work as advertised" question without breadboarding. It's a great sanity check- if it don't work in SPICE, it won't* work on a breadboard.

LTSpice is Linear Technologies' own incarnation of SPICE. It's a nice little product, and comes with models for many of Linear's products (power supplies, op-amps, etc) along with many supporting components from other manufacturers (diodes, transistors, capacitors, inductors, etc) and generic parts (555s, LEDs, etc). Obviously, they provide it expecting people to download it, sim up a solution involving Linear parts and then sell eleventy-billion a year, thus providing a nice income stream for Linear (and it works- LTSpice makes it much more likely that I'll reach for a Linear part for power supply design than another manufacturer's parts).

But for the tinkerer, it means that there's a free, very powerful SPICE tool out there, which is well supported. Give it a look!

*for some values of "won't". For instance, there are times you might want to put a transistor in "backwards", or use just the collector-base junction as a noise source. In that case, the model probably won't hold up- but if you're doing things like that, chances are you don't mind not having a model for it anyway.

Saturday, November 7, 2009

Breadboarding the Arduino- part 1

A few weeks ago, I purchased 10 Atmega328P's from Mouser, naively thinking it would be easy enough to get them working on a breadboard as an Arduino sans the fancy (expensive) board. I was planning to use an FTDI USB to TTL serial data cable to bootload (I bought a 5V one, then realized that the two 3.3V ones I already had would work fine- lesson one of this saga). I also bought a some 16MHz crystals and the appropriate capacitors.

At this point, I want to extend my thanks to whomever it was that decided a 6-pin AVRISP compatible ICSP header would be a value-add to the Arduino. I know that 95% of the Arduino users out there will NEVER use it, but I am very grateful.

So, first hurdle- I figure I'll take one of the 328s and upgrade my Arduino (it was one of the, what, six? Duemilanove's that shipped with a 168 from the factory). Supposedly, the Arduino IDE has the capability of loading the bootloader onto a blank chip through an AVRISP, but I'll be buggered if I could get it to work. After a little bit of monkeying around with it, I gave up, admitted defeat and downloaded AVRStudio. I highly recommend this route if the IDE bootloader program (using AVRDUDE) doesn't work right away- the pain level of a ~100MB download is much lower than trying to debug AVRDUDE- at least, on my internet connection.

At any rate, I had no Atmega328 Arduino to snoop, and nobody, nowhere, had clearly and definitively stated what the configuration bits are supposed to be. So, for posterity, I do that here, now (note that they've changed since I first posted this):
Extended- 0xFD
High- 0xDA
Low- 0xC6

EDIT: Note that these settings ARE NOT the settings suggested in the "boards.txt" file in the hardware\arduino directory. My suggested low bits- 0xC6 instead of 0xFF- will allow more types of crystals or resonators to work, at the expense of a slightly increased power draw. If you're THAT WORRIED about power draw, you're probably already digging deep enough that you've figured all this out, already.

I made a few educated guesses and got values which more or less seemed to work, and then I grabbed the bootloader binary from its home deep in the catacombs of the Arduino application folder. Success! I could upload and run sketches with my new 328 enhanced Duemilanove. Flushed with my success, I immediately launched into the next portion of the project: getting a breadboarded 328 loading sketches through the FTDI cable and the IDE.

At this point, and exactly how I managed to do this remains unclear to me, I proceeded to brick all three of my remaining 328s (I know, I bought 10, but I sold 6 to friends). I know I somehow wrote inappropriate fuse bits to the config registers, but I don't quite know how I did that, let alone how I managed to do it to all three. I can only assume that, briefly, I was MONUMENTALLY stupid.

So, of course, I began a bit of yak shaving. First, I tried my JTAG-ICE mkII, but that only works on JTAG capable parts (which the 328 is not). I THOUGHT it might have HV capability (Microchip's equivalent, the ICD2, does), but alas, no. Finally, thanks to Jeff Keyzer's excellent tutorial on making an Arduino into a high-voltage programmer (only high-voltage programming can resurrect an Arduino under some circumstances; for instance, if the reset pin is disabled), I was able to bring the chips back from the purgatory I thought I'd condemned them to forever.

So, here I sit, at 2:30 in the morning, weeks after starting all of this, about to embark on the next step: getting the parts to work on a breadboard, through the IDE, with minimal "tricks" involved. I will mention that I've been at this point before- before I bricked the chips, I had put them in a bread board and started tinkering, but I just couldn't get the thing to work. We'll see if this turns out any better- with luck, the next post will be a post-mortem on why it failed before and what I did right this time.