Go Down

Topic: Uno - 2 Separate PWM Frequency Outputs (Read 2222 times) previous topic - next topic

jay123

HI,

I have done a fair amount of Arduino programming before, but very little with PWM outputs. I'm trying to do something that I assumed would be fairly simple, but am going round in circles trying to achieve it .... so thought it best to ask for some expert advice  :)

I need to drive two standard square wave outputs on the Uno .... one at a frequency of 3040Hz and one at 2010Hz. Both will need to run at the same time.

I have tried helping myself by using the PWM.h library, but to be honest the instructions don't seem to be detailed enough to get my little grey cells into order LOL.

If anyone could help me, I would be so grateful.

Many thanks,

Jay  :) :)

PaulS

Quote
I have tried helping myself by using the PWM.h library,

What PWM.h library?
The art of getting good answers lies in asking good questions.

jay123

Hi sorry ..... it was this library:

http://forum.arduino.cc/index.php?topic=117425.0

PaulS

Quote
If anyone could help me, I would be so grateful.

So, what is it you need help with?

The art of getting good answers lies in asking good questions.

jay123

Hi thanks for the reply,

Well, I guess I can understand how to get the code to drive the second output .... I tried (really badly I guess) to use the example and adapt it, but the library didn't have the normal level of instructions and I think I am failing miserably ! I get the correct frequency on pin 9, but pin 5 is way too high a frequency. I am using very low frequencies at the moment so I can just prove concept with an LED before I increase the frequency to 3040 and 2010Hz and check it with the scope.

Code so far :


#include <PWM.h>

int led1 = 9;                // the pin that the LED is attached to
int led2 = 5;
int32_t frequency = 3; //frequency (in Hz)
int32_t frequency2 = 4; //frequency (in Hz) NOTE: I'm guessing at adding the 2 suffix as the guide didn't specify

void setup()
{
  //initialize all timers except for 0, to save time keeping functions
  InitTimersSafe();

  //sets the frequency for the specified pin
  bool success = SetPinFrequencySafe(led1, frequency);
 
  //if the pin frequency was set successfully, turn pin 13 on
  if(success) {
    pinMode(13, OUTPUT);
   digitalWrite(13, HIGH);   

bool success = SetPinFrequencySafe(led1, frequency2);
 
  }
}

void loop()
{
  pwmWrite(led1, 100);

  pwmWrite (led2, 100); // the original code had a second variable in the brackets .... I am guessing this sets the duty ratio so set 100??

     
 
  delay(30);     
}

PaulS

Code: [Select]
bool success = SetPinFrequencySafe(led1, frequency2);
You've already assigned a frequency to this pin. Likely, you meant led2. And, now you can see why numbering one variable but not the other is a bad idea.

Code: [Select]
pwmWrite (led2, 100); // the original code had a second variable in the brackets .... I am guessing this sets the duty ratio so set 100??
Why are you guessing? Look at the source code and KNOW.
The art of getting good answers lies in asking good questions.

jay123

Hi there,

Yes thanks I did mean led2 ..... Stupid mistake :)

The reason I said I had guessed a couple of points was because normally in the past the libraries I have used have had more documentation. The wiki page where I downloaded the library just says refer to this forum. Ok the forum it does mention things like the pwmWrite:

pwmWrite(uint8_t pin, uint8_t val)   

But doesn't actually specify what uint8_t val is. Well it may if you're more experienced than me in deciphering the files behing the library, but for me I couldn't see what was needed so I thought it best to ask :)

jay123

Update ... Changed the typo from led1 to led2 .... No improvement :(

PaulS

#8
Jan 27, 2014, 12:04 pm Last Edit: Jan 27, 2014, 12:06 pm by PaulS Reason: 1
Quote
But doesn't actually specify what uint8_t val is.

But, the thread did say that pwmWrite() was, for modified timers, functionally equivalent to analogWrite() for non-modified timers. And, I'm pretty sure that analogWrite() is documented.

Edit: What happens if you move the pwmWrite() calls to setup(), and leave loop() empty? Is calling pwmWrite() every 30 milliseconds not the correct thing to do?
The art of getting good answers lies in asking good questions.

jay123

Hi Paul,

Thanks for taking the time to help me. I tried the code with the suggestions you made (renaming the led to led2), moving the pwmWrite() entries into the setup() area.

Unfortunately it doesn't seem to make any difference. The o/p  on pin 9 seems to be around 2Hz. If I change the frequency in the code it doesn't alter the flash frequency of the LED. The frequency of pin 5 is far higher than the set frequency (it appears dim and permanently lit).

My suspicion is that the code used to set the frequency doesn't appear to be doing what I need:

int32_t frequency = 3;
int32_t frequency2 = 4;

I'm really sorry for being dumb on this..... I don't usually struggle using libraries like this.

Cheers,

J

jay123

I guessed it should be easier than it sounds to output two square waves from my Uno. Would it be easier to use a 556 chip ? Honest answer !


Cheers and thanks for your advice,

Jay :)

Go Up