Friday, February 10, 2017

Arduino MEGA 24 LED Knight Rider Display

built on a breadboard using 24 LEDs.
An Arduino MEGA 2560 is used to directly drive the LEDs from 24 of the Arduino's pins. An Arduino Uno can not be used to drive the LEDs directly because it has too few pins, although it would be possible to expand the number of outputs on an Arduino Uno using four PCF8574 I/O expander ICs.
The circuit is pretty simple as it is just an LED and series resistor connected to each pin and repeated 24 times. Pins 30 to 51 are used on the Arduino MEGA.
The video below shows the circuit operating.

The Circuit Diagram

The 24 LED knight rider circuit diagram is shown below. Only the first two and last two LEDs are shown in the circuit as the other LEDs are just a repetition of the LED and series resistor connected to the pins in sequential order.
24 LED Knight Rider Display Circuit
24 LED Knight Rider Display Circuit using the Arduino MEGA

Arduino Knight Rider Sketch

The code for the Arduino Knight Rider display is shown here.
const int delay_ms = 50;

void setup() {
  int i;
  
  for (i = 30; i <= 53; i++) {
    pinMode(i, OUTPUT);    // set pins as outputs
    digitalWrite(i, LOW);  // switch the output pins off
  }
}

boolean forward = true;    // LED direction flag
int pin_num;

void loop() {
  
  if (forward) {  // LEDs left to right
    for (pin_num = 30; pin_num <= 53; pin_num++) {
      digitalWrite(pin_num, HIGH);
      delay(delay_ms);
      digitalWrite(pin_num, LOW);
    }
    forward = false;
  }
  else {          // LEDs right to left
    for (pin_num = 53; pin_num >= 30; pin_num--) {
      digitalWrite(pin_num, HIGH);
      delay(delay_ms);
      digitalWrite(pin_num, LOW);
    }
    forward = true;
  }
}

The speed of the display can be set by changing the value of delay_ms at the top of the sketch.

No comments: