Search Code

Lesson 22/31: Using Seven Segment Display with Arduino and Electronic Dice | SunFounder Robojax

Lesson 22/31: Using Seven Segment Display with Arduino and Electronic Dice | SunFounder Robojax

In this lesson, we explore how to control a seven-segment display with an Arduino. Instead of using a dozen or more I/O pins to drive each LED segment directly, we employ a clever trick: a shift register. This small, inexpensive chip lets us control all seven segments (plus the decimal point) using just three wires from the Arduino. This is a foundational technique in electronics that allows you to expand the number of outputs available on your microcontroller, making it possible to build more complex projects without upgrading your board.

wiring on breadoard from video

Beyond the basic counter, we will build an electronic dice. This project not only reinforces the display control but also introduces user input via a push button and the concept of interrupts, creating a responsive and interactive device. This guide is based on a lesson from the Robojax Arduino Course, which uses the SunFounder 3-in-1 Arduino kit.

Here are a few practical ideas you can build with this knowledge:

  • Create a simple scoreboard for games like table tennis or basketball.
  • Build a digital timer for board games or a kitchen countdown timer.
  • Make a random number generator for games or decision-making.
  • Construct a basic digital thermometer by adding a temperature sensor.

Hardware and Components

Before we start, let's gather the necessary components. The core of this project is the 74HC595 shift register, which is the key to minimizing the number of pins needed. Instead of connecting each of the seven segments directly to the Arduino, we connect them to the shift register. The Arduino then sends the data for a digit serially (one bit at a time) to the chip, which then outputs the correct pattern to light up the segments.

  • Arduino Uno (or compatible board)
  • Seven-segment display (model 5161AS, common cathode)
  • 74HC595 shift register IC
  • Push button
  • Resistors (220Ω - 330Ω)
  • Jumper wires
  • Breadboard

The 5161AS display is a common cathode type. This means all the cathodes (the negative sides) of the internal LEDs are connected together to a common ground pin. To light up a segment, you provide a positive voltage to its specific anode pin. The 74HC595 acts as a bridge, taking serial data from the Arduino and outputting the parallel signals needed to drive these anodes.

Wiring Guide

circuit_6.5_segment_schematic
segment_wiring_sundounder_3-in-1-kit

Wiring the shift register and display can seem tricky, but following the diagram step-by-step is crucial. The video (at 04:48) provides a detailed walkthrough of this process. The 74HC595 chip has a notch at one end to identify pin 1. The pins are numbered counter-clockwise from the notch (1-8 on one side, 9-16 on the other).

Here is the wiring summary from the video:

  • Power and Ground: Connect pin 8 and pin 13 of the 74HC595 to the ground rail. Connect pin 10 and pin 16 to the 5V rail.
  • Data Lines: Connect the Arduino's digital pins to the shift register's data pins: Pin 12 (STcp) to chip pin 12, Pin 8 (SHcp) to chip pin 11, and Pin 11 (DS) to chip pin 14.
  • Display to Shift Register: This is the most complex part. The video (at 07:18) details each connection. For example, segment 'a' on the display connects to chip pin 6, segment 'b' to pin 5, and so on. The common cathode pins of the display are connected to ground through a resistor.

For a visual and precise guide, please refer to the official wiring diagram:

The exact pin connections are critical, and it's easy to make a mistake. Take your time and double-check each wire against the diagram. The video is an excellent resource to follow along with.

Dice

Code Explanation

Two code examples are provided in this lesson. The first is a simple counter, and the second builds upon it to create the electronic dice. Both share the same core logic for controlling the display.

Here is the code for the basic counter:

const int STcp = 12;//Pin connected to ST_CP of 74HC595
const int SHcp = 8;//Pin connected to SH_CP of 74HC595 
const int DS = 11; //Pin connected to DS of 74HC595 

int datArray[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

At the top of the code, you can configure the three pins connected to the shift register. STcp, SHcp, and DS are assigned to Arduino pins 12, 8, and 11, respectively. The datArray[] is a crucial part. It's a lookup table containing the hexadecimal values for digits 0 through 9. Each value represents the pattern of segments to light up for that specific digit.

The loop() function runs the counter. It uses a for loop to count from 0 to 9. Inside the loop, the code sends the data for the current number to the shift register. The delay(1000) at the end sets the speed of the counter in milliseconds. Change this value to 100 for a faster count, or 500 for a slower one.

The electronic dice code introduces a new element: user input via a button. The key configuration changes are:

const int buttonPin = 2;
//...
pinMode(buttonPin, INPUT);
attachInterrupt(digitalPinToInterrupt(buttonPin), rollDice, FALLING);

Here, buttonPin is set to digital pin 2. This pin is special because it can be used as an interrupt. An interrupt allows the Arduino to stop what it's doing immediately when the button is pressed, ensuring a fast and reliable response. The FALLING mode triggers the interrupt when the button is pressed (the signal goes from HIGH to LOW).

The rollDice function is called by the interrupt. It toggles the state variable between 0 and 1. When state is 0, the dice is "rolling," and the main loop() will randomly display numbers. When the button is pressed again, state changes to 1, stopping the loop and displaying the final result. The showNumber() function is the same as the counter logic, but it takes a number as an argument to display.

Live Project and Demonstration

Once the code is uploaded, the counter will start running, displaying numbers from 0 to 9 with a one-second delay (in video at 15:04). The video demonstrates changing the delay to make the counter run much faster.

The electronic dice demonstration (in video at 20:15) shows the button in action. When you press it once, the display starts rapidly cycling through numbers. Pressing it again stops the cycling, leaving a random number between 1 and 6 displayed, just like a real die.

Chapters

  • [00:00] Introduction to the Seven-Segment Display Project
  • [01:25] Understanding the Seven-Segment Display
  • [03:51] 5161AS Display Datasheet and Specifications
  • [04:48] Wiring the Display and Shift Register
  • [12:24] Arduino Code for the 0-9 Counter
  • [15:04] Counter Demonstration and Adjusting Speed
  • [16:27] Building the Electronic Dice Project
  • [20:15] Electronic Dice Demonstration and Debouncing

Images

Wiring on breadboard from video
Wiring on breadboard from video
Circuit 6.5 segment schematic
Circuit 6.5 segment schematic
Dice
Dice
Segment wiring for Sunfounder 3-in-1 kit
Segment wiring for Sunfounder 3-in-1 kit
Segments of segment display
Segments of segment display
Seven segment pins - common cathode
Seven segment pins - common cathode
Seven segment pins - DP decimal pin
Seven segment pins - DP decimal pin
Seven segment pins - common cathode
Seven segment pins - common cathode
902-Lesson 23/31: Arduino code Using Seven Segment Display
Language: C++
/*
Lesson 23/31: Using Seven Segment Display 
Get this code and watch video Download and resource page https://robojax.com/RJT605
YouTube video https://www.youtube.com/watch?v=3qoGAYh0lbc

*/
const int STcp = 12;//Pin connected to ST_CP of 74HC595
const int SHcp = 8;//Pin connected to SH_CP of 74HC595 
const int DS = 11; //Pin connected to DS of 74HC595 

int datArray[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

void setup ()
{
  //set pins to output
  pinMode(STcp,OUTPUT);
  pinMode(SHcp,OUTPUT);
  pinMode(DS,OUTPUT);
}
void loop()
{
  for(int num = 0; num <10; num++)
  {
    digitalWrite(STcp,LOW); //ground ST_CP and hold low for as long as you are transmitting
    shiftOut(DS,SHcp,MSBFIRST,datArray[num]);
    digitalWrite(STcp,HIGH); //pull the ST_CPST_CP to save the data
    delay(1000);
  }
}
903-Lesson 23/31: Electronic dice
Language: C++
/*
Lesson 23/31: Electronic Dice
Get this code and watch video Download and resource page https://robojax.com/RJT605
YouTube video https://www.youtube.com/watch?v=3qoGAYh0lbc

*/
const int STcp = 12;//Pin connected to ST_CP of 74HC595
const int SHcp = 8;//Pin connected to SH_CP of 74HC595
const int DS = 11; //Pin connected to DS of 74HC595
const int buttonPin = 2;
int datArray[] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f,  0x6f};
int number = 0;
int state = 0;

void setup ()
{
  //set pins to output
  pinMode(STcp, OUTPUT);
  pinMode(SHcp, OUTPUT);
  pinMode(DS, OUTPUT);
  pinMode(buttonPin, INPUT);

  attachInterrupt(digitalPinToInterrupt(buttonPin), rollDice, FALLING);
}

void loop()
{
  if (state == 0) {
    showNumber((int)random(1, 7));
    delay(50);
  }
}

void showNumber(int num) {
  digitalWrite(STcp, LOW); //ground ST_CP and hold low for as long as you are transmitting
  shiftOut(DS, SHcp, MSBFIRST, datArray[num]);
  //return the latch pin high to signal chip that it
  //no longer needs to listen for information
  digitalWrite(STcp, HIGH); //pull the ST_CPST_CP to save the data
}


void rollDice() {
  state = !state;
}

Resources & references

Files📁

Arduino Libraries (zip)