Dave K
Published © Apache-2.0

Creating a Seven Segment Display Driver for the Raspberry Pi

Create a seven segment display driver for the Raspberry Pi that can then be included in other projects.

IntermediateFull instructions provided4 hours2,697
Creating a Seven Segment Display Driver for the Raspberry Pi

Things used in this project

Hardware components

Raspberry Pi 2 Model B
Raspberry Pi 2 Model B
×1
Seven Segment Display
×1
Resistor 221 ohm
Resistor 221 ohm
×8

Story

Read more

Schematics

SSD Fritz

Basic Circuit Diagram

Code

Code snippet #1

Plain text
public Display(int segA, int segB, int segC, int segD, int segE, int segF, int segG, params int[] displayPins)
        {
            this.Displays = new GpioPin[displayPins.Length];

            for (int i = 0; i < displayPins.Length; i++)
            {
                GpioPin pin = GpioController.GetDefault().OpenPin(displayPins[i]);
                pin.Write(GpioPinValue.High);
                pin.SetDriveMode(GpioPinDriveMode.Output);
                this.Displays[i] = pin;
            }

            this.SetupOutputPin(ref this.PinSegA, segA);
            this.SetupOutputPin(ref this.PinSegB, segB);
            this.SetupOutputPin(ref this.PinSegC, segC);
            this.SetupOutputPin(ref this.PinSegD, segD);
            this.SetupOutputPin(ref this.PinSegE, segE);
            this.SetupOutputPin(ref this.PinSegF, segF);
            this.SetupOutputPin(ref this.PinSegG, segG);

            this.cts = new CancellationTokenSource();
            this.token = new CancellationToken();
        }
        
        private void SetupOutputPin(ref GpioPin pin, int pinNo)
        {
            pin = GpioController.GetDefault().OpenPin(pinNo);
            pin.Write(GpioPinValue.High);
            pin.SetDriveMode(GpioPinDriveMode.Output);
        }

Code snippet #2

Plain text
public void DisplayNumber(int number, bool displayLeadingZero = true)
        {
            this.displayNo = number;
            this.displayLeadingZero = displayLeadingZero;

            if (this.displayNo < 0)
            {
                throw new ArgumentOutOfRangeException("Number cannot be negative");
            }

            int checkMax = 1;
            for(int i = 0; i < this.DisplayDigits.Length; i++)
            {
                checkMax = checkMax * 10;
            }

            if(number >= checkMax)
            {
                throw new ArgumentException("Cannot display numbers greater than " + (checkMax - 1).ToString());
            }

            if (this.displayNo == 0)
            {
                this.Blank();
                if(this.DisplayDigits.Length > 0)
                {
                    this.DisplayDigits[0] = 0;
                }
            }
            else
            {
                List<int> listOfInts = new List<int>();
                while (this.displayNo > 0)
                {
                    listOfInts.Add(this.displayNo % 10);
                    this.displayNo = this.displayNo / 10;
                }

                if (displayLeadingZero)
                {
                    while (listOfInts.Count < this.Displays.Length)
                    {
                        listOfInts.Add(0);
                    }
                }
                else
                {
                    while (listOfInts.Count < this.Displays.Length)
                    {
                        listOfInts.Add(10);
                    }
                }                
                this.DisplayDigits = listOfInts.ToArray();
            }

Code snippet #3

Plain text
private void Start()
        {
            if (running)
            {
                return;
            }

            running = true;

            Task.Factory.StartNew(() =>
            {
                while (!this.cts.IsCancellationRequested)
                {
                    if (this.DisplayDigits == null)
                    {
                        this.Blank();
                    }

                    int[] arrDigs = this.DisplayDigits;

                    for (int i = 0; i < arrDigs.Length; i++)
                    {
                        this.SetDisplay(this.Displays[i], arrDigs[i]);
                    }
                }
            }, token);
        }

Code snippet #4

Plain text
private void SetDisplay(GpioPin displayPin, int value)
        {
            this.ClearDisplay();

            switch (value)
            {
                case 0:
                    this.SetHigh(new GpioPin[] { this.PinSegA, this.PinSegB, this.PinSegC, this.PinSegD, this.PinSegE, this.PinSegF });
                    this.SetLow(new GpioPin[] { this.PinSegG });
                    break;
                case 1:
                    this.SetHigh(new GpioPin[] { this.PinSegB, this.PinSegC });
                    this.SetLow(new GpioPin[] { this.PinSegA, this.PinSegD, this.PinSegE, this.PinSegF, this.PinSegG });
                    break;
                case 2:
                    this.SetHigh(new GpioPin[] { this.PinSegA, this.PinSegB, this.PinSegD, this.PinSegE, this.PinSegG });
                    this.SetLow(new GpioPin[] { this.PinSegC, this.PinSegF });
                    break;
                case 3:
                    this.SetHigh(new GpioPin[] { this.PinSegA, this.PinSegB, this.PinSegC, this.PinSegD, this.PinSegG });
                    this.SetLow(new GpioPin[] { this.PinSegE, this.PinSegF });
                    break;
                case 4:
                    this.SetHigh(new GpioPin[] { this.PinSegB, this.PinSegC, this.PinSegF, this.PinSegG });
                    this.SetLow(new GpioPin[] { this.PinSegA, this.PinSegD, this.PinSegE });
                    break;
                case 5:
                    this.SetHigh(new GpioPin[] { this.PinSegA, this.PinSegC, this.PinSegD, this.PinSegF, this.PinSegG });
                    this.SetLow(new GpioPin[] { this.PinSegB, this.PinSegE });
                    break;
                case 6:
                    this.SetHigh(new GpioPin[] { this.PinSegA, this.PinSegC, this.PinSegD, this.PinSegE, this.PinSegF, this.PinSegG });
                    this.SetLow(new GpioPin[] { this.PinSegB });
                    break;
                case 7:
                    this.SetHigh(new GpioPin[] { this.PinSegA, this.PinSegB, this.PinSegC });
                    this.SetLow(new GpioPin[] { this.PinSegD, this.PinSegE, this.PinSegF, this.PinSegG });
                    break;
                case 8:
                    this.SetHigh(new GpioPin[] { this.PinSegA, this.PinSegB, this.PinSegC, this.PinSegD, this.PinSegE, this.PinSegF, this.PinSegG });
                    break;
                case 9:
                    this.SetHigh(new GpioPin[] { this.PinSegA, this.PinSegB, this.PinSegC, this.PinSegD, this.PinSegF, this.PinSegG });
                    this.SetLow(new GpioPin[] { this.PinSegE });
                    break;
                case 10:  // Clear Display
                    this.SetLow(new GpioPin[] { this.PinSegA, this.PinSegB, this.PinSegC, this.PinSegD, this.PinSegE, this.PinSegF, this.PinSegG });
                    break;
                default:
                    this.SetLow(new GpioPin[] { this.PinSegA, this.PinSegB, this.PinSegC, this.PinSegD, this.PinSegE, this.PinSegF, this.PinSegG });
                    break;
            }

            this.SetLow(new GpioPin[] { displayPin });
        }

Github

https://github.com/davek17/PiSSD.Net

Credits

Dave K

Dave K

2 projects • 4 followers
Experenced C# [.net] Software Developer

Comments