Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Tone generation using PWM

    Scheduled Pinned Locked Moved Discussion
    3 Posts 1 Posters 57 Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P Offline
      pmulvey
      last edited by

      My routine is:

      def Tone(Speaker, Frequency, Duration):
          pwm = PWM(0, frequency = Frequency)
          pwm = pwm.channel(0, pin=Speaker, duty_cycle = 0.5)
          sleep_ms(Duration)
      

      Calling it with:

      Tone(Speaker, 800, 1000)
      

      How do I stop the Tone after the delay?

      This routine on the Esp8266 worked:

      def Tone(Speaker, Frequency, Duration):
          pwm = PWM(Pin(Speaker))
          pwm.freq(Frequency)
          pwm.duty(500)
          sleep_ms(Duration)
          pwm.deinit()
      
      P 1 Reply Last reply Reply Quote 0
      • P Offline
        pmulvey @pmulvey
        last edited by

        I'm answering my own query again but a workaround that I found is this:

        def Tone(Speaker, Frequency, Duration):
            pwm = PWM(0, frequency = Frequency)
            pwm = pwm.channel(0, pin=Speaker, duty_cycle = 0.5)
            sleep_ms(Duration)
            pwm = PWM(0, frequency = 4000)
        

        Setting the PWM frequency to >3999Hz kills the output. Must be a more elegant way though.

        1 Reply Last reply Reply Quote 0
        • P Offline
          pmulvey
          last edited by

          Actually this is a better fix, the previous fix left the Speaker pin High and using power, this leaves it Low.

          def Tone(Speaker, Frequency, Duration):
              pwm = PWM(0, frequency = Frequency)
              pwm = pwm.channel(0, pin=Speaker, duty_cycle = 0.5)
              sleep_ms(Duration)
              i = Pin(Speaker, mode=Pin.IN)
          

          I seem to be talking to myself a lot !!?? Anyone else around?

          1 Reply Last reply Reply Quote 1

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • 1 / 1
          • First post
            Last post

          Pycom on Twitter

          Pycom