Assignment 8: Electronic Output Devices




The Assignment

This week's assignment was to choose an output device that I have not used before and program a microcontroller to operate it. First, I decided to try to program a buzzer to make sounds and play basic songs. I tried out the following simplie projects:


Project 1: Play songs on a buzzer

I first built a simple circuit using the following components: an arduino uno, a piezo buzzer, 1 k ohm resistor, a breadboard, and some wires. I connected the components with the following steps:

  1. Connect the positive side of the buzzer to digital pin 11 on the Arduino Uno. Connect the negative size of the buzzer to one side of the resistor.
  2. Connect the other side of the resistor to GND pin on the Arduino.
  3. The image below shows the entire complete circuit:

    Block

  4. I then connected the Arduino Uno to my computer via the USB cable. I found various codes for songs online and tried different ones out.
  5. It is important to note that I assigned pin number to 11 for the buzzer variable if it was different on the source code.
  6. I found source codes for the Silent Night melody and Beethoven's Ode to Joy . I've linked the Github pages for each source code.
  7. I copied the source code into a new sketch on Arduino and uploaded it making sure I assigned the correct pin numbers.

Below is a video of the buzzer playing the Silent Night melody:

Project 2: Police siren using Arduino

After learning how to play basic melodies with the buzzer, I tried to build a police siren mechanism. I wanted to include the flashing lights and the siren sound as you would see on a police car or ambulance. I began by creating the LED circuit. I wanted to have an LED pattern of switching between blue and red flashes to create a realistic police siren. I didn't have red LEDS so I used blue and white instead. I followed the steps below to build the LED components of the circuit.

  1. I collected the materials I would need. This included:
    • 4 LEDS - two white and two blue
    • Breadboard
    • Arduino Uno
    • Some jumper cables
    • 4 220ohm resistors
  2. Next, I began putting the components together. I connected the two white LEDS as follows: connected each negative terminal (shorter end) of the LED with a 220 ohm resistor to the negative rail of the breadboard. I connected these to digital pin 3 and 7 on the Arduino.
  3. To connect the blue LEDs, I connected the negative ends also to the negative terminal of the breadboard with a resistor on each negative end. I connected each positive end to pin 10 and 12 of the Arduino.

Next, I wanted to include a buzzer to play the siren tone. To add the buzzer, I connected the positive end to pin 13 on the breadboard and the negative end to the GND rail on the breadboard (which connects to the GND pin on the Arduino).

The image below shows the entire complete circuit with LEDs and buzzer:

Circuit

With circuit complete, I began writing the code for the siren. I wanted the color pairs of the LEDs to flash separately and repeat in a loop. For the police siren sound, I found some code online of an ambulance siren. I played around with the delay() and tone() functions until the siren sounded more like a police siren. Original source code can be found here . I divided the delay into two parts two control both pairs of LEDS separately.

Complete sketch below:

 
        
  int input=0;
  int buzz=13; // Buzzer Pin
  int j=3;
  int k=12;
  void setup() {
  for(int i=3;i<=12;i++)
  pinMode(i,OUTPUT);
  pinMode(2,INPUT);
  }


  void settone(int input)
  {

  if(input==2)
  loop();
  }


   void loop() {        
    for(int i=4;i<=12;i+=2)
  digitalWrite(i,LOW);
  for(int i=3;i<=11;i+=2)
  digitalWrite(i,HIGH);
    for(int hz = 440; hz < 1000; hz++){
      tone(buzz, hz, 50);
      delay(4);
    }
  for(int i=3;i<=11;i+=2)
  digitalWrite(i,LOW);
   for(int i=4;i<=12;i+=2)
  digitalWrite(i,HIGH);
    for(int hz = 1000; hz > 440; hz--){
      tone(buzz, hz, 50);
      delay(4);
    }    }
   

I played a round with different delay() values and tones. I settled on a delay of (4) amd a tone of (50) for the closest police siren sound.

Below is a video of my police siren: