Dubsiren (My first project, could use some help :)

Hi all!

I am new to Arduino and new to electronics in general. I am (web) developer by trade, so I find the programming bit easier than the electronics.

As my first project I am building a dub siren. A dub siren is a small box which generates sounds like wailing siren, alarm, various waveforms with lfo. I managed to create 2 functions which are the wailing siren and the alarm, the alarm has a potmeter which is used to adjust the frequency of the tone.

I run in to the following problems (so far!! ;))

  • The only waveform or tone I can produce via the Arduino library is a squarewave. I have googled and found various posts from a couple of years back who use various other electronic parts to generate a sine and triangle wave. Is the only way to achieve this via electronic parts and not with code? Can someone maybe point me in a (noobfriendly) direction where I can learn how to achieve this? I want to try to achieve this with the parts I have at home (Arduino Uno starters kit + a lot of resistors, capacitators)

  • The sound output via a small 8ohm speaker is good, when I use a cable to hook up the prototype to my mixing desk the volumeoutput is extremely low. I believe I need to amplify the signal, is this correct? Can someone point me to a good tutorial using Arduino to achieve this? I've never done anything like that in my life and as we say in Holland. I can't see the forest because of all the trees :wink:

here is my code so far (warning, this is very very very very ugly prototyping code ;))

/**
 
const  float startHz = 100.0;
const  float endHz = 2000.0;
const  float maxPotential = 1023.0;

const int button0 = A0;
const int wailingStartHz = 300;
const int wailingEndHz = 3200;
int wailingStart = wailingStartHz;
int wailingEnd = wailingEndHz;
boolean wailingUp = true;

const int button1 = A1;


const int speakerPin = 7;
const int ledPin = 9;

const int potPin = A5;


void setup() {
  Serial.begin(9600);
  
//  pinMode(speakerPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(button0, INPUT);
  pinMode(button1, INPUT);
}

void loop() {
  boolean buttonState0 = digitalRead(button0);
  boolean buttonState1 = digitalRead(button1);
  
  if ( buttonState1 == 0 ) {
    laser(buttonState1);
  }
  
  if ( buttonState0 == 0 ) {
    if ( wailingUp ) {
      wailingSirenUp();
    } else {
      wailingSirenDown();
    }
  } 
  

}

void wailingSirenUp() {
  
  for (wailingStart < wailingEnd; wailingStart++;) {
    // break out of the for when the button is let go
    if ( digitalRead(button0) == 1 ) break;
    
    // Check if we need to reverse the sound
    if ( wailingStart > wailingEnd ) {
      wailingUp = false;
      wailingStart = wailingEndHz;
      wailingEnd = wailingStartHz;
      delay(10);
      break;
    }
    
    // Turn on the led
    analogWrite(ledPin, 255); 
    // Generate tone on frequency == wailingStart
    tone(speakerPin, wailingStart, 5);
    delay(1);
  }
  
  analogWrite(ledPin, 0);
}
void wailingSirenDown() {
  
  for (wailingStart > wailingEnd; wailingStart--;) {
    // break out of the for when the button is let go
    if ( digitalRead(button0) == 1 ) break;
    
    // Check if we need to reverse the sound
    if ( wailingStart < wailingEnd ) {
      wailingUp = true;
      wailingStart = wailingStartHz;
      wailingEnd = wailingEndHz;
      delay(10);
      break;
    }
    
    // Turn on the led
    analogWrite(ledPin, 255); 
    // Generate tone on frequency == wailingStart
    tone(speakerPin, wailingStart, 5);
    delay(1);
  }
  
  analogWrite(ledPin, 0);
}



void laser(int buttonState1) {

  /**
  
  ( (potValue - minimumPotValue) / (maxPotValue - minimumPotValue) )  * (endHz - startHz) + startHz
  
  **/
  
  int time = 100;
  if (buttonState1 == 0) {
    
    float analogIn = analogRead(potPin);
    float freq = ( (analogIn - 0) / (maxPotential - 0) ) * (endHz - startHz) + startHz;
    analogWrite(ledPin, 255);
    tone(speakerPin, freq, time);
    delay(time);
  }
  if ( buttonState1 == 0 ) {
    float analogIn = analogRead(potPin);
    float freq = ( (analogIn - 0) / (maxPotential - 0) ) * (endHz - startHz) + startHz;
    tone(speakerPin, freq*1.5, time);
    delay(time);
  }
  
  
  analogWrite(ledPin, 0);
}

Thanks!

Erik

//edit

I found this tutorial, but it states that it for the Arduino Due. Can someone please explain me why this can't work on Uno?

I found this tutorial, but it states that it for the Arduino Due. Can someone please explain me why this can't work on Uno?

i can explain this.

thats because the UNO and many others only has a ADC (analog to digital converter)
and the due, mega adk, mega 2560 als has a DAC (digitital to analog converter)

and maby as you know a Sound siginal is Analog and not digital unless you use Spdif but you get the point.

succes met je project.

/spirit

Is the only way to achieve this via electronic parts and not with code?

On the Uno, yes. The Uno can only output PWM (a square wave) or digital signals (also square wave). You can, with parts, convert a square wave into a sine or triangle wave.

Good to know! Let's see how far I get with only squarewav manipulation :slight_smile:

Any idea how to get the output of arduino circuit to my mixer? Small 8 ohm speaker directly in the circuit works, but the volume output to my mixingdesk is incredibly low

erik404:
Any idea how to get the output of arduino circuit to my mixer? Small 8 ohm speaker directly in the circuit works, but the volume output to my mixingdesk is incredibly low

Ohm's law says that connecting an 8 ohm speaker directly to an Arduino would attempt to conduct 625mA which is far in excess of the 40mA maximum (20mA recommended maximum) for an output pin. You may have damaged your Arduino if you did not use current-limiting resistors.

As for your mixer. Line-level audio signals are usually AC, alternating current, going from negative 1V to positive 1V. Your Arduino outputs 0V to 5V DC. I'm no expert on audio signals but I think you'll need some additional hardware to bias your signal.

tylernt:

erik404:
Any idea how to get the output of arduino circuit to my mixer? Small 8 ohm speaker directly in the circuit works, but the volume output to my mixingdesk is incredibly low

Ohm's law says that connecting an 8 ohm speaker directly to an Arduino would attempt to conduct 625mA which is far in excess of the 40mA maximum (20mA recommended maximum) for an output pin. You may have damaged your Arduino if you did not use current-limiting resistors.

As for your mixer. Line-level audio signals are usually AC, alternating current, going from negative 1V to positive 1V. Your Arduino outputs 0V to 5V DC. I'm no expert on audio signals but I think you'll need some additional hardware to bias your signal.

I tried connecting the 8ohm speaker directly to digital pin 7 (as per a tutorial which did not state to use a resistor). I eventually placed a 48 ohm resistor in between to turn the volume down a bit :slight_smile:

Pin delivers 5v. The speaker is 8ohm, I used a resistor of 48, which makes total resistance 56, that gives me the following formula I = 5/56 = 0.089mA

Which means im in the safe right?

Ohms Law Calculator says that 56? @ 5V gives 0.089A or 89mA, still way too high.

tylernt:
Ohms Law Calculator says that 56? @ 5V gives 0.089A or 89mA, still way too high.

Jups! I made a booboo :slight_smile: fixed it though, thanks for the lesson! and the url!