Skip to content

[Bug] Tone.cpp stop() sets pointer to NULL instead of driving GPIO LOW — Arduino Mbed 4.6.0 #1123

Open

Description

@RobMormile

Environment

  • Board: Arduino Nano 33 BLE (nRF52840)
  • Core: Arduino Mbed OS Nano Boards 4.6.0
  • IDE: Arduino IDE 1.8.19
  • Operating system: macOS
  • Source file: cores/arduino/Tone.cpp

Description

I discovered a bug in the Tone::stop() implementation that can leave a GPIO output stuck HIGH after a timed tone() call.

In my application, D3 controls an external MOSFET driving a magnetic buzzer. After a short 3500 Hz tone, D3 would intermittently remain HIGH instead of returning LOW.

This resulted in continuous DC current through the buzzer circuit, even though the buzzer was no longer producing sound.

Root cause

The Tone class declares:

mbed::DigitalOut *pin;

However, the stop() method contains:

void stop(void) {
    ticker.detach();
    pin = 0;
}

The statement pin = 0; sets the pointer to null rather than setting the physical GPIO output LOW.

Consequently, the pin may remain in whichever state it occupied when the ticker was detached.

Additionally, assigning null to the pointer loses the original dynamically allocated DigitalOut object, causing a memory leak when the destructor subsequently executes delete pin.

Proposed correction

Change:

pin = 0;

To:

*pin = 0;

The corrected method becomes:

void stop(void) {
    ticker.detach();
    *pin = 0;
}

This explicitly drives the GPIO LOW while retaining the pointer for proper deletion.

Steps to reproduce

  1. Configure an Arduino Nano 33 BLE with Arduino Mbed OS Nano Boards 4.6.0.
  2. Connect an oscilloscope to D3, referenced to GND.
  3. Execute:
tone(3, 3500, 30);
  1. Observe D3 after the tone finishes.
  2. Repeat until the intermittent fault occurs.

In my application, D3 sometimes remained at approximately 3.3 V after the tone ended.

The fault occurred even when no other tone requests were active.

Hardware measurements

During the fault:

Measurement Observed value
D3 output Approximately 3.3 V steady HIGH
MOSFET gate Approximately 3.0–3.3 V
Series resistor 47 ohms
Voltage across resistor Approximately 4 V
Continuous current Approximately 85 mA
Resistor dissipation Approximately 0.34 W

The external MOSFET was operating correctly. The MCU output was holding the MOSFET ON.

Additional testing

  • Changing the frequency from 3500 Hz to 2000 Hz did not reproduce the fault during limited testing.
  • A subsequent 2000 Hz tone could clear the stuck-HIGH condition.
  • The fault occurred without overlapping tone requests.
  • Calling noTone() after the tone duration did not reliably resolve the fault.
  • Adding digitalWrite(3, LOW) also did not reliably resolve it.
  • The problem was observed directly using an oscilloscope and multimeter.

Results after correction

I modified the installed Arduino Mbed core 4.6.0 source file, changing pin = 0; to *pin = 0;.

After recompiling and uploading the firmware:

  • The 3500 Hz tone generated the expected waveform.
  • D3 returned to 0 V after the tone.
  • The LOW output state was independently confirmed using a multimeter.
  • The previously observed stuck-HIGH condition has not recurred during initial testing.

Extended reliability testing is still pending.

Expected behaviour

After a timed tone() completes, the GPIO should return to LOW and dynamically allocated resources should be released correctly.

Actual behaviour

The GPIO can remain HIGH because stop() nulls the DigitalOut pointer rather than writing LOW to the output.

Request

Could the Arduino development team please review this implementation and confirm whether the proposed one-character correction is appropriate?

Thank you for maintaining the Arduino Mbed core.

Activity

  1. RobMormile commented on Nov 13, 2026

    @RobMormile
    Author

    I noticed that PR #1116 addresses the same Tone::stop() pointer ownership problem.

    However, that PR removes the pin = 0; assignment without explicitly driving the GPIO LOW.

    In my testing on the Nano 33 BLE, the output sometimes remained HIGH after the timed tone completed, leaving an external buzzer MOSFET continuously conducting.

    Changing pin = 0; to *pin = 0; addressed both the pointer ownership issue and the stuck-HIGH output during initial hardware testing.

    It may be worth considering this additional hardware behaviour when reviewing PR #1116 and issue #1123.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions