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?

Greenonline
  • 3,152
  • 7
  • 36
  • 48
Anti Earth
  • 145
  • 1
  • 2
  • 12

2 Answers2

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)

Russell McMahon
  • 2,421
  • 11
  • 14
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.

MAC
  • 254
  • 1
  • 4