Audio with Arduino - WordPress.com · 2017. 10. 10. · Audio with Arduino –Tone •Use the tone...

12
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

Transcript of Audio with Arduino - WordPress.com · 2017. 10. 10. · Audio with Arduino –Tone •Use the tone...

Page 1: Audio with Arduino - WordPress.com · 2017. 10. 10. · Audio with Arduino –Tone •Use the tone function in a sketch •Works with any digital pin (0-13) •tone(pin,frequency)

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

Page 2: Audio with Arduino - WordPress.com · 2017. 10. 10. · Audio with Arduino –Tone •Use the tone function in a sketch •Works with any digital pin (0-13) •tone(pin,frequency)

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

Page 3: Audio with Arduino - WordPress.com · 2017. 10. 10. · Audio with Arduino –Tone •Use the tone function in a sketch •Works with any digital pin (0-13) •tone(pin,frequency)

Exercise 1

• Use your sketch from the homework to make the active buzzer beep and control the rate of beeping with the potentiometer

Page 4: Audio with Arduino - WordPress.com · 2017. 10. 10. · Audio with Arduino –Tone •Use the tone function in a sketch •Works with any digital pin (0-13) •tone(pin,frequency)

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

Page 5: Audio with Arduino - WordPress.com · 2017. 10. 10. · Audio with Arduino –Tone •Use the tone function in a sketch •Works with any digital pin (0-13) •tone(pin,frequency)
Page 6: Audio with Arduino - WordPress.com · 2017. 10. 10. · Audio with Arduino –Tone •Use the tone function in a sketch •Works with any digital pin (0-13) •tone(pin,frequency)

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

Page 7: Audio with Arduino - WordPress.com · 2017. 10. 10. · Audio with Arduino –Tone •Use the tone function in a sketch •Works with any digital pin (0-13) •tone(pin,frequency)

Exercise 2

• Make a sketch that alters the pitch (frequency) of a tone based on an input of your choice

Page 8: Audio with Arduino - WordPress.com · 2017. 10. 10. · Audio with Arduino –Tone •Use the tone function in a sketch •Works with any digital pin (0-13) •tone(pin,frequency)

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”

Page 9: Audio with Arduino - WordPress.com · 2017. 10. 10. · Audio with Arduino –Tone •Use the tone function in a sketch •Works with any digital pin (0-13) •tone(pin,frequency)

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

Page 10: Audio with Arduino - WordPress.com · 2017. 10. 10. · Audio with Arduino –Tone •Use the tone function in a sketch •Works with any digital pin (0-13) •tone(pin,frequency)

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

Page 11: Audio with Arduino - WordPress.com · 2017. 10. 10. · Audio with Arduino –Tone •Use the tone function in a sketch •Works with any digital pin (0-13) •tone(pin,frequency)

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);

Page 12: Audio with Arduino - WordPress.com · 2017. 10. 10. · Audio with Arduino –Tone •Use the tone function in a sketch •Works with any digital pin (0-13) •tone(pin,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