P1050464.JPG
This project emulates a trumpet by playing notes using the three buttons (valves). This is easy to make and fun to play with, though it can only play one octave and no sharps/flats.
 
Remove these adsRemove these ads by Signing Up

Step 1: Code

Load this into your Arduino:


int speaker = 9; // Hook up speaker to digital pin 9
int sw1 = 15; // Switch hooked to analog pin 1
int sw2 = 16; // Switch hooked to analog pin 2
int sw3 = 17; // Switch hooked to analog pin 3
int valve1;
int valve2;
int valve3;

void c () { // Presets the note "c" to be used later
digitalWrite(speaker, HIGH);
delayMicroseconds(1915);
digitalWrite(speaker, LOW);
delayMicroseconds(1905);
}
void d () {
digitalWrite(speaker, HIGH);
delayMicroseconds(1700);
digitalWrite(speaker, LOW);
delayMicroseconds(1690);
}
void e () {
digitalWrite(speaker, HIGH);
delayMicroseconds(1519);
digitalWrite(speaker, LOW);
delayMicroseconds(1509);
}
void f () {
digitalWrite(speaker, HIGH);
delayMicroseconds(1432);
digitalWrite(speaker, LOW);
delayMicroseconds(1422);
}
void g () {
digitalWrite(speaker, HIGH);
delayMicroseconds(1275);
digitalWrite(speaker, LOW);
delayMicroseconds(1265);
}
void a () {
digitalWrite(speaker, HIGH);
delayMicroseconds(1136);
digitalWrite(speaker, LOW);
delayMicroseconds(1126);
}
void b () {
digitalWrite(speaker, HIGH);
delayMicroseconds(1014);
digitalWrite(speaker, LOW);
delayMicroseconds(1004);
}
void C () {
digitalWrite(speaker, HIGH);
delayMicroseconds(956);
digitalWrite(speaker, LOW);
delayMicroseconds(946);
}

void setup() {
pinMode(speaker, OUTPUT);
pinMode(sw1, INPUT);
digitalWrite(sw1, HIGH);
pinMode(sw2, INPUT);
digitalWrite(sw2, HIGH);
pinMode(sw3, INPUT);
digitalWrite(sw3, HIGH);
// Serial.begin(9600);
}

void loop() {
valve1 = digitalRead(sw1); // Reads switch 1
valve2 = digitalRead(sw2); // Reads switch 2
valve3 = digitalRead(sw3); // Reads switch 3

if ((valve1 == LOW) && (valve2 == LOW) && (valve3 == LOW)) { // If all valves are pressed, then
c(); // It plays a "c"
}
else if ((valve1 == LOW) && (valve2 == HIGH) && (valve3 == LOW)) { // If the first and third valves are pressed, then
d(); // It plays a "d"
}
else if ((valve1 == LOW) && (valve2 == LOW) && (valve3 == HIGH)) { // If the first and second valves are pressed, then
e(); // It plays a "e"
}
else if ((valve1 == LOW) && (valve2 == HIGH) && (valve3 == HIGH)) { // If the first valve is pressed, then
f(); // It plays a "f"
}
else if ((valve1 == HIGH) && (valve2 == HIGH) && (valve3 == HIGH)) { // If no valves are pressed, then
g(); // It plays a "g"
}
else if ((valve1 == HIGH) && (valve2 == HIGH) && (valve3 == LOW)) { // If the third valve is pressed, then
a(); // It plays a "a"
}
else if ((valve1 == HIGH) && (valve2 == LOW) && (valve3 == HIGH)) { // If the second valve is pressed, then
b(); // It plays a "b"
}
else if ((valve1 == HIGH) && (valve2 == LOW) && (valve3 == LOW)) { // If the second and third valves are pressed, then
C(); // It plays a "C"
}
else{
delay(1);
}
}
1-40 of 80Next »
Maker1721 says: Nov 24, 2012. 3:36 PM
Does this project need a breadboard?
hankenstien says: Jun 20, 2012. 1:00 AM
Cool IDea,
I took your idea, and instead of buttons i used potentiometers, just to see what would happen, it was kinda cool, you could get and hold various tones, might be fun if some one knew how to hack the code to read the potentiometer values, so it was more then an on or off type thing, any cool idea, i also like the piezo blow hole idea, read hears somewhere.
PEACE~
Hankenstien
rduino.com
cdousley says: May 3, 2010. 4:19 PM
well mine failed miserably......
jonoxer says: Jun 11, 2009. 3:32 PM
Adding a pressure sensor wouldn't be hard at all. You just need something like a MPX2010DP differential pressure transducer then feed it into an op-amp comparitor / amplifier, then read that with analog in on the Arduino. I've just done exactly that for a totally different purpose (measuring the depth of a water tank) and the circuit I built would work perfectly in this trumpet. In fact while building it I tested the circuit by blowing into the transducer and watching the analog read value change! There's more about it here including a photo of the shield: http://www.practicalarduino.com/news/id/101
srikarbeechu says: May 3, 2010. 9:28 AM
@ jonoxer sir kindly help me how to work with MPX@)!)DP DPT... i am unable to measure the output voltage of the transducer and the water tank depth sensor experiment. 
srikarbeechu says: May 3, 2010. 9:25 AM
 hello sir its great to hear as that we can use a DPT but sir i have a major problem making the transducer work for your own application for  TANK DEPTH SENSOR. Sir i kindly oblige you to help me or teach how to work on DPT. please my sincere request please i would be glad if u could give me your facebook userbook i really need to chat with u sir.please.
