Built-in project · Arduino Uno R3

Remote stepper on an Arduino Uno

An IR receiver on pin 12 and a 28BYJ-48 on its ULN2003 board: VOL+ turns the shaft a quarter turn and VOL- turns it back. The whole build, an Arduino Uno and 3 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 About 25 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $35. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Decoding IR remote buttons with IRremote
  • Driving a 28BYJ-48 stepper with the Stepper library
  • Why step counts give a fixed angle
  • Releasing coils to keep a driver cool
  • Difficulty: Intermediate
  • Time: About 25 minutes
  • Board: Arduino Uno R3
  • Parts: 5

How it works

An IR receiver on pin 12 listens to the remote. When it decodes a new button press, the sketch checks the command code. VOL+ is 0x15 and VOL- is 0x07. Each turns a 28BYJ-48 stepper a quarter turn, which is 512 of its 2048 steps.

The motor is driven through a ULN2003 board on pins 8 to 11. After each move, release sets the four pins low so the coils stop heating. Repeat codes from a held button are ignored, and any other button just prints its code.

Parts list

5 parts, plus the jumper wires. They retail for about $35; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Infrared receiver, VS1838B pin OUTArduino Uno R3 pin 12
Stepper motor, 28BYJ-48 with ULN2003 pin IN4Arduino Uno R3 pin 11
Stepper motor, 28BYJ-48 with ULN2003 pin IN3Arduino Uno R3 pin 10
Stepper motor, 28BYJ-48 with ULN2003 pin IN2Arduino Uno R3 pin 9
Stepper motor, 28BYJ-48 with ULN2003 pin IN1Arduino Uno R3 pin 8
Stepper motor, 28BYJ-48 with ULN2003 pin VCCArduino Uno R3 pin 5V
Infrared receiver, VS1838B pin VCCArduino Uno R3 pin 5V
Stepper motor, 28BYJ-48 with ULN2003 pin GNDArduino Uno R3 pin GND
Infrared receiver, VS1838B pin GNDArduino Uno R3 pin GND
Infrared remote, 21 buttons pin IRInfrared receiver, VS1838B pin IR

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
// Remote stepper: VOL+ and VOL- on the IR remote turn a stepper motor.
//
//   IR receiver   OUT -> pin 12, GND -> GND, VCC -> 5V
//   ULN2003 board IN1 -> pin 8, IN2 -> pin 9, IN3 -> pin 10, IN4 -> pin 11
//                 + -> 5V, - -> GND
//
// Each press of VOL+ turns the shaft a quarter turn clockwise, and VOL- a
// quarter turn back. The 28BYJ-48 takes 2048 full steps for one turn of its
// output shaft, so a quarter turn is 512 steps.
//
// The Stepper library wants the pins as IN1, IN3, IN2, IN4: that is why the
// constructor below says 8, 10, 9, 11. Given 8, 9, 10, 11 the motor hums and
// does not turn.

// The handset speaks NEC, so only that decoder is compiled in, as IRremote's
// own examples do to save flash. Without this line the library brings in
// every protocol's decoder, about 9 KB of the Uno's 32.
#define DECODE_NEC
#include <IRremote.hpp>
#include <Stepper.h>

const int RECEIVER_PIN = 12;
const int STEPS_PER_TURN = 2048;
const int QUARTER = STEPS_PER_TURN / 4;

Stepper motor(STEPS_PER_TURN, 8, 10, 9, 11);

void release() {
  // Let the coils go between moves, so the driver stays cool.
  for (int pin = 8; pin <= 11; pin++) digitalWrite(pin, LOW);
}

void setup() {
  Serial.begin(9600);
  IrReceiver.begin(RECEIVER_PIN, ENABLE_LED_FEEDBACK);
  motor.setSpeed(15);
  Serial.println("Mokxi Uno: remote stepper. VOL+ and VOL- turn the shaft.");
}

