IAN LANG ELECTRONICS

As you might have gathered from the jumble of wires above, this is the fiddliest circuit in the book, so check everything is in order and if you use the breadboard maps follow them to the letter. You might notice I've put a button on, ignore it for now as that's for something we're going to mess about with in a few minutes.

having built the circuit, you'll find the LEDs twinkle in what at first glance looks like a haphazard fashion.         It isn't though. What it's doing is displaying a count from 0 to 255, but it's doing it in binary.

If you upload the following code you'll find it does exactly the same thing and it's a bit smaller:

 

//Pin connected to ST_CP of 74HC595

int latchPin = 4;

//Pin connected to SH_CP of 74HC595

int clockPin = 3;

////Pin connected to DS of 74HC595

int dataPin = 2;

int buttonpress;

 

 

void setup() {

  //set pins to output so you can control the shift register

  pinMode(latchPin, OUTPUT);

  pinMode(clockPin, OUTPUT);

  pinMode(dataPin, OUTPUT);

  buttonpress=0;

}

 

void loop() {

  // count from 0 to 255 and display the number

  // on the LEDs

  for (int numberToDisplay = 0; numberToDisplay < 256; numberToDisplay=numberToDisplay+1) {

    // take the latchPin low so

    // the LEDs don't change while you're sending in bits:

    digitalWrite(latchPin, LOW);

    // shift out the bits:

    shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay);  

 

    //take the latch pin high so the LEDs will light up:

    digitalWrite(latchPin, HIGH);

    // pause before next value:

    delay(100);

}

}

 

This code by the way is not mine. It was written by someone as an Arduino development. Before we go into the intricacies of the code it might be better to look at what binary actually is.

 

Binary Numbers- the Power of Twos.

 

You and I count in terms of ten. The decimal system starts at 0 and goes to 9.  Then it's 10, until 19, then 20 and so on. Then there's 100, which is ten times ten, and 1000, which is ten times ten times ten, and so on. We can't do that in electronics. The device only knows if a voltage is there or not. This means it can only count up to one from zero, or in other words it only knows two numbers. ( hence   BInary). But- what you can do is make it count in powers of two. Anything to the power of zero is 1, and 2 to the power of 1 is 2. 2 to the power of 2 is 4, and 2 to the power of three is eight. The table below shows eight iterations:

 

 

 

 

 

 

 

If we kept on going we'd get to some big numbers pretty quickly. 2 to the power of 15 is 32768, 2 to the power of 64 is 18,446,744,073,709,551,616. Even 2 to the power of 20 is 1,048,576.

Now, sum up the decimal numbers on the bottom. It comes to 255. But there's also a 0 before that, so the total numbers are 256. How do we get a decimal number out of a binary? Piece of cake. Remember the computer or other device can only count 0 and 1. So, we sum up however many 1s are present to get our decimal number. Lets expand that table:

 

 

 

 

 

 

 

 

 

Unlike in decimal in binary we work from left to right. So we sum up from left to right and get 64+32+4 which in  decimal is 100. Similarly:

 

 

 

 

 

 

 

 

 

 

is 2. Using eight powers like this, in binary it's 00000010. In electronics and computing terms this is known as eight-bit binary. Were we to use two powers it would be two-bit and we'd only be able to count up to 3.  This leads us to the funniest binary joke in the World:

 

 

 

 

There are only 10 sorts of people-

those who understand

binary, and those who don't.

 

 

 

 

 

 

 

 

Anyway, eight bits is called a byte and it's how the chips inside the computer you're using and all of the others communicate with each other. The ATmega and the 74HC595 we're using in this circuit do it in the following manner.

 

 

The 74HC595

 

There are several kinds of shift register but this particular example is a series-in-parallel-out (SIPO) type.

Is it really? Yes it is. Oh good.

In plain English what you do is send it eight bits of data, one at a time, and it displays them all at the same time by turning pins high or low. In our case that makes LEDs shine or not. It does this by taking three inputs

from our Arduino Board and they are the clock, the latch and the data.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

In binary, the most significant bit is considered first. Since this is decimal 37, the nearest lesser number is 32. The assignment goes QA is 1 and QH 128, so Q H&G would be off, and so would the leds attached to them. Q6 is the pin that would be assigned a 1 and so the LED attached to it will shine. That leaves five to deal with (37-32=5) and the nearest lesser number is 4,  and that's QC so that would be assigned a 1 and the LED will shine. That leaves just 1, and that's QA so the LED on there will shine too. Q B,D & E will be assigned zero and their LEDs will not shine.

 

 

I know what you are thinking now. You're thinking  "Hang on, this thing makes LEDs twinkle does it? And eight of them at that. We did that in CIRC-01 and we didn't need no fancy kit there. What's the point? Eh?"

 

I'll tell you. In CIRC-01 we did indeed make eight LEDs twinkle, but we used eight connections to the board to do it. Here we used three, and got eight outputs. We could put three more shift registers on in the space of the board and that gives us thirty-two. Q'H which is not connected in our circuit can be used to hook a shift register onto a shift register. That gives us sixty-four.

 

Right, I think we've covered the hardware and theoretical aspects of this circuit pretty much, so over the page we go where it's time to mess about.

CIRC-05

Go Back

In this circuit we're going entirely digital and we are looking at shift registers. It's another well thought out circuit from Oomlout but the code they supply is definitely going the hard way round. What it does is count in binary and display that output still in binary via the shift register by the means of eight LEDs. A good idea as it lets you see a. what binary is and b. what a shift register does. Well, lets build the circuit then and fire it up with the Oomlout code. Here it is as it appears in the book:

Copy & Paste Code from Here:

C'mon, get your titters out......

Any of them could come from any digital pin of the Arduino board but in the above circuit the clock is at pin 3, the latch 4 and the data 2. The jump wire coming from 2  (data) goes to pin 14 of the  74HC595 which as you can from the diagram on the left is the serial input. Similarly that from pin 4 of the Arduino goes to 12, RCK which is the latch. When this is low, and pins 10 & 13 are high and low respectively (which they always are because they're connected to 5V and GND) and a clock pulse comes in at 11, data is transferred and stored in the register. One clock pulse means one bit of data. Once the latch goes high, the data is shifted to the output pins. They are: QA on the right, and QB to QH on the left. The data is stored as either a zero or a 1. If 00100101 came in through the serial input  this would be decimal 37.

More >