Page 1 of 3 123 LastLast
Results 1 to 15 of 36

Thread: Arduino Help ?

  1. #1
    Join Date
    Dec 2010
    Location
    WNY
    Posts
    10,083

    Arduino Help ?

    I'm trying to replace the noisy DC gear motor that rewinds my clock with a stepper and two Hall effect limit switches. I can make the stepper run with a Sketch like this for the Uno:
    The Hall effect sensors are listed here but play no role in the rest of it, yet.


    #include <ezButton.h>
    #include <AccelStepper.h>

    // Motor interface type must be set to 1 when using a driver
    #define DIR 2
    #define STEP 3
    #define motorInterfaceType 1
    #define HALL_SENSOR_A A0
    #define HALL_SENSOR_B A2

    // Create a new instance of the AccelStepper class:
    AccelStepper stepper = AccelStepper(motorInterfaceType, STEP, DIR);


    void setup()
    {
    // Setup the stepper controller pins as Outputs
    pinMode(DIR,OUTPUT);
    pinMode(STEP,OUTPUT);

    // Setup the Hall Effect switches as Inputs
    pinMode(HALL_SENSOR_A, INPUT);
    pinMode(HALL_SENSOR_B, INPUT);

    stepper.setMaxSpeed(1000.0); // set the maximum speed
    stepper.setAcceleration(100.0); // set acceleration
    stepper.setSpeed(200); // set initial speed

    }

    void loop(){

    stepper.runSpeed();
    }


    The single limit switch in the following Sketch will turn on the motor and it will run for a certain number of revolutions.

    #include <ezButton.h>
    #include <AccelStepper.h>

    // Motor interface type must be set to 1 when using a driver
    #define dirPin 2
    #define stepPin 3
    #define motorInterfaceType 1

    // Create a new instance of the AccelStepper class:
    AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);


    #define MAX_POSITION 5000 // 0x7FFFFFFF // maximum of position we can set (long type)


    ezButton limitSwitch_1(A0); // create ezButton object that attach to pin A0;
    ezButton limitSwitch_2(A2);

    bool isStopped = true; //false;

    void setup() {
    Serial.begin(9600);

    limitSwitch_1.setDebounceTime(50); // set debounce time to 50 milliseconds
    limitSwitch_2.setDebounceTime(50); // set debounce time to 50 milliseconds

    stepper.setMaxSpeed(500.0); // set the maximum speed
    stepper.setAcceleration(100.0); // set acceleration
    stepper.setSpeed(400); // set initial speed
    stepper.setCurrentPosition(0); // set position

    //stepper.moveTo(MAX_POSITION);
    }

    void loop() {
    limitSwitch_1.loop(); // MUST call the loop() function first

    if (limitSwitch_1.isPressed())
    {
    stepper.moveTo(MAX_POSITION);
    Serial.println(F("The limit switch: TOUCHED"));
    isStopped = false; //true;
    //stepper.run();
    //delay(5000);
    }

    if (isStopped == false)
    {
    stepper.run();
    if (stepper.distanceToGo() == 0) // if motor moved to the target position
    {
    stepper.setCurrentPosition(0); //reset position to 0
    //stepper.moveTo(MAX_POSITION); // move the motor to maximum position again
    isStopped = true;
    Serial.println(F("The stepper motor is STOPPED"));
    }
    }
    }


    This Sketch actually would work for my needs, except I'd have to tune the Max Position value for the motor to lift the weight the correct distance. Doable, but not as straight forward as having one switch to turn it on and a second to turn it off.

    Anyone here with enough experience on how to use one switch to turn on the motor, like the second Sketch does, but then turn it off with a second switch? I've driven myself nuts looking at YouTube and Arduino related videos and documents. You would think someone would specifically address this situation, but I haven't found it. Any help is most appreciated.

    John

  2. #2
    Join Date
    Apr 2017
    Location
    Tucson, Arizona
    Posts
    1,227
    I would recommend using a latching relay (or solid state latching relay) in your circuit - then one limit switch to turn it on and the other limit switch to turn it off. The relay will turn on/off your stepper motor drive.

    David

    https://www.pcbway.com/blog/PCB_Basi..._d665ce9b.html

  3. #3
    Join Date
    Oct 2007
    Location
    N of DFW
    Posts
    65
    IIRC, and YMMV, but initial state of (some) (all) Hall Effect switches are unknown.

  4. #4
    Join Date
    Dec 2010
    Location
    WNY
    Posts
    10,083
    Quote Originally Posted by David Buchhauser View Post
    I would recommend using a latching relay (or solid state latching relay) in your circuit - then one limit switch to turn it on and the other limit switch to turn it off. The relay will turn on/off your stepper motor drive.

    David

    https://www.pcbway.com/blog/PCB_Basi..._d665ce9b.html
    That's how I have it set up now with the mechanical relays I'm using with the DC motor now in use. It works really well. I'm sure the same approach would work with the Arduino IF I can figure out the code. That's what I'm asking for here, help with the code. The Sketch above will trigger the first Hall effect switch and run the motor for a certain number of revs. What I'd rather have is one sensor to start the motor and a second to turn it off. That way I can install the two sensors where I want them, rather than play suck and seek with the first option to have the motor stop where I want.


    Thanks.

    John

  5. #5
    Join Date
    Apr 2017
    Location
    Tucson, Arizona
    Posts
    1,227
    Quote Originally Posted by John TenEyck View Post
    That's how I have it set up now with the mechanical relays I'm using with the DC motor now in use. It works really well. I'm sure the same approach would work with the Arduino IF I can figure out the code. That's what I'm asking for here, help with the code. The Sketch above will trigger the first Hall effect switch and run the motor for a certain number of revs. What I'd rather have is one sensor to start the motor and a second to turn it off. That way I can install the two sensors where I want them, rather than play suck and seek with the first option to have the motor stop where I want.


    Thanks.

    John
    This looks like a fun little project. My personal experience using Arduinos is with GRBL programming which converts g-code to stepper motor command/control for things like 3 axis cnc routers, etc. I have no experience with the Sketch method you are attempting to use for your project. Another option for you would be to forget about the Arduino control and go with a simple stepper controller circuit and use your existing relays to turn the stepper motor on and off.

    https://github.com/electrical-pro/Stepper_without_programming/

    stepper motor controller without arduino.jpg

  6. #6
    Join Date
    Apr 2017
    Location
    Tucson, Arizona
    Posts
    1,227
    Quote Originally Posted by George Speed View Post
    IIRC, and YMMV, but initial state of (some) (all) Hall Effect switches are unknown.
    I have no idea what "IIRC" and "YMMV" mean. Please use your words.
    Thanks,
    David

  7. #7
    Quote Originally Posted by David Buchhauser View Post
    I have no idea what "IIRC" and "YMMV" mean. Please use your words.
    Thanks,
    David
    Maybe this’ll help. SMC acronym list: https://sawmillcreek.org/showthread....st-Of-Acronyms
    Last edited by Frederick Skelly; 06-23-2024 at 7:37 PM.
    "All that is necessary for the triumph of evil is that good men do nothing."

    “If you want to know what a man's like, take a good look at how he treats his inferiors, not his equals.”

  8. #8
    Join Date
    Apr 2017
    Location
    Tucson, Arizona
    Posts
    1,227
    Quote Originally Posted by Frederick Skelly View Post
    Maybe this’ll help. SMC acronym list: https://sawmillcreek.org/showthread....st-Of-Acronyms
    Thanks for that! No way I wish to wade thru a long list like that just to try and find out what the poster means. He needs to learn to use his words. How hard it it to type a few more words to his abbreviated post? But thanks for your reply.

    David

  9. #9
    Join Date
    Jul 2007
    Location
    NE OH
    Posts
    2,701
    John, I took a quick look at your sketch. I think what you want to do is use a function that just moves the stepper 1 step. In the basic stepper library this is step(), I imagine the acelstepper library has a similar function. Once your first sensor is made, you loop moving the motor one step plus a short delay, while checking to see if the second sensor is made.

    Here's a quick flowchart segment of the logic I think you need, if I understand what you want to do. Not robust, and doesn't have any of the initialization and housekeeping, but illustrates the basic logic.

    Flow.jpg

    If you need more, let me know.
    --I had my patience tested. I'm negative--

  10. #10
    Join Date
    Dec 2010
    Location
    WNY
    Posts
    10,083
    Thanks Paul. Yes, I think you are right. I saw some posts on the Arduino site with that logic and I think it will work if I get the commands right.

    John

  11. #11
    Join Date
    Dec 2010
    Location
    WNY
    Posts
    10,083
    Thanks David. I thought about doing it that way, and might well have if I were keeping the clock base I now have because the mechanical relays and wiring are already installed. But I'm making a new base to move the clock closer to the wall, so I thought I'd take a leap and use an Arduino. One advantage is that you can bring the motor up to speed slowly and avoid a sudden jerk like the DC motor I now use tends to do. But I'll keep this as a fall-back approach. Thanks.

    John

  12. #12
    Join Date
    Apr 2017
    Location
    Tucson, Arizona
    Posts
    1,227
    Quote Originally Posted by John TenEyck View Post
    Thanks David. I thought about doing it that way, and might well have if I were keeping the clock base I now have because the mechanical relays and wiring are already installed. But I'm making a new base to move the clock closer to the wall, so I thought I'd take a leap and use an Arduino. One advantage is that you can bring the motor up to speed slowly and avoid a sudden jerk like the DC motor I now use tends to do. But I'll keep this as a fall-back approach. Thanks.

    John
    John - I will look forward to seeing your result - whichever path you choose. Doing it with the Arduino is probably more fun - and more of a challenge - than the non-Arduino method. I'm sure it's just a matter of coding it - seems pretty simple and I think that Paul has perhaps put you on the right track.

    David

  13. #13
    Join Date
    Dec 2010
    Location
    WNY
    Posts
    10,083
    Quote Originally Posted by David Buchhauser View Post
    John - I will look forward to seeing your result - whichever path you choose. Doing it with the Arduino is probably more fun - and more of a challenge - than the non-Arduino method. I'm sure it's just a matter of coding it - seems pretty simple and I think that Paul has perhaps put you on the right track.

    David
    A friend of mine suggested I try Chat GPT, so I gave it a try. Holy cow, it gave me the answer on the first query. Amazing and scary at the same time. I haven't tried it yet, but it sure looks like it will work.

    // Define pin numbers for Hall Effect sensors
    const int hallSensorOnPin = 2; // Hall Effect sensor to turn on stepper motor
    const int hallSensorOffPin = 3; // Hall Effect sensor to turn off stepper motor

    // Define pin numbers for stepper motor control
    const int stepPin = 8; // Step pin for stepper motor driver
    const int dirPin = 9; // Direction pin for stepper motor driver
    const int enablePin = 10; // Enable pin for stepper motor driver

    // Variables to store Hall Effect sensor states
    volatile bool motorOn = false; // Flag to track if motor is on

    void setup() {
    // Initialize serial communication for debugging
    Serial.begin(9600);

    // Set up Hall Effect sensor pins as inputs with internal pull-up resistors
    pinMode(hallSensorOnPin, INPUT_PULLUP);
    pinMode(hallSensorOffPin, INPUT_PULLUP);

    // Set up stepper motor control pins
    pinMode(stepPin, OUTPUT);
    pinMode(dirPin, OUTPUT);
    pinMode(enablePin, OUTPUT);

    // Initially disable the stepper motor driver
    digitalWrite(enablePin, HIGH);

    // Attach interrupts to Hall Effect sensor pins
    attachInterrupt(digitalPinToInterrupt(hallSensorOnPin), onMotorOn, RISING);
    attachInterrupt(digitalPinToInterrupt(hallSensorOffPin), onMotorOff, RISING);
    }

    void loop() {
    // Motor control happens in interrupt service routines, so nothing needed here
    }

    // Interrupt service routine for Hall Effect sensor to turn on motor
    void onMotorOn() {
    if (!motorOn) {
    motorOn = true;
    Serial.println("Motor ON");
    digitalWrite(enablePin, LOW); // Enable the stepper motor driver
    // Additional code to set motor direction and steps as needed
    // Example:
    digitalWrite(dirPin, HIGH); // Set direction (example: clockwise)
    // Example: send steps to driver
    // digitalWrite(stepPin, HIGH);
    // delayMicroseconds(500);
    // digitalWrite(stepPin, LOW);
    // delayMicroseconds(500);
    }
    }

    // Interrupt service routine for Hall Effect sensor to turn off motor
    void onMotorOff() {
    if (motorOn) {
    motorOn = false;
    Serial.println("Motor OFF");
    // Additional code to stop the motor
    digitalWrite(enablePin, HIGH); // Disable the stepper motor driver
    // Additional code to stop motor motion if necessary
    }
    }

    It feels like cheating, but I'm not being graded so I'm OK with it.

    John

  14. #14
    Join Date
    Apr 2017
    Location
    Tucson, Arizona
    Posts
    1,227
    John - what specifically did you ask it? Perhaps you could share a screen shot or two showing your conversation with it.
    This is something (Chat GPT) that I have not heard of before - but it looks very interesting.

    Thanks,
    David

  15. #15
    Join Date
    Dec 2010
    Location
    WNY
    Posts
    10,083
    Quote Originally Posted by David Buchhauser View Post
    John - what specifically did you ask it? Perhaps you could share a screen shot or two showing your conversation with it.
    This is something (Chat GPT) that I have not heard of before - but it looks very interesting.

    Thanks,
    David
    I think I typed something like "Show me a Sketch for an Arduino Uno with a stepper motor being turned on by one Hall effect sensor and being turned off by a second sensor." Chat GPT is an AI platform. It searches for info and creates a targeted response, not links to information that contains some of the keywords you entered, which is what I got from Google and Microsoft. I can see kids creating term papers that will be hard to determine if they wrote it or Chat GPT was the author. I'm sure that already is a problem.

    John

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •