G+_James Hughes Posted October 2, 2017 Share Posted October 2, 2017 So I know this is possible Fr. Robert Ballecer, SJ to control the direction of a stepper motor with a potentiometer. I've found a bunch of tutorials that either use two buttons to control the direction and the potentiometer is just the speed controller or they use a proprietary board that is more than I want to spend on this project. I have a nema 17 stepper motor, a 10k potentiometer and a l298n motor driver. Can someone point me to a tutorial on how to use these items to control the stepper motor, when you turn the potentiometer in one direction the stepper rotates to match. And when you turn the potentiometer the other direction the stepper also turns likewise. Thanks in advance (and sorry if there is a bit of a tone, been looking and experimenting for a couple of days) Link to comment Share on other sites More sharing options...
G+_DigitalMatrixIO (Eric L Posted October 2, 2017 Share Posted October 2, 2017 Rotary encoder is what you are looking for. They look very similar to potentiometers. Link to comment Share on other sites More sharing options...
G+_Robert Gauld Posted October 2, 2017 Share Posted October 2, 2017 It could be done with a potentiometer (although an encoder would offer more accurate control). Read the value of the analog pin connected to the pot and work out which of three ranges it's in (e.g. lower half = forward, upper half = reverse & bit in the middle = off). Unless it's off move the motor one step then map the pot's value to a new range to control the delay. Repeat. Link to comment Share on other sites More sharing options...
G+_Jim Hofmann Posted October 2, 2017 Share Posted October 2, 2017 I assUme you want the motor stopped if knob is centered. As the knob is turned CW the motor goes forward, the farther CW the faster the motor runs. The same for CCW and the motor goes in reverse. If I remember correctly, the pot input will return a value 0 to 1024 and the stepper control needs -255 (rev) to +255 (fwd) or a full range of 512. Simple solution is to divide the AI by 2 and subtract 255. And condition with some deadband. if( pot < -25 || pot > 25 ){ cmd = (pot / 2 ) - 255; }else{ cmd = 0; } Good Luck. Link to comment Share on other sites More sharing options...
G+_Scott W Posted October 3, 2017 Share Posted October 3, 2017 /* * * Pan Tilt sketch for outside camera test * RSW - 2-16-2016 Testing with one stepper and one Potentiometer * RSW - 2-18-2016 Added Stop Rotation if Potentiometer value is between 480 - 530 * RSW - 2-27-2016 Added code for tilt stepper motor * */ // Define pan motor pins int panPin1 = 5; int panPin2 = 6; int panPin3 = 7; int panPin4 = 8; // Define Tilt motor pins int tiltPin1 = 10; int tiltPin2 = 11; int tiltPin3 = 12; int tiltPin4 = 13; int tiltSpeed = 0; // variable to set tilt motor speed int panSpeed = 0; // variable to set pan motor speed int potpPin = A2; // pan potentiometer connected to A2 int pottPin = A3; // tilt potentiometer connected to A3 int potpValue = 0; // variable to read A2 pot input int pottValue = 0; // variable to read A3 pot input void setup() { // define motor pins as output pinMode(panPin1, OUTPUT); pinMode(panPin2, OUTPUT); pinMode(panPin3, OUTPUT); pinMode(panPin4, OUTPUT); pinMode(tiltPin1, OUTPUT); pinMode(tiltPin2, OUTPUT); pinMode(tiltPin3, OUTPUT); pinMode(tiltPin4, OUTPUT); // Serial.begin(115200); // comment out serial monitor to use digital pin 1 } void loop(){ // Pan Stepper potpValue = analogRead(potpPin); // read the value of the potentiometer // Serial.println(potpValue);// Send pot value to Serial Monitor // comment out to use digital pin 1 if (potpValue < 480){ //if potentiometer reads 0 to 480 do this panSpeed = (potpValue/15 + 8); //scale potValue for motor clockwise(); //go to the cw rotation function } else if ((potpValue > 480) && (potpValue < 530)){ // if potValue is between 480 - 530 stop rotation stoppanrotation(); } else { //value of the potentiometer is 531 - 1020 panSpeed = ((1019-potpValue)/15 + 8); //scale potValue for motor speed counterclockwise(); //go to the ccw rotation function } // Tilt Stepper pottValue = analogRead(pottPin); // read the value of the potentiometer // Serial.println(pottValue);// Send pot value to Serial Monitor // comment out to use digital pin 1 if (pottValue < 480){ //if potentiometer reads 0 to 480 do this tiltSpeed = (pottValue/15 + 8); //scale potValue for motor tiltclockwise(); //go to the cw rotation function } else if ((pottValue > 480) && (pottValue < 530)){ // if potValue is between 480 - 530 stop rotation stoptiltrotation(); } else { //value of the potentiometer is 531 - 1020 tiltSpeed = ((1019-pottValue)/15 + 8); //scale potValue for motor speed tiltcounterclockwise(); //go to the ccw rotation function } } // Pan Stepper Functions void clockwise(){ digitalWrite(panPin4, HIGH); digitalWrite(panPin3, LOW); digitalWrite(panPin2, LOW); digitalWrite(panPin1, LOW); delay(panSpeed); digitalWrite(panPin4, LOW); digitalWrite(panPin3, HIGH); digitalWrite(panPin2, LOW); digitalWrite(panPin1, LOW); delay (panSpeed); digitalWrite(panPin4, LOW); digitalWrite(panPin3, LOW); digitalWrite(panPin2, HIGH); digitalWrite(panPin1, LOW); delay(panSpeed); digitalWrite(panPin4, LOW); digitalWrite(panPin3, LOW); digitalWrite(panPin2, LOW); digitalWrite(panPin1, HIGH); delay(panSpeed); } void counterclockwise (){ digitalWrite(panPin1, HIGH); digitalWrite(panPin2, LOW); digitalWrite(panPin3, LOW); digitalWrite(panPin4, LOW); delay(panSpeed); digitalWrite(panPin1, LOW); digitalWrite(panPin2, HIGH); digitalWrite(panPin3, LOW); digitalWrite(panPin4, LOW); delay (panSpeed); digitalWrite(panPin1, LOW); digitalWrite(panPin2, LOW); digitalWrite(panPin3, HIGH); digitalWrite(panPin4, LOW); delay(panSpeed); digitalWrite(panPin1, LOW); digitalWrite(panPin2, LOW); digitalWrite(panPin3, LOW); digitalWrite(panPin4, HIGH); delay(panSpeed); } // Tilt Stepper Functions void tiltclockwise(){ digitalWrite(tiltPin4, HIGH); digitalWrite(tiltPin3, LOW); digitalWrite(tiltPin2, LOW); digitalWrite(tiltPin1, LOW); delay(tiltSpeed); digitalWrite(tiltPin4, LOW); digitalWrite(tiltPin3, HIGH); digitalWrite(tiltPin2, LOW); digitalWrite(tiltPin1, LOW); delay (tiltSpeed); digitalWrite(tiltPin4, LOW); digitalWrite(tiltPin3, LOW); digitalWrite(tiltPin2, HIGH); digitalWrite(tiltPin1, LOW); delay(tiltSpeed); digitalWrite(tiltPin4, LOW); digitalWrite(tiltPin3, LOW); digitalWrite(tiltPin2, LOW); digitalWrite(tiltPin1, HIGH); delay(tiltSpeed); } void tiltcounterclockwise(){ digitalWrite(tiltPin1, HIGH); digitalWrite(tiltPin2, LOW); digitalWrite(tiltPin3, LOW); digitalWrite(tiltPin4, LOW); delay(tiltSpeed); digitalWrite(tiltPin1, LOW); digitalWrite(tiltPin2, HIGH); digitalWrite(tiltPin3, LOW); digitalWrite(tiltPin4, LOW); delay (tiltSpeed); digitalWrite(tiltPin1, LOW); digitalWrite(tiltPin2, LOW); digitalWrite(tiltPin3, HIGH); digitalWrite(tiltPin4, LOW); delay(tiltSpeed); digitalWrite(tiltPin1, LOW); digitalWrite(tiltPin2, LOW); digitalWrite(tiltPin3, LOW); digitalWrite(tiltPin4, HIGH); delay(tiltSpeed); } void stoppanrotation(){ digitalWrite(panPin1, LOW); digitalWrite(panPin2, LOW); digitalWrite(panPin3, LOW); digitalWrite(panPin4, LOW); } void stoptiltrotation(){ digitalWrite(tiltPin1, LOW); digitalWrite(tiltPin2, LOW); digitalWrite(tiltPin3, LOW); digitalWrite(tiltPin4, LOW); } Link to comment Share on other sites More sharing options...
G+_James Hughes Posted October 3, 2017 Author Share Posted October 3, 2017 Jim Hofmann actually looking for something that makes the stepper turn as the potentiometer is turned. I don't need it to turn continuous in either direction. Link to comment Share on other sites More sharing options...
G+_James Hughes Posted October 3, 2017 Author Share Posted October 3, 2017 Scott W looks like what I was searching for. Thanks! Link to comment Share on other sites More sharing options...
G+_Robert Gauld Posted October 3, 2017 Share Posted October 3, 2017 In which case James Hughes?? an encoder will be much better suited to your needs - every time the encoder turns one step move the moter however many steps you want. Also unlike a potentiometer it will continuously turn rather than only having a limited sweep. Link to comment Share on other sites More sharing options...
G+_James Hughes Posted October 3, 2017 Author Share Posted October 3, 2017 Robert Gauld I'm okay with a limited sweep and I like the price of potentiometers better. But thanks for the input. Link to comment Share on other sites More sharing options...
G+_Telford Dorr Posted October 3, 2017 Share Posted October 3, 2017 One issue you will have to address: power-on motor initialization. On power up, the motor will likely be in a random position. You will need some way to sync it with the pot position. Maybe something on the motor shaft to indicate a 'home' position that the motor seeks on power up? Link to comment Share on other sites More sharing options...
G+_Jim Hofmann Posted October 4, 2017 Share Posted October 4, 2017 I had the same thought. To keep it cheap you could turn CCW until you hit an end-switch then count pulses sent. Just a thought. Link to comment Share on other sites More sharing options...
G+_James Hughes Posted November 21, 2017 Author Share Posted November 21, 2017 Scott W Thank you for the code. I commented out all of the "pan" code and enabled the serial monitor (had to slow it down to get it to display) and everything appears to be working. One problem is the motor still doesn't turn, it just kinda clicks. I've tried a few (okay, a whole lot) of variations of connecting the motors. Would you happen to have a wiring diagram for the l298n and stepper motor? Thanks in advance. Link to comment Share on other sites More sharing options...
G+_James Hughes Posted November 23, 2017 Author Share Posted November 23, 2017 Well it looks like I'm going to switch to the sn754410ne and using a potentiometer. The l298n and new code couldn't get the stepper motor to move. I've had a little bit of luck with the sn754410ne, just can't get enough holding torque from the stepper. Here's the code that I'm using, it's a modified version of the MotorKnob example code: /* * MotorKnob * * A stepper motor follows the turns of a potentiometer * (or other sensor) on analog input 0. * * http://www.arduino.cc/en/Reference/Stepper * This example code is in the public domain. */ #include // change this to the number of steps on your motor #define STEPS 200 // create an instance of the stepper class, specifying // the number of steps of the motor and the pins it's // attached to Stepper stepper(800, 8, 9, 10, 11); // the previous reading from the analog input //int previous = 0; int Pval = 0; int potVal = 0; void setup() { // set the speed of the motor to 30 RPMs stepper.setSpeed(300); Serial.begin(9600); } void loop() { potVal = map(analogRead(A0),30,750,0,200); if (potVal>Pval) stepper.step(1); if (potVal stepper.step(-1); Pval = potVal; // get the sensor value // int val = analogRead(0); // move a number of steps equal to the change in the // sensor reading // stepper.step(val - previous); // remember the previous value of the sensor // previous = val; Serial.print("Pot value:"); Serial.println(potVal); } photos.google.com - New video by James Hughes Link to comment Share on other sites More sharing options...
Recommended Posts