R2D2 Sound Generator
Bring the force closer to you!
Hi, I am a science teacher and this tutorial was made to help you build a simple, yet not simplistic, experiment using Arduino Uno to generate sound waves; but we are not talking about just any sound: we are going to produce unlimited R2D2 phrases from all galaxies and beyond; some phrases which even C3PO may not know. Let's get the party started!
You Will Need Some Material and Tools
We need only a handful of components and some can be repurposed electronics:
Components:
Arduino Uno
Specification: you can use any Arduino or any compatible board
Tip: the board must have 16mhz clock frequency, otherwise the sound will not work.
Speaker
Specification: a 4 or 8 ohm speaker with 3 Watts or more will work just fine.
Tip: Usually the heavier the better. If you pick a small, cheap or light speaker, the sound quality will be poor.
Jumper Wires
Specification: male-to-male.
Tip: Sometimes the wires are broken or badly welded so check them (you can wire a simple led circuit; if it lights up, it's good; if it doesn't, throw it away and try another one.
Tools:
Soldering Iron
Specification: 30 Watts is enough.
Tip: with a thin tip; pick a good soldering iron
Solder
Tip: don't use a filler that's too old. The weld may become weak and not conduct electricity or even break apart; also, pick a quality solder.
This Is the Hardest Part
Weld one end of each wire to the speaker.
It may look simple but the final result depends on this. Be sure you are a Jedi Master on soldering techniques.
Now, a Little Assembling
Connect the other end of the wires to Arduino pins Ground and 11. The pin 11 is PWM pin (Pulse With Modulation). PWMs pins type make it sound better.
If you are using Arduino Uno, watch it blink; the LED light is built-in, in pin 13.
Now, a Little Coding
Load the code in https://github.com/marcelolarios/R2D2-Sound-Gener... and Run this code on your Arduino.
The code is divided in those parts:
- Setup pins;
- Phrase1 Function generates one type of phrases;
- Phrase2 Function generates a second type of phrases;
- Loop indefinitely into phrases type 1 and 2 and end phrase with a pause;
In Setup we are initializing variables "ledPin" and "speakerPin" to the Arduino pins. "speakerPin" must be a PWM (Pulse With Modulation) Arduino pin.
For is an internal loop a number of times. This number is randomly generated, so it will sound more natural, so to speak.
Switch runs an specific line each time.
Random function returns random numbers.
Tone function generate a wave, an oscillation in the pin voltage with a certain frequency.
Delay function starts a pause lasting a specific time in the code loop. After that, the loop proceeds.
The R2D2 Ajustable Personality
If you prefer a chattier robot, adjust the last line to delay(random(1000, 2000));
The numbers 1000 and 2000 (arguments of the random function) set a limit for the range the random numbers can populate, so the time between the phrases is shorter, and our robot can talk faster.
Testing
This is our final result until we start tweaking...
Well, I hope you are having fun!