Locate pin 1 of the 74HC595N shift register by finding the notch on the IC. If you orient the IC so that the end with the notch is facing left, pin 1 will be the first pin in the lower left below the notch.
Make the following connections on the 74HC595N shift register:
Pins 1, 2, 3, 4, 5, 6, 7 and Pin 15 Q0 thru Q7: Connect each of these pins to a 330 Ω resistor - one 330 Ω resistor for each pin
Pin 8 GND: Connect to the ground rail on the side of the breadboard (blue stripe)
Pin 9 Q7’: Leave unconnected
Pin 10 SRCLR: Connect to GPIO 12 (pin 32) on the RPi2 or RPi3 (pin mapping is below)
Pin 11 SRCLK: Connect to GPIO 18 (pin 12) on the RPi2 or RPi3
Pin 12 RCLK: Connect to GPIO 5 (pin 29) on the RPi2 or RPi3
Pin 13 OE: Connect to GPIO 6 (pin 31) on the RPi2 or RPi3
Pin 14 SER: Connect to GPIO 27 (pin 13) on the RPi2 or RPi3
Pin 15 Q7: See above.
Pin 16 VCC: Connect to the voltage supply rail on the side of the breadboard (red stripe)
Connecting the LEDs and Resistors
Let’s add the LEDs and resistors to the breadboard.
If not already connected, place eight 330 Ω resistors on the breadboard and connect each resistor to an output pin (Q0 thru Q7) on the shift register.
Connect a cathode (the shorter leg) of the four green and four blue LEDs to the other end of a 330 Ω resistor you placed on the breadboard in the previous step. Try to keep the LEDs lined up together in a row on the breadboard. The order of the green and blue LEDs is up to you. We chose to alternate LED colors.
Connect the anodes (the longer leg) of the LEDs to the voltage supply rail on the side of the breadboard (red stripe)
When done, you should have a row (or as close to a row as possible) of blue and green LEDs on your breadboard.
Each of the shift register outputs Q0 thru Q7 should be connected to a 330 Ω resistor.
The other end of each of the 330 Ω resistors should be connected to the cathode of an LED.
Each LED should have its anode connected to the voltage supply rail.
Connecting the Raspberry Pi 2 or 3
We need to hook up power, ground, and the I2C lines from on the Raspberry Pi 2 or 3 to the 74HC595N shift register and the breadboard.
Pin 2 5V PWR Connect to the voltage supply rail on the side of the breadboard (red stripe)
Pin 6 GND Connect to the ground rail on the side of the breadboard (blue stripe)
Pin 12 GPIO18 If not already connected, connect to SRCLK (pin 11) on the shift register
Pin 13 GPIO27 If not already connected, connect to SER (pin 14) on the shift register
Pin 29 GPIO5 If not already connected, connect to RCLK (pin 12) on the shift register
Pin 31 GPIO6 If not already connected, connect to OE (pin 13) on the shift register
Pin 32 GPIO12 If not already connected, connect to SRCLR (pin 10) on the shift register
Create the Sample App
When everything is set up, power your device back on. You can find the source code for this sample by downloading a zip of all of our samples here and navigating to the samples-develop\ShiftRegister,
but as an exercise, this tutorial will take you through the complete steps to create this app from scratch.
Open up Visual Studio and create a new C# Windows Universal Blank App. Click File -> New -> Project then select Templates -> Visual C# -> Windows -> Universal -> Blank App (Universal Windows).
For this sample, we cleverly named ours ShiftRegisterSample.
The code in this sample does three things:
Initializes the RPi2 or RPi3 GPIO pins and the 74HC595N shift register
At regular intervals, it clocks in one data bit on the shift register’s serial input.
Responds to user input
a. Changes the timer delay based on user input. This results in a change in the blinking rate of the LEDs
b. Inverts the LED flashing pattern if the user clicks the Invert button on the display
Add content to MainPage.xaml
Let’s add some content to the MainPage which will be displayed on a screen connected to the Raspberry Pi 2 or 3.
We want to add a couple TextBoxes, a Slider, and a Button.
The Slider allows the user to control the rate at which the LEDs flash.
The button allows the user to invert the pattern which the LEDs use to flash.
The Textboxes will provide info about the slider.
From Solution Explorer, select the MainPage.xaml file.
Locate the <Grid> tag in the XAML section of the designer, and add the following markup:
Add code to MainPage.xaml.cs
Before we add any code to MainPage.xaml.cs, we need to add a reference to the Windows IoT Extension SDK.
In Solution Explorer, right-click on References and select Add Reference…
In the Reference Manager window, click the arrow next to Windows Universal then click on Extensions
Place a check mark in the box next to Windows IoT Extension SDK and click OK
Open MainPage.xaml.cs from Solution Explorer (you may need to click on the arrow next to MainPage.xaml first)
Add the following line at the top of MainPage.xaml.cs
With the references added, let’s start adding code. The complete code we implemented for MainPage.xaml.cs is located at end of this section. Below are some key parts of the code with explanations
Variables and Constants
Here are what some of these constants and variables represent
SRCLK_PIN, SER_PIN, RCLK_PIN, OE_PIN, SRCLR_PIN represent the RPi2 or RPi3 GPIO numbers of the pins connected to the correspondingly named shift register control pins.
shiftRegisterClock, serial, registerClock, outputEnable, shiftRegisterClear are the GPIO objects that are used to control the RPi2 or RPi3 GPIO pins that connect to the correspondingly named shift register pins.
pinMask contains the data bits that we clock in to the shift register one bit at a time. The bits in pinMask represent the on/off pattern for the LEDs. A bit value of ‘1’ represents an off LED and a bit value of ‘0’ is an on LED.
The method InitializeSystem()
InitializeSystem() does the following:
Sets up the RPi2 or RPi3 GPIO that are used to control and send data to the shift register
Sets up the timer used control the frequency at which the RPi2 or RPi3 sends one bit of data to the shift register
The method SendDataBit()
SendDataBit() is called by the timer when the designated time interval has passed.
This method clocks out one bit of data representing the most significant bit (MSB) in the variable pinMask. The data bit is clocked into the first bit position
in the shift register. The toggling of the serial clock pin by the RPi2 or RPi3 to clock in the data bit also causes the previous eight bits of data in the shift register to shift one bit position with the bit in the last position being lost.
After clocking in the data bit, all bits in pinMask are left shifted one bit position. The value of pinMask is then checked and, depending on whether the LED lighting pattern is set as inverted, the least significant bit (LSB) of ‘pinMask’ is set to either a ‘1’ or ‘0’.
ToggleButtonClicked() is triggered when the app user clicks on the button on the display screen to invert the LEDs. This method inverts all the bits in pinMask, sets or clears the flag areLedsInverted depending on what the previous state was.
Finally, it changes the color of the button - red when the LEDs are inverted and gray when they are not.
Build, Deploy and Run the App
Let’s build, deploy and run the app on our Raspberry Pi 2 or 3.
If not already open, open in Visual Studio the app you created with the code above.