Class 2 · Turn it¶
Your catapult fires. Today it gets a turntable and a stepper motor, and the knob aims it left and right. It never turns more than 90° either way, so the wires don't wrap around it.
By the end of class
You aim with the knob and hit a cup placed off to the side.
New parts today¶
| Part | What it's for |
|---|---|
| 28BYJ-48 stepper motor | Turns the turret |
| ULN2003 driver board | Gives the motor the current the Arduino can't |
| Lazy-susan bearing | Lets the top plate spin smoothly |
| Two pieces of plywood/MDF, wood screws | The base and the top plate |
Build it¶
- Base and bearing. Screw the lazy-susan bearing between the base and the top plate.
- Motor in the middle. Mount the stepper on the base, at the center, with its shaft pointing up into the top plate. The shaft has to grip the top plate: Dr. Alvear has a hub for this. It must not slip.
- Catapult on top. Screw or glue the catapult to the top plate.
- Draw a line across the edge of the top plate and onto the base, so you can see when the turret points straight ahead. Your code will assume it starts pointing straight ahead, every time you plug in.
- Leave slack in the latch servo's wires: a loose loop, enough for a quarter turn each way.
-
Wire it, everything unplugged:
From To Motor's white plug ULN2003 socket (it only fits one way) ULN2003 +Motor rail +ULN2003 −Motor rail −ULN2003 IN1 Pin 8 ULN2003 IN2 Pin 11 ULN2003 IN3 Pin 12 ULN2003 IN4 Pin 13
Some ULN2003 boards have a small jumper next to the + and − pins that switches motor power on and off. If yours does, leave it on. The four LEDs on the board flash as the coils turn on: that's a free way to see the motor working. The Arduino's own "L" LED also flickers, because it's on pin 13. Both are normal.
Add the Stepper library to platformio.ini:
[env:uno]
platform = atmelavr
board = uno
framework = arduino
lib_deps =
arduino-libraries/Servo
arduino-libraries/Stepper
Tool: the Stepper library
A servo goes to an angle. A stepper moves a number of steps from wherever it is, and it doesn't know where it is. Your 28BYJ-48 takes about 2048 steps for one full turn of its shaft, because of the gearbox inside it.
Here's a shop sign that turns half a turn to show OPEN, waits, and turns back to show CLOSED:
#include <Arduino.h>
#include <Stepper.h>
const int STEPS_PER_TURN = 2048;
// Pins in the order IN1, IN3, IN2, IN4. Not a typo: see below.
Stepper sign(STEPS_PER_TURN, 8, 12, 11, 13);
void setup() {
sign.setSpeed(10); // 10 turns per minute
}
void loop() {
sign.step(1024); // half a turn one way: OPEN
delay(2000);
sign.step(-1024); // half a turn back: CLOSED
delay(2000);
}
step(1024)turns one way,step(-1024)turns the other. The program waits on that line until the motor finishes.setSpeed()is in turns per minute. This motor is slow: around 10 is right. If it buzzes and doesn't turn at a higher speed, slow it down.- The pin order. The library switches the motor's four coils on in an order meant for motors wired differently from this board. Giving it IN1, IN3, IN2, IN4 (pins 8, 12, 11, 13) puts the coils in the right order. With 8, 11, 12, 13 the motor shakes and buzzes in place instead of turning.
Tool: keep track of where you are
The motor doesn't know its position, so your code has to. Keep a variable that holds where it is, and change it every time you move.
Here's a pointer on a weather vane display, going to North (0°), East (90°), South (180°) and back:
#include <Arduino.h>
#include <Stepper.h>
Stepper vane(2048, 8, 12, 11, 13);
long position = 0; // in steps. 0 = where it was at power-up
// Degrees to steps. 2048 steps = 360 degrees.
long degToSteps(long degrees) {
return degrees * 2048 / 360;
}
// Go to a position (in steps), from wherever we are now.
void moveTo(long target) {
vane.step(target - position); // only the difference
position = target;
}
void setup() {
vane.setSpeed(10);
}
void loop() {
moveTo(degToSteps(90)); // East
delay(1000);
moveTo(degToSteps(180)); // South
delay(1000);
moveTo(degToSteps(0)); // back to North
delay(1000);
}
Going from East (512 steps) to South (1024), moveTo only steps the difference: 512.
Why long and not int
On the Uno, an int can't go past 32,767. 90 * 2048 is 184,320, so with int the math overflows and the motor goes somewhere strange. long holds numbers up to about 2 billion. Use long for anything in steps.
Mission 1 · Out and back
Line up the turret with your mark. In your catapult program, add the stepper, a position variable, degToSteps() and moveTo(). For a test, make setup() go to +90°, back to 0, to −90° and back to 0. Find out which way +90° turns, and write it down.
Done when
After the test, the line on the top plate lines up with the mark on the base again. The cables don't pull.
Mission 2 · Aim with the knob
Make the knob aim the turret. Turn the knob all the way one way: the turret points 90° to that side. All the way the other way: 90° to the other side. Middle: straight ahead.
map()turns the knob's 0–1023 into a target angle;constrain()keeps it between −90 and 90 no matter what.- Don't make the whole move in one
step()call. Move a few steps (try 8) toward the target each time throughloop(), so the fire button still works while you aim. - If the turret twitches back and forth while you're not touching the knob, only move when the target is more than a few steps away.
Firing still works exactly as before: only when armed.
Done when
Knob all the way left and right: the turret stops at 90° each side and never goes past. While it's turning, the fire button still works.
Mission 3 · The side shot
Put a cup 1 m away, off to one side (about 45°). Aim with the knob only. No touching the catapult except to cock and load.
Done when
You hit the side cup, aimed with the knob. Video it.
If you finish early¶
- Print the angle the turret is pointing at to the Serial Monitor.
- When it's disarmed and nobody's touching the knob for 10 seconds, return to straight ahead.
- The library leaves the coils on, which holds the turret firmly but keeps the motor warm. Write a
release()function that sets pins 8, 11, 12 and 13LOW, and call it when the turret has been still for a while. Does the turret still hold when you fire?
Sign-off with Dr. Alvear
- Out and back: the turret returns to its mark
- The knob aims, and the turret never passes ±90°
- Fire still works only when armed
- Video of the side-cup hit
- Build log updated (photo + two sentences)