Notifications
Clear all

Programming the Arduino Mega with PCA9685

6 Posts
2 Users
1 Likes
4,536 Views
 Gerd
(@gerd)
Member
Joined: 3 years ago
Posts: 7
Topic starter  

Hi,

I'm going to control 16 servos with 16 potentiometers on the Arduino Mega combined with the PCA9685.

In the last part of the Dronebot Youtube video about controlling servos with Arduino was the setup with the Arduino Uno combined with the PCA9685 controlled by Potentiometers. (For the MeArm) Instead of the Arduino Uno I will be using the Arduino Mega. (has more analog input pins)  Can I use the same programming as shown in the video? Or does it work differently. 

Gerd. NL

 


   
Quote
(@dronebot-workshop)
Workshop Guru Admin
Joined: 5 years ago
Posts: 1085
 

@gerd

Hi Gerd

It should be the same, you just need to add all those extra variables to represent the additional analog inputs and their values.

If you wanted to be clever about it you could create a couple of arrays, instead of 32 discrete variables, but once it's compiled it won't really matter how you coded it.

😎

Bill

"Never trust a computer you can’t throw out a window." — Steve Wozniak


   
ReplyQuote
 Gerd
(@gerd)
Member
Joined: 3 years ago
Posts: 7
Topic starter  

@dronebot-workshop

Hi Bill, I uploaded the code on to the Arduino mega (from the MeArm) as you said in your last repley. The servo does not work. When I turn the potentiometer, the servos only wiggels a little bit. Can you help me figure out what's wrong?

kind regards

Gerd


   
ReplyQuote
 Gerd
(@gerd)
Member
Joined: 3 years ago
Posts: 7
Topic starter  

hey Bill,

I figured it out. Apparently the Arduino Mega needed to have it's own DC hooked up. (the DC female plug) Can that be right?

Now it works. But I still have a problem. I can turn my Potentiometer about 315 degrees. When I turn the potentiometer the Servos reacts perfectly the first 90 degrees. (90 degrees on the potentiometer = 180 degrees on the servo) When I go further on the potentiometer the Servo starts to wiggel on its 180 degree. The further I go on the potentiometer it looks like it want to go over the 180 degrees of the Servo.

can you help me? Do i need an other potentiometer? or can we do something in the program?

Kind regard and a good wekend to you!

Gerd


   
ReplyQuote
(@dronebot-workshop)
Workshop Guru Admin
Joined: 5 years ago
Posts: 1085
 

@gerd

Hi Gerd. A couple of things:

 The Mega is powered the same way as the Uno. If you needed to use the barrel connector it indicates that you are not supplying enough current from your computer's USB port.

Hopefully, you have not tried to power the servo motors using the same 5-volt supply the Mega uses!!  The PCA9685 always needs its own, separate, power supply, with enough current for all the servos. As you have 16 servos that means several amperes. Also, most hobby servos work better at 6-volts instead of 5-volts.

Also, it's pretty hard to help you when you haven't published either the code or a schematic. Please see the Forum Instructions for Inserting Code into your post

As a troubleshooting technique, you could print the values of the analog inputs and the corresponding servo outputs to your Serial Monitor, which should reveal why your servos are not traveling the way they should.

😎

Bill

 

"Never trust a computer you can’t throw out a window." — Steve Wozniak


   
codecage reacted
ReplyQuote
 Gerd
(@gerd)
Member
Joined: 3 years ago
Posts: 7
Topic starter  

@dronebot-workshop,

I want to thank you for all this help and your patience. I'm learning a lot!

So I used your code from the MeArm tutorial. I added the extra code lines to come up to 16 servos. The rest is the same. It works great but when the Potentiometer goes over the 90 degree angel the Servo is allready at 180 and starts to wiggle . (potentiometer is a B10K and turns about 300 degrees)

Do I need an other type of Potentiometer or do I need to do something with the code?

Thanks again!

Gerd

Below is the schematic and code:

Scematic Arduino Mega   PCA9685   Potentiometer
/*
Ardruino Mega - PCA9685 PWM Servo Driver Example

*/

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

// Include Adafruit PWM Library
#include <Adafruit_PWMServoDriver.h>

#define MIN_PULSE_WIDTH 650
#define MAX_PULSE_WIDTH 2350
#define FREQUENCY 50

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

// Define Potentiometer Inputs

int controlA = A0;
int controlB = A1;
int controlC = A2;
int controlD = A3;
int controlE = A4;
int controlF = A5;
int controlG = A6;
int controlH = A7;
int controlI = A8;
int controlJ = A9;
int controlK = A10;
int controlL = A11;
int controlM = A12;
int controlN = A13;
int controlO = A14;
int controlP = A15;

// Define Motor Outputs on PCA9685 board

int motorA = 0;
int motorB = 1;
int motorC = 2;
int motorD = 3;
int motorE = 4;
int motorF = 5;
int motorG = 6;
int motorH = 7;
int motorI = 8;
int motorJ = 9;
int motorK = 10;
int motorL = 11;
int motorM = 12;
int motorN = 13;
int motorO = 14;
int motorP = 15;

void setup()
{
pwm.begin();
pwm.setPWMFreq(FREQUENCY);
}

void moveMotor(int controlIn, int motorOut)
{
int pulse_wide, pulse_width, potVal;

// Read values from potentiometer
potVal = analogRead(controlIn);

// Convert to pulse width
pulse_wide = map(potVal, 0, 1023, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
pulse_width = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096);

//Control Motor
pwm.setPWM(motorOut, 0, pulse_width);

}

void loop() {

//Control Motor A
moveMotor(controlA, motorA);

//Control Motor B
moveMotor(controlB, motorB);

//Control Motor C
moveMotor(controlC, motorC);

//Control Motor D
moveMotor(controlD, motorD);

//Control Motor E
moveMotor(controlE, motorE);

//Control Motor F
moveMotor(controlF, motorF);

//Control Motor G
moveMotor(controlG, motorG);

//Control Motor H
moveMotor(controlH, motorH);

//Control Motor I
moveMotor(controlI, motorI);

//Control Motor J
moveMotor(controlJ, motorJ);

//Control Motor K
moveMotor(controlK, motorK);

//Control Motor L
moveMotor(controlL, motorL);

//Control Motor M
moveMotor(controlM, motorM);

//Control Motor N
moveMotor(controlN, motorN);

//Control Motor O
moveMotor(controlO, motorO);

//Control Motor P
moveMotor(controlP, motorP);

}

   
ReplyQuote