Science and d Information Conference 2015 July J 28-30, 2015 | London, UK
Digital Sound Processing using Arduin no and MATLAB Sérgio Silva1,3 1
António Valente 1, 3 3
School of Sciences and Technoloogy Engineering Department UTAD, Vila Real
[email protected]
INESC TEC T INESC Technology y and Science (formerly INESC Portto, UTAD pole) Porto, Porttugal
Salviano Soares1,2
Sylvain T. Marcelino
2
IEETA UA, Aveiro, Portugal
IPLeiria/ESTG,, Portugal
[email protected]
Abstract—Over the last decade, impelled b by the huge open source software community support, the loow cost Arduino platform presents itself as an alternative foor digital sound processing. Although Arduino is generally used for small applications for the artistic and maker commu unity, its built-in Analog to digital converter can be used for sound capturing, processing and reproduction. Equipped with a powerful AVR 8 bit RISC microcontroller, the Arduino, can achiieve up to 200kHz with a 10 bit resolution according to the Atm mel ATmega328P datasheet that is the AVR core that we are goin ng to focus on this article. Realizing the hardware potential, softwarre suppliers like Matworks or National instruments, have inclu uded the Arduino packages on the software accessories of MATLA AB and LABView. This work presents some of the sound capabiilities and specific limitations of the Arduino platform, enfacing itts connection and installation with MATLAB software. A series oof examples of the Arduino interface with MATLAB are detail and shown in order to facilitate users initiation of MATLAB and Arduino Digital Sound Processing enhancing education fosteringg. Keywords—Digital Sound Processing; MA ATLAB; Arduino; ADC/DAC; Sampling; Vocoder; FM Synthesiis of Instrument Sounds
I.
INTRODUCTION
The Atmel ATmega328P is the core of ourr Arduino and, as all the AVR cores, is equipped with a 10 bits analog to digital converter (ADC). An analog signal, as often ccall, is a continue steam of analog values and can be read or saampled a certain number of times per second, which is refferred to as the sampling frequency (fs). Figure 1 shows an anaalog signal with a D.C. component around 2.5Volts.
Fig. 1. Analog Signal
To deal with sound signals center c around 0 Volts (V) ranging [-5; 5] (V) and to be able to o read then with Arduino we need to do some level adjustment with a circuit like the one showed on figure 2.
Fig. 2. Simple Level Shifting circuit
To get the right input, usually R3 and R2 have the same n replaced by a decoupling value around 10 k and R1 is often capacitor of 10 μF (Coupling capacitors are used to block D.C. and pass A.C. that represents the mu usic signal e.g). There are different types of AD DC architectures but most 8 bits AVR use successive approxim mation ADC, while 32 bits AVR uses comparative ADC. Figurre 3 is a simplified diagram of the 8 bit AVR ADC.
Fig. 3. ADC Block diagram [1]
1184 | P a g e www.conference.thesai.org
Science and d Information Conference 2015 July J 28-30, 2015 | London, UK TABLE I.
The AVR only has a 10 bit ADC and usses an 8 channel analog multiplexer to sample each of the 8 annalog. The ADC circuit takes samples between [13; 250] μs and AVR has a dedicated clock that ensures the independenccy of conversion from other microcontroller parts. The converrsion mechanism can be triggered either on demand or automattically. The ADC conversion result is stored in two registers the ADC high and low. The result is obtained by the formula: ൬
୍ ൰ ͵ʹͲͳ כ ୖ
will be 512 and if If VIN is 2.5 Volts the converted value w VIN = VREF than the result is 1023. The Arduino uses a simple command tto start a single conversion the “analogRead(pin number)”. IIf nothing else is performed the conversion is complete. Figurre 4 shows both ADC Data Register, in this particular case thhe ADLAR bit is set and the result is left adjust (the ADLAR biit is bit number 5 of the ADMUX – ADC Multiplexer Selection Register).
ADCSRA DIVISION FACTORS
ADCSRA ADPS2 0 0 0 0 1 1 1 1
Division Factors 2 2 4 8 16 32 64 128
ADP PS1 0 0 1 1 0 0 1 1
ADPS0 0 1 0 1 0 1 0 1
So in order to set the ADC Presscaler to 64 we should write the following setup instructions: bitSet(ADCSRA,ADPS2) ; bitSet(ADCSRA,ADPS1) ; bitClear(ADCSRA,ADPS0) . 5 clock cycles. AVR clock The total conversion takes 14.5 frequency is 16 MHz if we set the ADC prescaler to 2, the conversion period can be obtain usin ng the formula: ܶ௩ ൌ ͳͶǤͷ כ
ଶ ଵ
ൌ ͳǤͺͳʹͷߤݏ
(3)
Table 2 presents the ADC conversion period times and nations of the prescaler the frequencies for all possible combin convertion frequency has calcu ulated using the formula fconv=1/ Tconv. TABLE II.
Fig. 4. ADC Data Register, ADCH and ADCL[1]
Remember that the ADC stores 10 bits the ADC0 … ADC9 so if we read only ADCH we ignore the first 2 bits. It is also possible to use ADCW which is not found in Atmel datasheet in order to get the ADC resuult. To use it just assign it to an unsigned integer: unsigned int adc_value = ADCW. Clock frequencies between 50 kHz and 2000 kHz should be use to get the higher resolution. Higher frequuencies above the 200 kHz will produce less bits resolution. Thee ADC prescaler, is set by the ADC Control and Status Registeer A (ADCSRA), as shown in Figure 5.
ADC CONVERSION TIMES AND FREQUENCIES
ADC prescaler
Tconv (μs)
Tmconv (μs)
fconv (kHz)
2 4 8 16 32 64 128
1,8125 3,6250 7,2500 14,5000 29,0000 58,0000 116,0000
5,60 000 7,19 900 10,5 5800 17,0 0600 30,0 0600 56,1 1200 112,0700
178,571 139,082 94,518 58,617 33,266 17,819 8,923
The differences between measure and calculated times can be explain by the use of the micros() function that has a resolution of about 4 μs [2]. As statted by André Bianchi is not entirely true because on our implementation the results show he first 3 measurements and that this difference only applies to th all other presents smaller differences. With the ADC prescaler set to 16 and a clock speed of 1 we achieve a total of 58,617 samples per second A resolution. (fs~59kHz) without compromising ADC MHz
Fig. 5. ADCSRA - ADC Control and Status Register A[[1]
Combining the 3 ADPS bits sets the relaation from AVR clock frequency and the ADC clock is display in Table 1. Suppose we have system clock with freequency 16 MHz (16000000 Hz) and set division factor too 128 (Arduino default), then ADC clock frequency is: ଵ ଵଶ଼
ൌ ͳʹͷͲͲͲ ݖܪൌ ͳʹͷ݇ݖܪ
(2)
In fact the ADC accuracy also depends d on the ADC clock. The recommended maximum ADC C clock frequency is limited by the internal DAC in the circuitry y conversion. For optimum performance, the ADC clock sho ould not exceed 200 kHz, however, frequencies up to 1 MHz H do not reduce the ADC resolution [3]. II.
MENTATION ADC IMPLEM
In order to perform the ADC measurements m the following code was used:
1185 | P a g e www.conference.thesai.org
Science and Information Conference 2015 July 28-30, 2015 | London, UK
unsigned long start; unsigned long stop; unsigned long ADC_value[100]; unsigned int j; void setup() { Serial.begin(9600); // Begin Serial port pinMode(1, INPUT); ADCSRA &= ~(1