CSCI1600 Lab 4: Sound

Similar documents
ATmega16A Microcontroller

Microcontrollers and Interfacing

Timer/Counter with PWM

Timer 0 Modes of Operation. Normal Mode Clear Timer on Compare Match (CTC) Fast PWM Mode Phase Corrected PWM Mode

L13: (25%), (20%), (5%) ECTE333

A MORON'S GUIDE TO TIMER/COUNTERS v2.2. by

Portland State University MICROCONTROLLERS

Counter/Timers in the Mega8

CprE 288 Introduction to Embedded Systems (Output Compare and PWM) Instructors: Dr. Phillip Jones

RC Filters and Basic Timer Functionality

Lab 5 Timer Module PWM ReadMeFirst

Using the Z8 Encore! XP Timer

MICROCONTROLLER TUTORIAL II TIMERS

AVR PWM 11 Aug In the table below you have symbols used in the text. The meaning of symbols is the same in the entire guide.

What is Sound? Simple Harmonic Motion -- a Pendulum

ECED3204: Microprocessor Part IV--Timer Function

Arduino Microcontroller Processing for Everyone!: Third Edition / Steven F. Barrett

Lazy Clock Electronics and Software

Arduino Freq-Mite for Norcal NC40A Mike WA8BXN Jan 2018

Application Note: Using the Motor Driver on the 3pi Robot and Orangutan Robot Controllers

Generating DTMF Tones Using Z8 Encore! MCU

Atmel ATmega328P Timing Subsystems. Reading

Project Final Report: Directional Remote Control

Design with Microprocessors

ESE 350 Microcontroller Laboratory Lab 5: Sensor-Actuator Lab

Embedded Hardware Design Lab4

Module: Arduino as Signal Generator

AN4507 Application note

Fixed-function (FF) implementation for PSoC 3 and PSoC 5 devices

ECE 511: FINAL PROJECT REPORT GROUP 7 MSP430 TANK

Training Schedule. Robotic System Design using Arduino Platform

Measuring Distance Using Sound

DTMF Signal Detection Using Z8 Encore! XP F64xx Series MCUs

Using NeoPixels and Servos Together

INTERFACING WITH INTERRUPTS AND SYNCHRONIZATION TECHNIQUES

Screening Audiometer

