Sink chaser on an Arduino Uno
An NLSF595, the 595 with open-drain outputs, runs a dot along seven LEDs that hang from 5 V: a 0 bit pulls an output low and lights its LED. The whole build, an Arduino Uno and 15 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Intermediate About 25 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $34. On Mokxi they're free.
What you'll learn
- Open-drain outputs that sink current
- Why the bit pattern has to be inverted
- Driving seven LEDs from three pins
- Wiring LEDs from 5V into the chip, not to ground
- Bouncing a light back and forth
- Difficulty: Intermediate
- Time: About 25 minutes
- Board: Arduino Uno R3
- Parts: 17
How it works
The NLSF595 works like a normal shift register, but its outputs sink current. Each of the seven red LEDs gets 5V through a 220 ohm resistor, and its other leg goes to an output QB to QH. An output lights its LED by pulling it to ground. QA is not used.
That means a stored 0 turns an LED on and a stored 1 turns it off, so the sketch sends the opposite of the pattern it wants to see. One light runs from QB up to QH and then bounces back down to QC, over and over, 90 ms per step.
Parts list
17 parts, plus the jumper wires. They retail for about $34; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × NLSF595 tri-color LED driver
- 7 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 7 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| NLSF595 tri-color LED driver pin GND | Arduino Uno R3 pin GND |
| NLSF595 tri-color LED driver pin OE | Arduino Uno R3 pin GND |
| NLSF595 tri-color LED driver pin RCK | Arduino Uno R3 pin 6 |
| NLSF595 tri-color LED driver pin SCK | Arduino Uno R3 pin 5 |
| NLSF595 tri-color LED driver pin SI | Arduino Uno R3 pin 4 |
| NLSF595 tri-color LED driver pin SCLR | Arduino Uno R3 pin 5V |
| NLSF595 tri-color LED driver pin VCC | Arduino Uno R3 pin 5V |
| Resistor, 220 Ω (1) pin 1 | Arduino Uno R3 pin 5V |
| Resistor, 220 Ω (2) pin 1 | Arduino Uno R3 pin 5V |
| Resistor, 220 Ω (3) pin 1 | Arduino Uno R3 pin 5V |
| Resistor, 220 Ω (4) pin 1 | Arduino Uno R3 pin 5V |
| Resistor, 220 Ω (5) pin 1 | Arduino Uno R3 pin 5V |
| Resistor, 220 Ω (6) pin 1 | Arduino Uno R3 pin 5V |
| Resistor, 220 Ω (7) pin 1 | Arduino Uno R3 pin 5V |
| LED, red (1) pin C | NLSF595 tri-color LED driver pin QB |
| LED, red (2) pin C | NLSF595 tri-color LED driver pin QC |
| LED, red (3) pin C | NLSF595 tri-color LED driver pin QD |
| LED, red (4) pin C | NLSF595 tri-color LED driver pin QE |
| LED, red (5) pin C | NLSF595 tri-color LED driver pin QF |
| LED, red (6) pin C | NLSF595 tri-color LED driver pin QG |
| LED, red (7) pin C | NLSF595 tri-color LED driver pin QH |
| LED, red (1) pin A | Resistor, 220 Ω (1) pin 2 |
| LED, red (2) pin A | Resistor, 220 Ω (2) pin 2 |
| LED, red (3) pin A | Resistor, 220 Ω (3) pin 2 |
| LED, red (4) pin A | Resistor, 220 Ω (4) pin 2 |
| LED, red (5) pin A | Resistor, 220 Ω (5) pin 2 |
| LED, red (6) pin A | Resistor, 220 Ω (6) pin 2 |
| LED, red (7) pin A | Resistor, 220 Ω (7) pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Sink chaser: seven LEDs on an NLSF595, the 595 whose outputs sink current.
//
// Wiring, driver to board:
// SI (pin 14, serial data) -> pin 4
// SCK (pin 11, shift clock) -> pin 5
// RCK (pin 12, register clock) -> pin 6
// SCLR (pin 10) to 5 V, OE (pin 13) to GND
// each LED from 5 V through 220 ohm, its cathode to QB..QH (QA is free)
//
// The NLSF595's datasheet calls it functionally similar to a 74VHC595, and
// shiftOut() and a latch pulse drive it the same way. The difference is the
// outputs: they are open drain. A stored 0 pulls the output to ground and
// lights the LED wired to it; a stored 1 lets go and the LED goes dark. So
// the sketch sends the inverse of the pattern it wants to see.
//
// This is how the chip drives common-anode RGB LEDs: the anode to 5 V and
// each color's cathode to an output.
const int DATA_PIN = 4; // SI
const int SHIFT_PIN = 5; // SCK
const int LATCH_PIN = 6; // RCK
static void show(uint8_t lit) {
// Invert: a 0 bit is a lit LED on an open-drain output.
shiftOut(DATA_PIN, SHIFT_PIN, MSBFIRST, (uint8_t)~lit);
digitalWrite(LATCH_PIN, HIGH);
digitalWrite(LATCH_PIN, LOW);
}
void setup() {
Serial.begin(9600);
pinMode(DATA_PIN, OUTPUT);
pinMode(SHIFT_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
show(0);
Serial.println("NLSF595 ready");
}
void loop() {
// Bit n is output Qn: bit 1 is QB, bit 7 is QH.
for (int i = 1; i < 8; i++) {
show((uint8_t)(1 << i));
delay(90);
}
for (int i = 6; i > 1; i--) {
show((uint8_t)(1 << i));
delay(90);
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
show sketch.ino
Takes the pattern you want lit and sends its inverse ~lit with shiftOut. Then it raises and lowers the latch pin so the outputs update. The inversion is needed because a 0 bit pulls the output to ground and lights the LED.
static void show(uint8_t lit) {
// Invert: a 0 bit is a lit LED on an open-drain output.
shiftOut(DATA_PIN, SHIFT_PIN, MSBFIRST, (uint8_t)~lit);
digitalWrite(LATCH_PIN, HIGH);
digitalWrite(LATCH_PIN, LOW);
}setup sketch.ino
Opens serial at 9600, sets the data, shift and latch pins as outputs, and calls show(0) so every LED starts dark. Then it prints a ready message.
void setup() {
Serial.begin(9600);
pinMode(DATA_PIN, OUTPUT);
pinMode(SHIFT_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
show(0);
Serial.println("NLSF595 ready");
}loop sketch.ino
First it counts i from 1 to 7, lighting one bit at a time with 1 << i, so QB to QH light in turn. Then it counts back from 6 down to 2. Each step waits 90 ms. Leaving out 1 and 7 on the way back stops them from repeating.
void loop() {
// Bit n is output Qn: bit 1 is QB, bit 7 is QH.
for (int i = 1; i < 8; i++) {
show((uint8_t)(1 << i));
delay(90);
}
for (int i = 6; i > 1; i--) {
show((uint8_t)(1 << i));
delay(90);
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Every LED is on except one, or the pattern looks reversed. This project
The outputs sink current, so the pattern must be inverted. Keep the~inshow. - The LEDs never light. This project
Check each LED's anode side goes through a resistor to 5V and its cathode to QB..QH. Also tie OE to GND and SCLR to 5V. - LEDs are lit when they should be off, or the colors are backwards. NLSF595 tri-color LED driver
The outputs sink current, so they pull LOW to light a common anode LED. The anode goes to VCC through the supply, and each cathode goes to an output with a resistor. A 1 means off. - The LEDs look dim or burn out. NLSF595 tri-color LED driver
Each output sinks only a limited current, so use a resistor for each color, such as 220 ohm to 330 ohm. Red, green and blue need different values for balance, because their forward voltages differ. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The LED does not light, or it lights once and then dies. LED
LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin. - A blue or white LED stays dark on 3.3 V. LED
Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.