How to Make Sounds with Arduino
Free Arduino Course
Make Some Noise with Arduino
There are many ways to make sounds with Arduino. This video covers one of the easier ones.
Become the Maker you were born to be. Try Arduino Academy for FREE!
Note: the code is available at the bottom.
Making Sounds with Arduino Video Transcription
Hey everybody, it’s Brian Jenkins with circuit crush and welcome to another Arduino tutorial. This time, we’re going to do something that we haven’t done yet. We’re going to talk about making sounds with the Arduino.
If you’ve been using Arduino for a while, you know there’s a lot of different ways to make sounds. We’re going to start with the simple stuff and we’re going to start by using an active buzzer, which is this little black thing here.
There’s two different types of buzzers. You have the active and the passive. The active buzzers have circuitry built into them where you can apply a DC signal and it makes a noise, the passive ones do not — they need some kind of pulse width modulation or an AC signal something or like that. So, this here is an active buzzer.
And I’m going to bother drawing a schematic because this is real simple. We have one pin or one wire, rather, connected to the buzzer and then pin 10, digital pin 10, and then we have the other wire connected to the buzzer and to ground on the Arduino. So to demonstrate what this does I’m going to go ahead and upload the code.
So here we can hear it making a sound and let’s go ahead and take a look at the code — actually before we do that, these buzzers you could use this for a lot of different things. These buzzers are common in toys and such.
You can use this, for example, if you want to make a sound when you or somebody pushes a button or you could use it on your robot to communicate some kind of event — whether or not the robot hit a wall or when you first turn the robot on.
We’re going to go ahead and take a look at the code. Alright. So here we are in the Arduino IDE taking a look at the code that produces this sound.
Okay enough of that. So let’s talk about this code here. Let’s go over — actually, It’s pretty simple — It’s pretty simple and here we can see we declare an integer. We’re just going to call it buzzer. We’re going to set it equal to 10 and all this does is tell us that the buzzer pin is pin 10.
Then, in the setup function, we have one line of code. All we’re doing is taking pin 10, the buzzer pin and making it an output then we declare an integer i and we come over here to the heart of the program. These two for loops are really the heart of this program. This is what makes the sound — these two for loops. And we can see here that we step into the first one.
We write a high to pin 10. We wait one millisecond — remember the delay function works with milliseconds — It works in milliseconds so we’re going to wait just one millisecond and then we’re going to make that pin low and then wait another millisecond and we’re going to do that 80 times.
Then we’re going to fall out of the loop and into this for loop and we’re going to do something very similar only we’re going to do it for two milliseconds.
Now the sound that we were making is pretty interesting, but it’s not the only sound we can make. We might want to make different kinds of sounds and there’s two ways to do that. So before I get into how to do that, I want to let you know I love music but I am not a musician. I have zero musical talent and if I mix up some of the terms associated with music like pitch and tone and get them wrong, you’ll have just have to forgive me.
So, let’s see if we can change the sound a little bit. Let’s see if we can do that first by changing the delay. So I want to make this — let’s change that from two to three and we’ll do both of them here some both the three.
And we’ll go ahead and I to a plug the Arduino back in here. We’re going to hear that annoying sound for a minute. Okay, we can hear it. Now. I’m going to upload this code.
I’m going to bring this close to the microphone here. Hoping you guys can hear that. Just changing one lousy little number in the delay function for the second for loop, we can hear a difference.
So what if we put this back to it was at before? So let’s put it back to two.
And let’s change this one. Let’s change this one, too. Change this one from two to three.
Now go ahead and upload it.
You have a different sound.
So let’s try something different. We’re going to put this back to what we originally had it at. This was one millisecond. This was two and just to confirm I want to hear the sound.
Who could forget that sound, right? So let’s play with this for Loop.
Let’s make them both a hundred iterations. What do you say that sounds pretty similar but the speed is different. I don’t know if you guys could tell… Let’s see, we can control the speed for lack of my musical talent and knowing the correct term we control the speed of the sound.
I want to make it 50. This should be more noticeable.
There you go. Now we can see it’s a little bit faster.
Let’s make that 50 and let’s make this 200.
And let’s try something else here. Let’s put these back to eighty and a hundred and let’s make this delay one and then this delay three.
And here we go again.
Okay, so I think you guys get the point that by playing around with these variables — the delay function and also the number of iterations in the for loop, we can control the kind of sounds it’s making. The pitch the tone in the speed of the sound.
So this is just one simple way to make sounds and I encourage you guys to go ahead and create a sketch just like this and play with these values and just see for yourself. All you need is a couple of wires, a little buzzer, and your Arduino and you can see all kinds of crazy sounds you can make.
If you do have musical talent, you might even be able to make what resembles an actual, real song. I have none, so I’m not even gonna try to do that. I’m not talented enough to do that. But until next time guys keep making!
Food for Thought
How is it that the code in the for loops is able to make the buzzer make different sounds?
The Code
int buzzer = 10; //the buzzer pin void setup() { pinMode(buzzer,OUTPUT); //initialize the buzzer pin as an output } void loop() { int i; { //produce a frequency for(i=0; i<80; i++) { digitalWrite(buzzer,HIGH); delay(1); digitalWrite(buzzer,LOW); delay(1); } //produce another frequency for(i=0; i<100; i++) { digitalWrite(buzzer,HIGH); delay(2); digitalWrite(buzzer,LOW); delay(2); } } }
I got tһiѕ website from my buddy ѡhⲟ shared with
me regardіng thiѕ website аnd at the momеnt this time I am browsing this
web page and reading very informative posts аt thіѕ place.
I needed to thank you for this wonderful read!! I definitely loved every little bit of it.
I have got you book marked to look at new stuff you post?