Build it step by step
0 of 7 doneFit the pulley to the motor
And wind the string round it once or twice.
Mount the two limit switches
So the car presses one at each end.
Wire the driver and motor
VM to the battery pack, VCC and STBY to 5V, motor on A01 and A02.
Wire the switches and button
D2, D3 and D4, each other leg to GND.
Upload and watch the serial monitor
It says it is homing to station A.
Let the car run
It waits, travels, stops at the switch and returns.
Press the button at a station
The car leaves early.
How do you build a model cable car?
A small gear motor pulls a model cable car back and forth along a string between two stations, stopping at limit switches and waiting a few seconds at each end. A good small project in motor control and timing. Keep fingers clear of the string and the pulley. You need Arduino Nano, DC motor 130 size (toy motor), TB6612FNG Motor Driver, Limit switch (micro switch with lever) × 2, Tactile push button 6 mm, Battery holder 4 × AA with switch, AA alkaline battery × 4, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 100 minutes.
- Level
- Intermediate
- Time
- About 100 min
- Parts
- 9
Wiring
| From | To | Why |
|---|---|---|
| Battery pack + (4 AA, 6 V) | TB6612 VM | Motor power, kept separate from the Nano |
| Battery pack - | TB6612 GND and Nano GND | One shared ground |
| Nano 5V | TB6612 VCC and STBY | Logic power, driver always enabled |
| TB6612 PWMA, AIN1, AIN2 | Nano D5, D7 and D8 | Motor speed and direction |
| TB6612 A01 and A02 | Motor terminals | Drives the pulley |
| Station A limit switch | Nano D2 and GND | Pressed when the car reaches A |
| Station B limit switch | Nano D3 and GND | Pressed when the car reaches B |
| Call button | Nano D4 and GND | Sends the waiting car off early |
Code
The car hangs on a string stretched between two stations, wound around a pulley on the motor shaft by one turn or two so it grips. At power-up the sketch homes the car by driving toward station A until the switch there is pressed. After that it waits five seconds, or until the call button is pressed, then drives to B, waits, and drives back, forever. Each leg is stopped by a limit switch, and if a switch has not been pressed after 20 seconds the sketch stops the motor and reports a fault in the serial monitor, so a snapped string or wrong direction cannot run the motor hard against nothing. The toy motor is powered from four AA cells through the TB6612 driver, and the ground is shared with the Nano, so the Nano is not asked to supply the motor current. The speed is set low with PWM so the car moves gently. If the car goes the wrong way, swap the two motor wires or swap AIN1 and AIN2 in the code. Keep fingers clear of the string and pulley, and use a very light card car. Try adding a slow ramp-up in speed, or a soft stop before the switch.
Runs on a virtual Arduino Nano 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).
const uint8_t PWMA = 5;
const uint8_t AIN1 = 7;
const uint8_t AIN2 = 8;
const uint8_t LIMIT_A = 2;
const uint8_t LIMIT_B = 3;
const uint8_t CALL_BTN = 4;
const uint8_t SPEED = 140; // out of 255, gentle for a toy
const unsigned long WAIT_MS = 5000;
const unsigned long TRAVEL_TIMEOUT = 20000;
void motor(int dir) { // 1 toward B, -1 toward A, 0 stop
if (dir == 0) {
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, LOW);
analogWrite(PWMA, 0);
return;
}
digitalWrite(AIN1, dir > 0 ? HIGH : LOW);
digitalWrite(AIN2, dir > 0 ? LOW : HIGH);
analogWrite(PWMA, SPEED);
}
bool hit(uint8_t pin) { return digitalRead(pin) == LOW; }
bool driveUntil(int dir, uint8_t limitPin) {
unsigned long start = millis();
motor(dir);
while (!hit(limitPin)) {
if (millis() - start > TRAVEL_TIMEOUT) { // switch never pressed: stop, do not strain the motor
motor(0);
return false;
}
}
motor(0);
return true;
}
void waitAtStation(const char *name) {
Serial.print("At station ");
Serial.println(name);
unsigned long start = millis();
while (millis() - start < WAIT_MS) {
if (hit(CALL_BTN)) break; // button sends the car off early
}
while (hit(CALL_BTN)) {}
delay(300);
}
void fault() {
Serial.println("Limit switch not reached. Check the string, motor direction and switches.");
while (true) {}
}
void setup() {
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(LIMIT_A, INPUT_PULLUP);
pinMode(LIMIT_B, INPUT_PULLUP);
pinMode(CALL_BTN, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("Homing to station A...");
if (!hit(LIMIT_A) && !driveUntil(-1, LIMIT_A)) fault();
}
void loop() {
waitAtStation("A");
if (!driveUntil(1, LIMIT_B)) fault();
waitAtStation("B");
if (!driveUntil(-1, LIMIT_A)) fault();
}What the serial monitor should show
Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.
Homing to station A...
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Power-up | The car homes to station A |
| At a station | Waits five seconds or until the button |
| Travel | Gentle speed, stops at the limit switch |
| String removed | Fault message after 20 seconds |
| Serial monitor | Prints each station |
If it does not work
Car goes the wrong way
Swap the motor wires or AIN1 and AIN2.
Car does not move
Check VM has battery power and STBY is on 5V.
Fault message each time
Check the limit switch wiring and that the car presses it.
String slips on the pulley
Add a turn or roughen the pulley.
Nano resets
Keep the motor on its own battery and share only the ground.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the model cable car?
Arduino Nano, DC motor 130 size (toy motor), TB6612FNG Motor Driver, Limit switch (micro switch with lever) × 2, Tactile push button 6 mm, Battery holder 4 × AA with switch, AA alkaline battery × 4, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the model cable car take to build?
About 100 minutes for a intermediate maker, in 7 steps.
How do I know the model cable car works?
Power-up: The car homes to station A. At a station: Waits five seconds or until the button. Travel: Gentle speed, stops at the limit switch.
Car goes the wrong way: what should I check?
Swap the motor wires or AIN1 and AIN2.
Car does not move: what should I check?
Check VM has battery power and STBY is on 5V.
Fault message each time: what should I check?
Check the limit switch wiring and that the car presses it.
String slips on the pulley: what should I check?
Add a turn or roughen the pulley.
