Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Driving the speaker pin with hardware PWM produces only noise the first time #313

Open
microbit-carlos opened this issue May 5, 2023 · 6 comments
Assignees
Labels
bug Something isn't working micropython p2
Milestone

Comments

@microbit-carlos
Copy link
Collaborator

microbit-carlos commented May 5, 2023

This example programme produces a weird noise the first loop iteration, and the correct tone the others.
MICROBIT.hex.zip

#include "MicroBit.h"

MicroBit uBit;

static void playTone(int frequency, int ms) {
    Pin *audioPin = &uBit.io.speaker;
    if (frequency) {
        audioPin->setAnalogPeriodUs(1000000 / frequency);
        audioPin->setAnalogValue(127);
    }
    uBit.sleep(ms);
    audioPin->setAnalogValue(0);
}

int main() {
    uBit.init();

    // Don't know if we are meant to disable the pipeline to drive the pin directly, but this doesn't make a difference
    uBit.audio.disable();   

    for (int i = 0; i < 4; i++) {
        playTone(440, 500);
        uBit.sleep(500);
    }
}
@microbit-carlos
Copy link
Collaborator Author

This is still replicable in v0.2.55.

@microbit-carlos microbit-carlos modified the milestones: v0.2.57, v0.2.58, v0.2.59 Jul 25, 2023
@microbit-carlos
Copy link
Collaborator Author

I can confirm this is still replicable with CODAL v0.2.57

@martinwork
Copy link
Collaborator

The first call to setAnalogPeriodUs does nothing because the pin hasn't been configured as an analogue output.
https://github.com/martinwork/codal-nrf52/blob/master/source/NRF52Pin.cpp#L633

@microbit-carlos
Copy link
Collaborator Author

Thanks Martin!
I can confirm that switch the order and executing first setAnalogValue() and then setAnalogPeriodUs() fixes the issue.

However, it makes sense for the user to want to set up the analog out configuration before changing the pin mode to start outputting the signal.

@microbit-carlos
Copy link
Collaborator Author

microbit-carlos commented Aug 11, 2023

Discussed with @finneyj and we should be able to configure analog settings before using the pin as an analog input/output, so we can consider this a bug to fix.

With that in mind, this MicroPython issue is also related:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working micropython p2
Projects
None yet
Development

No branches or pull requests

4 participants