Type a digit: serial to a 74HC595 display (pins 11, 8 and 12), on an Arduino Uno
Type a digit from 0 to 9 into the serial monitor and a seven-segment display on a 74HC595 (DS 11, SH_CP 8, ST_CP 12) shows it. The whole build, an Arduino Uno and 3 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 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $33. On Mokxi they're free.
What you'll learn
- Reading typed characters from the serial monitor
- Turning a character into a number by subtracting '0'
- Shift register data, clock and latch pins
- Byte patterns for seven-segment digits
- Wrapping repeated steps in a helper function
- Difficulty: Intermediate
- Time: About 20 minutes
- Board: Arduino Uno R3
- Parts: 5
How it works
A 74HC595 shift register drives a one-digit seven-segment display from three pins: data on 11, clock on 8, latch on 12. You type a digit into the serial monitor, and the display shows it.
The sketch keeps a table of ten bytes, one per digit, with one bit per segment. When a character from 0 to 9 arrives, it subtracts the character '0' to get a number, looks up the byte and sends it to the register. Other characters are ignored. At startup it shows 0.
Parts list
5 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × 74HC595 shift registerIn LED Cube Kit.
- 1 × Seven segment display
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
Kits that can build it
These 8 starter kits have every part in the circuit. Each kit's page lists what else it can build.
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 |
|---|---|
| 74HC595 shift register pin GND | Arduino Uno R3 pin GND |
| 74HC595 shift register pin OE | Arduino Uno R3 pin GND |
| Resistor, 220 Ω pin 2 | Arduino Uno R3 pin GND |
| 74HC595 shift register pin STCP | Arduino Uno R3 pin 12 |
| 74HC595 shift register pin DS | Arduino Uno R3 pin 11 |
| 74HC595 shift register pin SHCP | Arduino Uno R3 pin 8 |
| 74HC595 shift register pin MR | Arduino Uno R3 pin 5V |
| 74HC595 shift register pin VCC | Arduino Uno R3 pin 5V |
| Seven segment display pin B | 74HC595 shift register pin Q1 |
| Seven segment display pin C | 74HC595 shift register pin Q2 |
| Seven segment display pin D | 74HC595 shift register pin Q3 |
| Seven segment display pin E | 74HC595 shift register pin Q4 |
| Seven segment display pin F | 74HC595 shift register pin Q5 |
| Seven segment display pin G | 74HC595 shift register pin Q6 |
| Seven segment display pin A | 74HC595 shift register pin Q0 |
| Resistor, 220 Ω pin 1 | Seven segment display pin COM1 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Show a number: type a digit and a 74HC595 puts it on the display.
//
// 74HC595 DS -> pin 11 SH_CP -> pin 8 ST_CP -> pin 12
// VCC and MR -> 5V OE and GND -> GND
// Q0..Q6 -> segments a..g (one 220 ohm on the display's common leg)
//
// Each digit is a byte with a bit per segment, bit 0 for a up to bit 6 for g.
// Serial.read() returns the character typed; subtracting '0' turns the
// character '7' into the number 7, which picks the pattern.
const int ST_CP = 12;
const int SH_CP = 8;
const int DS = 11;
const byte DIGITS[10] = {63, 6, 91, 79, 102, 109, 125, 7, 127, 111};
void write595(byte value) {
digitalWrite(ST_CP, LOW);
shiftOut(DS, SH_CP, MSBFIRST, value);
digitalWrite(ST_CP, HIGH);
}
void setup() {
pinMode(ST_CP, OUTPUT);
pinMode(SH_CP, OUTPUT);
pinMode(DS, OUTPUT);
write595(DIGITS[0]);
Serial.begin(9600);
Serial.println("Mokxi Uno: type a digit from 0 to 9");
}
void loop() {
if (Serial.available() > 0) {
char c = Serial.read();
if (c >= '0' && c <= '9') {
write595(DIGITS[c - '0']);
Serial.print("showing ");
Serial.println(c);
}
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
write595 sketch.ino
Sends one byte to the shift register. It sets ST_CP low, uses shiftOut to clock the byte in on DS, then sets ST_CP high so the outputs update together. The other code just calls this.
void write595(byte value) {
digitalWrite(ST_CP, LOW);
shiftOut(DS, SH_CP, MSBFIRST, value);
digitalWrite(ST_CP, HIGH);
}setup sketch.ino
Sets the three pins as outputs and shows DIGITS[0] right away so the display starts on 0. Then it opens the serial port and asks you to type a digit from 0 to 9.
void setup() {
pinMode(ST_CP, OUTPUT);
pinMode(SH_CP, OUTPUT);
pinMode(DS, OUTPUT);
write595(DIGITS[0]);
Serial.begin(9600);
Serial.println("Mokxi Uno: type a digit from 0 to 9");
}loop sketch.ino
Checks whether a character is waiting. If it is between '0' and '9', it uses c - '0' as the index into DIGITS, writes that pattern and prints which digit it is showing.
void loop() {
if (Serial.available() > 0) {
char c = Serial.read();
if (c >= '0' && c <= '9') {
write595(DIGITS[c - '0']);
Serial.print("showing ");
Serial.println(c);
}
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Typing does nothing This project
Make sure the serial monitor is open at 9600 baud and you send the digit, not a letter. - A digit looks wrong This project
Check the wires from Q0 to Q6 to segments a to g. One swapped wire changes the shape. - Outputs show garbage, or the register never clears. 74HC595 shift register
Tie OE low and SRCLR (pin 10) high. Left floating, these pins make the chip behave randomly. Then pulse the latch pin after shifting the byte. - The bits come out in the wrong order, or LEDs are dim. 74HC595 shift register
Choose MSBFIRST or LSBFIRST in shiftOut to flip the order. Give each LED its own resistor, since each output can source only about 6 mA safely. - Segments stay dark, or light when they should be off. Seven segment display
Check whether the display is common cathode or common anode. Common cathode: common pin to GND, drive segments HIGH. Common anode: common pin to VCC, drive segments LOW. - One segment is very bright or the LEDs burn out. Seven segment display
Each segment is an LED, so it needs its own resistor, about 220 ohm to 330 ohm on 5 V. One resistor on the common pin makes brightness change with the number of lit segments. - 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.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.