← All posts

Building a Bluetooth Rotary Lock, Part 1: From Breadboard to BLE

· 7 min read

  • #esp32
  • #arduino
  • #stepper-motor
  • #a4988
  • #ble
  • #diy

I want to lock and unlock a rotary door lock from my phone. No cloud, no subscription, no app store, just a stepper motor that turns the lock for me when I tap a button.

This is the first part of the build log. It covers everything up to a working prototype on the breadboard: a NEMA 17 stepper driven by an A4988, first from an Arduino Uno and then from an ESP32 that I control over Bluetooth Low Energy (BLE). Soldering it onto a real board and designing a 3D-printed enclosure are for part 2.

The breadboard prototype: Arduino Uno, A4988 driver and NEMA 17 stepper

The parts

Part What it does
NEMA 17 stepper (17HS3401S, 34 mm) Turns the lock
A4988 stepper driver (with heatsink) Converts STEP/DIR pulses into coil currents
Arduino Uno (clone) First test controller
ESP32-WROOM-32D The real controller, with BLE built in
9–12 V power source Motor supply for the driver
Multimeter More important than I expected

The full code, wiring and enclosure files live on GitHub: sudoShiba/rotary-lock.

Wiring the A4988

The A4988 is a cheap and well-documented stepper driver. On the logic side you only need two signals:

  • STEP: every pulse moves the motor one step
  • DIR: high or low decides the direction

I put the driver across the middle of the breadboard, connected STEP to pin 3 and DIR to pin 2 of the Arduino, fed VDD and GND from the Arduino’s 5 V, and bridged RESET and SLEEP so the driver is always awake. The MS1–MS3 pins stay unconnected, which means full-step mode: 200 steps per revolution.

The A4988 on the breadboard: logic on one side, motor and VMOT on the other

The other side of the driver takes the motor supply (VMOT and GND) and the two motor coils (1A/1B and 2A/2B).

Arduino Uno with the STEP, DIR, 5 V and GND wires

Setting the current limit (Vref)

Before letting the motor spin, you have to tell the A4988 how much current it may push through the coils. Too much and the driver or the motor overheats. That’s what the small potentiometer on the board is for.

The formula is:

I_max = Vref / (8 × R_sense)

R_sense is the value of the two tiny current-sense resistors next to the chip. On the red “StepStick” clones they’re usually marked R100, which means 0.1 Ω, so the formula becomes Vref = I × 0.8.

So what current does my motor want? The label says 17HS3401S, but the rated current depends on who you ask: some shops list it at 1 A, others at 1.3 A. I played it safe and aimed for 0.8 A, which means a Vref of 0.64 V.

The 17HS3401S: a short 34 mm NEMA 17

To measure it, put the multimeter on the 2 V DC range, black probe on GND and red probe on the metal screw of the potentiometer. I read 639 mV, which comes down to about 0.8 A. It was already right on target, so I didn’t have to touch the pot at all.

An old DT-830B multimeter: still perfectly good for this

Finding the coil pairs

A bipolar stepper has two coils, and each coil needs to go to one pair of outputs on the driver. Mix up the pairs and the motor just vibrates.

My motor has a 6-pin connector with only four pins in use. With the multimeter on the 200 Ω range I measured directly on the motor’s connector: pins 1–3 form one coil and pins 4–6 the other. Pins 2 and 5 are empty.

The 6-pin connector on the motor

Then came a small puzzle. Measuring at the other end of the cable gave me nothing on the “obvious” pairs. It turned out the two middle wires in the cable are crossed, so at the cable end the coils are on pins 1–4 and 3–6. Each coil measured about 3.7 Ω, which is exactly what you’d expect.

The lesson: always measure, never assume the cable is straight through.

The first spin (and a power supply adventure)

The A4988 needs at least 8 V on VMOT. I didn’t have a connector for my 12 V adapter yet, so I improvised.

First I tried a breadboard power module, but it converts its input down to 3.3 V or 5 V. I measured 3.2 V on VMOT and the motor didn’t move. Out of curiosity I switched the module to 5 V and, surprisingly, the motor did turn. That’s below the A4988’s specified minimum, so it’s not something to rely on, and the torque is low. A plain 9 V battery connected straight to VMOT worked much better for a short test.

The test sketch is as simple as it gets: one turn one way, wait, one turn back.

