05 |
.:74HC595 Shift Register:. |

What We're Doing
Time to start playing with chips, or integrated circuits (ICs) as they like to be called. The external packaging of a chip can be very deceptive. For example, the chip on the Netduino board (a micro controller) and the one we will use in this circuit (a shift register) look slightly similar but are in fact rather different. The price of the Atmel chip on the Netrduino board is a few dollars while the 74HC595 is a couple dozen cents. It's a good introductory chip, and once you're comfortable playing around with it and its datasheet (available online http://nedx.org/74HC595 ) the world of chips will be your oyster. The shift register (also called a serial to parallel converter), will give you an additional 8 outputs (to control LEDs and the like) using only three Netduino pins. They can also be linked together to give you a nearly unlimited number of outputs using the same three pins. To use it you "clock in" the data and then lock it in (latch it). To do this you set the data pin to either HIGH or LOW, pulse the clock, then set the data pin again and pulse the clock repeating until you have shifted out 8 bits of data. Then you pulse the latch and the 8 bits are transferred to the shift registers pins. It sounds complicated but is really simple once you get the hang of it.
(for a more in depth look at how a shift register works visit: http://nedx.org/SHIF )
Time to start playing with chips, or integrated circuits (ICs) as they like to be called. The external packaging of a chip can be very deceptive. For example, the chip on the Netduino board (a micro controller) and the one we will use in this circuit (a shift register) look slightly similar but are in fact rather different. The price of the Atmel chip on the Netrduino board is a few dollars while the 74HC595 is a couple dozen cents. It's a good introductory chip, and once you're comfortable playing around with it and its datasheet (available online http://nedx.org/74HC595 ) the world of chips will be your oyster. The shift register (also called a serial to parallel converter), will give you an additional 8 outputs (to control LEDs and the like) using only three Netduino pins. They can also be linked together to give you a nearly unlimited number of outputs using the same three pins. To use it you "clock in" the data and then lock it in (latch it). To do this you set the data pin to either HIGH or LOW, pulse the clock, then set the data pin again and pulse the clock repeating until you have shifted out 8 bits of data. Then you pulse the latch and the 8 bits are transferred to the shift registers pins. It sounds complicated but is really simple once you get the hang of it.
The Circuit
The Parts
![]() |
NCIR-05 Breadboard Sheet x1 |
![]() |
2 Pin Header x4 |
![]() |
Shift Register 74HC595 x1 |
![]() |
Wire |
![]() |
Red LED x8 |
![]() |
560 Ohm Resistor Green-Blue-Brown x8 |
![]() |
![]() |

Code (no need to type everything in just)
Download the Code from ( http://nedx.org/CODE05 )
(copy the text and paste it into an empty Netduino Sketch)
//Libraries that our program uses
using System; //use base classes
using System.Threading; //use threading classes
using Microsoft.SPOT; //use smart personal objects technology classes
using Microsoft.SPOT.Hardware; //use hardware related SPOT classes
using SecretLabs.NETMF.Hardware; //use Secret Labs hardware framework
using SecretLabs.NETMF.Hardware.Netduino; //use the Netduino specific classes
namespace NCIR05 /// Define the namespace we are in ///
{
public class Program /// Define any variables used in the program and subrout
//ines after Main()///
{
static OutputPort data = new OutputPort(Pins.GPIO_PIN_D2, false); //Set pin 2 as a
//n output and our data line
static OutputPort clock = new OutputPort(Pins.GPIO_PIN_D3, false); //Set pin 3 as a
//n output and our clock line
static OutputPort latch = new OutputPort(Pins.GPIO_PIN_D4, false); //Set pin 4 as a
//n output and our latch line
public static void Main() /// The Main loop (run at power up) ///
{
int delayTime = 100; //the number of milliseconds to delay between LED updates
while (true) /// Do Forever ///
{
for (int i = 0; i < 256; i++)
{
updateLEDs(i); //display the variable i on the LEDs in bina
//ry form
Thread.Sleep(delayTime); //wait delayTime milliseconds
}
}
}
///
/// sends the LED states set in value to the 74HC595 sequence
///
/// the 8 bit number to be displayed in binary on the LEDs
//m>
static void updateLEDs(int value)
{
latch.Write(false); //Pulls the chips latch low
for (int i = 0; i < 8; i++) //Will repeat 8 times (once for each bit)
{
int bit = value & 0x80; //We use a "bitmask" to select only the eighth
//bit in our number (the one we are addressing this
// time through
value = value << 1; //we move our number up one bit value so next time
//bit 7 will be
//bit 8 and we will do our math on it
if (bit == 128)
{
data.Write(true); //if bit 8 is set then set our data pin high
}
else
{
data.Write(false); //if bit 8 is unset then set the data pin low
}
clock.Write(true); //the next three lines pulse the clock pin
Thread.Sleep(1);
clock.Write(false);
}
latch.Write(true); //pulls the latch high shifting our data into being
//displayed
}
//These are used in the bitwise math that we use to change individual LEDs
//For more details http://en.wikipedia.org/wiki/Bitwise_operation
static int[] bits = {1, 2, 4, 8, 16, 32, 64, 128};
static int[] masks = {254, 253, 251, 247, 239, 223, 191, 127};
static int ledState = 0;
///
/// changeLED(int led, int state) - changes an individual LED
/// the LED we are addressing
/// whether that LED is to be on or off
///
public static void changeLED(int led, bool state){
ledState = ledState & masks[led]; //clears ledState of the bit we are addressin
//g
if(state){ledState = ledState | bits[led];} //if the bit is on we will add it to
// ledState
updateLEDs(ledState); //send the new LED state to the shift registe
//r
}
} /// Close the Program Loop ///
} /// Close the Namespace Loop ///
Download the Code from ( http://nedx.org/CODE05 )
(copy the text and paste it into an empty Netduino Sketch)
//Libraries that our program uses
using System; //use base classes
using System.Threading; //use threading classes
using Microsoft.SPOT; //use smart personal objects technology classes
using Microsoft.SPOT.Hardware; //use hardware related SPOT classes
using SecretLabs.NETMF.Hardware; //use Secret Labs hardware framework
using SecretLabs.NETMF.Hardware.Netduino; //use the Netduino specific classes
namespace NCIR05 /// Define the namespace we are in ///
{
public class Program /// Define any variables used in the program and subrout
//ines after Main()///
{
static OutputPort data = new OutputPort(Pins.GPIO_PIN_D2, false); //Set pin 2 as a
//n output and our data line
static OutputPort clock = new OutputPort(Pins.GPIO_PIN_D3, false); //Set pin 3 as a
//n output and our clock line
static OutputPort latch = new OutputPort(Pins.GPIO_PIN_D4, false); //Set pin 4 as a
//n output and our latch line
public static void Main() /// The Main loop (run at power up) ///
{
int delayTime = 100; //the number of milliseconds to delay between LED updates
while (true) /// Do Forever ///
{
for (int i = 0; i < 256; i++)
{
updateLEDs(i); //display the variable i on the LEDs in bina
//ry form
Thread.Sleep(delayTime); //wait delayTime milliseconds
}
}
}
///
/// sends the LED states set in value to the 74HC595 sequence
///
/// the 8 bit number to be displayed in binary on the LEDs
//m>
static void updateLEDs(int value)
{
latch.Write(false); //Pulls the chips latch low
for (int i = 0; i < 8; i++) //Will repeat 8 times (once for each bit)
{
int bit = value & 0x80; //We use a "bitmask" to select only the eighth
//bit in our number (the one we are addressing this
// time through
value = value << 1; //we move our number up one bit value so next time
//bit 7 will be
//bit 8 and we will do our math on it
if (bit == 128)
{
data.Write(true); //if bit 8 is set then set our data pin high
}
else
{
data.Write(false); //if bit 8 is unset then set the data pin low
}
clock.Write(true); //the next three lines pulse the clock pin
Thread.Sleep(1);
clock.Write(false);
}
latch.Write(true); //pulls the latch high shifting our data into being
//displayed
}
//These are used in the bitwise math that we use to change individual LEDs
//For more details http://en.wikipedia.org/wiki/Bitwise_operation
static int[] bits = {1, 2, 4, 8, 16, 32, 64, 128};
static int[] masks = {254, 253, 251, 247, 239, 223, 191, 127};
static int ledState = 0;
///
/// changeLED(int led, int state) - changes an individual LED
/// the LED we are addressing
/// whether that LED is to be on or off
///
public static void changeLED(int led, bool state){
ledState = ledState & masks[led]; //clears ledState of the bit we are addressin
//g
if(state){ledState = ledState | bits[led];} //if the bit is on we will add it to
// ledState
updateLEDs(ledState); //send the new LED state to the shift registe
//r
}
} /// Close the Program Loop ///
} /// Close the Namespace Loop ///
Not Working? (3 things to try)
The Netduino's power LED goes out
This happened to us a couple of times, it happens when the chip is inserted backwards. If you fix it quickly nothing will break.
Not Quite Working
Sorry to sound like a broken record but it is probably something as simple as a crossed wire.
Frustration?
Don't worry the Netduino has a great community around it. There's always someone keen to help out on the forums.
http://nedx.org/FORU
This happened to us a couple of times, it happens when the chip is inserted backwards. If you fix it quickly nothing will break.
Sorry to sound like a broken record but it is probably something as simple as a crossed wire.
Don't worry the Netduino has a great community around it. There's always someone keen to help out on the forums.
http://nedx.org/FORU
Making it Better?
Understanding What's Going On:
The Netduino makes rather complex actions easy to hide, shifting out data is one of these cases. Take a look at the updateLEDs(int value). If you look at the code you can see how we are communicating with the chip one bit at a time. (for more details http://nedx.org/SPI ).
Controlling individual LEDs:
Time to start controlling the LEDs in a similar method as we did in NCIR02. As the eight LED states are stored in one byte (an 8 bit value) for details on how this works visit http://nedx.org/BINA. A Netduino is very good at manipulating bits and there are an entire set of operators that help us out. Details on bitwise maths ( http://nedx.org/BITW ).
Our implementation.
Replace the code after while (true) with:
for (int i = 0; i < 8; i++)
{
changeLED(i, true);
Thread.Sleep(delayTime);
}
for (int i = 0; i < 8; i++)
{
changeLED(i, false);
Thread.Sleep(delayTime);
}
Uploading this will cause the lights to light up one after another and then off in a similar manner to NCIR02. Check the code and wikipedia to see how it works.
More animations:
Now things get more interesting. If you look back to the code from NCIR02 (8 LED Fun) you see we change the LEDs using leds[#].Write(true), this is a similar format as the routine we wrote changeLED(led#, state). You can use the animations you wrote for NCIR02 by copying the code into this sketch and changing all the leds[#].Write(true)'s to changeLED()'s. Powerful? Very. (you'll also need to change a few other things but follow the errors and it works itself out).
Understanding What's Going On:
The Netduino makes rather complex actions easy to hide, shifting out data is one of these cases. Take a look at the updateLEDs(int value). If you look at the code you can see how we are communicating with the chip one bit at a time. (for more details http://nedx.org/SPI ).
Controlling individual LEDs:
Time to start controlling the LEDs in a similar method as we did in NCIR02. As the eight LED states are stored in one byte (an 8 bit value) for details on how this works visit http://nedx.org/BINA. A Netduino is very good at manipulating bits and there are an entire set of operators that help us out. Details on bitwise maths ( http://nedx.org/BITW ).
Our implementation.
Replace the code after while (true) with:
for (int i = 0; i < 8; i++)
{
changeLED(i, true);
Thread.Sleep(delayTime);
}
for (int i = 0; i < 8; i++)
{
changeLED(i, false);
Thread.Sleep(delayTime);
}
Uploading this will cause the lights to light up one after another and then off in a similar manner to NCIR02. Check the code and wikipedia to see how it works.
More animations:
Now things get more interesting. If you look back to the code from NCIR02 (8 LED Fun) you see we change the LEDs using leds[#].Write(true), this is a similar format as the routine we wrote changeLED(led#, state). You can use the animations you wrote for NCIR02 by copying the code into this sketch and changing all the leds[#].Write(true)'s to changeLED()'s. Powerful? Very. (you'll also need to change a few other things but follow the errors and it works itself out).