Audio with Arduino - WordPress.com · 2017. 10. 10. · Audio with Arduino –Tone •Use the tone...
Transcript of Audio with Arduino - WordPress.com · 2017. 10. 10. · Audio with Arduino –Tone •Use the tone...
Sound
• Vibrations propagated by waves of pressure through a material(s) (often air)
• The frequency of the wave determines the pitch of the sound we perceive
• The higher the frequency, the higher the pitch
• Frequency measured in Hz (vibrations per second)
• Humans can hear sounds within the range of 20Hz – 20,000Hz
• Human voice is 85Hz-255Hz
Audio with Arduino – Active Buzzer
• Produces a sound when supplied with electric current
• It is a digital output (on/off)
• You cannot use it to produce specific frequencies
• The leg marked with a + on top of the buzzer connects to the output pin
• The other leg connects to GND
Exercise 1
• Use your sketch from the homework to make the active buzzer beep and control the rate of beeping with the potentiometer
Audio with Arduino – Passive Buzzer
• Produces a square wave when supplied with a pulse of electric current
• The frequency of the tone is the frequency of the electric pulse
• Connections are the same as with the active buzzer
Audio with Arduino – Tone
• Use the tone function in a sketch
• Works with any digital pin (0-13)
• tone(pin,frequency)
• example: tone(8, 200);• this produces a 200Hz tone on a passive buzzer attached to pin 8
• Optional 3rd argument duration (in ms)• example: tone(8 , 200, 1000);• this produces a 200Hz tone on a passive buzzer attached to pin 8 for one second
• noTone(pin) – stops the tone
Try Melody example
Exercise 2
• Make a sketch that alters the pitch (frequency) of a tone based on an input of your choice
Amplification
• To get a louder sound, we need to apply greater voltage to the buzzer
• A transistor can be used to control a higher voltage source using a lower voltage source
• For example, we can use the 3.3V output pin to control a 5V supply to make our sound louder
• We will use the NPN (negative positive negative) transistor in your kit marked with “8050”
Amplification - Connections
• With the flat side of the transistor facing you
• NPN’s left leg -> GND
• NPN’s center leg -> Arduino output (tone)
• NPN’s right leg -> buzzer’s negative leg
• Buzzer’s positive leg -> 5V pin on Arduino
Volume control
• Add a potentiometer to the circuit to dial the volume up/down
• Connections:• Potentiometer’s center leg -> 5V
• Potentiometer’s outer leg (either) -> Buzzer’s positive leg
Map function
• Translates a number from one range to another range
• map(value, fromLow, fromHigh, toLow, toHigh)
• For example, if you want to use input from the potentiometer to set a tone frequency
• Potentiometer range: 0 – 1023
• Human hearing range: 20 – 20000
• int frequency = map(analogRead(A0), 0, 1023, 20, 20000);
• tone(8, frequency);
In Class MiniProject
• Work with assigned partner to make your own musical instrument• Sarah – Jared
• Anthony – Marissa
• Dan – Tori
• Tony – Andres
• Michael – Fred
• Ali – Francisco
• Giselle – Salvador
• Experiment and challenge yourself