barrieron-8led.ino

Arduino Mega 2560 (Rev3) · File : barrieron-8led.ino · Language : Arduino · Use case : Faire courir une barre de 8 LED avec un registre à décalage 74HC595

The 74HC595 turns 3 pins into 8 outputs: the LED bar costs only three wires, and chips chain for 16, 24 outputs… The pattern sweeps like the « knight rider » scanner.

Hardware required

An Arduino Mega 2560 (ATmega2560, 16 MHz, 5 V): 54 digital I/O, 15 PWM, 16 analog inputs, 4 hardware UARTs. 1 74HC595, 8 LEDs + 8×330 Ω, 100 nF bypass capacitor.

Wiring

SER (14)→pin 22, SRCLK (11)→23, RCLK (12)→24; QA-QH to LEDs (via resistors); OE and GND to GND, VCC to 5 V.

How it works

shiftOut stacks 8 bits into the register with clock pulses, then the LATCH edge applies them to outputs at once. In the Arduino IDE, select « Arduino Mega 2560 » and the port, then Upload. PWM on 2-13 and 44-46; UART1=18/19, UART2=16/17, UART3=14/15; SPI on 50-53.

Going further

Chain a second 595 (SER out → next SER in) for a 16-LED bar on the same 3 pins.

barrieron-8led.ino

// Barre de 8 LED en "knight rider" via 74HC595 (3 broches seulement).
const int DATA = 22, LATCH = 23, CLOCK = 24;

uint8_t motif = 1;
int sens = 1;

void setup() {
  pinMode(DATA, OUTPUT); pinMode(LATCH, OUTPUT); pinMode(CLOCK, OUTPUT);
}

void loop() {
  digitalWrite(LATCH, LOW);
  shiftOut(DATA, CLOCK, MSBFIRST, ~motif); // LED actives a LOW
  digitalWrite(LATCH, HIGH);
  if (sens > 0) { if (motif & 0x80) sens = -1; else motif <<= 1; }
  else          { if (motif & 0x01) sens = 1;  else motif >>= 1; }
  delay(120);
}

← Back to the board