ESC with 4 speeds via 4 separate digital inputs

Hello all,

I'm trying to control a generic plane ESC/motor with an UNO. The control is via four separate pin inputs that are each switched to ground(hence the pullups on inputs). No two can be closed at the same time. This is coming from a rotary knob with 5 positions. 0-5. 1-4 have a discrete wire.

I think I have it close but I have no idea how to set the speeds or define off if no input is seen on 1-4.

The board specifically is a adafruit Metro Mini(figured I needed 5v logic) with 500Hz and 1000Hz outputs but I'm guessing using the servo.h fixes the frequency with timers.

This is literally my first time trying to create so go easy on me. Ha

#include <Servo.h>
#define ButtonPin 0
#define ButtonPin 1
#define ButtonPin 2
#define ButtonPin 3
#define MotorPin 11
Servo ESC;
int IN1 = 0;
int IN2 = 1;
int IN3 = 2;
int IN4 = 3;

void setup() {
  pinMode(IN1, INPUT);
  pinMode(IN2, INPUT);
  pinMode(IN3, INPUT);
  pinMode(IN4, INPUT);
  pinMode(IN1, INPUT_PULLUP);
  pinMode(IN2, INPUT_PULLUP);
  pinMode(IN3, INPUT_PULLUP);
  pinMode(IN4, INPUT_PULLUP);
  ESC.attach(11,1000,2000); // (pin, min pulse width, max pulse width in microseconds) 
 
}

void loop() {
  if (digitalRead(0) == LOW) {
    IN1 = map(11, 0, 1023, 0, 180);   // scale it to use it with the servo library (value between 0 and 180) 
    ESC.write(11);    // Send the signal to the ESC
  }
  if (digitalRead(1) == LOW) {
    IN2 = map(11, 0, 1023, 0, 180);   // scale it to use it with the servo library (value between 0 and 180) 
    ESC.write(11);    // Send the signal to the ESC
  }
  if (digitalRead(2) == LOW) {
    IN3 = map(11, 0, 1023, 0, 180);   // scale it to use it with the servo library (value between 0 and 180) 
    ESC.write(11);    // Send the signal to the ESC
  }
  if (digitalRead(3) == LOW) {
    IN4 = map(11, 0, 1023, 0, 180);   // scale it to use it with the servo library (value between 0 and 180) 
    ESC.write(11);    // Send the signal to the ESC
  }
else {
 have speed be 0
  ESC.write(11);
  }
}

Welcome to the forum

What you want to do should be simple but you seem to have made it complicated by trying to use the map() function for some reason.

Look closely at your code. Whichever button is pressed you write 11 to the ESC

All you need to do is test whether an input is LOW and, if so, write() your required value to the ESC. Write a different value for each switch position

There are smart ways to do this to reduce the code but start by with testing each input separately and writing the corresponding value. You can smarten it up later

Incidentally, it is not a good idea to use pins 0 and 1 because they are used by teh hardware Serial interface and to upload code to the board

The servo control signal has a frequency of about 50 Hz. You can use any digital pin to control the motor, the pin doesn't need to be PWM. Try this program.

#include <Servo.h>

Servo ESC;

const byte
IN1 = 4,
IN2 = 5,
IN3 = 6,
IN4 = 7,
MotorPin = 8
;

void setup ()
{
  pinMode ( IN1, INPUT_PULLUP );
  pinMode ( IN2, INPUT_PULLUP );
  pinMode ( IN3, INPUT_PULLUP );
  pinMode ( IN4, INPUT_PULLUP );
  ESC.attach ( MotorPin, 1000, 2000 );
}

void loop ()
{
  if ( digitalRead ( IN1 ) == LOW )
  {
    ESC.write ( 45 ); // 1/4 speed
  }
  else if ( digitalRead ( IN2 ) == LOW )
  {
    ESC.write ( 90 ); // 1/2 speed
  }
  else if ( digitalRead ( IN3 ) == LOW )
  {
    ESC.write ( 135 ); // 3/4 speed
  }
  else if ( digitalRead ( IN4 ) == LOW )
  {
    ESC.write ( 180 ); // full speed
  }
  else
  {
    ESC.write ( 0 ); // stand still
  }
}

This is the board I'm using. It has dedicated paths for the USB.

In seeing the changes made it's really obvious why they were made. I just have a syntax lack of knowledge. I'll give this a try tomorrow!

As far as I know, it uses a USB to serial chip. Pins 0 and 1 are Rx/Tx, as in the pinout diagram above.

Is there a way to put in a delay between selection and ramp up and down to avoid current spikes?

It's possible. You can change the program to change the value from the current one to the new one selected, or use a library that allows setting a speed like this one (I haven't used it personally, just know about it).

From what I can tell it's rather complicated to ramp in. I was really only able to find example of acceleration rates for stepper motors. Fortunately the speed controller has a fair bit of dampening that you can add in. I'll try there first.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.