Lesson 6 – Using a Buzzer

Booting up the Raspberry Pi

  1. This section assumes that you have read through “Lesson 0 – Setup” and have a working Raspberry Pi you can connect to remotely using SSH or VNC.
  2. If that’s not the case please head back to “Lesson 0 – Setup” and work through the instructions provided.
  3. Let’s now get started and boot up the Raspberry Pi.
  4. Grab a good quality USB cable and a USB power adaptor (2.5A).
  5. Plug one end of the USB cable into the plug and the other microUSB end into the Raspberry Pi.
  6. This should now power up the Raspberry Pi.
  7. Once the Raspberry Pi has booted up, please ensure that it is able to connect it to the network so that you can access it over VNC.
  8. If you are using a local monitor connected to the Raspberry Pi, you are all sorted.
  9. Else get connected to the Raspberry Pi using VNC.

Important Concepts

01 / What is a buzzer – A buzzer is an electronic device that makes beeping sounds and is used in electronic circuits to provide voice signals. Buzzers are widely used in computers, printers, photocopiers, alarms and other electronic products to create low volume audible signals alerts when actions are performed or rather when actions need to be performed. Buzzers can broadly be categorized as active and passive ones (see the pictures provided below). Turn the pins of two buzzers face upwards, and the one with a green circuit board is a passive buzzer, while the other enclosed with a black tape is an active one.

  1. The difference between an active buzzer and a passive buzzer is as follows – An active buzzer has a built-in oscillating source, so it is designed to make a noise when powered.
  2. But a passive buzzer does not have any such oscillating source, so it will not beep if DC (Direct current, the type of current provided by a battery as compared to the type of current that powers up lights at home which is called Alternating current) signals are used; instead, one has to inject a square waves whose frequency is between 2K and 5K to drive it.
  3. The active buzzer is often more expensive than the passive one because of multiple built-in oscillating circuits. Provided below is the electrical symbol of a buzzer.
  4. It has two pins with positive and negative poles. With a + sign  in the surface represents the anode and the other is the cathode with a – sign.
  5. With a buzzer the longer one is the anode and the shorter one is the cathode.

02 / What is a transistor – A transistor is a miniature electronic component that can do two different jobs. It can work either as an amplifier or a switch: When it works as an amplifier, it takes in a tiny electric current at one end (an input current) and produces a much bigger electric current (an output current) at the other. The transistor functions by amplifying a weak signal to a signal with larger amplitude (more powerful) while also being used as a non-contact switch.

While working as a switch it blocks the flow of current until certain conditions (forward bias) are reached only after which it allows current to flow through the device.  In the switching mode the transistor acts like a variable resistor. The value of resistor between Collector and Emitter is changed by the base current. The Transistor in this case acts as a Variable Resistor or a switch(ON/OFF).

A transistor is a semiconductor with a solid and non-moving part to pass a charge. It can amplify and switch electrical power and electronic signals. Transistors are made of semiconductor material with three or more terminals used to connect to an external circuit.

  1. A transistor is a three-layer structure composed of P-type (Positively doped material) or N-type (Negatively doped material) semiconductors.
  2. These transistors form the three regions internally which is why they are called P-N-P or N-P-N transistors.
  3. The thinner region in the middle is the base region (denoted by b); the other two regions on the sides can be either two N-type or two P-type ones.
  4. It’s this composition of a transistor that gives it the unique current amplification characteristics and it to be an amplifier.
  5. On a transistor the smaller region with intense majority carriers is the emitter region denoted by an e, when the other end is called as the collector region denoted by a c. The base of the transistor (Region in the middle) is denoted by a b.

Transistors are built by stacking three different layers of semiconductor material together. Some of those layers have extra electrons added to them (a process called “doping”), and others have electrons removed (doped with “holes” – the absence of electrons). A semiconductor material with extra electrons is called an n-type (n for negative because electrons have a negative charge) and a material with electrons removed is called a p-type (for positive). Transistors are created by either stacking an n on top of a p on top of an n, or p over n over p.

The NPN transistor is designed to pass electrons from the emitter to the collector (so conventional current flows from collector to emitter). The emitter “emits” electrons into the base, which controls the number of electrons the emitter emits. Most of the electrons emitted are “collected” by the collector, which sends them along to the next part of the circuit. A PNP works in a same but opposite fashion. The base still controls current flow, but that current flows in the opposite direction – from emitter to collector. Instead of electrons, the emitter emits “holes” (a conceptual absence of electrons) which are collected by the collector.

The transistor is kind of like an electron valve. The base pin is like a handle you might adjust to allow more or less electrons to flow from emitter to collector.

To read more about transistors please see – https://learn.sparkfun.com/tutorials/transistors

Let’s put together the circuit

  1. Our next tutorial is called, “Using a buzzer”.
  2. We will start off this tutorial by first putting together the circuit using a breadboard.
  3. We will wire up an RGB LED, connect it up to the Raspberry Pi through 3 x 220 Ohm resistors, power it up and write some python code.

In this experiment, we use an active buzzer, a PNP transistor and a 1k Ohm resistor. The resistor is used between the base of the transistor and Raspberry Pi GPIO pins to protect the transistor from large currents. When the B17 of Raspberry Pi output is supplied with low level (0 Volt) by programming, the transistor will conduct because it enters the current saturation mode and the buzzer should then produce sounds. However if a high signal is supplied to the B17 pin on the Raspberry Pi, the transistor will transition to cut off mode and the buzzer turns off.

Now, let’s get started. Components required for this tutorial include –

  1. 1 Raspberry Pi
  2. 1 Breadboard
  3. 1 Raspberry Pi Cobbler / T-Extension Board
  4. 1 40-Pin Cable to connect the Raspberry Pi Cobbler / T-Extension Board to the Raspberry Pi
  5. 1 x Resistor (1K Ω)
  6. 1 PNP Transistor (8550)
  7. Dupont jumper wires

PLEASE NOTEPlease make sure you have disconnected your breadboard from the Raspberry Pi before commencing build of the circuit. Once you have put the circuit together, get someone around you to review the circuit and confirm that the connections are proper before you proceed and power up the breadboard.

Wiring Up

Lets wire up your circuit based on the fritzing diagram below. Your connections should look like the following –

  1. Setup the Raspberry Pi Cobbler / T-Extension Board on the Breadboard.
    1. See the fritzing diagram below to understand what a suitable setup might look like.
    2. Note that one pair of the Ground/3.3V connects to the upper two (horizontally connected) rows of the breadboard.
    3. The second pair of the Ground/5V connects to the lower two (horizontally connected) rows of the breadboard.
  2. Wire up the positive pin (red wire) of the buzzer to 5V power source on the Raspberry Pi using the  Raspberry Pi Cobbler / T-Extension Board
  3. Wire up the 1K ohm resistor between pin  B17 on the Raspberry PI and the base of the PNP transistor
  4. Wire up the emitter of the PNP transistor to the negative pin of the buzzer (black wire)
  5. Wire up the collect of the PNP transistor to the ground pin of the Raspberry Pi using the Raspberry Pi Cobbler / T-Extension Board

You circuit should now look like the one below. Please run through the connections and compare them to the fritzing diagram below before you power on the circuit.

Here’s what the real world project will look like once you have wired up all the relevant components i.e. Raspberry Pi 3B+, Raspberry Pi Cobbler / T-Extension Board, breadboard, 1 x 1K Ohm resistors, a buzzer and 1 PNP transistor.

We would encourage you to try out some variation in the circuit.

  1. What if you introduced an LED on a different Raspberry Pi GPIO pin so that it blinked when the buzzer sounded.
  2. What if you introduced two LED’s of different colors connected to different pins of the Raspberry PI GPIO pins so that one blinks when the buzzer sounds and the other blinks when the buzzer doesn’t sound

These two additional challenges will require you to write additional code and extend the electronic circuit you’ve already built using the example provided in this tutorial.

Let’s Write Some Python Code

Open up the Thonny editor on your Raspberry Pi and let’s start putting together some code……

#!/usr/bin/env python

import RPi.GPIO as GPIO
import time

# Set #17 as buzzer pin
BeepPin = 17

def print_message():
	print ("========================================")
	print ("|                 Beep                 |")
	print ("|    ------------------------------    |")
	print ("|        Buzzer connect to GPIO0       |")
	print ("|                                      |")
	print ("|            Make Buzzer beep          |")
	print ("|                                      |")
	print ("|                            SunFounder|")
	print ("========================================\n")
	print 'Program is running...'
	print 'Please press Ctrl+C to end the program...'
	raw_input ("Press Enter to begin\n")

def setup():
	# Set the GPIO modes to BCM Numbering
	GPIO.setmode(GPIO.BCM)
	# Set LedPin's mode to output, 
	# and initial level to High(3.3v)
	GPIO.setup(BeepPin, GPIO.OUT, initial=GPIO.HIGH)

def main():
	print_message()
	while True:
		# Buzzer on (Beep)
		print 'Buzzer On'
		GPIO.output(BeepPin, GPIO.LOW)
		time.sleep(0.1)
		# Buzzer off
		print 'Buzzer Off'
		GPIO.output(BeepPin, GPIO.HIGH)
		time.sleep(0.1)

def destroy():
	# Turn off buzzer
	GPIO.output(BeepPin, GPIO.HIGH)
	# Release resource
	GPIO.cleanup()    

# If run this script directly, do:
if __name__ == '__main__':
	setup()
	try:
		main()
	# When 'Ctrl+C' is pressed, the child program 
	# destroy() will be  executed.
	except KeyboardInterrupt:
		destroy()

See a PDF copy of the tutorial here – <Link>

About the Raspberry Pi

The Raspberry Pi is a series of small single-board computers developed in the United Kingdom by the Raspberry Pi Foundation to promote the teaching of basic computer science in schools and in developing countries. It is a capable little computer which can be used in electronics projects, and for many of the things that your desktop PC does, like spreadsheets, word processing, browsing the internet, and playing games. The original model became far more popular than anticipated, selling outside its target market for uses such as robotics.

The Raspberry Pi does not include peripherals (such as keyboards, mice and cases). However, some accessories have been included in several official and unofficial bundles. According to the Raspberry Pi Foundation, over 5 million Raspberry Pis were sold by February 2015, making it the best-selling British computer. By November 2016 they had sold 11 million units, and 12.5m by March 2017, making it the third best-selling “general purpose computer”. In July 2017, sales reached nearly 15 million.In March 2018, sales reached 19 million. Most Pis are made in a Sony factory in Pencoed, Wales; some are made in China or Japan.

You can read more about the Raspberry Pi here – RaspberryPi.org.

Prerequisites

  1. This development track is based on the Rasbperry Pi and the SunFounder Super Starter Kit v3.0 for the Raspberry Pi.
  2. You will need access to both the Raspberry Pi 3 B and the electronics components part of the SunFounder Super Starter Kit v3.0 for the Raspberry Pi  kit to be able to work on these tutorials.
  3. If you haven’t purchased the Raspberry Pi 3 B yet please head over to our store and purchase one now. You can pick up the SunFounder Super Starter Kit v3.0 for the Raspberry Pi from SunFounder’s website.
  4. Depending on where you live you might also be able to pick up the Raspberry Pi and SunFounder Super Starter Kit v3.0 for the Raspberry Pi at your local electronics hobby store.
  5. You can read more about the Raspberry Pi here – RaspberryPi.org.

Questions