digitalWrite() not working with 7 segment display

Hello people! I want to make an animation with a 7 segment display.
I've a problem when I want to turn on a display, digitalWrite() method doesn't work.
Here's my circuit design:

And here's my code:

// variables
byte pins[] = { 1, 2, 3, 4, 5, 6, 7 };

const byte Segments[] =
  0b11000000, // 0
  0b11001111, // 1
  0b10100100, // 2
  0b10000110, // 3
  0b10001011, // 4
  0b10010010, // 5
  0b10010000, // 6
  0b11000111, // 7
  0b10000000, // 8
  0b10000011, // 9

void setup()
  for(int i = 0; i < sizeof(pins); i++)
  	pinMode(pins[i], OUTPUT);

void loop()
  for(int i = 0; i < sizeof(Segments); i++)

void SetDisplay(byte shownNumber){
  byte segments = Segments[shownNumber];
  for (int i = 0; i < sizeof(segments); i++){
  	digitalWrite(pins[i], segments & (0x40 >> i));

Is the display a common anode display?

Is that writing a LOW to the segments that you want lit?
A Serial print there might shed some light (:slight_smile: pun intended).
You will need a Serial.begin(baud) in setup to use the serial port.

Don't use pin 1 for a segment. It is the serial TX pin.

Real, or simulated hardware?

I forget that we have to ask that, nowadays.

Isn't the size always going to be 1 byte?


byte pins[] = { 1, 2, 3, 4, 5, 6, 7 };

And your picture show you are using pin 1 of the UNO, this is the Tx pin of the UNO that is used with pin 0 to program your code.
I would suggest you use these pins and reconfigure your circuit.

byte pins[] = { 2, 3, 4, 5, 6, 7, 8 };

Tom... :smiley: :+1: :coffee: :australia:


Yes, it is a common anode display.
Then, I want to write a HIGH statement.
Lastly, I'll change pin 1 for another.
Thanks @groundFungus !

You got the wrong size:

sizeof() returns strictly the compile-time size in bytes of a variable or type.

BTW thanks for posting code and circuit up-front, makes this an easy solution (I hope)!

Thank you Tom!

Ba ba bing.

If it is a common anode display, you must write a LOW to light a segment.

`sizeof (segments)' is 1 so this only goes through one of the seven bits. Try:

void SetDisplay(byte shownNumber){
  byte segments = Segments[shownNumber];
  for (int i = 0; i < 7; i++){
  	digitalWrite(pins[i], segments & (0x40 >> i));

Have you READ posts #5, #9, #13... ?

There is no delay anywhere, it is going to look like all segments are on but dimmed when you get it working

I didn't know that

You're right. Thanks for your code, I copied it for another post. I didn't know some binary operators, interesting.

Yes, you're right. The size is always going to be 1.

I've just add it and it works better.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.