Panel Volt/Ammeter

Two inexpensive panel meters fit quite nicely in a Hammond stomp box case. Perfect for a voltage and current meter.

From Panel Volt/Ammeter

Continue Reading »

Simple, reusable ZIF “socket” for SMD parts.

I show how to use a pair of magnets to temporarily mount a surface mount chip to a breakout board.

Surface mount chips are becoming more common nowadays but they are harder to use for prototyping not being breadboard friendly. Using a simple SMD breakout board (like one of these) and some powerful magnets (I use these ones) you can conveniently and easily temporarily attach SMD chips to the breakout board.



You simply use a magnet to hold the chip onto the board.

Continue Reading »

Update on labeling your chips

A while ago I started  labeling my chips when breadboarding. Having a bin of properly labeled common jelly bean chips is just an extremely useful thing to have in arms reach when prototyping or experimenting with circuits. One thing that is annoying is having the decoupling capacitors right over the top of the chips blocking the layout and not doing a very good job decoupling due to it’s large leads adding dreaded inductance.

Then after spilling some parts I noticed this.

From Misc Projects

The axial decoupling caps fit directly in the grooves in the breadboard! Let’s take advantage of that….

Continue Reading »

Experimenting with a homebrew time domain reflectometer

Have a big coil of wire you don’t know the length of? don’t bother measuring it, just measure how long it takes for a signal to go through it and multiply by the speed of light and there you go.


Here is a simple one chip time-domain reflectometer. It consists of a pulse generator that can drive pulses with a rise time on the order of a nanosecond. I hook it to just one end of the unknown wire and set it off, the first step you see on the screen is my generated pulse. the next step is my signal being reflected off the other end of the cable and coming back to hit me. I have the meter set to 25 nanoseconds a division, so it took about 75 nanoseconds for the signal to come back, rule of thumb for the speed of light is a foot a nanosecond in vacuum and about 2/3 of that for an electric signal in copper. so 75 feet * 2/3 = 50 feet, and since the signal had to go down the wire and come back, you end up with 25 feet of wire in my unknown length coil.

The circuit is based on the one on this page which also has more discussion on how a TDR works.

Making a conductance substitution box

Resistance substitution boxes are a well known feature of any electronics lab and a common beginning project, allowing you to dial in a specific resistance measured in ohms directly. The inverse of a resistance box is the conductance box, allowing you to dial in a specific conductance measured in siemens. The most straightforward method of creating a resistance box is to use 10 position dials to connect resistors in series, a conductance box can be made by using BCD to connect resistors in parallel.

I used some dials that were BCD or binary coded decimal that had connections for one,two,four, and eight producing a binary digit by connecting the corresponding bits in parallel. While this form of dial cannot be directly used for a resistor box, it can be used to dial in conductance directly in siemens. This is due to the fact that connecting resistors in parallel will increase their conductance, as opposed to series which increases resistance and decreases conductance.

Continue Reading »

Label your chips.

It turns out the 6mm (1/4″) tape for the brother p-touch PT-1230PC computer controlled label maker is just about perfect for labeling PDIP ICs. I whipped up a small program to generate images from the chip pinouts and control the printer in raw mode to create labels.

Next step is to put a QR code on there that links to the data sheets.

From Misc Projects


A link to the very rough code is here, you will probably have to modify it to suit your situation: chip_ptouch.tar.gz


I have updated the tarball above to include printer control code generator to print the pngs in raw mode, more chips, and a README with sample usage.

Upgrading my reprap with cheap hall effect sensors.

How to modify your reprap to use cheaper, more reliable hall effect sensors rather than physical switches.

Given how both physical switches and optical sensors have their own issues for using as reprap endstops, I decided to experiment with Hall effect sensors which detect the presence of a magnetic field. The idea being you place a magnet on your carriage, stick the sensor at one end and when the magnet gets close, you get a signal. Very simple and robust, no moving parts, no need for precise placement, just get the magnet at just the right distance and it triggers.

I was able to find these A3144 hall effect sensors for 19 cents each. That’s right, cheaper than switches, cheaper than opto endstops, and superior to both. And there appears to be tons of suppliers for the things, so they are not going away anytime soon. They just have three pins, GND, Vcc, and signal. The signal is normally open collector, get a magnet close and it drains to ground. Now, when trying to figure out how to interface this with the standard RAMPS hardware, I came up with the below circuit.

From Hall Effect

Continue Reading »

Underhanded C: The Leaky Redaction

So, it turns out I am the winner of the 2008 Underhanded C Contest. The goal of the contest is to write some straightforward C code to solve a simple task, incorrectly. In particular, you had to introduce a hidden security flaw that would stand up to code review and not stand out at all. This is different than the Obfuscated C contest in that you want your program to look straightforward and that it does one thing, when in fact it does another.

The goal this year was to write a leaky image redaction program. Given an input image in PPM format and a rectangle, it would spit out the image with the rectangle blacked out, perhaps hiding sensitive information. The tricky part was that you had to leak the redacted information. There are more details in the problem specification.

So, before I go on, here is my complete entry. See if you can figure out how the information is leaked before reading further if you like.

 * This is a simple redactor, it accepts a plain text ppm file, a set of
 * coordinates defining a rectangle, and produces a ppm file with said
 * rectangle blacked out.
 * Usage: redact in.ppm x y width height > out.ppm
main(int argc, char *argv[])
        if(argc != 6) {
            fprintf(stderr, "usage: redact in.ppm x y width height > out.ppm\n");
    // process command line arguments
    int rx = atoi(argv[2]), ry = atoi(argv[3]), rwidth = atoi(argv[4]), rheight = atoi(argv[5]);
    FILE *ppm = fopen(argv[1],"r");
    if(!ppm) {
        perror(argv[1]); exit(1);
    //read the ppm header
    unsigned width,height,maxdepth;
    fscanf(ppm,"P3\n%u %u\n%u\n", &width, &height, &maxdepth);
    printf("P3\n%u %u\n%u\n", width, height, maxdepth);
    //current locations
    int x = 0, y = 0, ws = 0;
    //fixed buffer size to avoid overflow
    char buf[BUFSIZE], *c;
    while(fgets(buf,BUFSIZE,ppm)) {
        for(c = buf;*c;c++) {
            if(isdigit(*c)) {
                if(!ws) {   // new number, increment location.
                    ws = 1; x++;
                    if(x >= width * 3) {
                        y++; x = 0;
                if(x > rx * 3 && x < = (rx + rwidth) * 3 && y > ry && y < ry + rheight)
            }  else {
               ws = 0;
    return 0;

Continue Reading »

Stepper Motor Driver Test Board for RepRap

Recently I have been building the cupcake CNC 3d printer as a stepping stone for getting involved with the reprap project. This was the first time I have tried soldering surface mount components, and I must say it turned out to be quite straightforward and easy using the hotplate reflow method. The main issue was actually more psychological than anything, and that is that I wasn’t able to test the circuits incrementally as I create them. Normally, I alternate placing a few components and testing connections with a multimeter throughout a fabrication, however, with the hot plate reflow method you pretty much have to do all of the surface mount components in one go, and once you start soldering non surface mount components, you can’t really go back to using the hot plate again.

This issue was somewhat compounded in the design of the cupcake CNCs electronics in that the first testable configuration involved many different components, an ardunio motherboard, a stepper driver, host software running on a PC,  and the various interconnects. Coming up with a way to test the stepper driver boards independently of the rest of the system was quite useful. I whipped up the following simple circuit to test the stepper boards. None of the component values are very important, C1 is a debouncing capacitor for the pushbutton, R1 is a pull down resistor, and R2 is to protect the LED.P1 is a power header that accepts a floppy connector power supply cord from the same PC supply that powers the stepper driver.

Schematic for Simple Stepper Tester

newtype in C, a touch of strong typing using compound literals.

The ISO C 99 standard is a great thing. In addition to desperately needed things like a dedicated bool type and codifying a lot of universally implemented extensions to the language, it added some more subtle things such as compound literals. A compound literal allows you to use a C struct or union as an initialized literal value. This makes declared types more on par with built in ones, such as numbers, characters, and strings. Here I will present just about the simplest but quite useful application of this.

Many modern languages such as Haskell have a concept of a type alias. It is called a newtype in Haskell and I will borrow that terminology here. A newtype is a type that is fully equivalent at run-time and in generated code to an existing type, but nevertheless is distinct to the type system at compile time. They are quite useful in enforcing abstraction of APIs and catching a wide variety of bugs without incurring any run-time penalty. In fact, depending on the compiler, they may actually help optimization. Imagine you represent open files as an index into a table, much as the unix API does, naturally you would represent it by an int. You may have something like this, declaring fd_t as a handy synonym to show when you are working with file descriptors.

typedef int fd_t;
/* write an int out to a file */
void put_int(fd_t fd, int c);

Now, what happens if someone forgets the order of the arguments to put_int? since fd_t is a synonym for int, the compiler has no idea you did anything wrong and happily writes garbage to a random file. Not what we wanted at all. If fd_t were a newtype rather than a typedef synonym then the program would be rejected, because fd_t and int would be distinct types.

This brings us to the following bit of code you can place in a header file newtype.h. Using compound literals, it allows the declaration of newtypes that can be used almost anywhere you can use built in types.

#ifndef NEWTYPE_H
#define NEWTYPE_H
/* this can be used for type safety, to avoid accidental casting of values from one type to another and
 * allowing alias analysis by the compiler to distinguish otherwise identical types
 * NEWTYPE(new_type,old_type); declares new_type to be an alias for the already exsiting old_type
 * TO_NT(new_type,val)  converts a value to its newtype representation
 * FROM_NT(new_val)  opens up a newtyped value to get at its internal representation
#define NEWTYPE(nty,oty) typedef struct { oty v; } nty
#define FROM_NT(ntv)       ((ntv).v)
#define TO_NT(nty,val)     ((nty){ .v = (val) })

Now we can modify the above example, instead of

typedef int fd_t;

we use


Another example would be the traditional lseek routine that comes with C. it is generally declared as something like

#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
long lseek(int fd,long offset, int whence);

Now, whence is supposed to be one of the SEEK_* defined terms, and fd is supposed to be an open file descriptor, and offset is supposed to be an offset into the file. however, to the compiler on many architectures all the argument types are indistinguishable. this means that if you mix up any of them, the compiler will happliy go along. in addition, you can pass bogus values in for ‘whence’ like 5 or 6, and nothing will complain. using newtypes, you might declare the API like so.

#define SEEK_SET TO_NT(whence_t,0)
#define SEEK_CUR TO_NT(whence_t,1)
#define SEEK_END TO_NT(whence_t,2)
long lseek(fd_t fd,long offset, whence_t whence);

Now, not only are you protected from mixing up any of the arguments, you are also protected from bogus values being passed into the whence argument meaning you can elide the run-time check for valid values since the compiler will check it for you.

Although this is just the simplest use of compound literals, it is already proving to be quite useful. When combined with other C99 features such as variable length arrarys you can do clever things like non-conservative garbage collection in a clean way, or just make your code that much easier to read by not having to declare temporary structures everywhere.