void loop() {
  if (!IrReceiver.decode()) return;
  bool repeat = IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT;
  uint8_t command = IrReceiver.decodedIRData.command;
  IrReceiver.resume();
  if (repeat) return;
  if (command == 0x15) {
    Serial.println("VOL+: a quarter turn clockwise");
    motor.step(QUARTER);
    release();
  } else if (command == 0x07) {
    Serial.println("VOL-: a quarter turn back");
    motor.step(-QUARTER);
    release();
  } else {
    Serial.print("button 0x");
    Serial.print(command, HEX);
    Serial.println(" does nothing here");
  }
}

Code walkthrough

The key functions of the sketch, and what each one does.

release sketch.ino

Sets pins 8 to 11 LOW so no current flows in the motor coils. This is called after each move. The shaft is then not held in place, but the driver stays cool.

void release() {
  // Let the coils go between moves, so the driver stays cool.
  for (int pin = 8; pin <= 11; pin++) digitalWrite(pin, LOW);
}

setup sketch.ino

Opens serial, starts the IR receiver on pin 12 and sets the stepper speed to 15 rpm. It then prints a hello line telling you which buttons to use.

void setup() {
  Serial.begin(9600);
  IrReceiver.begin(RECEIVER_PIN, ENABLE_LED_FEEDBACK);
  motor.setSpeed(15);
  Serial.println("Mokxi Uno: remote stepper. VOL+ and VOL- turn the shaft.");
}

loop sketch.ino

Returns right away if no IR code is ready. Otherwise it reads the command, calls IrReceiver.resume for the next one and skips repeats. VOL+ steps QUARTER forward, VOL- steps it back, and other codes are printed.

void loop() {
  if (!IrReceiver.decode()) return;
  bool repeat = IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT;
  uint8_t command = IrReceiver.decodedIRData.command;
  IrReceiver.resume();
  if (repeat) return;
  if (command == 0x15) {
    Serial.println("VOL+: a quarter turn clockwise");
    motor.step(QUARTER);
    release();
  } else if (command == 0x07) {
    Serial.println("VOL-: a quarter turn back");
    motor.step(-QUARTER);
    release();
  } else {
    Serial.print("button 0x");
    Serial.print(command, HEX);
    Serial.println(" does nothing here");
  }
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The motor hums but does not turn This project
    The Stepper library wants the pins as IN1, IN3, IN2, IN4. The constructor says 8, 10, 9, 11 for that reason.
  • A held button keeps turning it This project
    It should not: repeats are skipped. If it does, check the sketch still has the repeat check.
  • Nothing reacts to the remote This project
    Check the receiver's OUT is on pin 12 and the remote is a NEC one sending VOL+ as 0x15.
  • The motor buzzes or shakes and does not turn. The coil pins IN1 to IN4 are in the wrong order for the library. Stepper motor
    For Arduino's Stepper library on a 28BYJ-48, create it with pins 8, 10, 9, 11 (IN1, IN3, IN2, IN4). Or use AccelStepper with HALF4WIRE and the same pin order.
  • It stalls or skips when spun fast or powered from the Arduino 5 V pin. It also draws more than a USB port may give. Stepper motor
    Power the ULN2003 board from a separate 5 V supply and join the grounds. Run it slowly, about 10 to 15 RPM, and ramp up speed gradually.
  • No signal is seen, or the module gets hot, because the three legs are in the wrong order. Infrared receiver
    On the VS1838B, with the dome facing you, the legs are usually OUT, GND, VCC from left to right. Check your module's marking before powering it. Supply is 2.7 to 5.5 V.
  • A plain LED blinking at the receiver is never detected. Infrared receiver
    The receiver only responds to light modulated at about 38 kHz. Send with a library such as IRremote, which makes the carrier. OUT idles high and goes low during a burst, so decode that inverted signal.
  • Button codes come out wrong or do not match another sketch. Infrared remote
    Remotes differ. Print the raw codes for your buttons first, then compare against those values. A repeat while you hold a button is a special repeat code, not a new press.

Change it and keep it

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