Eight extra outputs for a bar graph
Build it step by step
0 of 8 doneWire the I2C
SDA to A4, SCL to A5, with power.
Set the address
A0, A1 and A2 to GND.
Wire eight LEDs
Anodes through 220 ohm to 5V, cathodes to P0 to P7.
Wire the knob
Outer legs to 5V and GND, middle to A0.
Upload the sketch
A message appears if the chip is missing.
Turn the knob
The bar grows from zero to eight LEDs.
Change the address
Move A0 to 5V and set ADDR to match.
Try a pattern
Call writeLeds with 0xAA.
How do you build an eight extra outputs for a bar graph?
A cheap expander drives eight LEDs over I2C, leaving the board pins free for other jobs. You need Arduino Nano, PCF8574 I/O expander module, LED 5 mm red × 8, Resistor 220 Ω × 8, Potentiometer 10 kΩ and Breadboard 830 Tie Points. It takes 8 steps and about 75 minutes.
- Level
- Beginner
- Time
- About 75 min
- Parts
- 6
Parts list
- Development BoardsBuyArduino Nano × 1
- InterfacesBuy
- SemiconductorsBuyLED 5 mm red × 8
- PassivesBuyResistor 220 Ω × 8
- PassivesBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Expander VCC | Nano 5V | Power |
| Expander GND | Nano GND | Common ground |
| Expander SDA | Nano A4 | I2C data |
| Expander SCL | Nano A5 | I2C clock |
| Expander A0, A1, A2 | GND | Sets the address to 0x20 on a PCF8574, 0x38 on a PCF8574A |
| Expander P0 to P7 | LED cathodes (short legs) | One LED on each pin |
| LED anodes (long legs) | 220 ohm resistors to Nano 5V | LEDs turn on when the pin goes low |
| Pot outer legs | Nano 5V and GND | Level input |
| Pot middle leg | Nano A0 | Reads the level |
Code
The PCF8574 is a chip that gives you eight more input and output pins over the two I2C wires. You send it one byte and each bit sets one pin, which makes it a cheap way to run a bar graph, a row of status lights or a keypad when the board has run out of pins. A feature to know about is that the chip can sink a good current, about 25 milliamps per pin, but can source only about 100 microamps. For that reason the LEDs are wired from 5 volts through a resistor to the pin, and a zero bit turns an LED on, which is the opposite of what you might expect. The sketch reads a knob on A0, turns it into a level from 0 to 8 and lights that many LEDs, so you get a bar graph. The module address is set by three pins: with all three on ground it is 0x20 for the PCF8574 or 0x38 for the PCF8574A variant. Up to eight expanders can share the bus by using different addresses.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <Wire.h>
const byte ADDR = 0x20; // use 0x38 for the PCF8574A
// The PCF8574 can sink current well but can only source a tiny amount, so the LEDs are
// connected to 5 V and the pin pulls them to ground. A 0 bit turns an LED on.
void writeLeds(byte onMask) {
Wire.beginTransmission(ADDR);
Wire.write(~onMask);
Wire.endTransmission();
}
void setup() {
Wire.begin();
Serial.begin(9600);
Wire.beginTransmission(ADDR);
if (Wire.endTransmission() != 0) Serial.println("Expander not found, check the address");
writeLeds(0);
}
void loop() {
int level = map(analogRead(A0), 0, 1023, 0, 8); // 0 to 8 LEDs
byte mask = 0;
for (int i = 0; i < level; i++) mask |= (1 << i);
writeLeds(mask);
Serial.println(level);
delay(50);
}What the serial monitor should show
Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.
Expander not found, check the address 4 3 4 3 4
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Knob at minimum | All LEDs off |
| Knob at maximum | All eight on |
| Knob half way | About four on |
| Expander unplugged | Not found message |
| Address pin changed | Not found until the sketch is updated |
If it does not work
Not found message
Try 0x38, and check the address pins.
All LEDs always on
The bits are not inverted, check the tilde in the sketch.
LEDs very dim
Normal if wired to source, rewire to sink as shown.
Only some LEDs work
Check each LED direction.
Flicker
Loose breadboard connection on SDA or SCL.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the eight extra outputs for a bar graph?
Arduino Nano, PCF8574 I/O expander module, LED 5 mm red × 8, Resistor 220 Ω × 8, Potentiometer 10 kΩ and Breadboard 830 Tie Points.
How long does the eight extra outputs for a bar graph take to build?
About 75 minutes for a beginner maker, in 8 steps.
How do I know the eight extra outputs for a bar graph works?
Knob at minimum: All LEDs off. Knob at maximum: All eight on. Knob half way: About four on.
Not found message: what should I check?
Try 0x38, and check the address pins.
All LEDs always on: what should I check?
The bits are not inverted, check the tilde in the sketch.
LEDs very dim: what should I check?
Normal if wired to source, rewire to sink as shown.
Only some LEDs work: what should I check?
Check each LED direction.