128 KB (128K 1 = 128K

ME 461 Laboratory #3 Analog-to-Digital Conversion

For this exercise, you will need a partner, an Arduino kit (in the plastic tub), and a laptop with the Arduino programming environment.

Hardware and software resources on the AVR family for the microcontroller project

PIC ADC to PWM and Mosfet Low-Side Driver

ELCT 912: Advanced Embedded Systems

Lab 2: Blinkie Lab. Objectives. Materials. Theory

SCHOOL OF TECHNOLOGY AND PUBLIC MANAGEMENT ENGINEERING TECHNOLOGY DEPARTMENT

AN3252 Application note

The Interface Communicate to DC motor control. Iu Retuerta Cornet

Using Circuits, Signals and Instruments

EE-110 Introduction to Engineering & Laboratory Experience Saeid Rahimi, Ph.D. Labs Introduction to Arduino

Microcontroller: Timers, ADC

Precalculations Individual Portion Introductory Lab: Basic Operation of Common Laboratory Instruments

Lock Cracker S. Lust, E. Skjel, R. LeBlanc, C. Kim

Community College of Allegheny County Unit 4 Page #1. Timers and PWM Motor Control

Fixed-function (FF) implementation for PSoC 3 and PSoC 5LP devices

Lecture 4: Basic Electronics. Lecture 4 Brief Introduction to Electronics and the Arduino

Iowa State University Electrical and Computer Engineering. E E 452. Electric Machines and Power Electronic Drives

Lab 5: Inverted Pendulum PID Control

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science Electronic Circuits Spring 2007

Timer A (0 and 1) and PWM EE3376

EE 314 Spring 2003 Microprocessor Systems

EE-110 Introduction to Engineering & Laboratory Experience Saeid Rahimi, Ph.D. Lab Timer: Blinking LED Lights and Pulse Generator

EE-4022 Experiment 2 Amplitude Modulation (AM)

UNIVERSITY OF VICTORIA FACULTY OF ENGINEERING. SENG 466 Software for Embedded and Mechatronic Systems. Project 1 Report. May 25, 2006.

νµθωερτψυιοπασδφγηϕκλζξχϖβνµθωερτ ψυιοπασδφγηϕκλζξχϖβνµθωερτψυιοπα σδφγηϕκλζξχϖβνµθωερτψυιοπασδφγηϕκ χϖβνµθωερτψυιοπασδφγηϕκλζξχϖβνµθ

ELECTRONICS PULSE-WIDTH MODULATION

OBJECTIVE The purpose of this exercise is to design and build a pulse generator.

Analog-to-Digital Converter. Student's name & ID (1): Partner's name & ID (2): Your Section number & TA's name

Written by Hans Summers Wednesday, 15 November :53 - Last Updated Wednesday, 15 November :07

Physics 2310 Lab #2 Speed of Sound & Resonance in Air

ECE 2111 Signals and Systems Spring 2012, UMD Experiment 9: Sampling

EE 308 Spring S12 SUBSYSTEMS: PULSE WIDTH MODULATION, A/D CONVERTER, AND SYNCHRONOUS SERIAN INTERFACE

Microprocessors & Interfacing

Green Electronics Library Documentation

Real time digital audio processing with Arduino

CI-22. BASIC ELECTRONIC EXPERIMENTS with computer interface. Experiments PC1-PC8. Sample Controls Display. Instruction Manual

Analog Input and Output. Lecturer: Sri Parameswaran Notes by: Annie Guo

Course Introduction. Content 20 pages 3 questions. Learning Time 30 minutes

Hello, and welcome to this presentation of the STM32 Infrared Timer. Features of this interface allowing the generation of various IR remote control

Massachusetts Institute of Technology MIT

Signal Forge 2500M Frequency Expansion Module. 1.5 GHz to 2.6 GHz. User Manual

School of Engineering Mechatronics Engineering Department. Experim. ment no. 1

EE445L Spring 2018 Final EID: Page 1 of 7

Sampling and Reconstruction

Electronic Concepts and Troubleshooting 101. Experiment 1

Sept 13 Pre-lab due Sept 12; Lab memo due Sept 19 at the START of lab time, 1:10pm

Signal Forge 1800M Frequency Expansion Module. 1.0 GHz to 1.8 GHz. User Manual

UNIVERSITY OF NORTH CAROLINA AT CHARLOTTE Department of Electrical and Computer Engineering

DS1802 Dual Audio Taper Potentiometer With Pushbutton Control

Controlling DC Brush Motor using MD10B or MD30B. Version 1.2. Aug Cytron Technologies Sdn. Bhd.

PWM System. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

Electronics Design Laboratory Lecture #10. ECEN 2270 Electronics Design Laboratory

CSE 3215 Embedded Systems Laboratory Lab 5 Digital Control System

PIC Functionality. General I/O Dedicated Interrupt Change State Interrupt Input Capture Output Compare PWM ADC RS232

_ Programming Manual RE729 Including Classic and New VoX Interfaces Version 3.0 May 2011

EARTH PEOPLE TECHNOLOGY, Inc. FAST ARDUINO OSCILLOSCOPE PROJECT User Manual

Lab 12 Laboratory 12 Data Acquisition Required Special Equipment: 12.1 Objectives 12.2 Introduction 12.3 A/D basics

Electronic Components

Lab 4: Analysis of the Stereo Amplifier

Fundamentals of Digital Audio *

Capacitive Touch Sensing Tone Generator. Corey Cleveland and Eric Ponce

University of North Carolina-Charlotte Department of Electrical and Computer Engineering ECGR 3157 Electrical Engineering Design II Fall 2013

Transcription:

CSCI1600 Lab 4: Sound November 1, 2017 1 Objectives By the end of this lab, you will: Connect a speaker and play a tone Use the speaker to play a simple melody Materials: We will be providing the parts necessary for this lab (other than the Arduino and breadboard). 2 Background: Sound Consider Wikipedia s definition of sound: a vibration that propagates as a typically audible mechanical wave of pressure and displacement A speaker produces sound by moving a cone back and forth to generate these presure waves. Figure 1: Sound wave. Source: physicsclassroom.com 1

A speaker is driven using electromagnetism: applying current in one direction pulls the speaker cone in one direction and applying current in the opposite direction pushed the speaker cone in that opposite direction. Therefore, applying an oscillating current will cause the speaker cone to move back and forth repeatedly, creating an audible sound. Sound waves can measured by frequency, which is the number of oscillations per second, referred to as Hertz (Hz). In this lab, we will use our Arduino to generate simple sounds consisting of a single frequency. We will expand on this to show how more complex sounds can be reconstructed via sampling, which you will use in your next project. The frequencies corresponding to common notes can be found here: http://www.phy.mtu.edu/~suits/notefreqs.html 3 Connecting the Speaker Now we are going to wire up our speaker to our Arduino s PWM port to apply the concepts above to make sound! To connect the speaker, you will need: 1. An 100 Ω resistor (You can use a larger resistor, but it will decrease the volume) 2. An 8Ω speaker Connect your circuit to match this diagram: Figure 2: Basic Speaker Circuit master d894eb2 Page 2 of 10

CSCI1600 Real-Time and Embedded Software Reiss Figure 3: Basic Speaker Circuit To connect the speaker, use the two pins that are located closest to each other as the terminals you can push these into your breadboard. Figure 4 shows an example for how to connect the speaker to a breadboard. Figure 4: 8Ω Speaker on Breadboard Task: Connect your speaker to your Arduino to match the configuration in the figure. 4 Playing a simple melody We can create simple sounds with our Arduino by generating PWM signals (ie, square waves) at a specific frequency. We can use this to play a simple melody by playing notes at a given frequency for a certain amount of time. Task: Use the tone() function to make your speaker play a simple melody. You can use the tone function to specify the pitch and duration of each note. master d894eb2 Page 3 of 10

5 Sampling: Producing more complex sounds To produce arbitrary sound waves, we would need to be able to output any analog voltage on a spectrum (0 to 5V, for example) in order to perfectly match our wave. Our Arduino, however, can only output digital signals: HIGH (5V) or LOW (0V). Calling the tone() function generates a PWM waveform on a particular pin (ie, a square wave) at a particular frequency. This works well for playing simple sounds consisting of single-frequency tones, but we need to go a step further to produce more complex signals. To approximate an arbitrary, continuous signal, digital systems represent these signals as a discrete series of samples, which are scalar values that represent the magnitude of the signal at a certain time index. An example of this is shown in Figure 5, with each S i representing a discrete sample of the the analog signal S(t). Figure 5: Sampling. Source: wikipedia.org Note that each sample is recorded at a periodic rate, T, the inverse of which is called the sampling frequency or sample rate. A full discussion of sampling theory is far beyond the scope of this lab. However: for the purpose of reproducing a waveform, this means that we need to output a new sample at every time interval T in order to accurately reconstruct the signal a hard real-time operation. Furthermore, it makes intuitive sense that we cannot reproduce waveforms of a frequency higher than our sampling frequency. In fact, to perfectly reconstruct a waveform without losing information, the maximum frequency we can reproduce is equal to half the sampling rate a fundamental theorem in sampling theory by Claude Shannon and Harry Nyquist. You can read more about this here: https://en.wikipedia.org/wiki/nyquist%e2%80%93shannon_sampling_theorem master d894eb2 Page 4 of 10

6 Generating Arbitrary Sounds on the Arduino Uno Our our microcontroller, we can meet our real-time constraint for playing sounds by using interrupts to control when to output each sample in a stored waveform. In this lab, we will configure interrupts to reconstruct sounds with a sampling frequency of 8 khz, which is high enough to reproduce lowquality audio. 6.1 Using Interrupts Our Arduino contains three hardware timers (called Timer0, Timer1, and Timer2) that can accurately measure time these are blocks of hardware separate from the CPU core that operate in an asynchronous manner from code execution. These timers are can be used for timekeeping operations, PWM generation, frequency measurement, and more. These timers can be configured to produce interrupts: when an interrupt occurs, the CPU stops its current operation and runs interrupt service routine, or ISR, which is a function with a special definition you define in your program. After the ISR returns, the CPU restores its previous context and continues running. In this way, we can use the ISR to perform operations that happen at precise time intervals. Rather than download an external library to configure the timer, we can do it ourselves in a few lines of code that manipulate the hardware configuration registers. Examine the sample code below, which configures interrupts to fire 8000 times per second: the first function defines the ISR we want to run, and the second function configures the timer. In this example template, the ISR just increments a counter (which you could use for timekeeping). We will extend this ISR slightly to output a sample from a given waveform each time it runs. If you want to learn more about how the timer configuration works, you can read about the Timer peripherals and their configuration registers in the Atmega328P datasheet the relevant sections are referenced in the comments for the example. master d894eb2 Page 5 of 10

# define SAMPLE_RATE 8000 volatile unsigned long timer_count = 0; // This function is the Interrupt Service Routine ( ISR ), which is called // every time an interrupt occurs for this timer ISR ( TIMER1_ COMPA_ vect ) { timer_count ++; // Example : count number of time intervals elapsed } //... Play a sample here... void init_timer ( void ) // Call this function from setup () { nointerrupts (); // Disable all interrupts // Clear Timer1 register configuration TCCR1A = 0; TCCR1B = 0; // Configure Timer1 for CTC mode ( WGM = 0 b0100 ) bitset ( TCCR1B, WGM12 ); // Disable prescaler bitset ( TCCR1B, CS10 ); // Set timer period, which is defined as the number of // CPU clock cycles ( 1/ 16 MHz ) between interrupts - 1 OCR1A = ( F_ CPU / SAMPLE_ RATE ) - 1; // Enable timer interrupts when timer count reaches value in OCR1A bitset ( TIMSK1, OCIE1A ); } interrupts (); // Enable interrupts Task: Copy this example into your code and call the init timer() function from your setup() function to configure the timer. To demonstrate how to use interrupts: use the provided counter timer count to blink an LED at a rate of once per second. If your Arduino has a built-in LED (try pin LED BUILTIN), you can use it for this purpose. Your code should not use any Arduino timing functions such as delay() or millis(). 6.2 Approximating Analog Sounds Before we can play sounds, however, we have one additional problem: the Arduino can only output digital signals (HIGH and LOW), it cannot output analog voltages corresponding to the magnitude master d894eb2 Page 6 of 10

of the waveform we want to reproduce. We can, however, approximate an analog signal using PWM. By varying the duty cycle of the PWM signal, we can approximate an analog signal by transmitting the same amount of power to the speaker per unit time as as the analog waveform we want to reproduce. The accuracy of the approximated signal is directly reliant on the frequency of the PWM output. To make this work, the frequency of the PWM signal must be much higher than the analog wave it is trying to reproduce, as is visible in the diagram in Figure 6 Figure 6: Sound wave. Source: adafruit.com Previously, we generated PWM waveforms using the analogwrite() function. Unfortunately, the Arduino implementation for generating PWM waveforms does not use a high enough PWM frequency for our purposes, so we need to configure another timer to generate PWM signals at a higher frequency. We have provided source code to configure the timer to do this on the course website in the files fastpwm.cpp and fastpwm.h. Download these files and add them to your sketch by selecting Sketch > Add File... from the Arduino IDE menus. Examine these files briefly to understand the interface provided to you. Most notably, the function fastpwm init() configures Timer2 to generate a PWM signal on pin 11 at a frequency of about 32 khz. You can call this function from your sketch to start the PWM generator. After initialization, you can set the duty cycle of the waveform (0 255) by passing a sample value to the function fastpwm play sample(). Some important notes about using the fast PWM method: It is important to understand the distinction between how Timer2 and Timer1 are used here: Timer1 (from the previous section) is used to generate periodic interrupts at 8000Hz you will use the ISR for this timer to handle playing samples and timekeeping operations. Timer2 is is only used to generate the PWM signal that we use to approximate sounds. Once configured, your sketch only interacts with it by changing the duty cycle of the signal using fastpwm play sample(). It is the change in PWM duty cycle between samples that produces an audible sound output. Therefore, calling fastpwm play sample() only once, or calling it repeatedly with the same master d894eb2 Page 7 of 10

value, will appear to have no effect as the frequency of the PWM signal is too high to hear. Timer2 is also used by the Arduino tone() and analogwrite() functions. Therefore, you cannot use these Arduino functions while playing samples with fast PWM, as it will change the timer settings. For the final portion of this lab, we can again play a simple melody, but this time we can compose it from arrays of samples for three waveforms representing tones at 200, 400, and 800 Hz. These tones will be played at 8000 samples per second with 8 bit resolution. 200hz (40 samples): 255, 253, 248, 241, 230, 217, 202, 185, 166, 147, 127, 107, 88, 69, 52, 37, 24, 13, 6, 1, 0, 1, 6, 13, 24, 37, 52, 69, 88, 107, 127, 147, 166, 185, 202, 217, 230, 241, 248, 253 400hz (20 samples): 255, 248, 230, 202, 166, 127, 88, 52, 24, 6, 0, 6, 24, 52, 88, 127, 166, 202, 230, 248 800hz (10 samples): 255, 230, 166, 88, 24, 0, 24, 88, 166, 230 Save these sequences as arrays in your sketch. You can now use them to generate a simple melody by playing each tone for a certain duration using the Timer1 ISR. For example: to play the 200Hz tone, play one sample from an array of the 200Hz on every timer interrupt (ie, every 1/8000 seconds); when reaching the end of the sample array, continue again from the starting index. Assuming a sampling frequency of 8000 Hz, playing 8000 samples corresponds to 1 second of audio. Task: Use the three sample arrays to play a simple melody lasting at least five seconds using interrupts. master d894eb2 Page 8 of 10

1 2 CSCI1600 Real-Time and Embedded Software Reiss 7 (Optional) A simple amplifier Our speaker is rather quiet. Part of this is unavoidable: our speaker is only rated for 0.2W, so it cannot produce a very powerful signal. However, we can provide some additional power and improve the quality using a single transistor an example circuit is shown in Figure 7. The transistor basically operates as a voltage-controlled switch, allowing a low-power source to drive a high-power load. The input signal from the Arduino controls this switch to provide additional power to the speaker supplied by the Arduino board s voltage regulator, rather than powering it directly from the PWM output pin. Warning: Be sure that you connect the capacitor C1 correctly! Large capacitors are polar components and must be connected in the correct direction. The negative end of the capacitor is marked with a grey stripe on the body of the capacitor and should be connected to the speaker. Connecting the capacitor incorrectly can cause explosive results! Note: The transistor used in this schematic has multiple variations with different pin configurations. To ensure you connect it correctly, look up the datasheet for the part number of the transistor you are using to find its pin diagram. 5V 1 2 R1 10Ω C1 1mF SPKR1 1 + SPEAKER PWM Output 1 R2 100Ω 2 1 2 3 Q1 2N2222 2 - Figure 7: Sound single transistor amplifier schematic master d894eb2 Page 9 of 10

8 Grading Rubric Task Total Points Points Earned Speaker circuit on breadboard 5 Playing simple melody with tone() 5 Blinking LED using interrupts 10 Playing melody using interrupts 10 (Optional) Simple amplifier circuit (10) Total 30 master d894eb2 Page 10 of 10