1

I’m trying to output 2 separate frequencies using 2 output pins from an Arduino using the Timer 1 library. A snippet of the code is show below.

void setup() 
{
 Serial.begin(9600);
 pinMode(button,INPUT_PULLUP);
 pinMode(button2,INPUT_PULLUP);
 pinMode(sig_out, OUTPUT);
 pinMode(sig_out2, OUTPUT);
Timer1.initialize(1000);
Timer1.pwm(sig_out,512,period);
Timer1.pwm(sig_out2,512,period2); 
}

The issue is the way I’ve wrote this makes sense as I’ve initialised 2 outputs and 2 inputs but the ‘period’ is getting overwritten as there’s 1 timer and I’m not sure how to stop this. I was thinking about storing them in variables (like 'T1=' 'T2=') but it would still use the same timer.

I just wanted to know if there’s a way to use 2 timers so the Arduino outputs the frequencies separately.

4
  • This is not normally how timers work. You can sometimes do some tricks though. Are the periods you want 1. reasonably slow (a few KHz) and/or 2. mathematically related to each other in a simple way (e.g. one is double the other, or 2/3 the other etc.)
    – Jack B
    Commented Sep 11, 2019 at 15:02
  • Also: Do you realise that a timer is usually a physical bit of hardware inside the chip, so you can't create more of them in software. The chips used in arduinos usually have three hardware timers though, so you could use one of the others?
    – Jack B
    Commented Sep 11, 2019 at 15:06
  • @Jack B How would I use the other ones?
    – Neamus
    Commented Sep 11, 2019 at 16:39
  • Normally, with these chips, you configure timers by writing to registers e.g. TCCR1A=0x01;. TimerOne looks like an Arduino-specific library which tries to make it simpler, but I'm not familiar with it. So you options are either to learn how to use the timer control registers directly, or find an Arduino library to do it. I see that there is a TimerThree library, but it doesn't support all boards.
    – Jack B
    Commented Sep 11, 2019 at 17:21

1 Answer 1

1

The Timer1 library only supports... Timer 1! So you only have one hardware timer available. If you are using an Arduino Uno or anything similar, you have a couple of other timers. However, unlike Timer 1, which is a 16-bit timer, the others are 8-bit only. This means they are very limited in the range and precision of the frequencies they can generate. If you have an Arduino Mega, then you have a few other 16-bit timers available. You may want to search for a library called Timer3, which should work just like Timer1, while using the 16-bit Timer 3.

If you don't find any suitable library, you may still be able to generate your two signals using only Timer 1. Here is the trick:

  • set both PWM outputs to “toggle on compare match”
  • enable both compare match interrupts
  • in each of these ISRs, advance the compare match register by half a period of the corresponding signal.

For doing this you will need to dig into the low-level details of how a timer actually works. I recommend you start by reading the Timers and counters tutorial by Nick Gammon. Take the time to read and study this. It's not a topic you will be able to master in just a couple of hours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.