Nano V3.0/The first hour/08. Chase the LEDs
The first hour · 08 of 9

Chase the LEDs

One sketch that proves the board is seated, the shield is working and the toolchain uploads: it lights seventeen pins one at a time, down one column and up the other, and leaves out the ones that would get in its way. No wiring at all.

Seventeen lights, five gaps

The obvious first sketch on a shield with a lamp on every pin is a loop over every pin. Here that would take the serial port down on the first pin it touched.

The chase, pin by pin
ready
Pin now
none
Lamps lit
0
Lamp current
about 0.0 mA
Seventeen pins in the list, five lamps left out. The crossed lamps are TXD, RXD, A6, A7 and D13. Press the button and the light walks the other seventeen.

So the sketch keeps a list: D2 to D12, then A0 to A5, seventeen pins. The five it leaves out are the ones the figure crosses. D0 and D1 are the serial line to the CH340C, which is how the sketch was uploaded and how it prints. A6 and A7 are analogue-in only, so pinMode(A6, OUTPUT) would do nothing useful. D13 works as an output, but it is also the lamp on the Nano itself, and a chase that lights it would be lighting two lamps.

Before you upload

Seat the Nano with the power off, then plug the cable in. Then set the three Tools rows from the previous page: Board Arduino Nano, Processor ATmega328P, Port the one that appears. The comment at the top of the sketch carries the same list.

The sketch

pinMode(PINS[i], OUTPUT) for every pin in the list is the line that matters. It stops those lamps floating. The flicker from the floating-pin page goes away the moment setup() runs, row by row.

The analogue pins are used as digital outputs. On this chip A0 to A5 are the same pins as 14 to 19, and pinMode and digitalWrite accept either.

Reading the result

Open the Serial Monitor at 9600 and it prints chasing 17 pins once. That separates two failures:

  • Nothing printed, nothing lit: the sketch is not running. That is the board or the upload.
  • Printed, and a row stays dark: the sketch is fine. Look at that row: the Nano's pin in its socket, or the lamp beside it.

All seventeen lit is about 13 mA, drawn from the 5 V rail and not from the pins. That figure is illustrative: it assumes the LED drops 2 V.

The code

chase_the_leds.ino

Walks a light down the left column, D2 to D12, then up the right, A0 to A5, then lights all seventeen at once and starts again. It prints the pin count once, so you can tell an upload fault from a shield fault without moving anything.

// Nano V3.0 + PinPulse shield: chase a light down the D2-D12
// column, then up the A0-A5 column.
//
// Wiring: none. With the power off, seat the Nano in the shield
// with its USB-C connector at the boxed USB mark, then plug the
// USB-C cable into the Nano. The lamps take their power from the
// Nano's 5V rail.
//
// Left out on purpose:
//   D0, D1   USB serial line (the TXD and RXD lamps)
//   D13      also the Nano's own lamp
//   A6, A7   analogue-in only, they cannot be outputs
//
// Arduino IDE, Tools menu:
//   Board      Arduino Nano
//   Processor  ATmega328P
//              (ATmega328P (Old Bootloader) if the upload says
//              "not in sync")
//   Port       the CH340 port that appears when the Nano is
//              plugged in
// No library needed.

const byte PINS[] = {
  2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,   // left column, D2 to D12
  A0, A1, A2, A3, A4, A5,               // right column, A0 up to A5
};
const byte COUNT = sizeof(PINS) / sizeof(PINS[0]);

void setup() {
  for (byte i = 0; i < COUNT; i++) {
    pinMode(PINS[i], OUTPUT);   // driven, so none of these lamps floats
    digitalWrite(PINS[i], LOW);
  }
  Serial.begin(9600);
  Serial.print("chasing ");
  Serial.print(COUNT);
  Serial.println(" pins");
}

void loop() {
  for (byte i = 0; i < COUNT; i++) {
    digitalWrite(PINS[i], HIGH);
    delay(80);
    digitalWrite(PINS[i], LOW);
  }
  for (byte i = 0; i < COUNT; i++) digitalWrite(PINS[i], HIGH);
  delay(600);                   // the whole lap at once
  for (byte i = 0; i < COUNT; i++) digitalWrite(PINS[i], LOW);
  delay(300);
}

The list is the whole lesson. D0 and D1 are the USB serial line, so a sketch that drives them as pins loses the serial port it was uploaded over. A6 and A7 cannot be outputs. D13 is also the Nano's own lamp, so it is left for a Blink sketch.

When it does not work

The upload fails before the sketch ever runs

That is the cable, the port or the Processor row, not the shield: uploading never goes through the shield's lamps. Check the Port in the Tools menu, try another cable, and if the error says not in sync choose ATmega328P (Old Bootloader).

The chase runs but one lamp never lights

Note which pin it is. If it is in the sketch's list, the lamp or its inverter channel is at fault, or the Nano's pin is not seated in that socket position. Power off, unplug the cable, press the Nano down along that row, and watch again.

TXD and RXD blink, and I did not put them in the sketch

They are on the USB serial line, which the sketch uses to print the pin count and which an upload uses all the time. They follow that line's activity. That is expected, and it is why D0 and D1 are not in the list.

Nothing lights and the Serial Monitor prints nothing

The sketch is not running. Check the Nano is seated and the right way round, and that the upload ended with Done uploading. If the Nano's ON lamp is off, unplug the cable first and check the seat and the cable with the Nano out of the shield.

Where this goes next

What to check, in order, and what is normal.

If a board gets warm →

Edit this page — content/books/nano/chase-the-leds.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

This page covers several products. Choose yours to see the right discussions.

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum →