I've done a lot of plc later style programming close to 30yrs, but I'm new to Arduino, Im trying to drive a actuator motor with an Arduino Due with a megmoto unit thru PWM pins A and B, and a button for rev and one for fwd and a speed control pot. Should be simple right, just send the analog input put out to the megamoto in forward mode with switch B and rev with switch B. but im lost on this coding. Any help in getting me going would be a great help.
Thanks
why not add the above code and use the pwm value instead of any not zero (i.e. LOW ) value to analogWrite()?
Thanks ill give it a try
I'm using a single motor with the megamotor on arduino. I would like to use outputs PWMA and B thru the mega and inputs on d3 & d4 with speedpot on A0
Not familiar with "megamotor", post link to datasheet and wiring diagram.
why don't you try modifying the code i posted?
I haven't been back to my shop yet today, ill try later tonight
Thanks
Its a megamoto from progressiveautiontion.com for use with an arduino unit with they're actuators, but i need speed control not position control
"Hmm. We’re having trouble finding that site."
tried as you wrote it but it didn't do anything
From the progressive automation .com site for using the MegaMoto Plus H-bridge for Arduino
I must use
const int enable = 8; //three pins for MegaMoto Outputs
const int PWMA = 11;
const int PWMB = 3;
And are use using 4 Pins 8,9,12,and 13 for the inputs?
Hi;

Its SHIELD.
Tom..
![]()
i'm not familiar with megamotor
Arduinos typically use motor shields to drive DC motors. motor shields typically use h-bridge chips which can typically drive 2 motor.
an h-bridge typically has an enable pin and 2 other pins: in1, in2 used to control direction and braking. in1 and in2 are typically opposite (LOW, HIGH) to determine direction
the enable pin is typically driven with a PWM signal. the in1 or in2 pin that is typically set HIGH could be driven with a PWM signal with the enable pin set HIGH, but it's more convenient to drive the enable pin with PWM and just set in1 and in2 HIGH/LOW
i don't understand why specific pins are identified unless a specific program is being used. can't find a spec sheet. don't understand what the board labels the pins the way it does
i'll guess that the "enable" pin is used to enable the board and is not the conventional h-bridge enable pin. it could probably just be pulled HIGH, connect to 5V
don't understand what the "input" pins are 4.
looks like it uses 2 BTN7960 which could be driven independently to control motor direction.
not clear how you've connected the shield to your motor
@kenpjr
This should work
const int A_PWM = 11;
const int B_PWM = 3;
const int Enable = 2;
void setup()
{
pinMode(A_PWM, OUTPUT);
pinMode(B_PWM, OUTPUT);
pinMode(Enable, OUTPUT);
digitalWrite (A_PWM,LOW);
digitalWrite (B_PWM,LOW);
digitalWrite (Enable,LOW);
}
void TurnRight(uint8_t pwm){
analogWrite(A_PWM, 0);
delayMicroseconds(200);
analogWrite(B_PWM, pwm);
}
void TurnLeft(uint8_t pwm){
analogWrite(B_PWM, 0);
delayMicroseconds(200);
analogWrite(A_PWM, pwm);
}
void Stop(){
analogWrite(B_PWM, LOW);
analogWrite(A_PWM, LOW);
}
void loop()
{
digitalWrite (Enable,HIGH);
TurnRight(128);
delay (5000);
Stop();
TurnLeft(200);
delay(5000);
}
your code turns on the D1 and D2 leds on the megamoto shield but not the motor drive outputs outputs
here's a program using a position pot and feedback that drives the motor, but won't work for my application
Im not sure how to change it to variable output for speed and not position
I'm not using feedback, I don't need to know where it is,
I just need to apply variable speed via an input pot so i can change speed when I need to as its moving via the pot while I have an input on
then change direction with a diff input also with speed via the pot
Again this is not what I need but it doe's drive the motor
Thanks for all your help
Kenny
/* Sample code to control the position of an actuator with potentiometer feedback using a MegaMoto.
The main loop of this program checks the potentiometer, and moves the actuator accordingly.
Written by Progressive Automations
This example code is in the public domain.
*/
const int feedback = A0; //potentiometer from actuator
const int pot = A1; //input pot from throttle
const int enable = 8;
const int PWMA = 11;
const int PWMB = 3;
int actMax = 760;
int actMin = 250;//positions of actuator
int potMin = 0;
int potMax = 1023;
int precision = 2;//how close to final value to get
int checkingInterval = 50;//how often position is checked (milliseconds)
int rawcurrentPosition = 0;
int currentPosition = 0;
int rawdestination = 0;
int destination = 0;
int difference = 0;//values for knowing location
void setup()
{
pinMode(feedback, INPUT);//feedback from actuator
pinMode(pot, INPUT);//feedback from potentiometer
pinMode(enable, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(PWMB, OUTPUT);//three pins for MegaMoto
digitalWrite(enable,HIGH);
Serial.begin(9600);
}
void loop()
{
destination = getDestination();
currentPosition = analogRead(feedback);//check where you are
Serial.print("Position ");
Serial.println(analogRead(feedback));
difference = destination - currentPosition;//find out how far you are from the destination
if (currentPosition > destination) pullActuatorUntilStop(destination);// choose what action to take
else if (currentPosition < destination) pushActuatorUntilStop(destination);
else if (difference < precision && difference > -precision) stopActuator();
}//end void loop
int getDestination()
{
rawdestination = analogRead(pot);//read the potentiometer to get the destination
destination = map(rawdestination, potMin,potMax,actMin,actMax);//convert the potentiometer feedback to match the actuator
return(destination);
}//end getDestination
void pushActuatorUntilStop(int destination)
{
destination = getDestination();
int temp = analogRead(feedback);
difference = destination - temp;//check difference to see if continue moving, or stop
while (difference > precision || difference < -precision)
{
destination = getDestination();
temp = analogRead(feedback); //continue checking difference
difference = destination - temp;
pushActuator();
}//end while
delay(25);
stopActuator();
}//end pushActuatorUntilStop
void pullActuatorUntilStop(int destination)
{
destination = getDestination();
int temp = analogRead(feedback); //check difference to see if continue moving, or stop
difference = destination - temp;
while (difference > precision || difference < -precision)
{
destination = getDestination();
temp = analogRead(feedback); //continue checking difference
difference = destination - temp;
pullActuator();
}//end while
delay(25);
stopActuator();
}//end pullActuatorUntilStop
void stopActuator()
{
analogWrite(PWMA,0);
analogWrite(PWMB,0);
}//end stopActuator
void pushActuator()
{
analogWrite(PWMB,255);
analogWrite(PWMA,0);
}//end pushActuator
void pullActuator()
{
analogWrite(PWMB,0);
analogWrite(PWMA,255);
}//end pullActuator
Hi,
To add code please click this link;
It will put your code in its own scrolling window?
Tom...
![]()
For debugging purposes, try my program again but change the TurnRight, TurnLeft to 255
TurnRight(255)
TurnLeft(255)
Does the motor run?
As now 11am cpt sunday; I modified your earlier code and it now runs my motor fwd and back for 2sec each way
This is the code I'm using,now but I need to control the speed with a pot connected to A0
and Direction controlled by Digital inputs
so the motor runs while an input is on at the speed of the pot
basically a motor speed controller
so I need to add
Example a pot at (A0) a input up button at D5, down button at D6 for example.
const int A_PWM = 11;
const int B_PWM = 3;
const int Enable = 8;
void setup()
{
pinMode(A_PWM, OUTPUT);
pinMode(B_PWM, OUTPUT);
pinMode(Enable, OUTPUT);
digitalWrite (A_PWM,LOW);
digitalWrite (B_PWM,LOW);
digitalWrite (Enable,LOW);
}
void TurnRight(uint8_t pwm){
analogWrite(A_PWM, 0);
delayMicroseconds(200);
analogWrite(B_PWM, pwm);
}
void TurnLeft(uint8_t pwm){
analogWrite(B_PWM, 0);
delayMicroseconds(200);
analogWrite(A_PWM, pwm);
}
void Stop(){
analogWrite(B_PWM, LOW);
analogWrite(A_PWM, LOW);
analogRead(A0);
}
void loop()
{
digitalWrite (Enable,HIGH);
TurnRight(200,);//out
delay (5000);
Stop();
TurnLeft(200);//in
delay(5000);
}
Well my original program had the PWM values set to 128 and 200 but you said the motor did not run. Maybe your motor speed cannot be controlled by PWM
You are going to have to do some testing to see if the motor speed can be controlled by PWM.
Try 250 then 240, 230, 220 etc to see if the speed changes
why not add the above code and use the pwm value instead of any not zero (i.e. LOW ) value to analogWrite()?
I'VE been programming Allen Bradley, directsoft omron for 30yrs. But guess i don't understand the syntex. This works to drive the actuator in and out by time.
But i need to drive it when an input to the arduino say pin 5 is true at the speed that is wired into V5, grn, A0.
If input goes false stop motor moving
Untill input is true agian then go again to what ever the speed A0 signal say
This is to test failure point on a cabinet.
I used a plc, hyd motor, valve and loadcell to do it but the motor on for only 1/2 sec would raise the pressure 100lbs or more.
I had a linear actuator i put inplace of the hyd cyl and used a variable power supply that i could adjust slowly and it would wook at the rate we needed. So now i need to the arduino, h bridge and my plc to drive the actuator to diffeeent setpoints for diff time for.up to 53 diff points. This way the operator can put his cab in the fixture and walk away and my contoler will run the testing cycle.I know this is alot but it should be a simple if input true run at analog in value.
Heres a copy that drives my motor now on time but I'm not sure where to modify it to use the inputs and analog input.
const int A_PWM = 11;
const int B_PWM = 3;
const int Enable = 8;
void setup()
{
pinMode(A_PWM, OUTPUT);
pinMode(B_PWM, OUTPUT);
pinMode(Enable, OUTPUT);
digitalWrite (A_PWM,LOW);
digitalWrite (B_PWM,LOW);
digitalWrite (Enable,LOW);
}
void TurnRight(uint8_t pwm){
analogWrite(A_PWM, 0);
delayMicroseconds(200);
analogWrite(B_PWM, pwm);
}
void TurnLeft(uint8_t pwm){
analogWrite(B_PWM, 0);
delayMicroseconds(200);
analogWrite(A_PWM, pwm);
}
void Stop(){
analogWrite(B_PWM, LOW);
analogWrite(A_PWM, LOW);
analogRead(A0);
}
void loop()
{
digitalWrite (Enable,HIGH);
TurnRight(200,);//out
delay (5000);
Stop();
TurnLeft(200);//in
delay(5000);
}