ESP32, Servo's and PCA9685 works but can not integrate a button / switch

Hi all,

I've racked my brains and tried multiple ways but I can not get a push button to work with a PCA9685.

I've gotten buttons to work with an ESP32 and two servo's, to move to a position when the button is pressed. Next step, the PCA9685 example works great with the servo's, as expected.
The next step is to integrate the two scripts to get the buttons to work with the PCA9685. But nothing I try has worked so far.
Could anyone direct me to the correct command to get the signal sent to send to the PCA9685.

I've done the obligatory google but can't find any tutorials or any relevant solutions to peoples forums post.

button code

 <ESP32Servo.h>
#include <ezButton.h>
#include <Adafruit_PWMServoDriver.h>
#include <Wire.h>

// Creat object to represent PCA9685 at default I2C address
Adafruit_PWMServoDriver pca9685 = Adafruit_PWMServoDriver(0x40);

//#define BUTTON_PIN 18 // ESP32 pin GIOP21 connected to button's pin
//#define BUTTON_PIN2 19 // ESP32 pin GIOP21 connected to button's pin

//from pca9685 test sketch maybe not needed
#define SERVOMIN  125  // Minimum value - previously 80
#define SERVOMAX  575  // Maximum value - previously 1024

// Define servo motor connections (expand as required)
#define SER0  0   //Servo Motor 0 on connector 0
#define SER1  4  //Servo Motor 1 on connector 12

Servo myservo;  // create servo object to control a servo
Servo myservo1;
// 16 servo objects can be created on the ESP32
ezButton button1(21); // create ezButton object that attach to pin 7;
ezButton button2(23); // create ezButton object that attach to pin 7;

//from pca9685 test sketch maybe not needed
// Variables for Servo Motor positions (expand as required)
//int pwm0;
//int pwm1;

int pos = 0;    // variable to store the servo position
// Recommended PWM GPIO pins on the ESP32 include 2,4,12-19,21-23,25-27,32-33 
int servoPin = 19;  
int servoPin1 = 18;

//from button sketch
int angle = 0; // the current angle of servo motor
int angle2 = 0; // the current angle of servo motor


void setup() {
  // Allow allocation of all timers
  ESP32PWM::allocateTimer(0);
  ESP32PWM::allocateTimer(1);
  ESP32PWM::allocateTimer(2);
  ESP32PWM::allocateTimer(3);
  myservo.setPeriodHertz(50);    // standard 50 hz servo
  myservo.attach(servoPin, 1000, 2400); // attaches the servo on pin 18 to the servo object
  myservo1.setPeriodHertz(50);    // standard 50 hz servo
  myservo1.attach(servoPin1, 1000, 2400); // attaches the servo on pin 18 to the servo object
  // using default min/max of 1000us and 2000us
  // different servos may require different min/max settings
  // for an accurate 0 to 180 sweep

  Serial.begin(115200);         // initialize serial
  button1.setDebounceTime(50); // set debounce time to 50 milliseconds
  button2.setDebounceTime(50); // set debounce time to 50 milliseconds

  myservo.write(angle);
  myservo1.write(angle);

  // Print to monitor
  Serial.println("PCA9685 Servo Test");

  // Initialize PCA9685
  pca9685.begin();

  // Set PWM Frequency to 50Hz
//  pca9685.setPWMFreq(50);


}

void loop() {


  button1.loop(); // MUST call the loop() function first

  if (button1.isPressed()) {
    // change angle of servo motor
    if (angle == 0)
      angle = 90;
    else if (angle == 90)
      angle = 0;

    // control servo motor arccoding to the angle
    Serial.print("The button is pressed => rotate servo to ");
    Serial.print(angle);
    Serial.println("°");
    myservo.write(angle);
  }

  button2.loop(); // MUST call the loop() function first

  if (button2.isPressed()) {
    // change angle of servo motor
    if (angle2 == 0)
      angle2 = 90;
    else if (angle2 == 90)
      angle2 = 0;

    // control servo motor arccoding to the angle
    Serial.print("The button is pressed => rotate servo to ");
    Serial.print(angle2);
    Serial.println("°");
    myservo1.write(angle2);
  }


}

