Go Down

Topic: Adding a limit switch to stop a pwm motor code. (Read 4 times) previous topic - next topic


I think you are going about solving your problem the wrong way.  This RC ESC is PWM based you could easily use analogWrite to control the frequency of the pin without servicing it in a busy loop.  analogWrite is the perfect way to do this.  Test it out with a servo hooked up to one of the PWM pins.  The Servo library is way overkill for controlling one PWM (servo/esc) and a limit switch (or two).

IE (pseudocode - not tested or compiled but similar to something I have used for higher powered motor contro):

Code: [Select]

#define ESC_PWM_PIN 3 // The pin the ESC/SERVO is connected to
#define LIMIT_SW_PIN 2 // The Limit Switch Pin (INT0)
                                 // Limit switch connected to VDD through 10K pullup
                                 //                  PIN 2
                                 //           10K    |
                                 //     VDD---VVVV---+----o/  o--- GND

void motor_fwd () {

void motor_rev() {
   analogWrite(ESC_PWM_PIN, 0)

void motor_brk() {
   // SET BRAKE ON MOTOR  (you might need to tune this value where the brake is set on the ESC - and I know the castle has a configurable brake)
   analogWrite(ESC_PWM_PIN, 120);

void setup () {
  // Initialize the ESC PIN as an output and set the brake
  // INT0 - PIN2 - call motor_brk when the switch closes

bool limit_sw_state_last = 0;

void loop() {
bool limit_sw_state_cur = digitalRead(LIMIT_SW_PIN);

   // toggle direction versus limit sw state.
   if (limit_sw_state_cur == limit_sw_state_last) {
     // do nothing
   } elseif (limit_sw_state) {
    // limit switch pulled high
  } else {
    // limit switch pulled low

  limit_sw_state_last = limit_sw_state_cur;




Apr 13, 2011, 06:07 pm Last Edit: Apr 13, 2011, 06:11 pm by AWOL Reason: 1
This RC ESC is PWM based

The output may be, but the input is usually PPM.

The Servo library is way overkill for controlling one PWM (servo/esc)

That depends on much abuse your servo/ESC can withstand.

Hint: Arduino PWM a shade under 500Hz with poor resolution in the 5 to 10% duty cycle region,versus Servo library at 50Hz with very good resolution.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


Hey thanks,

I'll let you know if that works. As of now I just have to fix the 11 errors I was getting. I think I was going at this the wrong way. I am kinda lost on this one, it was more than I expected!!

Yeah I will probably only run the motor for 30min or less. It is a 12,000 RPM motor even though it is geared down so I kinda don't need that speed, I'll definately be tweeking the code.  For errors see attached pictures. :~


Put semicolons after every line that doesn't have an open bracket in it.

Also, for those saying to use the servo library: He's not using a servo.

Analogwrite is the way to go  8)

Also, I hope you're not trying to drive the motor directly, and are using some sort of transistor, or else you'll blow out that pin on the arduino.


Also, for those saying to use the servo library: He's not using a servo.
Analogwrite is the way to go 

The OP said he is using an RC ESC with the motor. Most of these ESCs are made to be conrolled via servo control pulses (PPM) so they can be plugged directly into the RC receivers with which they are normally used. Bottom line, the OP should have some idea of how the equipment he has works.
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

Go Up