You are reading a single comment by @Robin and its replies. Click here to read the full conversation.
  • I'm trying to add a speaker to a project, following the Making Music page using an ESP8266 board (NodeMCU v3).

    The example is using analogWrite() to create sound on the speaker and it works.
    When the speaker is emmiting sound, I can enter new analogWrites with different frequencies to change the pitch and that works instantly.

    The problem is when the speaker is going from silent to emmiting sound. There is quite a long delay before it does anything (~1700ms). Is that normal behavior with the ESP8266 ?

    The final desired product chimes when a door is opened and 1700ms delay makes it weird.

    Example:

    // starting from silent
    let BUZZER = NodeMCU.D1
    // action : activate buzzer at 1000 freq
    analogWrite(BUZZER, 0.5, {freq:1000});
    // result : ~ 1700ms delay... then sound
    
    // action : change freq to 1500
    analogWrite(BUZZER, 0.5, {freq:1500});
    // result : instant pitch change
    
    // action : stop buzzer
    analogWrite(BUZZER, 0, {freq:1500});
    // result : instant stop
    
    // action : activate buzzer again
    analogWrite(BUZZER, 0.5, {freq:1000});
    // result : ~1700ms delay then sound
    
    

    So, copy-pasting the whole example code results in no sounds as there are no notes lasting over 1700ms.

    Example 2:
    I can also observe the same effect visually with the following snippet (breathing LED) :

    setInterval(()=>{ analogWrite(D2, (Math.sin(getTime()) / 2.01) + 0.5) }, 10) 
    

    And then at any point sending analogWrite(D2,0) or digitalWrite(D2,0) (or 1) will pause the effect for roughly 1700ms again. Actually, sending analogWrite between 0 and 1 (exclusively) to any pin that is not already been "activated" by another analogWrite will pause the effect on the unrelated D2 pin!

    What I have tried :

    1. Calling analogWrite twice
    2. Using digitalWrite for 0 and 1 values
    3. Using analogWrite 0 and 1 with and without frequency
    4. Using analogWrite in a setTimeout
    5. Using values very close to 0 and 1 as "off" state (that kinda works but big cheat as it is still audible from closer)
    6. Using a different ESP8266 dev board

    Bonus info:

    In the breathing LED effect snippet, I am dividing the sin of time by 2.01 instead of 2 which would allow the numbers to include 0 and 1. If I don't do so, the LED becomes unpredictable when going low, it can flash at full brightness for a very small amount of time, Im assuming when it reaches an actual 0 or 1, but the flash does not last 1700ms.

  • Fri 2020.02.21

    While I normally don't respond to non-officially supported boards posts @Drumline18, this one intrigued me as I recently played with similar code snippets, but with non ESP8266 boards.

    In my case, using a logic analyzer, the duration to produce a 1KHz signal using similar sample snippets as in post #1, would occur in one cycle or in this case ~1msec.

    'There is quite a long delay before it does anything'

    But in L9 it is stated 'instant' - so which is it?

    As all the code is not present, it is difficult to determine, but the one and a half second delay could be in another part of the code block. Post all the code.

    Without the datasheet for the specified pins, and their ability to drive the speaker, I'd have to guess here. Again, post the link to the specific device datasheet.

    How is the speaker connected? Is there a decoupling capacitor used? Does that have polarity? If so, it's possible the delay is caused as that cap is charging. Again, post a picture or better describe.

    re L3 '// action : activate buzzer at 1000 freq'

     
    reference to 'speaker' in text and 'buzzer' in code

    Is it an actual 4 ohm or 8 ohm speaker, or is it a passive/active buzzer as the keyword defined implies?

    I suppose there may be an internal issue. Clicking on the Right facing arrow adjacent to the analogWrite() function heading might lead to some clues.

    https://www.espruino.com/Reference#l__gl­obal_analogWrite

    Click on Right Facing arrow takes one to it's source:

    https://github.com/espruino/Espruino/blo­b/master/src/jswrap_io.c#L167

    Divisor for frequency perhaps? Not likely as 1000 base10 is not easily divided by a nice value such as 1024 or 2^10 base2

    What are the results of attempting a dozen other frequency values and another group of multiples of ten? a test to see if division by ten is causal

    Is memory being cleared between each new frequency test? That example 2 nesting of an analogWrite() within an interval might be problematic. The previous write to that pin might not be cleared before the intended frequency is desired. I found out something similar when the logic analyzer displayed more pulses (higher frequency) than what I thought I was attempting to output.

About

Avatar for Robin @Robin started