A one and a zero on a real wire.

In the middle of the 1930s an American graduate student named Otto Schmitt was trying to understand how a nerve carries a message. He worked with squid, whose nerve fibers are among the largest known, wide enough to see without a microscope. A nerve has a curious habit. Prod it gently and nothing happens, and prod it a little harder and still nothing happens. Then, past a certain point, it fires an impulse of full size, the same size every time, however hard it was prodded. A nerve has no way to send half an impulse.

Schmitt built a circuit out of vacuum tubes that behaved the same way, and in 1938 he published it under the name "a thermionic trigger." It has carried his own name ever since, and it stands guard on the inputs of chips in nearly everything electronic. His circuit is the answer to a problem that this chapter has to set up first, and the problem begins with a simple question. What is a one, and what is a zero, on a real piece of copper?

Two ranges and a gap between

The last two chapters drew numbers moving along arrows. Each arrow is a bundle of wires, and the only thing a wire can carry is a voltage, which is an electrical pressure, something like the pressure in a water pipe. A voltage can take any value at all. It can be 1.3 volts, or 1.31, or anything in between. Engineers call a quantity like that analog, so a wire is an analog thing, like the signals of every earlier chapter.

Digital circuits get their ones and zeros by agreeing to ignore most of that. The makers of a family of chips publish two ranges. Any voltage in the high range counts as a one, any voltage in the low range counts as a zero, and the stretch between them is promised to no one. In the family of chips that most computers of the 1970s and 1980s were built from, which ran on 5 volts, the numbers looked like this.

senderreceiver a 1a 1 2.4 V0.4 V5 V 2.0 V0.8 V
A sender in this family promises at least 2.4 volts for a one and at most 0.4 volts for a zero, the thin band at the bottom. A receiver agrees to read anything above 2.0 volts as a one and anything below 0.8 volts as a zero. The two short bars in the middle mark the 0.4 volts between each promise and each agreement, which is the noise margin.

The trick is in the difference between the two columns. The sender promises more than the receiver demands. A one leaves at 2.4 volts or better, and it is still read correctly if it arrives at 2.0. So the wire is allowed to pick up as much as 0.4 volts of noise along the way, from a neighboring wire or a motor starting or a radio station, and the receiver will still read exactly what was sent. That allowance is called the noise margin, and it is the most important number in digital electronics.

It is the same bargain as grading a test pass or fail. A score of 95 has to be wrong by a great deal before it goes down as a fail. A digital wire spends its whole range of voltages on a single yes or no, which is extravagant, and what the extravagance buys is certainty. Today's fastest chips run on about one volt, and their margins are a fraction of that.

Copies that stay clean

The margin does something more valuable than protect one trip down one wire. The smallest circuit in a computer that makes a decision is called a gate, and a computer holds billions of them. Every gate works the same way. It reads its inputs, decides between one and zero, and puts out a fresh voltage at full strength. Whatever noise rode in on the input is simply gone, as long as it stayed inside the margin. The signal is made new at every gate.

Compare that with the analog way. An amplifier is a circuit that makes a signal larger, and it has no means of telling signal from noise, so it enlarges both and adds a little noise of its own. A signal that has been through twenty amplifiers carries twenty helpings. This is the photocopy of a photocopy of a photocopy, each generation a little grayer and fuzzier than the last, and it is the print made from a print in chapter 21. The digital way is a line of typists, each one retyping the page from the one before. As long as every letter can still be read, the twentieth copy is as crisp as the first.

Below, the same thirty-two bits are handed down a line of stages, and every stage adds some noise. One line is made of amplifiers and the other of gates. Lengthen the line and raise the noise, and watch what arrives at the far end. A red mark under a bit means it would be read wrongly.

Two things show up as the noise rises. For a long while the gates are perfect and the amplifiers get steadily worse. Then, quite suddenly, the gates begin to fail as well, and their failures are of a nastier kind. A gate that misreads a bit sends the wrong answer on at full strength, and nothing downstream can tell that anything happened. Engineers call that edge the digital cliff. It is why a weak analog television picture used to turn snowy by degrees, while a weak digital one stays perfect right up to the moment it freezes into blocks.

Most of what remains in this part of the book is about staying well back from that cliff.

The trouble at the threshold

A receiver has to draw its line somewhere, and the simplest receiver draws exactly one. The circuit is called a comparator, and it does what the name says. It puts out a one whenever its input is above a threshold, and a zero whenever its input is below.

That works well as long as the signal crosses the threshold briskly. The trouble comes when it dawdles there. Suppose the input is rising slowly and carries a little noise, as every real signal does. While it is close to the threshold, each wiggle of the noise carries it across the line and back again, and the comparator faithfully reports every crossing. One slow change at the input comes out as a burst of rapid ones and zeros. Engineers call it chatter.

Chatter is common. A street lamp with a light sensor flickers on and off at dusk, while the daylight hovers at the level that switches it. A push button is worse. Its metal contacts bounce like a dropped ball when they meet, making and breaking the circuit a dozen times in a few thousandths of a second. A processor is easily fast enough to see every bounce, and unless something is done about it, one press of the button is counted as a dozen.

Two thresholds, and a memory

Schmitt's answer was to draw two lines. His circuit switches to a one only when the input climbs past an upper threshold, and it switches back to a zero only when the input falls below a lower one. In the gap between the two it does nothing at all, and its output stays whatever it last was.

