Black Lives Matter - Action and Equality. ... Adafruit is part of the Stop Hate for Profit campaign. Adafruit is open and shipping.
0

Piezo buzzer works, then doesn't
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Piezo buzzer works, then doesn't

by mcalleia on Sun Apr 23, 2017 5:06 pm

Gave the kiddo a challenge to use an Arduino and piezo buzzer to communicate "Hello World!" in Morse Code. Kiddo wrote a sketch (with some help, being 6 yo) and wired up our Arduino (with almost no help), and earned her "hello world" programming patch, but we ran into a problem.

The sketch runs, and the buzzer buzzes on and off in dots and dashes for a while, then it kind of just buzzes in a lower frequency.

Here is the sketch and how we wired it up: https://circuits.io/circuits/4718753-li ... breadboard

We tried with and without a 1k resistor, same thing happened in both cases.

The buzzer we are using is this one https://www.adafruit.com/product/1740.

mcalleia
 
Posts: 9
Joined: Sun Apr 23, 2017 4:47 pm

Re: Piezo buzzer works, then doesn't

by adafruit_support_mike on Mon Apr 24, 2017 4:31 am

Could you post a photo showing the actual hardware and connections please? 800x600 images usually work best.

adafruit_support_mike
 
Posts: 61121
Joined: Thu Feb 11, 2010 2:51 pm

Re: Piezo buzzer works, then doesn't

by mcalleia on Mon Apr 24, 2017 9:50 am

Attached below.

Note, before taking the photo, I ran a quick experiment that shows the issue is not our circuit. I removed all jumpers from the Uno, connected to USB and let the sketch run. While there is no buzzer on the Uno, we used pin 13, so the onboard LED flashes our Morse Code. After flashing dots and dashes correctly for a while it did the same thing, just stayed on.

Wondering if there is something about tone() causing this and we need to set the pin to LOW to keep the weirdness from happening.

Image

mcalleia
 
Posts: 9
Joined: Sun Apr 23, 2017 4:47 pm

Re: Piezo buzzer works, then doesn't

by adafruit_support_mike on Tue Apr 25, 2017 5:31 am

I duplicated the problem, and it seems to be related to the tone() function's duration parameter.. to the extent that it works, it's hard to sync with the delay() function.

Changing your dot() and dash() functions like so should get things working as expected:

Code: Select all | TOGGLE FULL SIZE
void dot()
{
  tone( buzzer, freq );
  delay( unit );
  noTone( buzzer );
}

void dash()
{
  tone( buzzer, freq );
  delay( unit * 3 );
  noTone( buzzer );
}
The trouble is that tone() is a non-blocking function.. when you call it, execution returns immediately and goes on to the next line of code. The business of generating sound is handled by a timer off in another part of the chip, and the duration parameter tells the timer when to stop toggling the pin. There's no easy way to predict when the pin will stop buzzing relative to the rest of your code though.

Non-blocking/asynchronous code is a challenge in general, and it looks like you wanted the sketch to run as it will with the functions above.

adafruit_support_mike
 
Posts: 61121
Joined: Thu Feb 11, 2010 2:51 pm

Re: Piezo buzzer works, then doesn't

by mcalleia on Tue Apr 25, 2017 12:23 pm

Got it.

Since only one tone can be played at a time, I incorrectly assumed tone() was totally blocked the next line of code until complete.

Thanks for the explanation and fix.

mcalleia
 
Posts: 9
Joined: Sun Apr 23, 2017 4:47 pm

Re: Piezo buzzer works, then doesn't

by adafruit_support_mike on Thu Apr 27, 2017 2:06 am

That's a reasonable thing to expect, since most code works that way. tone() uses a more advanced technique by offloading work from the CPU to other parts of the chip called 'peripherals'.

Peripherals make it possible to do all sorts of things that would be hard to code directly, but they do take some getting used to.

adafruit_support_mike
 
Posts: 61121
Joined: Thu Feb 11, 2010 2:51 pm

Re: Piezo buzzer works, then doesn't

by mcalleia on Thu Apr 27, 2017 10:14 am

Had a talk with the kid about this. Pretty sure she gets it, but we will go over it a little more.

She now wants to create a library of all the letters in Morse Code as her first open source project.

mcalleia
 
Posts: 9
Joined: Sun Apr 23, 2017 4:47 pm

Re: Piezo buzzer works, then doesn't

by adafruit_support_mike on Sat Apr 29, 2017 1:46 am

You can demonstrate the asynchronous code by calling tone() from setup(), then do something simple in loop() to show two things happening at once:

Code: Select all | TOGGLE FULL SIZE

void setup() {
    while ( ! Serial ) { delay( 1 ); }
    Serial.begin( 9600 );
    tone( 13, 440, 5000 );
}

int n = 0;

void loop() {
    Serial.println( n++ );
    delay( 250 );
}

adafruit_support_mike
 
Posts: 61121
Joined: Thu Feb 11, 2010 2:51 pm

Re: Piezo buzzer works, then doesn't

by mcalleia on Mon May 01, 2017 3:56 pm

Thank you.

Dealing with a 6 y.o. we tend to start out a bit more primal. Usually relating something to food (recipes = functions, etc.) or playacting what the electronics are doing (I do this at work too, with adults, but for the interaction between users and systems).

In this case, we started with cooking. Using an omelet as synchronous (french, not western—even if you have a filling, you have to cook it first), and pasta with sauce as asynchronous, you can cook both at the same time, then combine.

Then, we did a simulation of a 3 second tone() a 3 second delay() and then a 5 second tone(). First using the fingers on one hand for counting the seconds for both, then for what was really happening, running one hand for tone() and the other for delay(). Awkward, but the point was there.

We also do a lot of refactoring work on printed version of the sketch (less fidgeting and distrations—"Minecraft!"). We looked at Blink (with variables for the pin and wait time—much as we are doing in our Morse code sketch) and how it works and then at how we were using tone(). Together we rewrote the bit for dot(); Now, she just needs to copy her fix, add in the fix for dash and ...we should finally be ready to upload it to the Arduino.

We might finish just it tonight.

Then back to Becky's Instructable—or making a Morse Code Hello Word! wearable.

mcalleia
 
Posts: 9
Joined: Sun Apr 23, 2017 4:47 pm

Re: Piezo buzzer works, then doesn't

by adafruit_support_mike on Wed May 03, 2017 3:53 am

Sounds like you've got it under control. ;-)

Post photos when it's done.. we love to see finished projects, and we have a major soft spot for kids who make/code.

adafruit_support_mike
 
Posts: 61121
Joined: Thu Feb 11, 2010 2:51 pm

Re: Piezo buzzer works, then doesn't

by mcalleia on Fri May 05, 2017 1:06 pm

Here it is in action.
https://youtu.be/5xLGHElZjNw

And two patches earned from this project, "Hello World!" and the Lady Ada patch.

Image

mcalleia
 
Posts: 9
Joined: Sun Apr 23, 2017 4:47 pm

Re: Piezo buzzer works, then doesn't

by adafruit_support_mike on Tue May 09, 2017 1:43 am

And a Github tee no less.. awesome! ;-)

I made sure Ladyada and PT saw this one. Send a note containing a link to this thread to support@adafruit.com and we'll send her a copy of Ladyada's "E is for Electronics" coloring book.

BTW - if you aren't aware of it, check out Sylvia's Super-Awesome Maker Show:

http://sylviashow.com/

She's another Maker girl.

adafruit_support_mike
 
Posts: 61121
Joined: Thu Feb 11, 2010 2:51 pm

Re: Piezo buzzer works, then doesn't

by mcalleia on Sun Jun 18, 2017 1:03 pm

Mike,

Thank you again for your help, the coloring book, sharing with Lady Ada and PT.

Oh, and big thanks for turning us on to Sylvia—the kiddo loves Sylvia.

After a lot of work, we added A-Z, 0-9, punctuation, some accents, and procedural code; then converted the whole thing into an Arduino library and shared it on GitHub: https://github.com/MichaelCalleia/MorseCode-Library

Next up for the kiddo and I, returning to learn more about inputs in Becky Stern's Instructables course.

Cheers,
Michael

mcalleia
 
Posts: 9
Joined: Sun Apr 23, 2017 4:47 pm

Please be positive and constructive with your questions and comments.