This tutorial is to teach you about transistors, flyback diodes, and basic Arduino functions. This Instructable is based off of a circuit from the Arduino Experimentation Kit.
This is a good tutorial for anyone starting out with Arduino. These parts are very easy to come by in any starter kit or very cheap if you don't have them. The programming is also very simple and uses functions that are already part of the Arduino library.
This tutorial covers transistors and flyback diodes for educational purposes. This is not necessarily the most efficient way to accomplish this project.
Step 1: The Parts
The materials for this project can be seen below but are as follows:
--- Arduino Uno or similar. If you know how your Arduino works then any should be fine for this project.
--- Breadboard. If this is not a project you want to keep then a breadboard is fine. Otherwise you will need to find perfboard or another way to make the circuit permanent.
--- Jumpers or wires. Many Arduino kits come with jumpers. If you do not have one, regular solid core wire is fine.
--- A Small Speaker. These can be found in many electronic devices. You will need one that can be powered on 5v from the Arduino. A piezoelectric buzzer can also be used in this circumstance.
--- 2.2k Ohm Resistor (Red Red Red). This is a good value for the transistor I am using and will most likely work for yours. If not you will need to find the datasheet and determine the appropriate resistor.
--- A Diode. These all work generally the same but watch for the polarity. It matters which way around it goes.
--- A NPN Transistor. These are quite common as well. Watch for the polarity on this as well.
Step 2: Transistors and Flyback Diodes
If these parts are familiar to you then you can skip this step. If not, then read further.
A Transistor is a semiconductor used to amplify a signal. It has three pins: a Emitter, a Base, and A Collector.
The Emitter is where the amplified current comes out when signaled
The Base is the signal which controls the larger current. This is where the output signal from the Arduino will go.
The Collector is the pin which receives the higher current.
In our case the "higher current" will be the 5v output from the Arduino and the "lower current" will also be the 5v coming from a digital output pin but coming through the resistor to lower the current to below the transistors maximum voltage to properly switch the higher current.
The flyback diode is used to protect the Arduino pins from unwanted current created by devices with magnets such as motors and the speaker. A diode only lets current flow in one direction. When the device with the magnet becomes unpowered a small amount of current is quickly created which can damage pins when it flows backwards. The flyback diode removes this possibility. As I stated before, this is for educational purposes. The energy possibly created by the speaker is negligible but would not be for a toy motor for example.
Step 3: Assembling the Circuit
Following the picture assemble the circuit on the breadboard. You can follow the circuit diagram or you can follow the picture I created in Fritzing.
Make sure you connect the transistor pins in the correct order and make sure the diode is in the right way.
Connect the resistor to any digital output pin on the Arduino. I used #9 (pwm).
Step 4: Programming
To create a tone we will be using the Arduino's tone() function.
There is an example that comes with Arduino to play a simple tune. To open that click File -> Examples -> Digital -> toneMelody
To create our own tunes all that is required is to change the output pin defined in the tone() function and the noTone() function. To do this you have to find the line of code with these functions and replace 8 with the pin number you are using.
For example, I am using pin 9.
To change the tones and timing of the melody you have to change array containing the notes and timing. You will have to experiment to create your own tune if you want to. See the tones.h file that also opens with the example to find the other note names.
Here is my final product playing Guns N Roses: Sweet Child O' Mine.