XRL Tech Corner

Font size: +

Introduction to GPIO Raspberry PI

One of the most interesting features of the Raspberry pi is the gpio (general purpose input output) interface. It is an input / output interface to communicate with different electronic components, which allow the raspberry pi to use leds, sensors, buzzers, etc ...

gpio raspberry pi zero w

The gpio interface consists of 40 pins, among which there are 26 for input / output and the rest are used for power and ground. These pins can be used to issue digital states. You can give a 1 when it is set to 3.3V or a zero when it is off to 0V.

To start using this gpio interface, we are going to carry out a very simple project: a metronome.

The project consists of connecting the Raspberry PI to a buzzer and emit pulses like a metronome using a program in Python.

The electrical assembly scheme would look something like this:

raspberry pi zero and buzzer bb There are two types of buzzers (buzzers), passive and active. The assets have an oscillating source that emits a type of sound, it is something similar to an LED that turns on or off, they need a non-alternating signal.

Passives need an alternate signal and can emit more variety of sounds.

To assemble the project we need a Raspberry PI, a microUSB power supply, a microSD card with Raspbian installed, a Cobbler with its cable, a small breadboard and an active buzzer.

The Cobbler is a T-shaped card (or in an elongated form) that allows the transfer of the gpio pins of the Raspberry PI to a protoboard to facilitate connections with the different electrical components.

cobbler bus cable

To start, we connect the Cobbler to the breadboard so that it is centered and the power pins are just connected on the side bus (+ and -).

Next we connect the bus cable between the Raspberry PI and the Cobbler as shown in the picture.

Then we connect one end of the buzzer to pin 18 and the other end to the GRND label (ground).

Finally connect a microUSB power supply to the Raspberry PI and turn it on. In our case we have used an external battery that allows portability to the assembly. You can use batteries with the same purpose, or a typical USB plug to charge tablets.

full raspberry buzzer motte

If we do not have an external screen with HDMI, a keyboard and a mouse, nothing happens, since we will connect by ssh to the Raspberry PI. Of course, previously we must know that IP is assigned by the router.

Once inside the Raspbian system, we are located in the Documents directory.

Making use of Python (see tutorial to learn Python ) we are going to write a program that activates the PIN 18 and emits a signal to activate the buzzer. We will use the gpiozero library and the Buzzer object to emit the beeps.

As an example we are going to program a metronome that emits a pulse in each beat of a measure (4/4). The program can be completed later so that it can also generate measures of 2/4, 3/4, 6/8 etc ...

As an input value, the Python program will request the beats per minute (assigned to the variable bpm) and, based on that value, calculate the pulse durations to mark the compass. In addition, the first beat of each compass will make a double pulse to indicate that it is the beginning of the compass. So for example, if we introduce a bpm value of 60, that will generate a stroke every second in a 4/4 time signature.

Below is the program in Python:

from gpiozero import Buzzer

from time import sleep

buzzer = Buzzer (18)

bpm = float (input ("Beats per minute"))

print (bpm)

tempo = (60 / bpm)

sleep (5)

i = 4

c = 1

while True:

if i% 4 == 0:

print ("compas n =" + str (c))

buzzer.on ()

c = c + 1 sleep (.01)

buzzer.off ()

buzzer.on ()

buzzer.on ()

sleep (tempo)

buzzer.off ()

i = i +1

And that's it. When executing the program and entering the value of bpm, it will begin to emit the beeps in each time that we will be able to hear by the buzzer.

Rate this blog entry:
Cheap Vps in Spain
Buy the BBC micro: bit

Related Posts



No comments made yet. Be the first to submit a comment
Saturday, 25 May 2019

Captcha Image