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: 907
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: 907
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: 907
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.

Teryo
Posts: 4
Joined: Mon Aug 12, 2019 7:55 pm

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

Post by Teryo » Thu Aug 29, 2019 8:09 am

Hi Andrew,

I have been using your HCMotor Arduino library for driving DC and stepper motors with Arduino Mega and Nano. It works very perfect. Now I try to use it with board Pololu A-Star 32U4 and run in a trouble.

Well, I am very new in programming, but understood that the problem is with setting the timers. I got errors with:
TCCR2A, WGM21, TCCR2B, TCNT2, OCR2A and TIMSK2.

I try to figure out how to solve the problem but is seems to me that I have much more to learn before be able to do it myself ;) .

My Q is: Can you help me solving the problem?
Thanks

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

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

Post by andrew » Thu Aug 29, 2019 12:45 pm

The library uses a hardware timer that is specific to the ATMega328 which is the device used in Arduino Unos, Nanos, pro minis, etc. Unfortunately it is not compatible with 32U4 based boards which is why you're seeing those errors.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Teryo
Posts: 4
Joined: Mon Aug 12, 2019 7:55 pm

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

Post by Teryo » Fri Aug 30, 2019 6:18 am

Andrew,
thank you for your express answer... :D .

I read at the datasheet about the 32U4 hardware timers. It has 4 times as stated:
One 8-bit Timer/Counter with Separate Prescaler and Compare Mode
Two 16-bit Timer/Counter with Separate Prescaler, Compare- and Capture Mode
One 10-bit High-Speed Timer/Counter with PLL (64MHz) and Compare Mode

that is clear, but the problem persists.

Cheers

P.S. Still there is a long way to go until I get the right place... ;)

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

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

Post by andrew » Sun Sep 01, 2019 7:56 am

The 32U4 does have hardware timers but the code to set them up and use them is different to the timers in the ATMega328 which is why the library doesn't work when you try to compile it for the 32U4.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Post Reply