microbit Buzzer

DC Buzzers


DC Buzzers

The U:Create 3.3kHz DC buzzers are really easy to use. Unlike similar piezo buzzers, these buzzers will produce a tone simply by applying a fixed DC voltage across the buzzer terminals. What’s more, these buzzers will work across a wide range of voltages, from around 1.5V to 15V. This ability to work across a large voltage range makes them ideal for use with a large number of different boards. In this article, we’re going to take a look at how this buzzer may be used with a Raspberry Pi, a BBC micro:bit, and an Arduino.

Raspberry Pi

Connecting these buzzers to a Raspberry Pi couldn’t be easier, and simply involves connecting the ground terminal of the buzzer to one of the ground pins on the Pi, and the positive terminal of the buzzer to one of the GPIO pins. The diagram below shows the required circuit.

Raspberry Pi Buzzer

To sound the buzzer, the GPIO pin connected to the positive terminal of the buzzer (in this case GPIO4) needs to be set high. The code below shows how this may be done.

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO_BUZZER = 4
GPIO.setup(GPIO_BUZZER, GPIO.OUT)
GPIO.output(GPIO_BUZZER, False)

while True:
    GPIO.output(GPIO_BUZZER, True)
    time.sleep(0.5)
    GPIO.output(GPIO_BUZZER, False)
    time.sleep(0.5)

This code will sound the buzzer at half second intervals, so if you want to try something a little more interesting the buzzer could be combined with an HC-SR04 ultrasonic ranging sensor as shown in the diagram below.

Raspberry Pi HC-SR04 and Buzzer

With this circuit, we can create a proximity alarm; similar to a reversing sensor for a car. The code for this is shown below.

import _thread
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO_TRIGGER = 18
GPIO_ECHO = 24
GPIO_BUZZER = 4

GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
GPIO.setup(GPIO_BUZZER, GPIO.OUT)

GPIO.output(GPIO_TRIGGER, False)
GPIO.output(GPIO_BUZZER, False)

keepRunning = True
distance = 0

def measureDistance():
    GPIO.output(GPIO_TRIGGER, False)
    time.sleep(0.5)
    GPIO.output(GPIO_TRIGGER, True)
    time.sleep(0.00001)
    GPIO.output(GPIO_TRIGGER, False)
    start = time.time()
    while GPIO.input(GPIO_ECHO) == 0:
        start = time.time()

    while GPIO.input(GPIO_ECHO) == 1:
        stop = time.time()

    elapsed = stop - start
    distance = elapsed * 17150
    return distance

def playSound(threadName, delay):
    keepRunning
    distance
    while keepRunning:
        if distance <= 30:
            GPIO.output(GPIO_BUZZER, True)
            time.sleep(0.01 * distance)
            GPIO.output(GPIO_BUZZER, False)
            time.sleep(0.05 * distance)

        time.sleep(delay)

try:
    distance = measureDistance()
    _thread.start_new_thread(playSound, ("BuzzerThread1", 0.01))
    while True:
        print (distance)
        time.sleep(0.1)
        distance = measureDistance()
except:
    keepRunning = False
    time.sleep(1)
    GPIO.cleanup()

BBC micro:bit

Connecting the buzzer to a BBC micro:bit is also very simple, since the input voltage of the buzzer is compatible with the pin voltage of the micro:bit.

microbit Buzzer

The code for the micro:bit buzzer project is written using PXT, and is shown below.

microbit Buzzer PXT

As with the Raspberry Pi buzzer project, you could combine the buzzer with an HC-SR04 ultrasonic ranging sensor to make something a little more interesting (and guaranteed too annoy your co-workers). We’ve done precisely that, using the circuit shown below.

microbit HC-SR04 and Buzzer

Using this circuit we’ve created a PXT project to produce a simple proximity alarm.

microbit HC-SR04 and Buzzer PXT

Arduino

Connecting these DC buzzers to an Arduino is, yet again, very simple. It involves connecting the negative terminal of the buzzer to one of the ground pins, and the positive terminal of the buzzer to one of the GPIO pins.

Arduino Uno Buzzer

Having connected the buzzer, the code for controlling the buzzer involves simply setting the GPIO pin on and off as desired; for example, to control a buzzer on GPIO pin 9:-

#define buzzer 9

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

void loop() {
    digitalWrite(buzzer,HIGH);
    delay(500);
    digitalWrite(buzzer,LOW);
    delay(500);
}

Again, to make things a little more interesting, the buzzer can be combined with an HC-SR04 ultrasonic ranging sensor.

Arduino Uno HC-SR04 and Buzzer

The code below turns the HC-SR04 into a proximity alarms with LED indicators.

#define triggerPin 12
#define echoPin 13
#define ledGreen 10
#define ledRed 11
#define buzzer 9

void setup() {
    pinMode(triggerPin, OUTPUT);
    pinMode(echoPin, INPUT);
    pinMode(ledGreen, OUTPUT);
    pinMode(ledRed, OUTPUT);
    pinMode(buzzer, OUTPUT);
}

void loop() {
    long duration, distance;
    digitalWrite(triggerPin, LOW); // Added this line
    delayMicroseconds(2); // Added this line
    digitalWrite(triggerPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(triggerPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance = (duration / 2) / 34.3;
    if (distance < 10) {
        digitalWrite(ledRed,HIGH);
        digitalWrite(ledGreen,LOW);
        digitalWrite(buzzer,HIGH);
    }
    else {
        digitalWrite(ledRed,LOW);
        digitalWrite(ledGreen,HIGH);
        digitalWrite(buzzer,LOW);
    }
    delay(500);
}