jonoxer says: May 3, 2010. 7:07 PM
 @srikarbeechu Have you read the explanation in the book? I can't really help you unless I know what you've tried.
Kulf says: Mar 11, 2010. 6:50 PM
 Would you be able to modify this code to use with a guitar hero guitar? So when you press the frets and strum the bar it plays the tones. That's what I'm planning on doing.
cdousley says: Mar 21, 2010. 1:19 PM
you could try this
gh arduino trumpet.png
cdousley says: Mar 19, 2010. 2:03 PM
i ordered my first arduino yesterday
no0x says: Mar 13, 2010. 2:49 AM
That's great !
How can we do to have more sound from the piezo ?
Have we to put an amplifier or something else ?
Thanks !
mman1506 says: Nov 7, 2009. 7:45 PM
don't you need a pull down resistor or somthing 
Ouroboros439 says: Jun 11, 2009. 9:02 AM
No vote until I hear it. How do I know if yours even works? Come on just a little blues or some Louie Armstrong.
ReCreate says: Sep 18, 2009. 7:20 PM
It would theoretically work, And most likely would too, by looking at the code. Though it might sound a bit bad, as it generates square waves, the only easy way to create sound in the arduino, it would sound better if it was a sine wave...
QuiksilverRox (author) says: Sep 19, 2009. 4:44 PM
Yah the square wave is kind-of robotic but it gets the job done.
ReCreate says: Sep 19, 2009. 7:00 PM
Yeah it sure does ;)
Mdob says: Sep 19, 2009. 9:57 AM
It sounds fine I made it and it works. I'm in the process of making it only work if you press a button and making it light up when you press down the valves.
ReCreate says: Sep 19, 2009. 10:30 AM
Well then, It works, Just as i thought, Well good luck with that ;)
Istarian says: Jun 11, 2009. 12:39 PM
This is really cool. However I have some suggestions. One, there is a page on the arduino website which has a number of sound related project/things here: http://www.arduino.cc/playground/Main/InterfacingWithHardware#Output
Which has some interesting stuff on generating sound or playing it. Also this page: http://www.arduino.cc/en/Tutorial/Melody has code for playing a pre coded series of tones on a piezo speaker/earphone and has the actual tones as variables. That think is a little easier. Like: a = 946; (that's not actually true) and then something like this: play(a) I'm thinking you might be able to simply/optimize/shorten your code to make it a little less complicated.
QuiksilverRox (author) says: Jun 11, 2009. 2:58 PM
I based mine off the melody example.
Istarian says: Jun 11, 2009. 3:43 PM
Ah, ok. I',m curious why you chose not to have one function and preset tonal values, but rather a function for each sound.
ReCreate says: Sep 18, 2009. 7:18 PM
Its more fun the overly complicated way :D
Mdob says: Jul 4, 2009. 9:10 AM
Um sorry if i'm being a noob but would <a rel="nofollow" href="http://www.dipmicro.com/store/index.php?act=viewProd&productId=767">this</a> piezo work?<br/>
QuiksilverRox (author) says: Jul 5, 2009. 3:15 PM
It would work but it's operating voltage is 1.5v so you'll need a resistor. Also it's sound level is 75db, that's pretty loud.
ReCreate says: Sep 18, 2009. 7:17 PM
Now imagine Pumping 5V into that sucker, I guess that the sound level could reach 200DB O_O
Mdob says: Jul 5, 2009. 6:21 PM
I'm just starting in electronics so could you please tell me where to put the resistor (i think on the + wire of the piezo connecting to pin 9) and what the value of the resistor would be. Sorry for all of the questions. Thanks in advance.
QuiksilverRox (author) says: Jul 5, 2009. 8:55 PM
I use this to help me with resistors. You'll need a 56ohm resistor. Thats green blue black and attach it to a leg of the Piezo to pin 9 or ground.
Mdob says: Jul 6, 2009. 8:41 AM
Ok thank you so much! I'll start building it as soon as i get my arduino in a day or two.
ReCreate says: Jun 26, 2009. 2:42 PM
This is actually pretty good! I get the code pretty well, I understand it, I will try this when i get my arduino, i am ordering one ;)
blackwellj says: Jun 16, 2009. 9:53 AM
code does not work
ReCreate says: Jun 25, 2009. 7:40 PM
it should, but since you are providing so much information there is just too much we can do to help you.
QuiksilverRox (author) says: Jun 26, 2009. 2:31 PM
i fixed the code problem. It was missing a "{" at the end.
ReCreate says: Jun 26, 2009. 2:38 PM
Well then, It was missing the closing bracket? He still should have provided some more information...
scooterkid92 says: Jun 6, 2009. 3:07 PM
I have a few questions: Does that mean it plays constantly until it's turned off? Do you know if an air pressure sensor could be used to make it more realistic?
abadfart says: Jun 8, 2009. 9:11 AM
i had the same thought
squeakyneb says: Jun 12, 2009. 4:14 AM
I'm sure that everyone did :P Everyone that read the code, anyway. I think it should have some kind of sensor or at least a button for realism, and so that it only plays when you want it to. But otherwise, looks nice.
daltore says: Jun 11, 2009. 11:48 AM
Please don't take this as a criticism of your work, this is an awesome project, and it looks really impressive. I'm into robot building, and so I'm a bit obsessive compulsive about short code and short runtime. Here are a couple of ways to make this code a bit cleaner and a tiny bit faster. Since this is a timing-based program, speed is of the essence.

