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.
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);
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.
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 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
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?
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.