A household thermostat works this way, for the same reason. Set it to 68 degrees and it turns the furnace on at 67 and off at 69. With a single line at 68, the furnace would click on and off every few seconds as the air around the thermostat wavered across it. With a gap, a wavering input has to travel the whole distance between the two lines before anything happens, and noise smaller than the gap gets no say.

This behavior has a name, hysteresis, from a Greek word for lagging behind. The output depends on where the input is and also on where it has been. In most machinery hysteresis is a fault to be designed out, like the slack in a worn steering wheel that has to be taken up before the car begins to turn. Here it is put in on purpose, and that small dose of deliberate stickiness is what makes the decision firm.

Inside the circuit the trick is feedback. A share of the output is fed back to the input side, in the direction that moves the threshold away from the signal the moment the output switches. Once the circuit has said yes, it takes a much firmer no to change its mind.

The drawing sends one noisy signal into two comparators. Choose a signal, add noise, and open the gap between the two thresholds until the false changes disappear. Then keep opening it, and see what it costs.

the same circuit as a map

Going up, the output follows the green line and waits for the upper threshold. Coming down, it follows the other line and waits for the lower one.

The small map is the same circuit drawn another way. The signal coming in runs from left to right, and the output is either low or high. The loop in the middle is the hysteresis, and a little drawing of that loop is the mark that stands for a Schmitt trigger on a circuit diagram.

The gap has a price, and the drawing shows both parts of it. The output always switches a little late, because the signal has to carry on past the middle to the far threshold. And a signal smaller than the gap is ignored altogether. The rule for choosing the gap is the kind of rule this book keeps arriving at: wider than the noise, and narrower than the signal.

The push button deserves a note of its own. Its bounces are full-sized, far too big for any gap to ignore, so the usual cure has two parts. Two small components, a resistor and a capacitor, first smear the bounces into the wobbling climb shown in the drawing, and a Schmitt trigger then turns that climb into a single clean edge. Both jobs are often done in software, with a few lines like these.

/* A comparator with two thresholds. Call it once for every new sample.
   Between the two thresholds the answer stays as it was. */
typedef struct { float low, high; int state; } schmitt_t;

int schmitt_step(schmitt_t *s, float sample) {
    if (sample > s->high) s->state = 1;
    else if (sample < s->low) s->state = 0;
    return s->state;
}

/* The same idea for a push button, read as 0 or 1 a thousand times a second.
   A count climbs while the button reads 1 and falls while it reads 0.
   The answer changes only when the count has gone all the way to one end. */
#define FULL 10

int button_step(int *count, int *state, int reading) {
    if (reading) { if (*count < FULL) (*count)++; }
    else         { if (*count > 0)    (*count)--; }
    if (*count == FULL) *state = 1;
    else if (*count == 0) *state = 0;
    return *state;
}
def schmitt(samples, low, high, state=0):
    """A comparator with two thresholds.
    Between the two thresholds the answer stays as it was."""
    out = []
    for sample in samples:
        if sample > high:
            state = 1
        elif sample < low:
            state = 0
        out.append(state)
    return out


def button(readings, full=10):
    """The same idea for a push button read as 0 or 1 many times a second.
    A count climbs on every 1 and falls on every 0, and the answer
    changes only when the count has gone all the way to one end."""
    count, state, out = 0, 0, []
    for reading in readings:
        count = min(full, count + 1) if reading else max(0, count - 1)
        if count == full:
            state = 1
        elif count == 0:
            state = 0
        out.append(state)
    return out

That little circuit also has a memory. Its output can be a one or a zero for the very same input, depending on what happened earlier. A circuit that holds on to its answer after the question has gone away is the seed of every memory cell in a computer, and Petzold's Code builds one out of two gates in just this way.

When to look

A receiver needs one more thing besides a threshold. It needs to know when to look. A wire that carries one bit after another spends part of its time in between, on the way from one level to the other, and a receiver that looked at that moment would read nonsense.

So digital circuits look only at agreed moments, marked by the ticks of the clock. At each tick a small circuit called a flip-flop takes a snapshot of its input and holds it until the next tick. Like a camera with a slow shutter, it needs its subject to hold still. The data has to be steady for a short time before the tick and for a short time after it.

clockdatasteady here
A flip-flop reads the data line at the moment the clock rises. The data is free to change at any other time, and it has to hold still inside the shaded window around that moment.

Nearly everything a designer does about timing comes down to keeping changes out of that window. When a change does land inside it, the flip-flop can be caught halfway, like a coin that lands on its edge, and take an unpredictable time to fall one way or the other.

So a bit that arrives safely has passed two tests. It was on the right side of the threshold, with margin to spare, and it was holding still at the moment it was read. One test is about voltage and the other is about time. The next chapter puts both of them into a single picture, which engineers call the eye.

Try it: hunt for the gap

  1. Listen to a thermostat, a refrigerator or a well pump as it cycles. Note the reading at which it switches on and the reading at which it switches off. The difference is its gap.
  2. Find a lamp that turns itself on at dusk. Shade its sensor slowly with your hand, and see whether it flickers at the edge or switches once and stays.
  3. If you have a small microcontroller board, wire a push button to an input and count how many times the input changes in the hundredth of a second after one press. A count of five or more is ordinary.
  4. Then run the readings through the button routine above, and count again.