====== How To Control an Unipolar Stepper Motor with LEGO NXT Using Only Transistors ====== **Author:** Xinke (Rebecca) Cao \\ **Email:** caox1@unlv.nevada.edu \\ **Date:** Last modified on 08/04/2017 \\ **Keywords:** unipolar stepper motor, LEGO NXT, transistors, IRF510 \\ ===== Motivation and Audience ===== This is a continuation of the first option, but this tutorial is specifically for the audiences who doesn't have the access to ULN2003A Darlington transistor array. \\ In this tutorial, the focus will be on using the IRF510 N-channel MOSFET to control the stepper motor, where the IRF510 acts as a switch to sending the signal into the stepper motor for the motor to turn. \\ \\ \\ \\ ==== Parts List and Sources ==== * (1) 9V battery * (1) 9V battery snaps * (1) 28BYJ-48 5V unipolar stepper motor * (4) IRF510 * (4) 100k Ohms resistors * (1) PCF8574A * (1) NXT to breadboard adaptor * (1) NXT Brick * (various quantities) gauge wires Here is the [[https://youtu.be/TSvf9iBYM5g |demo]] of the motor turning with the following schematics and coding. \\ All the codes can be found [[https://github.com/XKRC/NXT_Stepper_Motor|here]], titled "Stepper_IRF510_Basic.nxc". \\ \\ \\ \\ ==== Circuit Schematics ==== Below is a picture of the circuit schematics. {{ :xinke:circuit_ver_2.png?600 |}} \\ D7 controls yellow. \\ D6 controls orange. \\ D1 controls pink. \\ D0 controls blue. \\ \\ \\ \\ ==== Construction ==== **Step 1** \\ \\ Insert a PCF8574A. \\ {{:xinke:img_7476.jpg?300|}} \\ \\ Insert the clock line from PCF8574A to the NXT adaptor. \\ {{:xinke:img_7475.jpg?300|}} \\ \\ Insert the data line. \\ {{:xinke:img_7474.jpg?300|}} \\ \\ Insert the voltage source to the PCF8574A. \\ {{:xinke:img_7473.jpg?300|}} \\ \\ Insert the ground lines, which will also be the ground to the NXT Brick. \\ {{:xinke:img_7472.jpg?300|}} \\ \\ **Step 2** \\ \\ Insert four IRF510 resistors. \\ {{:xinke:img_7471.jpg?300|}} \\ \\ **Step 3** \\ \\ Gather an NXT adaptor. \\ {{:xinke:img_7469.jpg?300|}} \\ \\ Insert the NXT adaptor. \\ {{:xinke:img_7468.jpg?300|}} \\ \\ **Step 4** \\ \\ Gather four 100k Ohms resistors. \\ {{:xinke:img_7467.jpg?300|}} \\ \\ Connect the resistors to the "Gate" pins of all of the transistors. \\ {{:xinke:img_7466.jpg?300|}} \\ \\ \\ {{:xinke:img_7465.jpg?300|}} \\ \\ \\ {{:xinke:img_7464.jpg?300|}} \\ \\ **Step 5** \\ \\ Connect the source of all transistors to the ground. \\ {{:xinke:img_7463.jpg?300|}} \\ \\ \\ {{:xinke:img_7462.jpg?300|}} \\ \\ \\ {{:xinke:img_7461.jpg?300|}} \\ \\ \\ {{:xinke:img_7460.jpg?300|}} \\ \\ **Step 6** \\ \\ Insert data lines from the correct output pins from the PCF8574A to the corresponding transistor's "drain" pin. \\ {{:xinke:img_7458.jpg?300|}} \\ \\ \\ {{:xinke:img_7457.jpg?300|}} \\ \\ **Step 7** \\ \\ Connect the center line (red) of the stepper motor to a 9V source and the other four lines to the corresponding "drain" pin of the transistors. \\ {{:xinke:img_7456.jpg?300|}} \\ \\ \\ {{:xinke:img_7455.jpg?300|}} \\ \\ **Step 8** \\ \\ Connect the battery. \\ {{:xinke:img_7454.jpg?300|}} \\ \\ \\ \\ \\ \\ ==== Programming ==== First, I defined where the input port and the address for the PCF8574 will be. #define I2Cport S1 // Port number #define I2CAddr8574 0x70 // I2C address Then, in the main task, the variables for the function I2CBytes() will be initialized. byte WriteBuf[2]; byte ReadBuf[]; int RdCnt = 1; Next, since the PCF8574A is connected to the NXT on input port S1; therefore, PCF8574A will be initialized and set up for writing to the PCF8574. SetSensorLowspeed (I2Cport); // PCF8574A connect to NXT on S1 WriteBuf[1] = 0x00; // i.e. write zeros to port sets up PCF8574A for writing WriteBuf[0] = I2CAddr8574; // i.e. address is 0x70 I2CBytes(S1, WriteBuf, RdCnt, ReadBuf); To allow the motor to turn, a sequence of data has to be send to the motor in order for the motor to turn in a certain direction. In this case, the motor will be turning in clockwise direction. \\ The sequence that the PCF8574A will be sending to allow the transistors to open or close is as follows: WriteBuf[1] = 192; //1100 0000 I2CBytes(S1, WriteBuf, RdCnt, ReadBuf); Wait(10); WriteBuf[1] = 130; //1000 0010 I2CBytes(S1, WriteBuf, RdCnt, ReadBuf); Wait(10); WriteBuf[1] = 3; //0000 0011 I2CBytes(S1, WriteBuf, RdCnt, ReadBuf); Wait(10); WriteBuf[1] = 65; //0100 0001 I2CBytes(S1, WriteBuf, RdCnt, ReadBuf); Wait(10); \\ \\ \\ \\ ==== Final Words ==== Comparing version one and version two of the stepper motor control, the only difference is the circuit diagram, which resulted in different numbers sent out of the PCF8574A from the LEGO Brick. \\ If you have any questions, please contact me at caox1@unlv.nevada.edu.