void rotate(bool direction) {
  digitalWrite(DIR_PIN, direction);
  for (int i = 0; i < 200; i++) {      // 200 steps = 1 revolution
    digitalWrite(STEP_PIN, HIGH);
    delayMicroseconds(2000);
    digitalWrite(STEP_PIN, LOW);
    delayMicroseconds(2000);
  }
}

Turning right unlocks, turning left locks. For now the lock gets one full revolution. I still need to measure how far my actual lock has to turn, and that’s a single number in the code.

Mac gotcha: “bad CPU type in executable”

Uploading to the Uno from my Apple Silicon Mac failed with:

fork/exec .../avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++: bad CPU type in executable

Downloading the Apple Silicon version of the Arduino IDE doesn’t help: the AVR compiler it downloads is still Intel-only. The fix is to install Rosetta 2:

softwareupdate --install-rosetta --agree-to-license

Moving to the ESP32

The Arduino was great for testing, but I want wireless control, so I swapped it for an ESP32-WROOM-32D. Only the four logic wires change:

A4988 ESP32
STEP GPIO 26
DIR GPIO 25
VDD 3V3 (not 5 V)
GND GND

The A4988 is happy with 3.3 V logic, so nothing else changes. This is the complete prototype wiring:

Wiring diagram: ESP32, A4988, motor supply and the motor cable pinout After installing the esp32 by Espressif Systems package in the Boards Manager and selecting ESP32 Dev Module, the same test sketch ran first try.

Controlling it over Bluetooth Low Energy

Why BLE and not Wi-Fi? It’s built into the ESP32, it works with an iPhone (classic Bluetooth serial doesn’t), and it means the lock doesn’t depend on my network. The trade-off is range: about 10 meters. For a lock that you only operate when you’re at the door, that’s fine.

The ESP32 exposes one BLE service with a writable “command” characteristic: write 1 to unlock and 0 to lock. One important detail: the BLE callback only stores the command, and the motor runs in the main loop. Running a two-second motor move inside the callback would block the Bluetooth stack.

class CommandCallback : public BLECharacteristicCallbacks {
  void onWrite(BLECharacteristic *characteristic) {
    String value = characteristic->getValue().c_str();
    if (value.length() == 0) return;
    if (value[0] == '1' || value[0] == 1) pendingCommand = UNLOCK;
    if (value[0] == '0' || value[0] == 0) pendingCommand = LOCK;
  }
};

For testing I used the free LightBlue app: connect to the ESP32, pick the characteristic, write 1, and the motor turns. Seeing that happen from my phone for the first time was the highlight of the evening.

Locking it down

A door lock that anyone in Bluetooth range can open is a bad idea, so the next step was pairing with a PIN. The command characteristic now requires an encrypted, authenticated connection, and the ESP32 uses a static 6-digit passkey with bonding. Your phone asks for the PIN once and remembers the lock after that. The PIN lives in a separate secrets.h file that never goes to GitHub.

At the time of writing I’m still ironing out the pairing flow on iOS. iPhones cache a lot about BLE devices, and “forget this device” turned out to be an important debugging step. More on that in part 2.

A nicer remote than LightBlue

Typing 1 into a BLE debugging app isn’t exactly a great user experience. Safari on iOS doesn’t support Web Bluetooth, but the free Bluefy browser does. So I made a tiny web page with three buttons: Connect, 🔓 Unlock and 🔒 Lock. It’s hosted on GitHub Pages and talks to the ESP32 directly from the phone. No app to build, no App Store.

What I learned

  • Measure everything. Current limit, coil pairs, supply voltage: the multimeter answered every “why doesn’t it work?” this time.
  • The A4988 needs a real motor supply. USB or 3.3 V won’t do it; plan for 9–12 V from the start.
  • Don’t trust cable pinouts. Mine had the middle wires crossed.
  • Keep BLE callbacks short. Store the command, do the work in loop().
  • Security from day one. Pairing with a PIN should be part of the prototype, not an afterthought.

Next up

In part 2 the breadboard becomes a real device:

  • A soldered protoboard with sockets for the ESP32 and the A4988
  • One 12 V supply for everything, with a buck converter for the ESP32
  • A manual push button and an enable pin, so the driver only draws power while moving
  • A parametric, 3D-printed enclosure and motor bracket
  • Fixing the iOS pairing and mounting it on the actual door

All code, wiring diagrams and enclosure files are on GitHub: sudoShiba/rotary-lock.