Buzzing with timer interrupt

I have a project where we need to buzz a sound for a duration that’s set via a potentiometer. Usually it’s fairly easy with a delay function but here I need to use timer interrupts. I still can’t figure out how they work and after some research i came up with this code.

#define BUZZER 13
#define MIN_BPM 0      // minimum bpm value
#define MAX_BPM 200     // maximum bpm value
#define POT A0          // pot analog pin
int bpm=200;                // bpm value
void setup()


  pinMode(BUZZER, OUTPUT);

  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1  = 0;
  OCR1A = 15624;
  OCR1B= 7812;
  TCCR1B |= (1 << WGM12);   // CTC mode
  TCCR1B |= (1 << CS12);    // 256 prescaler 
  TIMSK1 |= (1 << OCIE1A);  // enable timer compare interrupt
  interrupts();             // enable all interrupts

  digitalWrite(BUZZER, LOW);  

void loop()
  digitalWrite(BUZZER, LOW); 
  bpm = map(analogRead(POT), 0, 700, MIN_BPM, MAX_BPM);

  OCR1A = (16000000) / ((bpm*1024) - 1);           // compare match register 16MHz/256/2Hz

It’s not working as intended and i still can’t understand how to change the interrupt frequency with an analogue signal coming from a potentiometer.
My general idea is:
digitalwrite(buzzer) is set to high, the timer interrupts it with the frequency in “bpm” setting digitalwrite(buzzer) to low. bpm is constantly read in the loop function and sets the according interrupt time.

Could anyone please help me with this project?

Darn I have the perfect solution but I cannot tell you how to fix it, your schematic does not show that area. Post a complete schematic, not a frizzy drawing. Yes you must show all connections including power supplies. Also provide links to technical data on all the hardware items.

You mean the code? I posted the complete code, i know it doesn't work could you help me with that please? Thanks

The buzzing could be a hardware problem, that is why I asked for a schematic. It is your choice, if you do not want to post it that is OK.

I don't have a schematic. The buzzing works with a normal code (without using interrupts). The red cable is connected to pin 13 and the black one to GND

Try a different pin, pin 13 has a led connected on the Arduino board. I am assuming you re using a UNO or Nano. You can see that in the Arduino schematic. Most of us draw our own schematics. Had you shown a schematic your problem would have been immediately visible. Remember when learning to draw schematics do it in baby steps and look at others and understand them. It is a whole new picture language.

"pin 13 has a led connected on the Arduino board" UNO DOES NOT HAVE AN LED CONNECTED - D13 drives an Op Amp that drives the LED.
Nano does have an LED directly connected.



""pin 13 has a led connected on the Arduino board" UNO DOES NOT HAVE AN LED CONNECTED - D13 drives an Op Amp that drives the LED." This is correct on REV 3 but not on previous versions. This leaves REV 3 currently unique. Since the Model and version information was not given I took the safe solution.

thanks, i tried with pin 7 but it still doesn’t work, my idea is that the issue is not hardware related since i’m just trying to understand how interrupts work and how to control the interrupt frequency with a potentiometer.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.