Sunday, June 28, 2015

Stepper Motors

I began my exploration of stepper motors by building the circuit described in the OSEPP Learn Arduino Basics book. The Arduino Basics Starter Kit they sell comes with a 28BYJ-48 stepper motor, which is a nice little low-cost and basic stepper. It also comes with a small stepper motor driver board containing a ULN2003A Darlington Transistor array.
Running their stepper with the provided driver is a simple task.

Here is a picture of the OSEPP driver:


The large IC in the driver is a ULN2003A Darlington Transistor Array. In addition to that, there is a set of LEDs which show which of the signals is active. I'm not sure what the component labelled RP1 is, though I assume a bank of resistors for the LEDs.

Now the motor itself is a unipolar stepper motor which means that there are two coils, each with a common center tap.  The two common center tap wires are tied together, which means that there are 5 wires for this motor. The red lead is the common center tap which should be attached to VCC. The pink and orange leads are the wires for one of the coils and the yellow and blue leads are for the other coil.


A very good description about how this motor works and how to drive it using a ULN2003A transistor array comes from Bret Stateham and is available on YouTube at

For the video above, I used a simple Adrunio Sketch that used the Stepper class to run the motor. Here is my own slightly modified version of the original sketch

#include <stepper.h>
int stepIN1Pin = 8;
int stepIN2Pin = 9;
int stepIN3Pin = 10;
int stepIN4Pin = 11;
int stepsPerRevolution = 2048;
Stepper myStepper(stepsPerRevolution,
                  stepIN1Pin, stepIN3Pin,
                  stepIN2Pin, stepIN4Pin);
void setup()
    // set the RPM
void loop()
    // step one revolution in one direction
    // wait a second
    // step one revolution in the other direction
    // wait a second

This seems to work well enough to just turn the motor forwards and backwards, but can I actually use this to do something useful?

Here's something interesting about the numbering of the output pins. The globals stepIN1Pin, etc. define pin 1-4 as being on GPIO outputs 8-11. However when passing the GPIO pin assignments to the constructor of Stepper, the assignments are:

Stepper myStepper(stepsPerRevolution,

Note how the ordering is different. Tracing the wires, I find that the blue lead from the motor is assigned to GPIO 8, the orange to GPIO pin 9, the yellow to GPIO pin 10, and the orange to GPIO 11.

I hooked my scope up to the circuit to see how the Stepper class drives the output signals.


You can see the operation of the full stepping in this image. Each coil is high for 2 cycles, followed by 2 low cycles. The period of a full iteration through the stepping progression is 8.2ms, so each cycle here is around 2.05ms, which is about as fast as this motor can drive.

Thursday, June 11, 2015

DC Motors

One of the first experiments I did with my Arduino Uno was to try to drive a motor. I followed the instructions at to run a very simple DC motor using the PWM output pins on the Arduino.

The Adafruit lesson suggests a PN2222 transistor, but I used a 2N3904 transistor, which is also a NPN type transistor. My understanding is that the primary difference between them is the amount of current that they can handle, the PN2222 being able to support more current. The datasheet says that the 2N3904 has a maximum collector current (IC) of 200 mA, which should be sufficient to drive a small DC motor.

The diode is a very important part of the circuit and protects your Arduino from currents coming from the inductive load of the motor.

I used the code from the sketch on the Adafruit site. This reads a value from the serial input and uses that to set the level on a PWM (pulse width modulation) output pin. I'll talk more about PWM later.

It was interesting to see how different values were able to run the motor at different speeds. For fun, I hooked this circuit up to a Rigol DS1054Z Oscilloscope to see the signals at the PWM output and also across the motor itself.

Tuesday, June 9, 2015

I, Robot

“The Three Laws of Robotics:

1: A robot may not injure a human being or, through inaction, allow a human being to come to harm;

2: A robot must obey the orders given it by human beings except where such orders would conflict with the First Law;

3: A robot must protect its own existence as long as such protection does not conflict with the First or Second Law;"

- Issac Asimov, I, Robot

Now, I doubt that I will ever built a sentient robot, but a robot I shall endeavor to build nonetheless.

My son has been playing with Lego Mindstorms for a few years now, and I've recently begun to play and experiment with Beaglebone Black and Arduino. I must admit that I find Arduino much more compelling, even though it is clearly less powerful. However, when I am doing electronics projects, I'm doing so because I like to fiddle with stuff, and I'd rather be writing C/C++ code (or even assembler!) than coding in JavaScript or Python and interacting with the GPIO subsystem through a kernel filesystem modules.  Yes, I've seen that you can use /dev/mem for direct GPIO access, but so far the Adruino Uno seems to be right at the level that I am interested in when programming microcontrollers.

Our first goal in robotics is to build an autonomous self-driving robot that can react to the environment around it and move about the house.  I'm thinking that a simple self-driving robot with some touch sensors to detect collisions and perhaps an ultrasonic range finder would be a great first project.

But before we can build our robot, we will spend a bit of time on some simple projects in which we will learn the basics of programming the Arduino and the various other components that we will use (motors, range finders, speakers, etc.).

I started out with the OSEPP 201 Arduino Starter Kit.  It has a nice ultrasonic sensor, a servo motor and a stepper motor, plus of course an Arduino Uno.


As I document our progress in building our first robot, I hope some of the things I learn may be of interest to other people playing with the fun world of Arduino.