OK, I have a little hardware issue I need to fix in software for the time being. I say, "For the time being," because I already have the PC Boards printed, and I need to make do for this run of boards. (So sue me for not bread boarding first.) I THOUGHT the little buzzers I purchased sounded off on a DC input, but I was wrong. I used the nearest pin to the buzzer to activate it, which is D14. The next run of boards I will use A6 instead, but until then I will need to output a square wave of about 2731 Hz on D14 of an ATTiny88. The straightforward approach would seem to be something like:

bool buzzer(bool x) {
  if (x) {
    return true;
  return false;
bool PinVal = false;
void loop() {
  <some code here>
  buzzerVal = buzzer(true | false);
  <some code here>
  if (buzzerVal && micros()%376 > 365) {
    PinVal = !PinVal;
  while (micros()%376 > 365);

Is there a smarter, more efficient way? If so, I might consider not even changing the board layout. Pin 14 has an alternate function of register PB7. It occurs to me it might be more efficient to write directly to PB7, rather than using the digitalWrite function, which presumably has more overhead. Yes? No? Any other ideas?

  • 3
    Try the tone() library or built-in function to generate the signal.
    – 6v6gt
    Commented Mar 4, 2023 at 1:07
  • 1
    "The next run of boards I will use A6 instead", there is a fundamental misunderstnad of what A6 means, the pin is capable to be configure as an ADC input of ADC channel 6, but when you using it as an GPIO output, it just another GPIO pin like D14 pin.
    – hcheung
    Commented Mar 4, 2023 at 2:07
  • 1
    Also, depending on the microcontroller, A6 may not even be usable as an output pin of any sort. For example, the SMD packages of the ATmega328P break out analog pins A6 and A7 but these are pure input pins. The remaining analog pins A0 to A5 do not have this restriction and can also be used as digital pins.
    – 6v6gt
    Commented Mar 4, 2023 at 3:24
  • 3
    you don't need PWM (sound frequencies are not that high) and you don't need analog output (it would not vibrate the buzzer and there isn't a DAC on the ATTiny). you can use any pin. if the buzzer is 'active', then set the pin HIGH. if the buzzer is passive, use the tone() function or a timer with interrupt.
    – Juraj
    Commented Mar 4, 2023 at 16:03
  • 1
    Why not buy the correct buzzers, that appears to me to be the easiest solution.
    – Gil
    Commented Mar 6, 2023 at 7:01


Your Answer

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

Browse other questions tagged or ask your own question.