PCA9685 example (without buttons)

// Include Wire Library for I2C
#include <Wire.h>

// Include Adafruit PCA9685 Servo Library
#include <Adafruit_PWMServoDriver.h>

// Creat object to represent PCA9685 at default I2C address
Adafruit_PWMServoDriver pca9685 = Adafruit_PWMServoDriver(0x40);

// Define maximum and minimum number of "ticks" for the servo motors
// Range from 0 to 4095
// This determines the pulse width

#define SERVOMIN  125  // Minimum value - previously 80
#define SERVOMAX  575  // Maximum value - previously 1024

// Define servo motor connections (expand as required)
#define SER0  0   //Servo Motor 0 on connector 0
#define SER1  4  //Servo Motor 1 on connector 12

// Variables for Servo Motor positions (expand as required)
int pwm0;
int pwm1;

void setup() {

  // Serial monitor setup
  Serial.begin(115200);

  // Print to monitor
  Serial.println("PCA9685 Servo Test");

  // Initialize PCA9685
  pca9685.begin();

  // Set PWM Frequency to 50Hz
  pca9685.setPWMFreq(50);

}

void loop() {

  // Move Motor 0 from 0 to 180 degrees
  for (int posDegrees = 0; posDegrees <= 180; posDegrees++) {

    // Determine PWM pulse width
    pwm0 = map(posDegrees, 0, 180, SERVOMIN, SERVOMAX);
    // Write to PCA9685
    pca9685.setPWM(SER0, 0, pwm0);
    // Print to serial monitor
    Serial.print("Motor 0 = ");
    Serial.println(posDegrees);
    delay(30);
  }

  // Move Motor 1 from 180 to 0 degrees
  for (int posDegrees = 180; posDegrees >= 0; posDegrees--) {

    // Determine PWM pulse width
    pwm1 = map(posDegrees, 0, 180, SERVOMIN, SERVOMAX);
    // Write to PCA9685
    pca9685.setPWM(SER1, 0, pwm1);
    // Print to serial monitor
    Serial.print("Motor 1 = ");
    Serial.println(posDegrees);
    delay(30);
  }

  // Move Motor 0 from 180 to 0 degrees
  for (int posDegrees = 180; posDegrees >= 0; posDegrees--) {

    // Determine PWM pulse width
    pwm0 = map(posDegrees, 0, 180, SERVOMIN, SERVOMAX);
    // Write to PCA9685
    pca9685.setPWM(SER0, 0, pwm0);
    // Print to serial monitor
    Serial.print("Motor 0 = ");
    Serial.println(posDegrees);
    delay(30);
  }


  // Move Motor 1 from 0 to 180 degrees
  for (int posDegrees = 0; posDegrees <= 180; posDegrees++) {

    // Determine PWM pulse width
    pwm1 = map(posDegrees, 0, 180, SERVOMIN, SERVOMAX);
    // Write to PCA9685
    pca9685.setPWM(SER1, 0, pwm1);
    // Print to serial monitor
    Serial.print("Motor 1 = ");
    Serial.println(posDegrees);
    delay(30);
  }


}

I have no experience with PCA9685 but understand that

  • the first sketch does not work as expected
  • the second sketch works

Is this correct?

If yes: If you compare the two sketches you can see that you are not using the Adafruit_PWMServoDriver functions for PCA9685 to control the servos. You seem to be using the ESPServo lib instead that works with PWM on direct pins of the ESP.

The PCA9685 is connected to the controller via I2C interface and requires the drivers that are integrated in the Adafruit lib and the use of the appropriate functions:

You have to set the PWM frequency in setup() with

 // Set PWM Frequency to 50Hz
  pca9685.setPWMFreq(50);

and to call functions like "pca9685.setPWM(SER0, 0, pwm0);":

// Define servo motor connections (expand as required)
// Read the manual and check your specific installation!
// This is just a part of the example but probably has to be adopted:
#define SERVOMIN  125  // Minimum value - previously 80
#define SERVOMAX  575  // Maximum value - previously 1024
#define SER0  0   //Servo Motor 0 on connector 0
#define SER1  4  //Servo Motor 1 on connector 12

  if (button1.isPressed()) {
    // change angle of servo motor
    if (angle == 0)
      angle = 90;
    else if (angle == 90)
      angle = 0;

    // control servo motor arccoding to the angle
    Serial.print("The button is pressed => rotate servo to ");
    Serial.print(angle);
    Serial.println("°");
   int   pwm0 = map(posDegrees, 0, 180, SERVOMIN, SERVOMAX);
   pca9685.setPWM(SER0, 0, pwm0);
  }

The angle has to be mapped to the specific values SERVOMIN to SERVOMAX which you have found before (I guess).

As I said I cannot check it, but from what I could read from the example and - at least from theory - this should solve your problem :innocent:

1 Like

Hi @ec2021

Thank you so much for the reply, that looks amazing and I'll give that a try.

the first sketch does not work as expected
the second sketch works
Is this correct?

No, both sketches work on there own but I couldn't get the button wokring with the PCA9685

The angle has to be mapped to the specific values SERVOMIN to SERVOMAX which you have found before (I guess).

I have roughly set them, just need to dial them in when I have the sketch working.

Again, thank you so much, I'll give it a try shortly.

Thank you so much for the help. That has been driving me up the wall.

It is almost there but the servo goes from 0 degree's to 90 degrees and back to 0 degree's, twice.

I'm overdue on sleep, so I will pick this back up tomorrow but wanted to let you know it is almost there.


// Include Wire Library for I2C
#include <Wire.h>
#include <ezButton.h>
#include <Adafruit_PWMServoDriver.h>

// Creat object to represent PCA9685 at default I2C address
Adafruit_PWMServoDriver pca9685 = Adafruit_PWMServoDriver(0x40);

// Define maximum and minimum number of "ticks" for the servo motors
// Range from 0 to 4095
// This determines the pulse width

// Define servo motor connections (expand as required)
// Read the manual and check your specific installation!
// This is just a part of the example but probably has to be adopted:
#define SERVOMIN  125  // Minimum value - previously 80
#define SERVOMAX  575  // Maximum value - previously 1024
#define SER0  0   //Servo Motor 0 on connector 0
#define SER1  4  //Servo Motor 1 on connector 12

// Variables for Servo Motor positions (expand as required)
int pwm0;
int pwm1;

//Servo myservo;  // create servo object to control a servo
//Servo myservo1;
// 16 servo objects can be created on the ESP32
ezButton button1(18); // create ezButton object that attach to pin 7;
ezButton button2(19); // create ezButton object that attach to pin 7;

int pos = 0;    // variable to store the servo position


//from button sketch
int angle = 0; // the current angle of servo motor
int angle2 = 0; // the current angle of servo motor


void setup() {


  
  // Serial monitor setup
  Serial.begin(115200);

  button1.setDebounceTime(50); // set debounce time to 50 milliseconds
  button2.setDebounceTime(50); // set debounce time to 50 milliseconds

//  myservo.write(angle);
//  myservo1.write(angle);

  // Print to monitor
  Serial.println("PCA9685 Servo Test");

  // Initialize PCA9685
  pca9685.begin();

  // Set PWM Frequency to 50Hz
  pca9685.setPWMFreq(50);

}



void loop() {

  button1.loop(); // MUST call the loop() function first
  if (button1.isPressed()) {
    // change angle of servo motor
    if (angle == 0)
      angle = 90;
    else if (angle == 90)
      angle = 0;

  // Move Motor 1 from 0 to 180 degrees
  for (int posDegrees = 0; posDegrees <= 180; posDegrees++) {

    // control servo motor arccoding to the angle
    Serial.print("The button is pressed => rotate servo to ");
    Serial.print(angle);
    Serial.println("°");
     pwm0 = map(posDegrees, 0, 180, SERVOMIN, SERVOMAX);
   pca9685.setPWM(SER0, 0, pwm0);
  }
  }
}

I know that quite well :wink:

Sorry, if I mention information or procedures which you know, a lack of forums is that there is no sound knowledge about the experience and skills of thread owners ... which sometimes leads to the situation that the help level is not appropriate ..

Anyway, that can be solved by (frank and friendly) communication.

I found this informative website which you probably already know, but to be sure:

https://learn.adafruit.com/16-channel-pwm-servo-driver?view=all

The main difference to other servo libraries seems to be that you do not directly address an angle, but influence the PWM duty cycle and the use of empirical parameters.

You may try this to check different angles and also different parameters for SERVOMIN and SERVOMAX ...

// Include Wire Library for I2C
#include <Wire.h>
//#include <ezButton.h>
#include <Adafruit_PWMServoDriver.h>

// Creat object to represent PCA9685 at default I2C address
Adafruit_PWMServoDriver pca9685 = Adafruit_PWMServoDriver(0x40);

// Define maximum and minimum number of "ticks" for the servo motors
// Range from 0 to 4095
// This determines the pulse width

// Define servo motor connections (expand as required)
// Read the manual and check your specific installation!
// This is just a part of the example but probably has to be adopted:
#define SERVOMIN  125  // Minimum value - previously 80
#define SERVOMAX  575  // Maximum value - previously 1024
#define SER0  0   //Servo Motor 0 on connector 0
#define SER1  4  //Servo Motor 1 on connector 12

// Variables for Servo Motor positions (expand as required)
int pwm0;
int pwm1;

//Servo myservo;  // create servo object to control a servo
//Servo myservo1;
// 16 servo objects can be created on the ESP32
//ezButton button1(18); // create ezButton object that attach to pin 7;
//ezButton button2(19); // create ezButton object that attach to pin 7;

int pos = 0;    // variable to store the servo position


//from button sketch
int angle = 0; // the current angle of servo motor
int angle2 = 0; // the current angle of servo motor


void setup() {


  
  // Serial monitor setup
  Serial.begin(115200);

 // button1.setDebounceTime(50); // set debounce time to 50 milliseconds
  //button2.setDebounceTime(50); // set debounce time to 50 milliseconds

//  myservo.write(angle);
//  myservo1.write(angle);

  // Print to monitor
  Serial.println("PCA9685 Servo Test");

  // Initialize PCA9685
  pca9685.begin();

  // Set PWM Frequency to 50Hz
  pca9685.setPWMFreq(50);
  MoveServoFromTo(0, 90);
  delay(1000);
  MoveServoFromTo(90, 0);
  delay(1000);
  MoveServoFromTo(0, 180);
 delay(1000);
 ServoTest(125,575);
 delay(1000);
 //Change these data carefully to identify any differences in angles:
 ServoTest(125,600); 
}

void loop() {}

void MoveServoFromTo(int StartAngle, int EndAngle){
  // Move Motor 1 from StartAngle to EndAngle degrees
  Serial.print("Move from\t"); Serial.print(StartAngle);
  Serial.print("\tto\t"); Serial.println(EndAngle);

  for (int posDegrees = StartAngle; posDegrees <= EndAngle; posDegrees++) {
   pwm0 = map(posDegrees, 0, 180, SERVOMIN, SERVOMAX);
   pca9685.setPWM(SER0, 0, pwm0);
  }
}

void ServoTest(int ServoMin, int ServoMax){
  // Move Motor 1 from StartAngle to EndAngle degrees
  Serial.print("ServoMin \t"); Serial.print(ServoMin);
  Serial.print("\tServoMax\t"); Serial.println(ServoMax);

  for (int posDegrees = 0; posDegrees <= 180; posDegrees++) {
   pwm0 = map(posDegrees, 0, 180, ServoMin, ServoMax);
   pca9685.setPWM(SER0, 0, pwm0);
  }
}

