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);
}
}