4

According to my current (poor) understanding, only the PWM pins (with the ~) among the digital pins (ignoring Analog pins) can be passed to analogWrite(). I understand the servo therefore - since interfaced by a variable voltage signal - must be connected to such a PWM pin.

  • Why is the humble Piezo Buzzer not bound to this condition?
  • Why can I connect the buzzer to a digital pin that is not PWM (and so should only be able to output binary LOW, HIGH voltage at some consistent frequency) and still achieve a varying sound by calls to the tone() function?
  • Surely under the limitations of being digital and non-PWM, the pin could only cause some consistent buzzer frequency?

I'm reading online that the Piezeo Buzzer does indeed require a PWM digital pin, but I can connect it to any digital pin on my Galileo gen2 and have it function seemingly correctly (differing tones produced).

Also, since slightly relevant; what exactly is the difference between a PWM digital pin and an analog pin, in terms of output? They both use PWM, right? Why can't even a PWM digital pin read analog input?

1
  • 1
    The PWM pins do hardware PWM. You can however also have the software do PWM. I guess that is what the library you use does. The analog pins are for reading an analog voltage between 0 and 5 volts. This has noting to do with PWM, besides the misleading name analogWrite arduino uses.
    – Gerben
    Feb 1, 2015 at 13:57

2 Answers 2

4

Why does a Piezo Buzzer not require a PWM digital pin?

Tone() uses the microcontroller's hardware timers to control the related code function but do not use the hardware PWM features. The timer can be assigned to any pin that has a digital output capability.
Tone() sends a "rail to rail" square wave (50% duty cycle) to the selected pin

Slightly more lower level information here under Ugly details
They say:

  • The library uses the hardware timers on the microcontroller to generate square-wave tones in the audible range.

    You can output the tones on any pin (arbitrary). The number of tones that can be played simultaneously depends on the number of hardware timers (with CTC capability) available on the microcontroller.

    ATmega8: 2 (timers 2, and 1)
    ATmega168/328: 3 (timers 2, 1, and 0)
    ATmega1280: 6 (timers 2, 3, 4, 5, 1, 0)

    The timer order given above is the order in which the timers are allocated. Timer 0 is a sensitive timer on the Arduino since it provides millis() and PWM functionality.


... what exactly is the difference between a PWM digital pin and an analog pin, in terms of output? They both use PWM, right? Why can't even a PWM digital pin read analog input?

There is a misconception there.
Analog pins are capable of Analog INPUT functionality .
"PWM" output is known as "analogWrite" but is actually a purely digital function and is not related to whether a pin has analog read functionality.
PWM out = analogWrite is available on pins which have associated PWM hardware.
The actual signal is a "rail to rail" square wave with a mark:space (on:off)that varies. Filtering the PWM produces DC at the average DC level. PWM range is 0 to 255 (8 bits)

12
  • 1
    Is there a difference in the square-wave produced by a digital pin via timers and that of a PWM digital pin? Why is any PWM hardware needed if timers can emulate analog output on all digital pins? Why can the servo not be operated from a non-PWM digital pin by a square wave produced using timers?
    – Anti Earth
    Feb 2, 2015 at 0:05
  • @AntiEarth, the PWM can be set up to do their thing, and then left to it (indefinitely, or until the settings are changed). To change any other pin from high to low, or from low to high, requires attention at the exact time of the transition. Feb 2, 2015 at 0:42
  • @RussellMcMahon, so it's a matter of computational intensity (or at least, scheduling)? Why still is the servo bound to hardware PWM?
    – Anti Earth
    Feb 2, 2015 at 1:26
  • @AntiEarth AMADANON's answer would be correct in some systems but not here (probably). I'll check the hardware features of the uC before saying too much more, but whereas AMADANON is suggesting a pure software based answer, the tone() command uses system timers. It appears that the timers can be assigned to any pin. | Re PWM versus square wave. "Square wave" unless specified otherwise is assumed to mean a rail to rail waveform with 50:50 mark space ratio. A servo's position control requires a variable mark:space ratio. The PWM does that, the timer does not. Note that ALL digital capable .... Feb 2, 2015 at 3:23
  • .... pins are capable of outputting PWM under software control. For a single channel this is trivially easy. But, with due care and knowledge and skill [tm] you could probably have every output providing PWM by using the millis() or micros() functions. These allow you to run in an uninterrupted loop while checking the timer "on the fly". You can use this like a "round robin" scheduler with each PWM having its own timing register to compare with micros() result OR all channels could be combined with the routine waiting until each way-point arrived and changing relevant output bits. .... Feb 2, 2015 at 3:28
0

The signals that you mention are of different nature.

1) The PWM signal is a digital signal (output voltage is either ground or supply) that is modulated in time. It has a fixed period and its duty cycle varies from 0% (always ground) for value 0, to 100% (always supply) for value 255, the middle value being a signal that switches between ground and supply with 50% of its period at ground and 50% of its period at supply.

2) A servo is controlled by a signal that has a fixed period of 20 ms and a pulse of varying duration (between 0.5 to 2.5 ms).

3) A tone for a buzzer has a variable period (or variable frequency - both being linked by the equation frequency * period = 1). The period is audible if is is between 0.06 ms (15 kHz) and 60 ms (15 Hz) (the exact value change from person to person. If the frequency is higher than 15 kHz, you are entering ultra-sonic.

All three signals are digital in their voltage and modulated in time. But the modulation is very different as indicated above and the Arduino driver is therefore different for each one.

5
  • You don't explain how these differences make it possible to generate a tone on an arbitrary output pin. Truth is all of these could have been made to work on any pin with slightly less accuracy using timer interrupts, but the Arduino creators chose not to for reasons unbeknownst to me.
    – osvein
    Feb 7, 2019 at 23:11
  • @osvein There can be a good reason: The MCU has specific hardware to support PWM output, it is therefore more efficient to use this hardware instead of interrupts when possible. But the way a PWM hardware is controlled is not ideal for some applications (like tone generation). The second part of your comment is on coding preferences, and that's to be discussed with the Arduino coding team.
    – MAC
    Feb 9, 2019 at 9:00
  • The hardware timers can do tone generation directly on the output compare pins without interrupts just as well as they do PWM. See the datasheet.
    – osvein
    Feb 9, 2019 at 9:40
  • Yes, sometimes, but you do not use a PWM to do tone generation. It's another way to use the timer hardware (and in some processors, these are separate timers).
    – MAC
    Feb 10, 2019 at 16:39
  • but in the case of the Arduino, they're not separate hardware. It can use the exact same hardware (the same output compare pins on the same timers) for PWM and tone generation.
    – osvein
    Feb 10, 2019 at 18:23

Your Answer

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

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