LED bar graph/What ten segments cost/09. Ten pins, or three
What ten segments cost · 09 of 11

Ten pins, or three

One bar wired straight to the pins is the right answer and costs ten of them. The second bar is where that stops working, because an Uno has twenty pins and two of them are the serial port you are printing to.

Counting what is left

An Uno has twenty pins that can be outputs: D0 to D13, plus A0 to A5, which are ordinary digital pins with an analogue input bolted on. D0 and D1 are the USB serial port, so eighteen are free while the Serial Monitor is open.

One bar takes ten of them.

Ten pins a bar, or three for all of them
1 bar · straight to gpio
How the segments are driven
Bar graphs1
Pins used
10
Pins left
8
Shift registers
Ten wires, and nothing in the way. One pin per segment. Nothing between the pin and the LED but the 220 Ω. For one bar this is the right answer: no library, no clocking, and every segment can change on its own. It leaves 8 pins for everything else the project has to do.

That leaves eight, which is enough for a potentiometer, a couple of buttons and an I²C sensor. It is not enough for a second bar.

When ten wires is right

Almost always, for one bar. There is no library, no clocking, no latch, and any segment can change on its own without touching the others. A digitalWrite is the whole interface. Start here and only move when you run out of pins.

When it is not

Two bars, or one bar and a display, or a project already using most of the board. The usual answer is a 74HC595 shift register: you clock bits into it on three wires — data, clock and latch — and it drives eight outputs. Chain two for a bar's ten segments, chain more for more bars, and the three wires never become four.

The other thing a shift register buys is that the LED current comes out of its supply rather than the microcontroller's port group, which makes ten segments, one chip stop being a constraint.

The trade, in one line

Ten pins buys you independence and simplicity. Three pins buys you room, at the cost of rewriting every segment each time you change one.

When it does not work

Can I use A0 to A5 as digital pins?

Yes. On an Uno they are ordinary digital pins that also happen to have an analogue input attached, and pinMode(A0, OUTPUT) works exactly as it does for D4. That is what gets you to eighteen usable pins rather than fourteen.

Can I use D0 and D1?

You can, and then you lose the USB serial port — uploads fight with whatever is wired there, and Serial.print goes into the bar graph. Leave them alone unless the project genuinely has nothing to say.

How many 74HC595s does one bar need?

Two. Each register has eight outputs and a bar has ten segments, so one is not enough and two give you sixteen with six spare. The second register chains off the first and costs no extra pins.

Is a shift register slower?

For this, no. Shifting sixteen bits out with shiftOut() takes a fraction of a millisecond, and a bar graph that updates a hundred times a second is already smoother than anyone can see. What it costs is that a segment cannot change on its own — the whole register is rewritten each time.

Where this goes next

A potentiometer, a level, and the rounding that decides which segment lights.

A knob and a bar

Edit this page — content/books/led-bar-graph/ten-pins-or-three.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

Ask a question ↗

10-Segment LED Bar Graph Kit, 12 Bars in 6 Colours

Loading discussions…

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum