Built-in project · Arduino Uno R3

DC throttle with momentum on an Arduino Uno

A knob, a direction switch and a brake button drive a DC motor through a TB6612, easing up to speed and down again like a heavy train, and reversing only at a standstill. The whole build, an Arduino Uno and 5 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate Runs in your browser. Free, and no account needed.

DC throttle with momentum · Arduino Uno R3live0.000 s 0.00x
Press Run, then turn the knob and watch the motor ease up to speed
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// DC throttle with momentum: a knob, a direction switch, a brake and a TB6612.
//
//   A0       the throttle knob (its ends on 5 V and GND)
//   pin 2    direction switch (to GND for reverse, internal pull-up)
//   pin 3    brake button (to GND, internal pull-up)
//   pin 9    PWM into the TB6612's PWMA     pins 7 and 8: AIN1 and AIN2
//
// This is how a DC layout runs a train: the speed is the average voltage on the
// rails, and PWM sets that average by switching the full supply on and off.
// What makes it feel like a train is momentum. A heavy train does not jump to
// the speed the knob asks for; it eases up to it at ACCEL per step and slows at
// DECEL, so a quick twist of the knob still gives a gentle start. The brake
// button slows much harder, and the direction switch is only obeyed at a
// standstill: flip it at speed and the train coasts down, stops, then reverses,
// as an engineer would make it.
//
// DCC works differently. The rails carry constant power as a square wave that
// also carries digital packets, and a decoder inside each locomotive does the
// PWM and the momentum itself (its CV3 and CV4 set acceleration and braking).
// Mokxi does not simulate DCC; this sketch is the DC way, and the momentum logic
// is the same idea a decoder runs.
//
// The supply here is 12 V, like most HO and N scale DC power packs. On a real
// layout fit a fuse or a current limit: a derailed wheel across both rails is a
// dead short.

const int KNOB_PIN = A0;
const int REVERSE_PIN = 2;
const int BRAKE_PIN = 3;
const int PWM_PIN = 9;
const int AIN1 = 7;
const int AIN2 = 8;

const int ACCEL = 2;       // speed steps gained per 20 ms tick, out of 255
const int DECEL = 3;       // lost when the knob is turned down
const int BRAKE = 12;      // lost per tick with the brake held
const int START_AT = 30;   // below this a typical motor only hums, so treat it as stopped

int speed = 0;             // what the motor gets now, 0 to 255
bool reverse = false;      // the direction the motor is turning (or will turn)
unsigned long lastTick = 0;
int lastReported = -1;

void drive() {
  digitalWrite(AIN1, reverse ? LOW : HIGH);
  digitalWrite(AIN2, reverse ? HIGH : LOW);
  analogWrite(PWM_PIN, speed);
}

void setup() {
  Serial.begin(9600);
  pinMode(REVERSE_PIN, INPUT_PULLUP);
  pinMode(BRAKE_PIN, INPUT_PULLUP);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);
  reverse = digitalRead(REVERSE_PIN) == LOW;
  drive();
  Serial.println("DC throttle ready: turn the knob");
}

void loop() {
  unsigned long now = millis();
  if (now - lastTick < 20) return;
  lastTick = now;

  int asked = map(analogRead(KNOB_PIN), 0, 1023, 0, 255);
  if (asked < START_AT) asked = 0;
  bool wantReverse = digitalRead(REVERSE_PIN) == LOW;
  bool braking = digitalRead(BRAKE_PIN) == LOW;

  // A change of direction waits for a standstill: aim for zero until then.
  if (wantReverse != reverse) {
    if (speed == 0) {
      reverse = wantReverse;
      Serial.println(reverse ? "reverse" : "forward");
    } else {
      asked = 0;
    }
  }
  if (braking) asked = 0;

  if (speed < asked) speed = min(speed + ACCEL, asked);
  else if (speed > asked) speed = max(speed - (braking ? BRAKE : DECEL), asked);
  drive();

  int percent = speed * 100 / 255;
  if (percent / 5 != lastReported / 5 || (speed == 0 && lastReported != 0)) {
    lastReported = percent;
    Serial.print(reverse ? "reverse " : "forward ");
    Serial.print(percent);
    Serial.println(braking ? "%  braking" : "%");
  }
}

Parts list

9 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

14 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Ground: Arduino Uno R3 pin GND; Arduino Uno R3 pin GND; Slide switch pin 3; Pushbutton pin 2a; Pushbutton pin 2b; Potentiometer pin 1
  • Arduino Uno R3 pin 9; Motor driver, TB6612FNG pin PWMA
  • Arduino Uno R3 pin 8; Motor driver, TB6612FNG pin AIN2
  • Arduino Uno R3 pin 7; Motor driver, TB6612FNG pin AIN1
  • Arduino Uno R3 pin 3; Pushbutton pin 1a; Pushbutton pin 1b
  • Arduino Uno R3 pin 2; Slide switch pin 2
  • Arduino Uno R3 pin 5V; Potentiometer pin 3
  • Arduino Uno R3 pin A0; Potentiometer pin 2
  • 5 V: Motor driver, TB6612FNG pin STBY; Motor driver, TB6612FNG pin VCC
  • Ground: Motor driver, TB6612FNG pin GND1
  • 12 V: Motor driver, TB6612FNG pin VM
  • Motor driver, TB6612FNG pin AO1; DC motor pin 1
  • Motor driver, TB6612FNG pin AO2; DC motor pin 2
  • Ground: Motor driver, TB6612FNG pin GND3

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.