Tutorial 003: Making Sounds with Buzzer

Electromagnetic BuzzerSo far we’ve used a LED as output to produce light of different colors and intensity (Tutorial 001 and Tutorial 002) but we haven’t generated any sound yet.

In fact that isn’t very difficult to do.

We will use a buzzer for output.

According to Wikipedia … the buzzer or beeper is an audio signalling device, which may be mechanical, electromechanical, or piezoelectric. Typical uses of buzzers and beepers include alarm devices, timers and confirmation of user input such as a mouse click or keystroke.

Tinusaur BuzzerWe will use electromechanical buzzer. When voltage is applied to it its membrane moves up (or down, depending on the particular device) and respectively when there is no voltage the membrane goes back to its normal position. Applying constantly changing voltage will generate audio waves perceived by us as a sound.

Let’s connect the buzzer to the PB2 of the ATtiny85 on the Tinusaur board.

The program should look very much like the one for blinking LED except that the delay between switching the port should be very short.

In the example below we have a delay 500 and since we’re using the _delay_us() function that means the delay is 500 uS (microseconds). That means the period of the signal will be 2 x 500 uS = 1000 uS (or 0.0001 sec.) and then the frequency is 1 / 0.0001 S = 10000. That means the sound will have frequency of 10 KHz.

Here is the source code:

#include <stdint.h>
#include <avr/io.h>
#include <util/delay.h>
#define BUZZER_PORT     PB2     // Buzzer I/O Port
#define BUZZER_DELAY    500     // Delay for each tick
int main(void)
{
    DDRB |= (1 << BUZZER_PORT); // Set port as output
    while (1) {
        PORTB |= (1 << BUZZER_PORT);
        _delay_us(BUZZER_DELAY);
        PORTB &= ~(1 << BUZZER_PORT);
        _delay_us(BUZZER_DELAY);
    }
    return (0);
}

Full source code with more comments and the other necessary files such as Makefile is available at https://bitbucket.org/tinusaur/tutorials/src/default/tut003_buzzer/

Build the program:

$ make

Upload the code to the controller:

$ avrdude -c usbasp -p t85 -U flash:w:"main.hex":a

The buzzer should start making sound immediately.

Let’s do some more experiments.

Let’s make the delay between the buzzer ticks change over time and see what sound it will produce.

This time instead of _delay_us() we will use the _delay_loop_2() function. According to the _delay_loop_2(int) documentation it produces 4 empty CPU cycles per iteration – in other words with parameter 100 it will produce delay of 400 CPU cycles. That tells us that the maximum is 65536 x 4 = 262252 cycles. That, at 1MHz CPU clock, is approximately 262 mS (milliseconds) maximum delay, … or about 3.8 Hz minimum frequency – perfect for our experiments.

Below is the source code:

#include <stdint.h>
#include <avr/io.h>
#include <util/delay.h>
#define BUZZER_PORT     PB2     // Buzzer I/O Port
#define BUZZER_DELAY    200     // Delay for each tick
int main(void)
{
    DDRB |= (1 << BUZZER_PORT); // Set port as output
    int delay = 0;
    while (1) {
        if (delay < 1) delay = BUZZER_DELAY;
        PORTB |= (1 << BUZZER_PORT);
        _delay_loop_2(delay);
        PORTB &= ~(1 << BUZZER_PORT);
        _delay_loop_2(delay);
        delay--;
    }
    return (0);
}

After building and uploading this should start making sound like of a car alarm.

With similar techniques a lot more complex sounds could be generated.

This post will eventually become Tutorial 003.

 

Quick Update for September and October

This is quick update for September and October.

