Skip to content

11. Output devices

This week, I added an output device to my board.

Group Work

A detailed documentation of our group assignment work can be found on the shared group assignment webpage.

Background - Input Devices

In the past, I used different output devices on projects of mine. Here’s a few examples of past output devices usage:

  • motor driver chips/modules (for stepper control and DC motor control)
  • LEDs (standard, stupid LEDs as well as smart, programmable LEDs like NeoPixels)
  • speakers and piezo buzzers
  • displays (both 7/9 segment display devices as well as pixel LCD devices)
  • and possibly more…

Adding an Output to my Board

In our lab, we have several educational kits of little actuator or other output boards. Since I had made a light-based morse board earlier, I simply extended it to use a buzzer as an additional output for this week:

The buzzer is already soldered to a small module, which does not come with extra components. It can be added to the circuit using a 3-lead cable.

How to Connect the Buzzer

I connected the buzzer board to 5V (+ on the buzzer), GND (unlabeled middle pin on the buzzer) as well as one of my own board’s FTDI header pins (S for Signal on the buzzer) (brown/yellow/orange servo cable, the other 6-pin cable is just used for programming the ATTiny board):

Since I was not using Serial Communication in this example, I felt free to simply reuse the pins for the output device here. The signal pin I connected to was Pin 0 on the ATTiny44, as by the clockwise notation from this diagram (notice this is used as TX in Neil Gershenfeld’s Software Serial implementation of the t44 Echo firmware, not as in the pinout image where pins 1 and 2 are TX and RX):

How to use the Buzzer

I tested the buzzer with some simple code that simply brute forces a square wave on an output pin in two different, subsequent frequencies:

#include <avr/power.h>

int buzzer = 1 ;

void setup ()
{
  clock_prescale_set(0);
  pinMode (buzzer, OUTPUT) ;
}


void loop ()
{
  unsigned char i;
  while (1)
  {
    for (i = 0; i <80; i++) 
    {
      digitalWrite (buzzer, HIGH) ;
      delay (1) ;
      digitalWrite (buzzer, LOW) ;
      delay (1) ;
    }
    for (i = 0; i <100; i++) 
    {
      digitalWrite (buzzer, HIGH) ;
      delay (2) ;
      digitalWrite (buzzer, LOW) ;
      delay (2) ;
    }
  }
}

Here is the output of that code playing two different fequencies on the buzzer:

I then played around with the frequencies a little to produce a more police sounding sound (code not documented, I just changed the delay() values and for-loop iterations until it fit):

I then changed over my code from the week where I made a morse blinking device to also beep the morse code on the buzzer. Here is the code:

#include <avr/power.h>

int ledPin = 8;
int buttonPin = 7;

int ditMs = 100;
int dahMs = 3 * ditMs;
int delayBetweenCharacters = 2 * ditMs; //theoretically, this is 3 dit, but 1 dit is already included in the dit and dah functions
int delayBetweenSos = 6 * ditMs;  //theoretically, this is 7 dit, but 1 dit is already included in the dit and dah functions

int buzzerPin = 11 ; // Deklaration des Buzzer-Ausgangspin
long buzzerLast = 0;
long buzzerNow = 0;
int buzzerFrequency = 220;
int buzzerIntervalMillis = 1000/buzzerFrequency/2;
int buzzerIntervalMicros = 1000000/buzzerFrequency/2;
boolean buzzerState = 0;

void setup() {
  clock_prescale_set(0);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH);  //Internal pullup on Button Pin
  pinMode (buzzerPin, OUTPUT) ;// Initialisierung als Ausgangspin
  //beep(buzzerPin, 1000);
  //delay(1000);
  //beep(buzzerPin, 500);
  //delay(1000);
  //beep(buzzerPin, 250);
  //delay(1000);
}

void loop() {
  if(digitalRead(buttonPin) != LOW){
    /*digitalWrite(ledPin, HIGH);   
    delay(1000);                     
    digitalWrite(ledPin, LOW);    
    delay(1000);*/
    //sosBlink(ledPin);
    sosBeep(buzzerPin);
    delay(delayBetweenSos);
  }
}

void dit(int blinkPin){
  digitalWrite(blinkPin, HIGH);
  delay(ditMs);
  digitalWrite(blinkPin, LOW);
  delay(ditMs);
}

void dah(int blinkPin){
  digitalWrite(blinkPin, HIGH);
  delay(dahMs);
  digitalWrite(blinkPin, LOW);
  delay(ditMs);
}

void sosBlink(int blinkPin){
  dit(blinkPin);
  dit(blinkPin);
  dit(blinkPin);
  delay(delayBetweenCharacters);
  dah(blinkPin);
  dah(blinkPin);
  dah(blinkPin);
  delay(delayBetweenCharacters);
  dit(blinkPin);
  dit(blinkPin);
  dit(blinkPin);
}

void ditBeep(int buzzerPin){
  beep(buzzerPin, ditMs);
  delay(ditMs);
}

void dahBeep(int buzzerPin){
  beep(buzzerPin, dahMs);
  delay(ditMs);
}

void sosBeep(int buzzerPin){
  ditBeep(buzzerPin);
  ditBeep(buzzerPin);
  ditBeep(buzzerPin);
  delay(delayBetweenCharacters);
  dahBeep(buzzerPin);
  dahBeep(buzzerPin);
  dahBeep(buzzerPin);
  delay(delayBetweenCharacters);
  ditBeep(buzzerPin);
  ditBeep(buzzerPin);
  ditBeep(buzzerPin);
}

void beep(int buzzerPin, int duration){
    long start = millis();
    while((millis()-start) < duration){
      buzzerNow = micros();
      if((buzzerNow - buzzerLast) >= buzzerIntervalMicros){
        buzzerLast = buzzerNow;
        buzzerState = !buzzerState;
        digitalWrite(buzzerPin, buzzerState);
      }
    }
}

And here it is in action:

Conclusion

This was fun! I really liked working on the same board and code that I had already worked with in a previous week and just adding to it, adding another spiral to its functionality. I don’t know whether I will use this for my final project, but a useful exercise nonetheless.

Design Files


Last update: June 27, 2022 00:50:59
Back to top