Musical Instruments // PCOMP W3
Sep 22, 2024
But first, some theory: Analog Output in Microcontrollers
This week's PCMOP work was light and went by like a breeze. I started by reading through the theory and taking notes on analog output in microcontrollers. Here's what I learned :
Pulse Width Modulation (PWM) is the key to "faking" analog output on digital pins.
PWM applications include LED dimming and DC motor speed control.
The
analogWrite()
function controls PWM on specific Arduino pins.Servomotors can be precisely controlled using PWM signals.
The
tone()
function generates different frequencies for audio output.Understanding the value ranges for digital and analog input/output is essential for effective microcontroller programming.
While the work was light, these concepts were very helpful in understanding the labs and help set a concrete foundation.
Lab 1 : Tone Output
This week in our labs, we delved into sound generation using Arduino. Here's a breakdown of my experience through the lab exercises:
Sensor input range
I started by wiring up my Arduino with a force-sensing resistor. Using a simple sketch to read the analog input, I watched the serial monitor as the values changed with my touch. Below is code and video of the readings.
Speaker Test
Next, I connected a small speaker to my Arduino. Using the tone()
function, I programmed it to play a constant 440Hz tone - Hearing that first beep was pretty cool, translating code to sound.
Play Tones
I mapped the sensor input to a frequency range of 100Hz to 1000Hz. It had created a simple instrument! Moving my hand by pressing the sensor produced a range of tones, and I spent some time just playing around with it.
Play it Loud
To amplify the sound, I modified the circuit by adding a transistor. This little addition made a big difference in the volume output.
A More Complex Example
Using the pitches.h
file, I programmed an array of notes and durations. This made the arduino sent output to the speak to play a tone.
A Musical Instrument
The final part of the lab was to create a simple musical instrument. I followed the instructions to connect three force-sensing resistors to my Arduino, each corresponding to a different note. The code programmed the Arduino to play a specific note when each sensor was pressed.
The result was a basic three-key keyboard!
Lab 2 : Servo Motor Control Using an Arduino
This lab was also pretty straightforward, here's a brief overview of my experience:
Circuit Setup: Wired an analog sensor (FSR) and a servo motor to the Arduino. T
Programming:
Read analog input from the sensor
Mapped sensor values (0-1023) to servo angles (0-179)
Used Servo library to control motor position
Fine-tuning: Learned to update servo every 20ms for smoother motion.