Tutorial 18: Two Wire Arduino Knight Rider

Created on: 6 August 2012
Updated on: 30 January 2023

This tutorial shows how to interface eight LEDs to an Arduino using only two Arduino pins to make a two wire Arduino Knight Rider. It is possible to do this by using a PCF8574 I/O expander IC. A Knight Rider or LED chaser display is shown on the LEDs.

The following video shows the two wire Arduino Knight Rider, or LED chaser circuit in action.

Prerequisites

Complete tutorial 4 – Arduino Knight Rider. Be able to use DIP IC packages, e.g. from tutorial 17.

Two Wire Arduino Knight Rider Components

In addition to an Arduino Uno or compatible board, USB cable, wire links and a breadboard, you will need:

Qty Part Designator Notes Type
2 2k2 resistors (red - red - red) R9, R10 1/4W 5% or better Resistors
8 470 ohm resistors (yellow - violet - brown) R1 to R8
1 100n capacitor C1 Non-polorized capacitor Capacitor
1 PCF8574P U1 8-bit input/output (I/O) expander Semiconductors
8 LEDs D1 to D8 LEDs – red or green 3mm or 5mm diffused LEDs

Two Wire Arduino Knight Rider Circuit Diagram

The two wire Arduino LED Knight Rider or LED chaser circuit is shown below. In this circuit diagram, the Arduino board is not shown. Only the pin labels of the Arduino pins to be connected to the PCF8574 (U1) are shown. The points marked +5V are to be connected to the Arduino 5V pin. The inverted triangle (GND) is to be connected to one of the Arduino GND pins.

Two wire knight rider circuit diagram
Arduino Two Wire Knight Rider Circuit - click image for bigger circuit

Arduino analog pins A5 and A4, shown at the left of the circuit diagram, connect to the PCF8574 IC chip. These pins are I²C or TWI pins on an Arduino Uno. I²C or TWI is a special serial communication protocol. These specific pins on an Arduino Uno can be configured to use this serial communication protocol instead of their analog function.

Building the Circuit

The photo below shows the complete circuit built on breadboard. Click the photo for a bigger image.

Arduino two wire knight rider circuit on breadboard
Arduino Two Wire Knight Rider Circuit on Breadboard

The suggested sequence of building the circuit is:

  1. Insert the PCF8574 IC
  2. Make power and ground connections to the IC via the top and bottom breadboard rails
  3. Connect pin 1, 2 and 3 of the IC (U1) to ground
  4. Insert capacitor C1 (100n) and wire it between power (5V) and ground (GND)
  5. Insert the eight LEDs with anodes (longer pin) on the left
  6. Connect resistors R1 to R8 between the LED anodes and the top breadboard rail (5V)
  7. Wire the cathode of each LED to the correct pins on the IC
  8. Connect R9 and R10
  9. Connect the two wires from Arduino pins A4 and A5 to the IC
  10. Connect the Arduino 5V to the top breadboard rail and GND to the bottom breadboard rail

You may have noticed that the breadboard circuit swaps the position of the LEDs and resistors from the circuit diagram – e.g. R1 and D1 swap positions. This will make no difference to how the circuit operates. It has only been done to simplify the breadboard circuit.

Programming the Arduino

Copy the two_wire_knight_rider sketch below and paste it into the Arduino IDE.

/*--------------------------------------------------------------
  Program:      two_wire_knight_rider

  Description:  Uses a PCF8574 IO Expander IC on the Arduino
                TWI bus to interface 8 LEDs. A "knight
                rider" display is shown on the LEDs.

  Date:         25 April 2012
 
  Author:       W.A. Smith, http://startingelectronics.org
--------------------------------------------------------------*/
#include <Wire.h>

// address of PCF8574 IC on TWI bus
#define IO_ADDR (0x40 >> 1)

void setup() {
  Wire.begin();        // initialize the I2C/TWI interface
}

void loop() {
  static unsigned char data = 0x01;  // data to display on LEDs
  static unsigned char direc = 1;    // direction of knight rider display
  
  // send the data to the LEDs
  Wire.beginTransmission(IO_ADDR);
  Wire.write(~data);
  Wire.endTransmission();
  delay(70);  // speed of display
  
  // shift the on LED in the specified direction
  if (direc) {
    data <<= 1;
  }
  else {
    data >>= 1;
  }
  // see if a direction change is needed
  if (data == 0x80) {
    direc = 0;
  }
  if (data == 0x01) {
    direc = 1;
  }
}

Operating the Two Wire Arduino Knight Rider Circuit

The Knight Rider display on the LEDs will start as soon as the sketch has been loaded to the Arduino.

As with other circuits in this tutorial series that have LEDs in the same configuration, a single resistor could be used for all eight LEDs. Once again, this is because only one LED is switched on at a time. Using one resistor for each LED tends to make construction of the circuit on a breadboard simpler.