HCMotor Arduino library for driving DC and stepper motors.

Useful guides, libraries, and example sketches to support our Arduino based products.
andrew
Site Admin
Posts: 786
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCMotor Arduino library for driving DC and stepper motor

Post by andrew » Mon Jan 29, 2018 10:23 am

I'm afraid the HCMotor library doesn't have a clock (step) multiplier feature. Mainly as that's not what the library is intended for. Also a TB6560 can only divide down a clock. I believe you need to multiply up your clock by a factor of 8 (?). Depending on your maximum step frequency you'd probably be able to do it with something like an Uno but you'll need a clock multiplier / PLL sketch to do achieve this.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

josht
Posts: 2
Joined: Wed Mar 14, 2018 10:28 pm

Re: HCMotor Arduino library for driving DC and stepper motor

Post by josht » Thu Mar 15, 2018 4:31 pm

Hi Andrew, I want to know how to use this code control 4 stepper motors? how to define more then one CLK_PIN in code?
Thanks :D

andrew
Site Admin
Posts: 786
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCMotor Arduino library for driving DC and stepper motor

Post by andrew » Fri Mar 16, 2018 11:24 am

You can use the HCMotor.attach() function to add more motors (default maximum is 4 motors) like this:

#define MOTOR_PIN0 7
#define MOTOR_PIN1 8
#define MOTOR_PIN2 9
#define MOTOR_PIN3 10

HCMotor.attach(0, STEPPER, MOTOR_PIN0);
HCMotor.attach(1, STEPPER, MOTOR_PIN1);
HCMotor.attach(2, STEPPER, MOTOR_PIN2);
HCMotor.attach(3, STEPPER, MOTOR_PIN3);

From then on when you use any other of the library functions just set the MotorNum parameter to a value of either 0, 1, 2, or 3 depending on which motor you want it to apply to. E.g.

HCMotor.Direction(2, FORWARD);

Sets motor number 2 to the forward direction.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

josht
Posts: 2
Joined: Wed Mar 14, 2018 10:28 pm

Re: HCMotor Arduino library for driving DC and stepper motor

Post by josht » Fri Mar 16, 2018 10:00 pm

very helpful. Thanks Andrew!!

csaba85
Posts: 1
Joined: Sat May 05, 2018 3:19 pm

Re: HCMotor Arduino library for driving DC and stepper motor

Post by csaba85 » Sat May 05, 2018 3:32 pm

Hello Andrew! I'm verry newbie, never programmed. Can you help me, how can I add end limit switches to the the code? Example: we have two end point, when one is reach, motor stop, and it can only operate in the opposite direction. I think it would be useful to others too.

andrew
Site Admin
Posts: 786
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCMotor Arduino library for driving DC and stepper motor

Post by andrew » Sun May 06, 2018 7:46 am

Assuming your endstops are just clean contact switches you can just connect them to a couple of the digital pins on your Arduino. So for example you could connect the common & normally open contacts of the forward endstop to digital pin 3 and ground, and the common & normally open of the reverse direction to digital pin 4 and ground. In either case when the endstop is reached the switch will close and pull the pin low (low = false).

You then need to tell your Arduino that these two pins will be will be 'inputs' by putting this somewhere in the setup() section of the sketch:

Code: Select all

pinMode(3, INPUT_PULLUP);
 pinMode(4, INPUT_PULLUP);

Then on each iteration of the main() loop you can read the state of the two pins by putting this towards the top of the loop:

Code: Select all

boolean Forward_endstop = digitalRead(3);
boolean Reverse_endstop = digitalRead(4);

Finally, in the sections of code that checks the position of the pot to determine which direction to turn the motor in you can add and extra condition that also checks the state of the appropriate endstop. So for the stepper motor example you would change this:

Code: Select all

if (Pot >= POT_REV_MIN && Pot <= POT_REV_MAX)
to this...

Code: Select all

if (Pot >= POT_REV_MIN && Pot <= POT_REV_MAX && Reverse_endstop != false)

and this...

Code: Select all

}else if (Pot >= POT_FWD_MIN && Pot <= POT_FWD_MAX)
to this

Code: Select all

}else if (Pot >= POT_FWD_MIN && Pot <= POT_FWD_MAX && Forward_endstop != false)
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Mooslee
Posts: 1
Joined: Mon Aug 20, 2018 6:54 am

Re: HCMotor Arduino library for driving DC and stepper motors.

Post by Mooslee » Mon Aug 20, 2018 7:04 am

Hello all

#include <HCMotor.h>
HCMotor.Init();

Post Reply