# Vex motor code help need

**URL:** <https://forum.arduino.cc/t/vex-motor-code-help-need/1088689>\
**Category:** Programming\
**Created:** [February 10, 2023, 5:21pm UTC](https://forum.arduino.cc/t/vex-motor-code-help-need/1088689 "2023-02-10T17:21:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmathew213123](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@jmathew213123](https://forum.arduino.cc/u/jmathew213123)\
**Post date:** [February 10, 2023, 5:21pm UTC](https://forum.arduino.cc/t/vex-motor-code-help-need/1088689/1 "2023-02-10T17:21:54Z")

</div>

I wrote some test code to make a vex motor move using an Arduino. I wrote this code to try to get a better understand on how to make a vex motor work in Arduino. I uploaded to code to my Arduino but nothing happens I'm not sure what the issue might be, I think the code is good but I'm not sure. Also do I a battery or is the computer enough

here is my code

#include \<Servo.h\>

int MotorPin =4;  
Servo motorControl;

void setup()

{  
pinMode(MotorPin, OUTPUT);  
motorControl.attach(MotorPin);  
}

void loop() {

digitalWrite(MotorPin, HIGH);  
int motorControl(100);  
delay(5000);

}

---

<div class="post-metadata">

**Author:** ![gilshultz](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gilshultz](https://forum.arduino.cc/u/gilshultz)\
**Post date:** [February 10, 2023, 5:45pm UTC](https://forum.arduino.cc/t/vex-motor-code-help-need/1088689/2 "2023-02-10T17:45:46Z")

</div>

Please post an annotated schematic and links to technical information on the hardware devices. Frizzys do not work for me. You also need to follow the forum guidelines on posting your code. It also helps to comment the code.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [February 10, 2023, 5:57pm UTC](https://forum.arduino.cc/t/vex-motor-code-help-need/1088689/3 "2023-02-10T17:57:32Z")

</div>

> [@jmathew213123](#):
>
> vex motor

Please post a link to the product page or data sheet for this motor, and the details of the motor power supply. Why do you think it acts like a hobby servo?

Don't forget to use code tags when posting code.

---

<div class="post-metadata">

**Author:** ![jmathew213123](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@jmathew213123](https://forum.arduino.cc/u/jmathew213123)\
**Post date:** [February 10, 2023, 6:08pm UTC](https://forum.arduino.cc/t/vex-motor-code-help-need/1088689/4 "2023-02-10T18:08:00Z")

</div>

So this is a vex 393 motor. Here is a link to a data sheet I found from the vex website.

> **[Motors](https://www.vexrobotics.com/motors.html)**
>
> VEX 393 motors are the primary motors used for robot mechanisms. We also offer replacement gears to give your motors a speed boost. Use 393's to power your intakes, lifts, and drivetrains.

I also rewrote my code but it still doesn't run.

#include \<Servo.h\>

int MotorPin =4;  
Servo motor;

void setup()

{  
pinMode(MotorPin, OUTPUT);  
motor.attach(MotorPin);  
}

void loop() {

motorControl(100);

}

int motorControl(int value){  
motor.write(map(value,-100,100,1000,200));

}

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [February 10, 2023, 6:18pm UTC](https://forum.arduino.cc/t/vex-motor-code-help-need/1088689/5 "2023-02-10T18:18:19Z")

</div>

Please edit your posts to add code tags.

The Vex 393 is just a motor, and requires a motor controller, a separate motor power supply, and code that is appropriate for whatever motor controller you choose.

Do not try to use the Arduino 5V output to power motors or servos, and avoid any tutorials that suggest it is OK to do so (there are lots of them, and they are all wrong).

---

<div class="post-metadata">

**Author:** ![jmathew213123](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@jmathew213123](https://forum.arduino.cc/u/jmathew213123)\
**Post date:** [February 10, 2023, 6:43pm UTC](https://forum.arduino.cc/t/vex-motor-code-help-need/1088689/7 "2023-02-10T18:43:32Z")

</div>

I plugged it into vin and I'm using an external power supply but it still wont run when I do upload.  
Also what do you mean add code tags. Im not sure what that is..

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [February 10, 2023, 7:32pm UTC](https://forum.arduino.cc/t/vex-motor-code-help-need/1088689/8 "2023-02-10T19:32:56Z")

</div>

> [@jmathew213123](#):
>
> what do you mean add code tags. Im not sure what that is..

Code tags are devices which will make your code appear in a selectable box like this which make it easy for others to copy and run your code.

```arduino
type or paste code here

```

There is an automatic function for doing this in the Arduino-IDE  
just three steps

1)press Ctrl-T for autoformatting your code  
2)do a rightclick with the mouse and choose "copy for forum"  
3)paste clipboard into write-window of a posting

Also clicking on the \</\> icon in the header to a post will produce the box to place the code.

```arduino
type or paste code here

```

---

<div class="post-metadata">

**Author:** ![sherzaad](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sherzaad/32/226173_2.png) [@sherzaad](https://forum.arduino.cc/u/sherzaad)\
**Post date:** [February 10, 2023, 8:29pm UTC](https://forum.arduino.cc/t/vex-motor-code-help-need/1088689/9 "2023-02-10T20:29:46Z")

</div>

from the link you shared, there are 2 versions of that motor and I'm guessing you have the 3-wire version which should work with the Servo library, based on the info given:

> **2 Wire Motor 393**  
> Free Speed: 100 rpm (As Shipped)/160 rpm (High Speed Option)  
> Stall Torque: 1.67 N-m (14.76 in-lbs) (As Shipped)/1.04 N-m (9.2 in-lbs) (High Speed Option)  
> Stall Current: 4.8A  
> Free Current: 0.37A
> 
> **Note:** All motor specifications are at 7.2 volts.
> 
> **3-Wire Servo**  
> Rotation: 100 degrees  
> Stall Torque: 6.5 in-lbs  
> Voltage: 4.4 - 9.1 Volts (Motor life will be reduced operating outside this range)  
> PWM Input: 1ms - 2ms will give full reverse to full forward, 1.5ms is neutral  
> Black Wire: Ground  
> Orange Wire: Power  
> White Wire: PWM signal  
> Current Draw: 20mA to 1.5 A per Servo  
> Max Power: 4.9 W when rated @ 6V

assuming you have wired everything correctly, you should also be able to test your motor/servo with this code:

```arduino
/*the sg90 accepts 3 command pulse width
  1ms - CCW movement
  1.5ms - return to center position
  2ms - CW movement
  minimum time between pulses/steps - 20ms

  This example used delay() to demonstrate servo operation.

*/

uint8_t DirectionInputPin = 9; //change this to the ACTUAL pin being used!!!
uint8_t sg90ControlPin = 8; //white wire - change this to the ACTUAL pin being used!!!

uint8_t CCWpulseWidth = 1; // milliseconds
uint8_t CWpulseWidth = 2; // milliseconds
int millisbetweenSteps = 20; // milliseconds - or try larger values for slower steps

uint8_t numberOfSteps = 100;
uint8_t dir_req;

void move_servo(int Steps, uint8_t pulseWidth) {
  for (uint8_t n = 0; n < Steps; n++) {
    digitalWrite(sg90ControlPin, LOW);
    delay(millisbetweenSteps);
    digitalWrite(sg90ControlPin, HIGH);
    delay(pulseWidth);
  }
}

void setup() {

  Serial.begin(115200);

  pinMode(sg90ControlPin, OUTPUT);
  digitalWrite(sg90ControlPin, HIGH); //reduces chances of jitter on powerup/reset
  //a 1k pullup resistor between sg90ControlPin and VCC would also help

  pinMode(DirectionInputPin, INPUT_PULLUP);

  //initialise direction flag
  dir_req = digitalRead(DirectionInputPin);

  Serial.println("Starting Sg90 Demo");

  //abitrary delay
  delay(2000);

  //servo initialisation
  //move servo to center positon
  digitalWrite(sg90ControlPin, LOW);
  delay(20);
  digitalWrite(sg90ControlPin, HIGH);
  delayMicroseconds(1500);
  digitalWrite(sg90ControlPin, LOW);
  delay(20);
  //move servo fully CounterClockwise (approximately 90 steps)
  move_servo(90, CCWpulseWidth);

  //abitrary delay
  delay(2000);
}

void loop() {

  if (digitalRead(DirectionInputPin) != dir_req) {

    dir_req = digitalRead(DirectionInputPin);

    //move servo Clockwise
    if (dir_req == HIGH) {
      move_servo(numberOfSteps, CWpulseWidth);
    }
    //move servo Counter-Clockwise
    else {
      move_servo(numberOfSteps, CCWpulseWidth);
    }

  }

  delay(millisbetweenSteps);
}

```

Also I would advise to power your motor/servo directly and not from your arduino board.

hope that helps....

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [February 10, 2023, 8:59pm UTC](https://forum.arduino.cc/t/vex-motor-code-help-need/1088689/10 "2023-02-10T20:59:02Z")

</div>

If you have the two-wire-version it will work definitely not with the servo-library.  
This motor looks like a servo but the two wire-version is **not** a servo  
This company is bad on documentation. at first glance the website looks like explaining all things but it **doesn't**.

The two wire-version must be driven not with the servo-library but with function analogWrite()

About codetags: make the code look like this

```arduino
this is text 
formatted with code-tags

```

use the \</\>-Button

> [@Make experienced users help: two ways to quickly post code as a code-section](https://forum.arduino.cc/t/topic/937298/1):
>
> Hi newcomer, This thread wants to show you how you can speed-up getting helpful answers with small effort. If you manage at least to post this way. You have the sympathy of the experienced users. If you are willing to invest another 20 minutes these 20 minutes extra-time will save you a minimum of 200 minutes to finish your project. always post your full sketch. As a code-section from the very first to the very last line A code-section has a small height =\> keeps your posting "short" but e…

best regards Stefan

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [February 10, 2023, 9:08pm UTC](https://forum.arduino.cc/t/vex-motor-code-help-need/1088689/11 "2023-02-10T21:08:50Z")

</div>

> [@jmathew213123](#):
>
> motor.write(map(value,-100,100,1000,200));

I see you found the error where _motorControl_ was not being called from _loop_(). Your original code actually wouldn't have compiled, or shouldn't have.

Now you've got a typo, did you mean 2000?

The compiler is merciless, it has no capability to think for you or give you the benefit of the doubt. Fortunately we different.

a7

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [February 10, 2023, 9:59pm UTC](https://forum.arduino.cc/t/vex-motor-code-help-need/1088689/12 "2023-02-10T21:59:27Z")

</div>

> [@jmathew213123](#):
>
> `motor.write(map(value,-100,100,1000,200));`

I think you meant:  
`motor.write(map(value,-100,100,1000,2000));`

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [February 10, 2023, 10:04pm UTC](https://forum.arduino.cc/t/vex-motor-code-help-need/1088689/13 "2023-02-10T22:04:06Z")

</div>

```cpp
void setup() {
// DON'T touch the pin you are giving to the Servo library
// to control.
//// Remove: pinMode(MotorPin, OUTPUT); 
  motorControl.attach(MotorPin);
}

void loop() {
// DON'T touch the pin you are giving to the Servo library
// to control.
//// Remove: digitalWrite(MotorPin, HIGH);

// This is declaring a local variable with the same name 
// as your function. It does not call your function.
int motorControl(100);
delay(5000);
}

```

---

<div class="post-metadata">

**Author:** ![jmathew213123](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@jmathew213123](https://forum.arduino.cc/u/jmathew213123)\
**Post date:** [February 15, 2023, 4:13pm UTC](https://forum.arduino.cc/t/vex-motor-code-help-need/1088689/14 "2023-02-15T16:13:34Z")

</div>

the vex motor I'm using has two pins but it goes into a motor controller which has three pins. The motor controller part is wired up to the Arduino. Does this change the code at all and is the vex motor considered as a motor or a servo in Arduino .

> **[Motor Controller 29](https://www.vexrobotics.com/276-2193.html#attr-vex_docs_downloads)**
>
> The VEX Motor Controller 29 uses a standard PWM signal (like that originating from the 3-wire ports on VEX Microcontrollers) to drive a VEX 2-Wire Motor.
> Note: We recommend using no more more than one (1) 3-Wire extension cable in between the Motor...

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [February 15, 2023, 4:29pm UTC](https://forum.arduino.cc/t/vex-motor-code-help-need/1088689/15 "2023-02-15T16:29:11Z")

</div>

> [@jmathew213123](#):
>
> Does this change the code at all

It seems clear that a two wire motor (usual simple DC motor number of wires) is being controlled by something that uses 3 wires to the controlling microprocessor (usual number of wires for a servo).

So it's a servo from the code point of view.

Now you may have a servo that goes from 0 to 180 degrees as the servo pulse goes from 1 to 2 ms or…

… you may have a continuous rotation servo that goes CW if the pulses are shorter than 1.5 ms, faster the more so, and CCW if the pulses are longer than 1.5, again faster the more so.

HTH

a7

---

<div class="post-metadata">

**Author:** ![jmathew213123](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@jmathew213123](https://forum.arduino.cc/u/jmathew213123)\
**Post date:** [February 15, 2023, 5:03pm UTC](https://forum.arduino.cc/t/vex-motor-code-help-need/1088689/16 "2023-02-15T17:03:34Z")

</div>

Ok thank you. I've been try all the sketches that were suggested to me here but when I press upload the motor still wont move. I think maybe its a wiring issue but I'm not sure. Is there a different forum that can help me troubleshoot my wiring.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [February 15, 2023, 5:05pm UTC](https://forum.arduino.cc/t/vex-motor-code-help-need/1088689/17 "2023-02-15T17:05:28Z")

</div>

> [@jmathew213123](#):
>
> Is there a different forum

_Here_. Post a schematic of your wiring and point out or post you current code.

a7

---

<div class="post-metadata">

**Author:** ![jmathew213123](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@jmathew213123](https://forum.arduino.cc/u/jmathew213123)\
**Post date:** [February 15, 2023, 5:06pm UTC](https://forum.arduino.cc/t/vex-motor-code-help-need/1088689/18 "2023-02-15T17:06:18Z")

</div>

can I just take a picture of the wiring and post it here

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [February 15, 2023, 5:09pm UTC](https://forum.arduino.cc/t/vex-motor-code-help-need/1088689/19 "2023-02-15T17:09:50Z")

</div>

You can, but a hand drawn schematic in addition would be the best.

Just draw all the parts and all the wires and make sure to show where power is being provided and by what.

If you've never drawn a schematic, google

electronic schematic basics

pick "images" and poke around a bit.

It's very simple and again, pencil and paper and your cellphone is all you need, and is often the best, as you need to know how to do it by hand if you ever get around to using CAD schematic drawing, as it will not think for you anyways. A distraction you don't need here.

a7

---

<div class="post-metadata">

**Author:** ![jmathew213123](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@jmathew213123](https://forum.arduino.cc/u/jmathew213123)\
**Post date:** [February 15, 2023, 5:24pm UTC](https://forum.arduino.cc/t/vex-motor-code-help-need/1088689/20 "2023-02-15T17:24:56Z")

</div>

here is my code

````cpp

#include <Servo.h>

int MotorPin = 4;
Servo motor;

void setup()

{
  pinMode(MotorPin, OUTPUT);
  motor.attach(MotorPin);
}

void loop() {

  int motorControl(100);
}

int motorControl(int value) {
  motor.write(map(value, -100, 100, 1000, 2000));
}```
````

---

<div class="post-metadata">

**Author:** ![jmathew213123](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@jmathew213123](https://forum.arduino.cc/u/jmathew213123)\
**Post date:** [February 15, 2023, 5:26pm UTC](https://forum.arduino.cc/t/vex-motor-code-help-need/1088689/21 "2023-02-15T17:26:27Z")

</div>

and here are the pics

 ![20230215_111924](https://europe1.discourse-cdn.com/arduino/original/4X/c/9/d/c9d79256bacc888e29d6ea83c38081135144e795.jpeg)  
 ![20230215_111947](https://europe1.discourse-cdn.com/arduino/original/4X/2/5/3/2534f44b56ee74baf8e8382fc36115aac1df6e9c.jpeg)  
 ![20230215_111934](https://europe1.discourse-cdn.com/arduino/original/4X/8/a/6/8a602d353b85c4bf427a17fbe8a6137ba14be9d0.jpeg))  
 ![20230215_111918](https://europe1.discourse-cdn.com/arduino/original/4X/2/3/9/239fd06231f3029d0a6feb3d9067589ca689fc1c.jpeg)

[Next page](https://forum.arduino.cc/t/vex-motor-code-help-need/1088689.md?page=2)