I just "coded" this, but could only check it on Wokwi where there is no simulation for the PCA9685 ... :innocent:

Here you may see it https://wokwi.com/arduino/projects/324929976949277268

Here is your code from above with some small additions in and after the for() loop. As you are not using delays to wait for the servo to reach its position, your sketch runs through loop() too quickly which should not happen here:

// Include Wire Library for I2C
#include <Wire.h>
#include "ezButton.h"
#include "Adafruit_PWMServoDriver.h"

// Creat object to represent PCA9685 at default I2C address
Adafruit_PWMServoDriver pca9685 = Adafruit_PWMServoDriver(0x40);

// Define maximum and minimum number of "ticks" for the servo motors
// Range from 0 to 4095
// This determines the pulse width

// Define servo motor connections (expand as required)
// Read the manual and check your specific installation!
// This is just a part of the example but probably has to be adopted:
#define SERVOMIN  125  // Minimum value - previously 80
#define SERVOMAX  575  // Maximum value - previously 1024
#define SER0  0   //Servo Motor 0 on connector 0
#define SER1  4  //Servo Motor 1 on connector 12

// Variables for Servo Motor positions (expand as required)
int pwm0;
int pwm1;

//Servo myservo;  // create servo object to control a servo
//Servo myservo1;
// 16 servo objects can be created on the ESP32
ezButton button1(18); // create ezButton object that attach to pin 7;
ezButton button2(19); // create ezButton object that attach to pin 7;

int pos = 0;    // variable to store the servo position


//from button sketch
int angle = 0; // the current angle of servo motor
int angle2 = 0; // the current angle of servo motor

void setup() {
  
  // Serial monitor setup
  Serial.begin(115200);

  button1.setDebounceTime(50); // set debounce time to 50 milliseconds
  button2.setDebounceTime(50); // set debounce time to 50 milliseconds

//  myservo.write(angle);
//  myservo1.write(angle);

  // Print to monitor
  Serial.println("PCA9685 Servo Test");

  // Initialize PCA9685
  pca9685.begin();

  // Set PWM Frequency to 50Hz
  pca9685.setPWMFreq(50);

}



void loop() {

  button1.loop(); // MUST call the loop() function first
  if (button1.isPressed()) {
    // change angle of servo motor
    if (angle == 0)
      angle = 90;
    else if (angle == 90)
      angle = 0;
      // control servo motor arccoding to the angle
    Serial.print("The button is pressed => rotate servo to ");
    Serial.print(angle);
    Serial.println("°");
 
  // Move Motor 1 from 0 to 180 degrees
  for (int posDegrees = 0; posDegrees <= 180; posDegrees++) {
     if (posDegrees % 10 == 0) Serial.println(posDegrees);
     pwm0 = map(posDegrees, 0, 180, SERVOMIN, SERVOMAX);
     pca9685.setPWM(SER0, 0, pwm0);
    // try to find the minimum required delay here OR better use a millis() controlled function to increase posDegrees:
     delay(50);
  }
// The following function makes sure that you have released the button 
// when the loop starts again. It may be not necessary with the delays, 
// but without them it ensures that you do not run into the button1.isPressed() 
// in the next "round"

  while (button1.isPressed())  button1.loop();
 
  }
}

Good luck!

1 Like

Hi there.

Thank you once again for the reply. I do appreciate the time you've invested and will never be offended by somebody explaining, teaching, sharing experiences which has information I already know. All information is welcomed, especially when sharing experiences and/or on forums, for others to find.

Thank you for the link, I've come across the Adafruit one but could not get the button to do as it should with the drivers, as it now does. I was getting mixed up with trying to use angle and pwm, which now the button works, I'll try and advance for that.

