Projects / Eight extra outputs for a bar graph

Eight extra outputs for a bar graph

Beginner1.3 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. NANO

    Wire the I2C

    SDA to A4, SCL to A5, with power.

  2. NANO

    Set the address

    A0, A1 and A2 to GND.

  3. NANO14.4 mA

    Wire eight LEDs

    Anodes through 220 ohm to 5V, cathodes to P0 to P7.

  4. NANO

    Wire the knob

    Outer legs to 5V and GND, middle to A0.

  5. NANO

    Upload the sketch

    A message appears if the chip is missing.

  6. NANO14.5 mA

    Turn the knob

    The bar grows from zero to eight LEDs.

  7. NANO

    Change the address

    Move A0 to 5V and set ADDR to match.

  8. NANO14.5 mA

    Try a pattern

    Call writeLeds with 0xAA.

In short

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

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Expander VCCNano 5VPower
Expander GNDNano GNDCommon ground
Expander SDANano A4I2C data
Expander SCLNano A5I2C clock
Expander A0, A1, A2GNDSets the address to 0x20 on a PCF8574, 0x38 on a PCF8574A
Expander P0 to P7LED cathodes (short legs)One LED on each pin
LED anodes (long legs)220 ohm resistors to Nano 5VLEDs turn on when the pin goes low
Pot outer legsNano 5V and GNDLevel input
Pot middle legNano A0Reads 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.

CheckExpected
Knob at minimumAll LEDs off
Knob at maximumAll eight on
Knob half wayAbout four on
Expander unpluggedNot found message
Address pin changedNot 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 →

Ask Lark AI

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.