Test a Digit

Plug the digit into the JST SM 3-conductor plug connected to pin 4 on your ScrewShield.

Make sure your 5V 10A power supply is plugged into AC power, and then plug it into the barrel jack on your PermaProto board. 

Now, plug the Arduino M0 Pro's Programming port into your computer via a USB cable.

Each digit is will draw roughly 1.5A of current at 5V when using the colors in this sketch. Remember, a pure R, G, or B color requires 1/3 the current compared to running all three colors at the same time at full power.

You'll need to have the NeoPixel library installed for your Arduino IDE -- you can do this from the Arduino Libary Manager by going to Arduino Sketch > Include Library > Manage Libraries... and searching for NeoPixel. In case you don't see it, be sure to add the board manager URL for Adafruit libraries as shown here.

Check out the excellent NeoPixel Überguide for more info on setup in case you have not used NeoPixels before.

Open the "simple" sketch, which is located at File > examples > Adafruit NeoPixel > simple

Change the PIN to pin 4 and the number of NeoPixels (NUMPIXELS) to 56 as seen here:

Download: file
// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN            4

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      56

Now, upload the code to the board.

The display should light up!

Now, you're ready to use the digit as a proper seven segment display. Normally, a seven segment display is made from seven individual LED elements that can be individually addressed in code. We'll use code to treat our single, physical LED strip like seven logical segments. In the code, I created the segLight() function to call "segment" -- which are really just pre-defined set of eight LEDs, 0-7, 16-23, 24-31 and so on. These segLight() calls are, in turn, called by the digitWrite() function, which is a set of predefined characters. 

For example, to display a "1" on the first digit panel, in the color green, we'll use the command:

digitWrite(0, 1, 2);

Where the first argument is the digit panel 0, the second argument is the character value "1", and the third value is the color green.

The digitWrite() command is really just a set of pre-defined segLight() commands, in this case:

