![Hardware with Arduino & JavaScript](https://static.frontendmasters.com/assets/courses/2023-09-26-arduino-javascript/thumb.webp)
Check out a free preview of the full Hardware with Arduino & JavaScript course
The "Playing Notes on the Piezo" Lesson is part of the full, Hardware with Arduino & JavaScript course featured in this preview video. Here's what you'd learn in this lesson:
Steve wires a piezo to the Arduino, which can detect vibration and create sound. Once the piezo is wired up, notes are sent to the play method, and the piezo plays the sequence.
Transcript from the "Playing Notes on the Piezo" Lesson
[00:00:00]
>> So we are going to wire up the piezo. As far as I can tell I don't think this one is polarized. I know that when I ran through this before I just put it in there and it didn't work. It did work unlike some other things today.
[00:00:14]
The nice part is is that if we mess this up, or throw it Fixing this circuit is pretty easy. We have got one side going to the ground and the other side, cause this is, again, this is the opposite of what we have been doing. This is not a sensor, this is an actuator, right?
[00:00:30]
Our code has it do stuff, right? So we are not reading, this is like, we will send signals from the Arduino board to the actual code itself. So we can, I'm gonna put it in on the other side. I just gotta make sure I have all the connections places.
[00:00:49]
I'm running out of room. The one thing I will say about this one, it is big. And thereby somewhat hard to remember exactly where you plugged it in so if you're putting the pins in it makes sense to look at the numbers of where you're placing it. I'm going to place it on this other side just in the very first spot cause it makes it easy for me.
[00:01:11]
But if it doesn't work, one of the things I will realize is that I just got the wrong pin cause I'm trying to see it. And then look it looks like if I put in this first pin, it looks like I end up five pins later on the other side.
[00:01:31]
Four pins later, so we'll go Doesn't give you a lot of room to work with me. We'll go there and I put it in six doesn't really matter. It does as long as you have a pulse width as long as squiggly Six and then the ground is just the ground.
[00:01:54]
Cause this diagram makes it look like they're right next to each other, as you can tell, that's a lie. But the basic concept is true. All right, so for this one. I've got a directory called piezo. We're going to. I didn't need to fill up an entire window for that.
[00:02:29]
We'll go in to piezo. Again, if you plugged it into a different pin than six, you will need to adjust that It could be interesting to also play around with the light and the REPL to see what some of the other options were, because IntelliSense wasn't totally running, it was like trying different stuff.
[00:03:09]
So we can try that in a second while we're in here, too. But let's get it working with the piezo. So this one, again, I have no server on this one. We can pull it into a server in a little bit. But let's just try to play with the piezo itself first.
[00:03:22]
So go ahead and do And we'll inject both into that rapport. So piezo It's got this play. And you can see I injected a song in there, too. And the songs are basically you just give it the notes of the frequency, like a tempo and fractions for quarter notes or whatever.
[00:03:54]
And then theoretically, it should play the song if I've wired it up correctly.
[00:03:58]
[MUSIC]
[00:04:04]
Perfect, right? Yeah, that happened. Just deal with it [LAUGH]. So we can look at the song itself and see that code let's go into the song At some point, there's a reason why I broke it out into its own file. Not that I really if any of you looked at this and knew what was going to happen, you would have gotten a gold star, but just in case.
[00:04:30]
So there's a bunch of different options. You can make it work at a certain frequency and as I pull it back up, I'll show you. You can give it a bunch of notes, but otherwise an array of notes it will like figure out the tempo, and play for is.
[00:04:43]
Again these are just abstractions right there are libraries that like just help out with this. But you could also grab it and say like, is. Frequency and given 440. Would work on either duration for 4500 milliseconds, right that's a 440. You can go to 880, it's an octave higher.
[00:05:22]
[SOUND] Welcome to Music Theory 101. [LAUGH] Fun fact, 220 is an octave lower. [SOUND] Great, so you can theoretically, you can get whole whooshing sounds if you want, all of those things, but you can take that.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops