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!

Step 1: 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.

Step 2: 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.

Step 3: 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.

Step 4: Now, a Little Coding

Load the code.

<p>/*************************************************** <br>  Project R2D2 Sound Generator
  
  To all fans of StarWars and Arduino!</p><p>  Written by Marcelo Larios
  
  BSD license, all text above must be included in any redistribution
****************************************************/ </p><p>#define speakerPin 11
#define ledPin 13</p><p>void setup() {
    
    pinMode(speakerPin, OUTPUT);
    pinMode(ledPin, OUTPUT);
    randomSeed(analogRead(0));
}</p><p>void phrase1() {
    
    int k = random(1000,2000);
    digitalWrite(ledPin, HIGH);
    for (int i = 0; i <=  random(100,2000); i++){
        
        tone(speakerPin, k+(-i*2));          
        delay(random(.9,2));             
    } 
    digitalWrite(ledPin, LOW);   
    for (int i = 0; i <= random(100,1000); i++){
        
        tone(speakerPin, k + (i * 10));          
        delay(random(.9,2));             
    } 
}</p><p>void phrase2() {
    
    int k = random(1000,2000);
    digitalWrite(ledPin, HIGH);  
    for (int i = 0; i <= random(100,2000); i++){
        
        tone(speakerPin, k+(i*2));          
        delay(random(.9,2));             
    } 
    digitalWrite(ledPin, LOW);   
    for (int i = 0; i <= random(100,1000); i++){
        
        tone(speakerPin, k + (-i * 10));          
        delay(random(.9,2));             
    } 
}</p><p>void loop() {
    
    int K = 2000;
    switch (random(1,7)) {
        
        case 1:phrase1(); break;
        case 2:phrase2(); break;
        case 3:phrase1(); phrase2(); break;
        case 4:phrase1(); phrase2(); phrase1();break;
        case 5:phrase1(); phrase2(); phrase1(); phrase2(); phrase1();break;
        case 6:phrase2(); phrase1(); phrase2(); break;
    }
    for (int i = 0; i <= random(3, 9); i++){
        
        digitalWrite(ledPin, HIGH);  
        tone(speakerPin, K + random(-1700, 2000));          
        delay(random(70, 170));  
        digitalWrite(ledPin, LOW);           
        noTone(speakerPin);         
        delay(random(0, 30));             
    } 
    noTone(speakerPin);         
    delay(random(2000, 4000));             
}</p>

The code is divided in those parts:

  1. Setup pins;
  2. Phrase1 Function generates one type of phrases;
  3. Phrase2 Function generates a second type of phrases;
  4. 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.

Below is the R2D2 code for download. Run this code on your Arduino.

Step 5: ‚Äč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.

Step 6: Testing

This is our final result until we start tweaking...

Well, I hope you are having fun!

Arduino Contest 2019

Participated in the
Arduino Contest 2019

Share

    Recommendations

    • Frozen Treats Challenge

      Frozen Treats Challenge
    • Growing Beyond Earth Maker Contest

      Growing Beyond Earth Maker Contest
    • Fandom Contest

      Fandom Contest

    3 Discussions

    1
    None
    ihart

    22 days ago on Step 4

    Cool project!
    Just a small comment. It doesn't look like the variable i in this line of code is actually used.
    for (int i = 0; i <= random(3, 9); i++){

    2 replies
    0
    None
    MarceloLariosihart

    Reply 22 days ago

    Thanks! Glad you like it, it is my very first instructable.

    The variable i is being used in the for loop.
    The syntax of the for loop in C (Arduino uses C++, which was built on C) is:

    for (initialization; test; increment) {
    // codes
    }

    In
    for (int i = 0; i <= random(3, 9); i++){
    We initialize i, and set it to zero.
    Then we test i to see if it is still smaller than the value randomly found before the loop started (random is executed only once).
    If yes, then the code inside for is executed and then i is updated.

    This is the same (operationally speaking) as :
    int i = 0;
    int r = random(3, 9);
    while (i<=r) {
    // codes
    i++;
    }
    0
    None
    ihartMarceloLarios

    Reply 21 days ago

    Ahhh. I haven't seen that done before. You are generating a random terminal value for your for loop. Very cool.