PC Speaker without tone() or timers

Hi guys,

I need your help. I'm working on a project that has become rather big and uses a lot of pins. Now I want to implement a PC Speaker to send our some beep codes (always the same frequenzy beep with different delays between).

That is easy on my prototype with the tone() library. But not when I want to merge it in my full project because the TONE library makes some of my PINs unusable (timer, PWM)

I found this on arduino playground:

For Arduino Mega: (tested on Arduino Mega 2560)
timer 0 (controls pin 13, 4)
timer 1 (controls pin 12, 11)
timer 2 (controls pin 10, 9)
timer 3 (controls pin 5, 3, 2)
timer 4 (controls pin 8, 7, 6)

Pin8 - RF TX Module
Pin 9, 11, 12, 13 are used for 4 PWM signals (white, r,g,b)
10 is used for one wire temp sensor
0 - 7 is used for my touchscreen
14 - 15 ph sensor
16 - 17 ESP8266
18 - Ultrasonic Range Sensor
19 - RF RX Module
21, 21 SDA SCL for multiple stuff (RTC, breakout Expander,..)

So I can't sacrifice a single pin for the tone library. At least I think so.

So I need some code to let the speaker beep without any use of tone().

Any help is welcome

thank you

Just toggle a pin high and low at a given frequency. In simplistic form

void loop()
{
  digitalWrite(13, HIGH);
  delay(1);
  digitalWrite(13, LOW)
  delay(1);
}

This will give you a frequency of (roughly) 500Hz on pin 13.

You may want to use micros() and avoid delays so other code can run.

thanks for the hint sterretje - I'll try that.

And for sure I will use millis and micros.. ..in my 7000lines of code you wont find a lot of delays - delay is bad! :slight_smile:

If you don't care about frequency, use an active piezo buzzer available on eBay and elsewhere for almost nothing. That way you only need to turn it on and off, no pwm, and any pin works.