Projects / Nucleo serial command console

Nucleo serial command console

Intermediate2.5 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. STM32

    Plug in the board

    A drive and a serial port appear.

  2. STM3211.7 mA

    Wire the external LED

    D8 through 330 ohms to the LED, cathode to GND.

  3. STM32

    Upload the sketch

    Using the Nucleo-64 board settings.

  4. STM32

    Open the serial monitor

    115200 baud, newline line ending.

  5. STM32

    Type help

    The command list prints.

  6. STM3211.8 mA

    Type led on and led off

    The built-in LED follows.

  7. STM3211.8 mA

    Type ext on and uptime

    The outside LED lights and the time is shown.

In short

How do you build a nucleo serial command console?

A Nucleo board reads text commands over its USB serial link to turn LEDs on and off, report the uptime and print a help list. You need STM32 Nucleo-64, LED 5 mm red, Resistor 330 Ω and Breadboard 830 Tie Points. It takes 7 steps and about 150 minutes.

Level
Intermediate
Time
About 150 min
Parts
4

Parts list

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

Wiring

FromToWhy
Nucleo USB cableComputerPower, programming and serial
Red LED anode330 ohm resistor to Nucleo D8External LED
Red LED cathodeNucleo GNDReturn
Built-in LEDAlready on the boardControlled with the led commands

Code

A serial console is the simplest way to talk to a microcontroller. The board collects characters into a line buffer until it sees the end of a line, then splits the line into a command and an argument and decides what to do. The buffer has a fixed size, so an over-long line is cut off instead of overrunning memory, which is a habit worth keeping in any code that reads text from outside. The sketch converts the text to lower case so commands are not case sensitive, and prints a help list for anything it does not know. The Nucleo board has a programmer called ST-LINK built in, and it also passes the serial port through the USB cable, so no extra wires or adapter are needed. The built-in LED is on LED_BUILTIN, and the external LED needs a series resistor of about 330 ohms. The board pins work at 3.3 V.

Runs on a virtual STM32 Blue Pill 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 <ctype.h>
#include <string.h>

char line[48];
uint8_t len = 0;
const int EXT_LED = D8;

void prompt() { Serial.print("> "); }

void setLed(int pin, const char *arg, const char *name) {
  if (arg && strcmp(arg, "on") == 0)       { digitalWrite(pin, HIGH); Serial.print(name); Serial.println(" on"); }
  else if (arg && strcmp(arg, "off") == 0) { digitalWrite(pin, LOW);  Serial.print(name); Serial.println(" off"); }
  else Serial.println("use: on or off");
}

void handle(char *text) {
  for (char *p = text; *p; p++) *p = tolower((unsigned char)*p);
  char *cmd = strtok(text, " ");
  char *arg = strtok(NULL, " ");
  if (!cmd) return;
  if (strcmp(cmd, "help") == 0) {
    Serial.println("Commands:");
    Serial.println("  led on|off    built-in LED");
    Serial.println("  ext on|off    external LED on D8");
    Serial.println("  uptime        time since start");
  } else if (strcmp(cmd, "led") == 0) {
    setLed(LED_BUILTIN, arg, "led");
  } else if (strcmp(cmd, "ext") == 0) {
    setLed(EXT_LED, arg, "ext");
  } else if (strcmp(cmd, "uptime") == 0) {
    unsigned long s = millis() / 1000;
    Serial.print(s / 3600); Serial.print(" h ");
    Serial.print((s / 60) % 60); Serial.print(" min ");
    Serial.print(s % 60); Serial.println(" s");
  } else {
    Serial.println("unknown command, type help");
  }
}

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(EXT_LED, OUTPUT);
  Serial.begin(115200);
  delay(300);
  Serial.println("Nucleo console ready, type help");
  prompt();
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();
    if (c == '\r' || c == '\n') {
      if (len > 0) { line[len] = 0; handle(line); len = 0; }
      prompt();
    } else if (len < sizeof(line) - 1) {
      line[len++] = c;
    }
  }
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
helpCommand list prints
led onBuilt-in LED lights
ext offExternal LED turns off
uptimeHours, minutes and seconds
unknown wordError message with a hint

If it does not work

Nothing appears in the monitor

Check the port, 115200 baud and the board settings.

Commands ignored

Set the line ending to newline.

Built-in LED does nothing

Some boards use an inverted LED, try the opposite state.

External LED dark

Check polarity and that D8 exists on your board.

Garbled text

Set the monitor baud rate to 115200.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the nucleo serial command console?

STM32 Nucleo-64, LED 5 mm red, Resistor 330 Ω and Breadboard 830 Tie Points.

How long does the nucleo serial command console take to build?

About 150 minutes for a intermediate maker, in 7 steps.

How do I know the nucleo serial command console works?

help: Command list prints. led on: Built-in LED lights. ext off: External LED turns off.

Nothing appears in the monitor: what should I check?

Check the port, 115200 baud and the board settings.

Commands ignored: what should I check?

Set the line ending to newline.

Built-in LED does nothing: what should I check?

Some boards use an inverted LED, try the opposite state.

External LED dark: what should I check?

Check polarity and that D8 exists on your board.