The presentation at Maker Faire in New York (http://makerfaire.com/new-york-2014/) on Sept. 21st went very well – 80% full of the small auditorium. The Faire itself was terrific.

On October 15, as part of the European CodeWeek (http://codeweek.eu/) the Tinusaur Project was presented to the students and teachers at one of the schools in Gorna Oryahovitsa, Bulgaria – PGEE “M. V. Lomonosov”.

On October 25, at HOW.CAMP (http://how.camp/) 4 of the prizes were Tinusaur Starters kits.

What’s coming up …

— Video tutorial: “How to assemble the Tinusaur Board”. We need some more time to process the video-materials we shot.

 

Tutorial 002: Fading LED x1

UPDATE: 2022
WINAVR is (or was) a great project. Its most “recent” package is from 2010-01-20. In other words, it is outdated. There are many projects that attempted to replace it but none of them (AFAIK) is extremely popular. We’ve put together a very detailed guide on how to setup everything manually – AVR GCC Toolchain – Setup for Windows.

Another beginners tutorial is on the way – this time about a fading in and out LED.

This is simple tutorial that shows how to connect a LED to the ATtiny85 based Tinusaur board and write a program that makes the LED to fade in and out using PWM (pulse-width-modulation) technique.

PWM Diagram

Note: The code in this tutorial does not use the built-in PWM capabilities of the ATtiny microcontrollers, instead it uses direct bit manipulation since this un easier way to understand how it works. Another tutorial should cover the PWM functionality that is built into the microcontroller.

Tinusaur Board with LED

The Tinusaur board is a standard ATtiny breakout board so this could be applied to almost any other board that has ATtiny microcontroller on it. The code was tested to work with ATtiny13, ATtiny25, ATtiny45 and ATtiny85 but will probably work on any other ATtiny microcontrollers as well.

Please go to Tutorial 002: Fading LED x1 to see the full document.

You can also check the Tinusaur Board – Assembling Guide and the WinAVR – Setup Guide.

(UPDATED) Tutorial 001: Blinking LED

Tinusaur Tutorial 001 schematics

Our first tutorial Tutorial 001: Blinking LED (that’s the older one) was just updated and put under the Tutorials menu.

This is very simple tutorial that shows how to connect a LED to the Tinusaur board and write the “Hello World” of the microcontrollers – very simple program that makes a LED to blink.

Since the Tinusaur board is a very standard ATtiny breakout board this could be applied to almost any other board that has ATtiny microcontroller.

The code was tested to work with ATtiny13, ATtiny25, ATtiny45 and ATtiny85 but will probably work with other microcontrollers too.

Please go to the Tutorial 001: Blinking LED x1 page to see the full document.

 

Tutorial 001: Blinking LED

Tinusaur Tutorial 001: Blinking LED

UPDATE: New version of this tutorial is available at the Tutorial 001: Blinking LED x1 page.

Tinusaur Tutorial 001: Blinking LEDThis is very simple tutorial how to make a LED blinking.

Since the Tinusaur board is a very standard ATtiny breakout board this could be applied to almost any such other board.

The code was tested to work with ATtiny13, ATtiny25, ATtiny45 and ATtiny85 but will probably work other chips too.

We assume that the Tinusaur board is already assembled, successfully; connected through the ISP programmer to the computer; and development environment . It is not the subject of this tutorial how to assemble the board or how to setup development environment.

The LED should be connected on pin 2 of the ATtiny – this is PB3 – through a resistor, and to the GND.

The LED, marked as D1, is just a standard light emitting diode.

The resistor, marked as R1,  is 270 to 330 ohm.

The most important fragment of the code is this:

	while (1) {
		PORTB |= (1 << LED_PORT);
		_delay_ms(200);
		PORTB &= ~(1 << LED_PORT);
		_delay_ms(400);
	}

What is does is this:

  1. Start an infinite loop.
  2. Set the LED wire signal to “1” – that will make it to light.
  3. Wait a little – 200 milliseconds.
  4. Clear the LED wire signal to “0” – that will turn it off.
  5. Wait a little -400 milliseconds.
  6. Do it again.

Here is the entire source code:

/**
 * The Tinusaur Project
 *
 * Tutorial 001: Blinking LED
 *
 * file: main.c
 * created: 2014-01-04
 *
 **/

#include <avr/io.h>
#include <util/delay.h>

// ====================================
//                ATtiny
//               25/45/85
//              +--------+
//            --+ o  Vcc +------------
//  LED - PB3 --+        +--
//            --+        +--
//  ------------+ GND    +--
//              +--------+
// ====================================

// Define the I/O port to be used for the LED.
// This a number between 0 and 7 that tells which bit to use.
#define LED_PORT PB3

int main(void) {

	// Set the LED port number as output.
	// The DDRB is the data direction for port B.
	// This ...
	//  - shifts the "1" on left to the desired position and ...
	//  - does bitwise "OR" with the value in the port register.
	DDRB |= (1 << LED_PORT);

	// Start infinite loop.
	// (this is how most programs work)
	while (1) {

		// Set the LED bit to "1" - LED will be "on".
		PORTB |= (1 << LED_PORT);

		// Wait a little.
		// The delay function simply does N-number of "empty" loops.
		_delay_ms(200);

		// Set the LED bit to "0" - LED will be "off".
		PORTB &= ~(1 << LED_PORT);

		// Wait a little.
		_delay_ms(400);

		// Do it again ...
	}

	// Return the mandatory for the "main" function value.
	return (0);
}

Copy the code above to your “main.c” file.

The source code could be also found on Bitbucket at this address: https://bitbucket.org/tinusaur/tutorials/src/1f61873ae382/tut001/src/main.c.

The circuit schematics, even though very simple, was drown on 123d.circuits.io and it is available at this address: http://123d.circuits.io/circuits/76781.