Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Fruit Piano
This project is a simple fruit piano that reads input from an MPR121 touch sensor and plays music through a DAC. In other words, we’ve turned fruits into a keyboard, allowing you to play music by simply touching them.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Elite Explorer Kit |
300+ |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
---|---|
- |
|
- |
|
- |
Wiring
Schematic
Code
Note
You can open the file
02_fruit_piano.ino
under the path ofelite-explorer-kit-main\fun_project\02_fruit_piano
directly.Or copy this code into Arduino IDE.
Note
To install the library, use the Arduino Library Manager and search for “Adafruit MPR121” and install it.
How it works?
Here’s a step-by-step explanation of the code:
Library and Object Initialization:
Import the necessary libraries:
Wire
library (for I2C communication),Adafruit_MPR121
library (for driving the MPR121),analogWave
library (for generating analog waveforms), andpitches.h
(which defines the frequencies of notes). Create instances ofAdafruit_MPR121
andanalogWave
objects. Define a notes array to store the note corresponding to each touch channel.setup()
:Initialize Serial communication and wait for it to start. Check and initialize the MPR121; if not found, print an error message on the serial monitor and halt execution. Initialize the
analogWave
object and set the initial sine wave frequency to 10Hz.loop()
:Read the currently touched channels of the MPR121. Iterate through all channels, check which one is touched, and play the corresponding note. Add a small delay between each iteration.
Play Note
playNote()
:The
playNote
function takes anote
parameter and sets the DAC frequency to play the corresponding note. Delay for a period to play the note. Stop playing the note.