-
1Watch the Video
-
2Get All the Stuff
For this project, you will need:
- An Arduino microcontroller board
- A PS/2 touchpad from a laptop(Try to get one with a Synaptics chip onboard)
- *A ULN2003 stepper motor driver(For unipolar stepper motors(5-wire))
- *An L298N stepper motor driver(For bipolar stepper motors(4-wire) as well as unipolar stepper motors)
- 6 male to female jumper wires(2 for power and 4 for digital signals)
- A stepper motor
- A 5-12 volt DC power source(Depending on the stepper motor)
Here, the setup is powered from a mobile phone charger that supplies 5-volts to the Arduino board and the stepper driver. Even though the stepper motor is rated for 12-volt, you can use a lower voltage supply if the torque requirements of the motor are not high because using a lower voltage supply will keep the motor as well as the driver cooler.
*Both the stepper motor drivers have the same pin connections with the Arduino board.
-
3Get the Ps2 and Accel Stepper Libraries
Download the folder of the ps2 library from here. Move the downloaded folder to the desktop as it will be easier to find. Open the Arduino IDE and click Sketch>Include Library>Add .ZIP Library... and then select the ps2 folder from the desktop. The library will be included and you now be able to use the ps2 library.
For the Accel Stepper library, you can get it by pressing Ctrl+Shift+I and then typing in 'Accel Stepper' and installing the library.
-
4Figure Out the Connections of the Touchpad
If you have a Synaptics touchpad like the above one, the pad 'T22' is +5V, 'T10' is 'Clock', 'T11' is 'Data' and 'T23' is 'GND'. You can also solder the 'GND' wire to a large exposed copper as shown above.
Click on the above image to know more.
If you have a different touchpad, try searching for its part number on the internet with 'pinouts' or you can ask the r/Arduino community on Reddit if you get stuck.
-
5Test the Touchpad
Make sure the correct connections are made to the touchpad. To test the touchpad, upload the ps2 mouse code on the Arduino microcontroller from Examples>ps2. Connect 'Clock' wire to D6, 'Data' wire to D5, GND to GND, and +5V or VCC to +5V pin of the Arduino board respectively. Reconnect the Arduino board to the computer and open the serial monitor. If you see the numbers changing as you move your finger across the touchpad, the touchpad is working properly and you can proceed.
-
6Program the Arduino Microcontroller
Copy and paste any of the two codes in the Arduino IDE:
Code for stepper motor control with homing function and no acceleration/deceleration feature:
/* * Arduino code to control a stepper motor's postion using a PS/2 touchpad or mouse. * Made by Tech Build:-https://www.youtube.com/channel/UCNy7DyfhSD9jsQEgNwETp9g?sub_confirmation=1 * Code originally made to run a stepper motor using a * ULN2003 or similar driver(For unipolar motors) * OR * L298N or driver(For both unipolar and bipolar motors) * * Feel free to modify the code to adapt it to any oter motor driver or shield being used. * * The code is tested only with a PS/2 touchpad and might not work properly with a mouse! * * Swipe finger on the touchpad or move the mouse to make the motor rotate * in a particular direction for a particular number of steps. * * Tap the touchpad to move the motor back to starting position. */ #include <ps2.h> #include<Stepper.h> Stepper stepper(200, 8, 10, 9, 11);//If the motor steps backand forth, try replacing the //sequence of the pins to(200, 8, 9, 10, 11); #define statLED 13 char mstat; char mx; char my; int x, y, m; int p = 0; int sp = 120; /* * an arduino sketch to interface with a ps/2 mouse. * Also uses serial protocol to talk back to the host * and report what it finds. */ /* * Pin 5 is the mouse data pin, pin 6 is the clock pin * Feel free to use whatever pins are convenient. */ PS2 mouse(6, 5);//(Clock, Data) /* * initialize the mouse. Reset it, and place it into remote * mode, so we can get the encoder data on demand. */ void mouse_init() { mouse.write(0xff); // reset mouse.read(); // ack byte mouse.read(); // blank */ mouse.read(); // blank */ mouse.write(0xf0); // remote mode mouse.read(); // ack delayMicroseconds(100); } void setup() { pinMode(statLED, OUTPUT); Serial.begin(9600); mouse_init(); stepper.setSpeed(sp); } /* * get a reading from the mouse and report it back to the * host via the serial line. */ void loop() { /* get a reading from the mouse */ mouse.write(0xeb); // give me data! mouse.read(); // ignore ack mstat = mouse.read(); mx = mouse.read(); my = mouse.read(); if(x != 0) digitalWrite(statLED, HIGH); else digitalWrite(statLED, LOW); x = (int)mx; y = (int)my; m = (int)mstat; stepper.step(x); p += x; if(m == 9) { stepper.step(-p); p = 0; } }
Code for stepper motor control with acceleration/deceleration feature but no homing function(Uses the AccelStepper library):
/* * Arduino code to control a stepper motor's postion using a PS/2 touchpad or mouse. * Acceleration/decceleration feature, thanks to AccelSteper library. * Made by Tech Build:-https://www.youtube.com/channel/UCNy7DyfhSD9jsQEgNwETp9g?sub_confirmation=1 * Code originally made to run a stepper motor using a * ULN2003 or similar driver(For unipolar motors) * OR * L298N or driver(For both unipolar and bipolar motors) * * Feel free to modify the code to adapt it to any oter motor driver or shield being used. * * The code is tested only with a PS/2 touchpad and might not work properly with a mouse! * * Swipe finger on the touchpad or move the mouse to make the motor rotate * in a particular direction for a particular number of steps. * * Tap the touchpad while the motor is moving to make it stop instantly. */ #include<ps2.h> //Library for interfacing the PS/2 touchpad with the Arduino MCU. #include<Stepper.h> //Library for controlling the stepper motor. #include<AccelStepper.h>//Library for acceleration/deccelration feature, very useful for //stepper motor control. Stepper stepper(200, 8, 10, 9, 11);//If the motor steps backand forth, try replacing the //sequence of the pins to(200, 8, 9, 10, 11); #define statLED 13 char mstat; char mx; char my; int x, y, m; long p = 0; int d; /* * Pin 5 is the mouse data pin, pin 6 is the clock pin * Feel free to use whatever pins are convenient. */ PS2 mouse(6, 5);//(Clock, Data) /* * initialize the mouse. Reset it, and place it into remote * mode, so we can get the encoder data on demand. */ void mouse_init() { mouse.write(0xff); // reset mouse.read(); // ack byte mouse.read(); // blank */ mouse.read(); // blank */ mouse.write(0xf0); // remote mode mouse.read(); // ack delayMicroseconds(100); } void forwardstep() { stepper.step(1); } void backwardstep() { stepper.step(-1); } AccelStepper stepperM(forwardstep, backwardstep); void setup() { pinMode(statLED, OUTPUT); Serial.begin(9600); mouse_init(); stepperM.setMaxSpeed(250); stepperM.setAcceleration(500); } /* * get a reading from the mouse and report it back to the * host via the serial line. */ void loop() { /* get a reading from the mouse */ mouse.write(0xeb); // give me data! mouse.read(); // ignore ack mstat = mouse.read(); mx = mouse.read(); my = mouse.read(); m = (int)mstat; x = (int)mx; y = (int)my; //Serial.println(p); p += x; if(m == 9) { digitalWrite(statLED, HIGH); p = stepperM.currentPosition(); d = stepperM.distanceToGo(); stepperM.setAcceleration(10000); stepperM.moveTo(p); while(d != 0) { stepperM.run(); d = stepperM.distanceToGo(); } digitalWrite(statLED, LOW); stepperM.setAcceleration(500); } else digitalWrite(statLED, LOW); stepperM.moveTo(p); stepperM.run(); }
-
7Study the Circuit Schematic
-
8Make the Wiring Connections
Connect the stepper motor driver's power and digital input pins to the Arduino board as shown the circuit schematic in the previous step.
Connect the motor to the motor driver.
-
9Connect the Arduino Board to Power and Turn It On
After powering up the setup, slide your finger along the length of the touchpad and check if the motor moves.
-
10Troubleshooting
If you encounter some problems, you can do the following:
If the stepper motor steps back and forth instead of moving in a particular direction:
- Change the sequence of pin declaration. For example: replace Stepper stepper(200, 8, 10, 9, 11) with Stepper stepper(200, 8, 9, 10, 11).
- Check all the signal wires connecting the motor driver to the Arduino board as well the wires of the stepper motor are properly connected where they should be and are not loose and faulty.
If the motor does not move at all:
- Check if the touchpad is working correctly, go back to Step 5.
- Check if the motor driver and the touchpad are getting power.
- Check if the motor or the motor driver is faulty.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.