Tutorial: raspberrypi-electronics-III

This is the second part of my RaspberryPi tutorial series.

We now create a sound board:

raspberrypi-III.jpg

This time we plit the power lines to make the wirering easier.

Each button is connected to ground on the buttom side and connected to a line where a GPIO pin and a resistor (the 1k Ohm ones) are connected to the + power line.

The buttons are connected to the GPIO pins #18, #22, #23, #24 and #25.

The GPIO pin #17 is connected to the buzzer (PT-1504P).

The buzzer itself is connected to the ground.

If you like the colorful square buttons -> visit AdaFruit.

Create a script (nano ~/board2.sh) with following content:

#!/bin/bash
echo enable pins 17,18,22,23,24,25
echo 17 > /sys/class/gpio/export
echo 18 > /sys/class/gpio/export
echo 22 > /sys/class/gpio/export
echo 23 > /sys/class/gpio/export
echo 24 > /sys/class/gpio/export
echo 25 > /sys/class/gpio/export

echo setting directions
echo out > /sys/class/gpio/gpio17/direction
echo in > /sys/class/gpio/gpio18/direction
echo in > /sys/class/gpio/gpio22/direction
echo in > /sys/class/gpio/gpio23/direction
echo in > /sys/class/gpio/gpio24/direction
echo in > /sys/class/gpio/gpio25/direction

echo 0 > /sys/class/gpio/gpio17/value

function callbeep {
for (( i = 0; i <= $1; i++ )); do
    : do
  echo 1 > /sys/class/gpio/gpio17/value
  sleep 1
  echo 0 > /sys/class/gpio/gpio17/value
done
}

echo start loop
while [ 1 ]
do
value1=`cat /sys/class/gpio/gpio18/value`
value5=`cat /sys/class/gpio/gpio22/value`
value2=`cat /sys/class/gpio/gpio23/value`
value3=`cat /sys/class/gpio/gpio24/value`
value4=`cat /sys/class/gpio/gpio25/value`

echo $value1 - $value2 - $value3 - $value4 - $value5

if [ $value1 -eq 0 ]
then
   callbeep 1
   sleep 1
fi
if [ $value2 -eq 0 ]
then
   callbeep 2
   sleep 1
fi
if [ $value3 -eq 0 ]
then
   callbeep 3
   sleep 1
fi
if [ $value4 -eq 0 ]
then
   callbeep 4
   sleep 1
fi
if [ $value5 -eq 0 ]
then
   callbeep 5
   sleep 1
fi
done

echo "done"

So what are we doing here?

  • Initializing GOIP pins
  • Setting the direction
  • Ensure that the buzzer is deactivated (echo 0)
  • Define the function "callbeep" which
    • Loop $1 + 1 times:
      • Enable buzzer
      • Wait 1 second
      • Deactivate buzzer
  • Start endless loop to check buttons
    • Read states of buttons
    • Call method "callbeep" if button is pressed

This time the buttons are connected to the power line +.

So energy is floating as long as a button is pressed, because if it is pressed the circuit is grounded.

That's it - the third part is finished.

Hopefully you had some fun.

Please add comments and/or suggestions - both are welcomed.