A Simple Raspberry Pi Electronic Organ Based on MPR121

0 51665 Easy

I am a big fan of TwoSetViolin, they bring me a lot of fun and I start to appreciate the joy of music since I followed their channel last year. They have a program called LINGLING40HOURS, in which there are many linglings (talented fans) showing their music memes. Well, I know nothing about instruments, so it’s hard for me to come up with any good music memes. Okay, at last, I decided to utilize my expertise to DIY an electronic organ to join them.

Hardware

Raspberry Pi 4B X1

Raspberry Pi IO Expansion Board X1

Capacitive Touch Kit For Arduino X1

Gravity: Digital Speaker Module X1

Ice Cream Sticks (3*7)

Double Side Adhesive

Copper Foil Tape with Double-Sided Conductive

Conductor WireCardboard

Type-C power cable

Ethernet Cable X1 (Can be replaced by WiFi)

projectImage

Electronic Organ Key Connection

1. First, we have to figure out the logic before soldering: MPR121 can output 12 signals, but we need 3*7=21 signals. So here I am gonna divide the 12 signals into two groups: X (0-4), Y(5-11), then we can get 35 signal combinations.

Solder conductor wires onto the touch board(21 in total). I soldered the green and yellow points.

projectImage
projectImage

2. Prepare the organ plate

Cut cardboard of three layers and prepare some small cardboard strips for fix the organ keys.

projectImage
projectImage

Stick the touch board onto the middle layer of the cardboard. And drill holes on the boards, then pull out the conductor wires.

projectImage
projectImage

Organize the wires and cut out the extra parts.

projectImage
projectImage

3. Prepare the organ keys

Wrap the copper tapes around the ice cream sticks.

projectImage

Stick them onto the cardboard.

projectImage
projectImage

Software Preparation

I used the MobaXterm here.

Operation

1. Hardware Connection

Connect the speaker module to P22 of the expansion board, the touch board to IIC port.

projectImage
projectImage

You can also select other pins, but please make sure it is a GPIO port.

At last, plug in the power cable and Ethernet cable.

2. Programming

1) Download libraries files. I have placed them into a zip, click the link to download them to your PC.

The unzipped files should be like that in the image below.
projectImage

Drag the files to mobaxterm. (Please select Desktop path)

projectImage

Create a python file in your computer and name it as 36.py.

projectImage

Once a button is pressed, the speaker will play the corresponding note. The touch function in MPR121 library can only output 0~11, and we have to extend the 12 signals. So here I take 0~4 as X, 5~11 as Y, so there will be 5*7=35 combinations. I only set the G clef (treble clef), the F clef (bass clef), the C clef (alto clef). When x=1, bass clef, the frequency will be determined by the value of Y. When x=2, alto clef, the frequency will be determined by the value of Y, and so on.

Copy the following codes into 36.py file.

CODE
import sys
sys.path.append('../')
import time
 
 
import Adafruit_MPR121.MPR121 as MPR121
 
import RPi.GPIO as GPIO
Buzzer = 22
GPIO.setmode(GPIO.BCM)
GPIO.setup(Buzzer,GPIO.OUT)
Buzz = GPIO.PWM(Buzzer,440)
Buzz.start(0)
 
print('Adafruit MPR121 Capacitive Touch Sensor Test')
 
# Create MPR121 instance.
cap = MPR121.MPR121()
CL = [131, 147, 165, 175, 196, 211, 248]
CM = [262, 294, 330, 350, 393, 441, 495]
CH = [523, 587, 659, 698, 784, 880, 988]
 
if not cap.begin():
    print('Error initializing MPR121.  Check your wiring!')
    sys.exit(1)
     
print('Press Ctrl-C to quit.')
last_touched = cap.touched()
print(last_touched)
x = 0
y = 0
while True:
    global Buzz
    touch = []
    current_touched = cap.touched()
    n=current_touched
    print(n)
     
    #y=current_touched[1]
    for i in range(12):
        if n&1:
            touch.append(i)
        n = n >> 1
    if len(touch) == 2:
        x=touch[0]
        y=touch[1]-5
    print(x)
    print(y)
    if x==1:
        Buzz.start(50)
        Buzz.ChangeFrequency(CL[y])
    elif x==2:
        Buzz.start(50)
        Buzz.ChangeFrequency(CM[y])
    elif x==3:
        Buzz.start(50)
        Buzz.ChangeFrequency(CH[y])
    else:
        Buzz.stop()
    x = 0
    y = 0
    #last_touched = current_touched
 
    time.sleep(0.5)
     
    #GPIO.cleanup()

Drag the 36.py file into the examples of mobaxterm.

projectImage

Effect Display

As shown below, I pressed the sixth note of the bass clef. That's all for my work, thanks for reading!

projectImage
icon Adafruit_Python_MPR121.rar 101KB Download(4)
License
All Rights
Reserved
licensBg
0