Arduino includes a special function called 'shiftOut' that is designed specifically for sending data to shift registers.

Here is the full sketch, the discussion of how it works follows on from it.
```/*
Adafruit Arduino - Lesson 4. 8 LEDs and a Shift Register
*/

int latchPin = 5;
int clockPin = 6;
int dataPin = 4;

byte leds = 0;

void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}

void loop()
{
leds = 0;
delay(500);
for (int i = 0; i < 8; i++)
{
bitSet(leds, i);
delay(500);
}
}

{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}```
The first thing we do is define the three pins we are going to use. These are the Arduino digital outputs that will be connected to the latch, clock and data pins of the 74HC595.
```int latchPin = 5;
int clockPin = 6;
int dataPin = 4;```
Next, a variable called 'leds' is defined. This will be used to hold the pattern of which LEDs are currently turned on or off. Data of type 'byte' represents numbers using eight bits. Each bit can be either on or off, so this is perfect for keeping track of which of our eight LEDs are on or off.
`byte leds = 0;`
The 'setup' function just sets the three pins we are using to be digital outputs.
```void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}```
The 'loop' function initially turns all the LEDs off, by giving the variable 'leds' the value 0. It then calls 'updateShiftRegister' that will send the 'leds' pattern to the shift register so that all the LEDs turn off. We will deal with how 'updateShiftRegister' works later.

The loop function pauses for half a second and then begins to count from 0 to 7 using the 'for' loop and the variable 'i'. Each time, it uses the Arduino function 'bitSet' to set the bit that controls that LED in the variable 'leds'. It then also calls 'updateShiftRegister' so that the leds update to reflect what is in the variable 'leds'.

There is then a half second delay before 'i' is incremented and the next LED is lit.
```void loop()
{
leds = 0;
delay(500);
for (int i = 0; i < 8; i++)
{
bitSet(leds, i);
delay(500);
}
}```
The function 'updateShiftRegister', first of all sets the latchPin to low, then calls the Arduino function 'shiftOut' before putting the 'latchPin' high again. This takes four parameters, the first two are the pins to use for Data and Clock respectively.

The third parameter specifies which end of the data you want to start at. We are going to start with the right most bit, which is referred to as the 'Least Significant Bit' (LSB).

The last parameter is the actual data to be shifted into the shift register, which in this case is 'leds'.
```void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}```
If you wanted to turn one of the LEDs off rather than on, you would call a similar Arduino function  (bitClear) on the 'leds' variable. This will set that bit of 'leds' to be 0 and you would then just need to follow it with a call to 'updateShiftRegister' to update the actual LEDs.

This guide was first published on Dec 03, 2012. It was last updated on Dec 03, 2012.
{"ft_lang_label":"__label__en","ft_lang_prob":0.60871583,"math_prob":0.91673106,"size":1742,"snap":"2022-05-2022-21","text_gpt3_token_len":474,"char_repetition_ratio":0.136939,"word_repetition_ratio":0.35460994,"special_character_ratio":0.28760046,"punctuation_ratio":0.19822486,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96699834,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-24T10:03:35Z\",\"WARC-Record-ID\":\"<urn:uuid:1a298e0e-756e-42e9-b0e5-d266458c7f62>\",\"Content-Length\":\"63637\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:990d346f-6f41-46a7-9986-f0436fb8c900>\",\"WARC-Concurrent-To\":\"<urn:uuid:5e636b39-b277-4952-9cb7-3125197a80cc>\",\"WARC-IP-Address\":\"104.20.38.240\",\"WARC-Target-URI\":\"https://learn.adafruit.com/adafruit-arduino-lesson-4-eight-leds/arduino-code\",\"WARC-Payload-Digest\":\"sha1:5T3AZ3AJ2IH24MIAIUX6DA4XENTVRZLO\",\"WARC-Block-Digest\":\"sha1:OB44BUFCJ7SB2XAAPDLU36D4CB433ALY\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662570051.62_warc_CC-MAIN-20220524075341-20220524105341-00029.warc.gz\"}"}