Hello everyone! This is my first time ever posting here, also English is not my first language, so sorry for any grammar mistakes.
Anyway, I'm trying to write two programs:
One to control a 12V dc pump( The rs-385 model) and a servo ( PDI 6221 MG High torque), both requires pwm control. However I'm having trouble in making the arduino(a Mega 2560) generate the pwm signal.
From what I've learned thus far, there are a few ways to do that. Using the function analogWrite(it generates a 490 hz pwm) or using the servo.h library(it generates a 50hz signal). Or modifying Timers and that whole stuff which I reaaaly want to avoid.
However, the servo I am working with, works with 330hz, and I kinda don't know what frequency the pump works with(I'm assuming it's a dc motor so...15k?). Anyway, I want to be able to generate any kind of frequency without having to mess with Timers and prescalers.
I've tried two libraries already: PWM.h and AVR PWM.h, both compile just fine, without errors, but no PWM signal is generated. It's not the board(Servo.h works just fine), nor the ports(I'm using ports 3 and 9).
My code for PWM it's as following:
#include <PWM.h>
int pinoServo = 9;
int32_t frequency = 330;
void setup()
{
InitTimersSafe();
bool success = SetPinFrequencySafe(pinoServo,frequency);
if (success)
{
pinMode(9,OUTPUT);
}
}
void loop()
{
pwmWrite(pinoServo,50);
}
Also are there other pwm libraries which I can use on Mega?
I agree, with DrDiettrich.
Do you have some specific reason to complicate your life with some certain frequency?
Be aware both of your motors need a beefy power supply, I don't know specs, but could imagine >3A.
Well the fact that it compiles does not guarantee that the program logic is correct!
A couple of questions:
How do you know there is no PWM being generated? For example, are you seeing no signal on an oscilloscope?
Have you tested that success is being set to true? Activating pin 9 depends on that being the case.
How are you driving and powering the pump motor?
BTW, the data on the PDI 6221 MG is a bit confusing. I have seen at least two references that quote (1520uS) 330MHz as being the 'dead band', which means that the servo will not move at this frequency. Yet another quotes this as the working frequency? A couple of sources quote the operating PWM range as 500-2500μs. Another quotes it as 500-2500Hz. Both cannot be correct. Difficult to know what the exact specification is, although they all seem to agree that the operating voltage is between 4.8V and 6V. I have yet to find an actual datasheet. The above is from vendor descriptions.
Not an uncommon misstatement of the very common servo pulse widths, on AliExpress. Would be my first guess, and I would try using the simplest of servo examples in the IDE.
I agree, with DrDiettrich.
Do you have some specific reason to complicate your life with some certain frequency?
Be aware both of your motors need a beefy power supply, I don't know specs, but could imagine >3A.
It's because I needed a high torque servo for my project(graduation project actually). I tried the highest compatible servo I could find(which was MG996r) and it didn't work as intended, so I had to get a servo with a extra power.(which was this PDI 6221MG).
This servo, as I said above appears to work with a frequency of 330 hz, instead of 50hz from the servo library. So I kinda need a specific frequency if I want this servo to work properly.
For the power supply, you are mostly correct. I'm using a DC power source and I've seen some high currents up to 1,5~2A give or take.
Yes...trying to find a datasheet about any High-torque servo is a pain, I gave up a long time ago.
I'm using this one assuming:
– Voltage: 4,8 to 6 VDC
– Dead band: 2 µs
– Frequency: 1520 µs / 330 Hz
– Speed operation (4,8 V): 0,18 sec/60°
– Speed operation (6 V): 0,16 sec/60°
– Stall Torque (4.8V): 17,25 kg.cm (239,55 oz/in)
– Stall Torque (6V): 20,32 kg-cm (281,89 oz/in)
Just after posting this, I started to explore this forum and found out a code that actually works...kinda? It's still a bit confusing tho.
Even servo.h could be swiftly modified to do what you need, while ignoring those things that you cannot do with it as a result. YMMV, and depending upon your skill level, it might be "out of reach".
Most digital servos work at frequency 50-330Hz.
Chances are high that you servo works equally at 50Hz, there's no need to drive it at highest frequency.
Do not use the PWM output with a Mega, that is not the way things work. Use the servo library on the Mega. While this is doing PWM of sorts is generated, but not in the "normal" sort of PWM produced by the AnalogWrite function.