segLight(digit,1,0);
segLight(digit,2,0);
segLight(digit,3,col);
segLight(digit,4,col);
segLight(digit,5,0);
segLight(digit,6,0);
segLight(digit,7,0);
segLight(digit,8,col;

Each of those segLight() calls has an argument for digit panel number (in this case 0), logical segment set (0-7, 8-15, and so on), and color value where 0 is off and "col" is the 4 for green as specified by the digitWrite() call.

Copy the test code below, create a new sketch in Arduino, paste the code, and save. This program will cycle the numbers 0-9, each in a different color.

 

Download: file
#include <Adafruit_NeoPixel.h>

////////////////////////////////////////////////////////////////////////////////
#define NUMPIXELS 57 // Number of LEDs in a strip (some are actually 56, 
//some 57 due to extra colon/decimal points)
#define DATAPIN0 4 //digit 0 NeoPixel 60 strip far RIGHT
#define DATAPIN1 5 //digit 1 (plus lower colon dot)
#define DATAPIN2 6 //digit 2 (plus upper colon dot)
#define DATAPIN3 7 //digit 3 (plus decimal dot)
#define DATAPIN4 8 //digit 4 far LEFT

Adafruit_NeoPixel strip[] = { //here is the variable for the multiple strips
  Adafruit_NeoPixel(NUMPIXELS, DATAPIN0, NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(NUMPIXELS, DATAPIN1, NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(NUMPIXELS, DATAPIN2, NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(NUMPIXELS, DATAPIN3, NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(NUMPIXELS, DATAPIN4, NEO_GRB + NEO_KHZ800)
};
const int bright = 200; //adjust brightness for all pixels 0-255 range,
// 32 being pretty dim, 255 being full brightness
////////////////////////////////////////////////////////////////////////////////

void setup() {
////////////////////////////////////////////////////////////////////////////////
  delay(500); //pause a moment to let capacitors on board settle
  //NeoPixel array setup
  for(int s=0;s<5;s++){
    strip[s].begin(); // Initialize pins for output
    strip[s].setBrightness(bright);
    strip[s].show();
    delay(200);
  }
  
  //flash an eight
  for(int t=0;t<5;t++){
    digitWrite(t,8,0); //clear it
    strip[t].show();
    digitWrite(t,8,2); //display it
    strip[t].show();
  }
  delay(1500);
//END void setup()
////////////////////////////////////////////////////////////////////////////////
}

////////////////////////////////////////////////////////////////////////////////
void loop() { 
  //this uses digitWrite() command to run through all numbers with one second 
  //timing on each of the five digit panels in the display simultaneously
  for(int i=0;i<10;i++){
    for(int j=0;j<5;j++){
      digitWrite(j, i, (i+1));
      strip[j].show();
    }
    delay(1000); //one second intervals
    for(int j=0;j<5;j++){ 
      digitWrite(j, i, 0); //clears the digit between refreshes
      strip[j].show();
    }

  }
}
//END void loop()
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
void digitWrite(int digit, int val, int col){
  //use this to light up a digit
  //digit is which digit panel one (right to left, 0 indexed)
  //val is the character value to set on the digit
  //col is the predefined color to use, R,G,B or W
  //example: 
  //        digitWrite(0, 4, 2); 
  //would set the first digit
  //on the right to a "4" in green.

/*
// Letters are the standard segment naming, as seen from the front,
// numbers are based upon the wiring sequence

          A 2     
     ----------
    |          |
    |          |
F 1 |          | B 3
    |          |
    |     G 7  |
     ----------
    |          |
    |          |
E 6 |          | C 4
    |          |
    |     D 5  |
     ----------    dp 8

*/
//these are the digit panel character value definitions, 
//if color argument is a 0, the segment is off
  if (val==0){
    //segments F,A,B,C,D,E,G, dp
    segLight(digit,1,col);
    segLight(digit,2,col);
    segLight(digit,3,col);
    segLight(digit,4,col);
    segLight(digit,5,col);
    segLight(digit,6,col);
    segLight(digit,7,0);
    segLight(digit,8,col);
  }
  if (val==1){
    segLight(digit,1,0);
    segLight(digit,2,0);
    segLight(digit,3,col);
    segLight(digit,4,col);
    segLight(digit,5,0);
    segLight(digit,6,0);
    segLight(digit,7,0);
    segLight(digit,8,col);
  }
  if (val==2){
    segLight(digit,1,0);
    segLight(digit,2,col);
    segLight(digit,3,col);
    segLight(digit,4,0);
    segLight(digit,5,col);
    segLight(digit,6,col);
    segLight(digit,7,col);
    segLight(digit,8,col);
  }
  if (val==3){
    segLight(digit,1,0);
    segLight(digit,2,col);
    segLight(digit,3,col);
    segLight(digit,4,col);
    segLight(digit,5,col);
    segLight(digit,6,0);
    segLight(digit,7,col);
    segLight(digit,8,col);
  }
  if (val==4){
    segLight(digit,1,col);
    segLight(digit,2,0);
    segLight(digit,3,col);
    segLight(digit,4,col);
    segLight(digit,5,0);
    segLight(digit,6,0);
    segLight(digit,7,col);
    segLight(digit,8,col);
  }
  if (val==5){
    segLight(digit,1,col);
    segLight(digit,2,col);
    segLight(digit,3,0);
    segLight(digit,4,col);
    segLight(digit,5,col);
    segLight(digit,6,0);
    segLight(digit,7,col);
    segLight(digit,8,col);
  }
  if (val==6){
    segLight(digit,1,col);
    segLight(digit,2,col);
    segLight(digit,3,0);
    segLight(digit,4,col);
    segLight(digit,5,col);
    segLight(digit,6,col);
    segLight(digit,7,col);
    segLight(digit,8,col);
  }          
  if (val==7){
    segLight(digit,1,0);
    segLight(digit,2,col);
    segLight(digit,3,col);
    segLight(digit,4,col);
    segLight(digit,5,0);
    segLight(digit,6,0);
    segLight(digit,7,0);
    segLight(digit,8,col);
  }
  if (val==8){
    segLight(digit,1,col);
    segLight(digit,2,col);
    segLight(digit,3,col);
    segLight(digit,4,col);
    segLight(digit,5,col);
    segLight(digit,6,col);
    segLight(digit,7,col);
    segLight(digit,8,col);
  }
  if (val==9){
    segLight(digit,1,col);
    segLight(digit,2,col);
    segLight(digit,3,col);
    segLight(digit,4,col);
    segLight(digit,5,col);
    segLight(digit,6,0);
    segLight(digit,7,col);
    segLight(digit,8,col);
  }    
}
//END void digitWrite()
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
void segLight(char digit, int seg, int col){ 
  //use this to light up a segment
  //digit picks which neopixel strip
  //seg calls a segment
  //col is color
  int color[3];

  //color sets
    if (col==0){ //off
      color[0]={0};
      color[1]={0};
      color[2]={0};
    }
    if (col==1){ //red
      color[0]={255};
      color[1]={0};
      color[2]={0};
    }
    if (col==2){ //green
      color[0]={0};
      color[1]={255};
      color[2]={0};
    }
    if (col==3){ //blue
      color[0]={0};
      color[1]={0};
      color[2]={255};
    }
    if (col==4){ //white -- careful with this one, 3x power consumption
      //if 255 is used
      color[0]={100};
      color[1]={100};
      color[2]={100};
    }

      if (col==5){ //yellow
      color[0]={220};
      color[1]={150};
      color[2]={0};
    }
        if (col==6){ //
      color[0]={0};
      color[1]={150};
      color[2]={150};
    }
        if (col==7){ //
      color[0]={150};
      color[1]={0};
      color[2]={150};
    }
        if (col==8){ //
      color[0]={220};
      color[1]={50};
      color[2]={50};
    }
        if (col==9){ //
      color[0]={200};
      color[1]={100};
      color[2]={0};
    }
        if (col==10){ // 
      color[0]={0};
      color[1]={50};
      color[2]={200};
    }

  //sets are 0-7, 8-15, 16-23, 24-31, 32-39, 40-47, 48-55, 56
  //seg F
  if(seg==1){
    //light first 8
    for(int i=0; i<8; i++){
      strip[digit].setPixelColor(i,color[0],color[1],color[2]);
    }  
  }
  //seg A
  if(seg==2){
      //light second 8
      for(int i=8; i<16; i++){
      strip[digit].setPixelColor(i,color[0],color[1],color[2]);
      } 
  }
  //seg B
  if(seg==3){
      for(int i=16; i<24; i++){
      strip[digit].setPixelColor(i,color[0],color[1],color[2]);
      }   
  }
  //seg C
  if(seg==4){
      for(int i=24; i<33; i++){
      strip[digit].setPixelColor(i,color[0],color[1],color[2]);
      }   
  }
  //seg D
  if(seg==5){
      for(int i=32; i<41; i++){
      strip[digit].setPixelColor(i,color[0],color[1],color[2]);
      }   
  }
  //seg E
  if(seg==6){
      for(int i=40; i<49; i++){
      strip[digit].setPixelColor(i,color[0],color[1],color[2]);
      }   
  }
  //seg G
  if(seg==7){
      for(int i=48; i<57; i++){
      strip[digit].setPixelColor(i,color[0],color[1],color[2]);
      }   
  }
  //seg dp
  if(seg==8){
      for(int i=56; i<57; i++){
      strip[digit].setPixelColor(i,color[0],color[1],color[2]);
      }   
  }
}
//END void segLight()
////////////////////////////////////////////////////////////////////////////////

Then, select Tools > Board:"Arduino M0 Pro (Programming Port)" in the Arduino IDE, and choose your port from Tools > Port...

Then, click Sketch > Upload and after a couple of seconds your display will beging counting up in seconds!

This guide was first published on Mar 15, 2017. It was last updated on Mar 15, 2017. This page (Test a Digit) was last updated on Aug 17, 2019.