Introduction: Counter Using the MAX7221

Creating a counter that increments when a button is pressed using the MAX7221.

Step 1: Parts

MAX7221 or MAX7219

Switch

4 7-Segment Display (common cathode)

2 Resistors: 10k and 9.53k ( I used a 9.1k and it still works)

Arduino

Step 2: Building the Circuit

I only wrote down the pins that I used, you can find the complete pin layout on the datasheet or I attached it. Make sure to look up your 7-Segment Display because it will probably differ from mine but the procedure is the same.

1. Connect Digits 0 - 3 on the MAX7221 to Digits 1 - 4 on the display. The MAX digit 0 connects to the displays digit 1 etc. Displays are just unconventional and start at 1 instead of 0.

2. Connect the Segments A - G on the MAX7221 to Segments A - G on the display.

3. Supply power and ground to the MAX. VCC goes to pin 19. The 10k resistor is also connected to pin 19 and 18. Ground goes to pins 4 and 9.

4. The MAX7221 communicates with the Arduino via SPI (Serial Peripheral Interface). In this case only 3 wires are needed because I'm not using MISO (Master In Slave Out). On the MAX7221 connect pin 1 (Din) to the Arduino, in my case I used pin 12. This is your MOSI (Master Out Slave In) or the data. Pin 12 on the MAX is CS (Chip Select Input) and is how the data is loaded into the Shift Register, I connected this to pin 9 on the Arduino. The last connection is CLK which is pin 13 on the MAX, I connected this to pin 10 on the Arduino.

Make Sure to supply power and ground through the Arduino using 5 volts. I ran into the problem where my Arduino wasn't giving my board enough power.

This is for the MAX7221 Datasheet. Also where the pictures came from.

https://www.mouser.com/datasheet/2/256/max7219-max...

Step 3: The Switch

I wired the switch from the Schematic above. Found at https://www.arduino.cc/en/tutorial/button

I used pin 8 on the Arduino to control the switch, and a 10k resistor.

Step 4: Code

I used the LedControl.h library found on GitHub, and their basic code to turn on the MAX7221. I then modified the code to count from 0 - 9999 when the button is being pressed.

#include "LedControl.h"

int button = 8;

LedControl lc=LedControl(12,10,9,1);

void setup() {

pinMode(button, INPUT);

lc.shutdown(0,false); // MAX7221 is in power saving mode, so we have to wake it up

lc.setIntensity(0,15); // Setting brightness, max 15

lc.clearDisplay(0); // clearing display

}

void loop(){

int state = digitalRead(button);

int i = 0;

int j = 0;

int k = 0;

int l = 0;

while(1){

state = digitalRead(button);

while(state == 1){

state = digitalRead(button);

lc.setDigit(0,3,i, false);

i++;

delay(100); // you can mess around with this

if(i == 10){

i = 0;

j++;

if(j == 10){

j = 0;

k++;

if(k == 10){

k = 0;

l++;

if(l == 10){

while(1){

lc.setRow(0,0,0x3E); // when display reached 9999 it'll display U -- 1

lc.setRow(0,1, 0x1); // In Hexi decimal value

lc.setRow(0,2, 0x1);

lc.setDigit(0,3,1,false);

}

}

lc.setDigit(0,0,l,false);

}

lc.setDigit(0,1,k,false);

}

lc.setDigit(0,2,j,false);

}

}

}

}