First, you can define your speaker and switch ports using #define instead of declaring them as integer variables. Declaring them as variables stores the value in the memory register instead of in the program itself, which takes up registers, and requires a lookup every time you want to recall the pin value. By defining them, when you compile, the compiler automatically inserts the values themselves into the program, requiring no lookup, making the whole process faster. It would look like this:

#define speaker 9
#define sw1 15
#define sw2 16
#define sw3 17

Second, the brackets on all of those if() statements are not strictly necessary. To make it cleaner, you can do it without any brackets at all because you're only running one line of code. It would look like this:

if ((valve1 == LOW) && (valve2 == LOW) && (valve3 == LOW)) c();
else if ((valve1 == LOW) && (valve2 == HIGH) && (valve3 == LOW)) d();
else if ((valve1 == LOW) && (valve2 == LOW) && (valve3 == HIGH)) e();
else if ((valve1 == LOW) && (valve2 == HIGH) && (valve3 == HIGH)) f();
else if ((valve1 == HIGH) && (valve2 == HIGH) && (valve3 == HIGH)) g();
else if ((valve1 == HIGH) && (valve2 == HIGH) && (valve3 == LOW)) a();
else if ((valve1 == HIGH) && (valve2 == LOW) && (valve3 == HIGH)) b();
else if ((valve1 == HIGH) && (valve2 == LOW) && (valve3 == LOW)) C();
else delay(1);

This is obviously with the comments removed, but you can see what I mean. This makes absolutely no difference in performance and is just for readability. It's pure individual preference, so if you like it the other way, feel free to completely ignore me. And the integer/define thing isn't really necessary if it works, it just makes it run a tiny bit faster in case you ever want to get more advanced. Do like very much, though! I'd love to see what this is like with an emulated trumpet waveform output.

Keep doing stuff like this, it's awesome!
QuiksilverRox (author) says: Jun 11, 2009. 2:56 PM
Thanks for that. But I recognised that the code would take a while to read and found out it took 10 microseconds longer on the bottom of the square wave than the top. Thats why its 10 off:

void a () {
digitalWrite(speaker, HIGH);
delayMicroseconds(1136);
digitalWrite(speaker, LOW);
delayMicroseconds(1126);
}
daltore says: Jun 11, 2009. 3:09 PM
Hmm, I didn't notice that, interesting solution. I like it!
jaywar says: Jun 11, 2009. 7:33 AM
If you add another piezo to blow into, you can have the same effect as an air pressure sensor (with some scaling of the piezo's output in the software), just be sure to use diodes to protect the arduino from the Piezo's output.
1-40 of 80Next »
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!