Appreciate the examples, I've never heard of that website to simulate the code. Excellent! The first code did not work as it should, the servo seemed to hit it's stop I think or was stalling. Not really sure. Need to get my head around the code.

The second code also did the same as the other button code. Travels from 0 to 180, back to 0, then 180 and stops at zero. That's with one button press also, same as before. Strange one. Again, being mid-week, I'll have to carry on tomorrow and will play with the pwm. Now the button works, I just need to get it to stop after it's first travel. Will update with what I get to work next.

With the example I sent, the delay is in the 'setup' section of the code, where the delay can be set using the ezbutton library, so I don't think it's that. Head scratcher.

  // Serial monitor setup
  Serial.begin(115200);

  button1.setDebounceTime(50); // set debounce time to 50 milliseconds
  button2.setDebounceTime(50); // set debounce time to 50 milliseconds

Again, thanks again for all of the help.

Seems to be a difficult one (or an easy one once the reason is found...)...

Just a remark: The debounce time set in the ezButton function does not delay! It uses the millisecond counter of the processor and does not block loop(). So when you check for a pressed button with if, loop() may enter this if clause multiple times per second... To avoid this it is sometimes better to check for change from pressed to released and react on that event because that is a real "event" while pressed or released are states that may stay for several loops....

Good luck!

1 Like

Ah okay, I got mixed up with the debounce of the library. Will look at that.
I’ve got something in my mind which may work, just need to test it tonight. Fingers crossed

1 Like

I assume you used the Adafruit sample code to get the parameters, didn't you?

Maybe you have to check that again or play around with the SERVOMIN/SERVOMAX data .... Sometimes strict logic and sometimes trial and error help to progress..

Anyway, I'll keep m fingers crossed :wink:

Well, I've spend a good few hours this week trying various things. Watching video's, reading websites and github pages. I realised I was getting mixed up with regards to angle and pwm, not in terms of theory but with calling certain parts of the code, so to speak.
The following part of the code works but after about 6 goes on the switch, the whole thing stops working, until I button smash for a while and it commences. Not sure if I've got some dodgy hardware or something or the code is stuck in the loop. It can't be debounce, as it happens for seconds, nothing happening, although I tried various debounce parts in the code. Currently I'm not using a pull up resistor or anything of the like, so not sure that would help but the ESP32 has an internal pullup. Maybe I should try that?

working code;

#include <Wire.h>                     
#include <Adafruit_PWMServoDriver.h>
#include <ezButton.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40);  // PCA9685 default I2C address

#define SERVOMIN 160  //MG995 153.6 round up to 160
#define SERVOMAX 540 //MG995 532.5 round up to 540

ezButton button1(18); // create ezButton object that attach to pin 7;
ezButton button2(19); // create ezButton object that attach to pin 7;

int Servo_Pin = 0;     
int angle;

void setup() {

  button1.setDebounceTime(50); // set debounce time to 50 milliseconds
  button2.setDebounceTime(50); // set debounce time to 50 milliseconds
  pwm.begin();         
  pwm.setPWMFreq(50);   
  delay(1000);

}

void loop() {

  button1.loop(); // MUST call the loop() function first

  if (button1.isPressed()) {
  angle = 0;
  angle = map(angle, 0, 180, SERVOMIN, SERVOMAX);  // 
  pwm.setPWM(Servo_Pin, 0, angle);                 
  delay(50);
  }

  button1.loop(); // MUST call the loop() function first

  if (button1.isPressed()) {
  angle = 180;
  angle = map(angle, 0, 180, SERVOMIN, SERVOMAX);
  pwm.setPWM(Servo_Pin, 0, angle);
  delay(50);
  }

}

I feel as though having the following of snippet of code should not be listed twice and maybe that is an issue but I couldn't get it to work in another format, without the servo constantly returning to zero.

button1.loop(); // MUST call the loop() function first

Now I did also try the following section in replace of the code above, which I'm sure worked also but now it won't. Not sure if it never worked or the HW is faulty, as previously mentioned but thought I'd mention it, just in case it helps someone.

void loop() {

  button1.loop(); // MUST call the loop() function first

  if (button1.isPressed()) {
  angle = 0;
  angle = map(angle, 0, 180, SERVOMIN, SERVOMAX);  // 角度(0~180)を4096の範囲に変換
  pwm.setPWM(Servo_Pin, 0, angle);                 // サーボを動作させる
  delay(1000);
  }

  else if (button1.isPressed()) {
  angle = 180;
  angle = map(angle, 0, 180, SERVOMIN, SERVOMAX);
  pwm.setPWM(Servo_Pin, 0, angle);
  delay(1000);
  }
}

Please check your code! You are calling button1.isPressed() two times instead of button1 and button2....

Guess you are in need of a break :wink:

Haha I am indeed.

That was intentional, although I know it is not correct. I’ve not put button2 in there yet. Which is why I mentioned that it shouldnt be there twice.

I’m wanting to build a controller and use an rf24 module eventually but this code is slow going. For now I managed to get it all working with the Blynk app, so then my son can use it too. He’s only 5 and really want me to build this project, which is an R2 model.

I’ll have a break from this and come back to it I think. Although I’ll still probably try again tonight ha. Thanks for all of your help!

Maybe I am too ... :wink:

How about replacing the button by two simple wires for a test (shortcutting them will bounce of course, but may be better than a faulty button)?

1 Like

Very good idea. I’ll give that try!

I made some small additions to your code from post # 10 ( the upper one) like this

#include <Wire.h>                     
#include <Adafruit_PWMServoDriver.h>
#include "ezButton.h"

// #################  Just for Testing #######################
#include "Streaming.h"
#include "ESP32Servo.h"
Servo myservo;
// ###########################################################

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40);  // PCA9685 default I2C address

#define SERVOMIN 160  //MG995 153.6 round up to 160
#define SERVOMAX 540 //MG995 532.5 round up to 540

ezButton button1(18); // create ezButton object that attach to pin 7;
ezButton button2(19); // create ezButton object that attach to pin 7;

int Servo_Pin = 0;     
int angle;

void setup() {

// #################  Just for Testing #######################
   Serial.begin(115200);
   myservo.attach(15);
// ###########################################################

  button1.setDebounceTime(50); // set debounce time to 50 milliseconds
  button2.setDebounceTime(50); // set debounce time to 50 milliseconds
  pwm.begin();         
  pwm.setPWMFreq(50);   
  delay(1000);

}

void loop() {

  button1.loop(); // MUST call the loop() function first

  if (button1.isPressed()) {
  angle = 0;

// #################  Just for Testing #######################
   Serial << "Button 1 :\t";
   myservo.write(angle);
// ###########################################################
   
   ServoGoto(angle);

  }

  button2.loop(); // MUST call the loop() function first

  if (button2.isPressed()) {
  angle = 180;

// #################  Just for Testing #######################
   Serial << "Button 2 :\t";
   myservo.write(angle);
// ##########################################################  

   ServoGoto(angle);

  }

}

void ServoGoto(int angle){
  angle = map(angle, 0, 180, SERVOMIN, SERVOMAX);
  pwm.setPWM(Servo_Pin, 0, angle);

// #################  Just for Testing #######################
   Serial << angle << "\n";
// ###########################################################

  delay(50);
}

and tested it successfully in Wokwi

https://wokwi.com/projects/325308355348267604

So if there are still problems with the buttons, it is quite likely that there is a hardware/wiring problem. You can also try to use button.isReleased() instead of button.isPressed() to see if there is any difference in behaviour (except that the function will start after a button was released.

1 Like

Hello there. Sorry, not trying to be rude! Really appreciate the code there and yet to try. Had a busy couple weeks at work and not touched any electronics!
Will have another look into this as I’ve picked it up today and getting some aweful noise on the MG995, which I’m not when not built into the model. Not sure if they’re just fakes and can’t take the weight

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