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
  • 2,844
  • 7
  • 31
  • 46
Anti Earth
  • 147
  • 1
  • 1
  • 11
  • 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 01 '15 at 13:57

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
  • 9
  • 14
  • 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 02 '15 at 00: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. – AMADANON Inc. Feb 02 '15 at 00: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 02 '15 at 01: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 .... – Russell McMahon Feb 02 '15 at 03: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. .... – Russell McMahon Feb 02 '15 at 03:28
  • Super rough pseudo code example of former. E&OE! : |// say 32:68 PWM; int mark = 32; int space = 68; thenM=micros(); MainLOOP { IF thenM = micros() { setPWMhigh(); thenS = micros() + mark;} IF thenS = micros() {setPWMlow(); thenM = micros() + space;} Other stuff ... ; } // end of main loop || This does not handle micros() rollover and has other issues but shows general principle. Simplistically you could run N of these in a loop independently. Size of N depends on time taken clock speed etc. – Russell McMahon Feb 02 '15 at 03:37
  • The timer isn't "connected" to an arbitrary output pin. The timer is set to trigger an interrupt at a set frequency, and the interrupt handler uses digitalWrite() (or equivalent) to toggle the pin. – osvein Feb 07 '19 at 23:04
  • @Osvein Your comment does not relate to my answer - maybe you intended to post it against the original question? | The word 'connected' - which you quote and comment on, does not appear in my answer. | You mention an arbitrary output pin - as does my answer. I explain the relationship between output pins and timers. It's not at all apparent that your comment relates to this. it PERHAPS adds a little as a comment on the OP's question. If you think that you have something useful to add I suggest that you create a new answer, or transfer your comment to the original post. – Russell McMahon Feb 10 '19 at 05:49
  • @Russell I was referring to your comment "it appears that the timers can be assigned to any pin". There also seems to be a misconception here that timers and PWM are separate parts of the hardware - PWM is just one of several different ways to configure the timers to generate a signal on the timer's output compare pins. – osvein Feb 10 '19 at 07:50
  • @osvein My answer, which is my definitive statement of what I'm stating. including viz " ... The timer can be assigned to any pin that has a digital output capability ...", which is true. The slightly more general comment in the comments re "any pin" is correct enough in context. IF a pin CAN be a digital output a timer can be assigned to it when using the TONE() command (which is what the question was and is about.). ... – Russell McMahon Feb 12 '19 at 10:25
  • @osvein ... Re misconceptions -> I dont see any. You seem to be straining at finding somewhat obscure points in the fine detail which THE ANSWER as it is now and has been since 1 Feb 2015 seems to address the query without confusion, for most people, anyway. If, 4 years on, you think that a better answer can be given, you are welcome to supply one. [Please do not attempt to modify my one to say something other than it already says]. – Russell McMahon Feb 12 '19 at 10:27
  • @Russell I wasn't trying to refute your answer, I was just adding a piece of detail that I think is necessary to understand the difference between the implementation of analogWrite() and tone(). Re misconception, you seemed to imply that the duty cycle of a PWM signal was not controlled by the timer - "The PWM does that, the timer does not" – osvein Feb 12 '19 at 10:39
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
  • 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 07 '19 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 09 '19 at 09: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 09 '19 at 09: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 '19 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 '19 at 18:23