The Arduino board has several digital pins which can be configured as digital I/O pins and among them some can also be used as analog output pins. There are dedicated analog input pins in most of the Arduino boards. The Arduino pro-mini board has 8 analog input pins marked as A0, A1 up to A7. In this particular project the variable pin of a potentiometer is connected at the analog input pin A0.
The Arduino IDE provides functions to access analog input and analog output of the board. The code written for this project uses the built-in function provided by the Arduino IDE namely analogRead().
analogRead()
This function can read an analog value from an analog pin mentioned in its argument and can returns that value. Suppose if there is a variable ‘var’ into which the vlue of the analog pin A0 is need to be read into, one can use the analogRead() function as shown below;
var = analogRead(A0);
The above statement will enable the built-in ADC of the arduino’s microcontroller which then converts the analog value to its 10 bit digital equivalent and then stores in the variable ‘var’. The variable ‘var’ is expected to be of the type integer.
The Arduino IDE provides certain functions to generate a square wave at a particular frequency which is make use in this project. The functions are namely tone() and noTone() for start generating a square wave at a particular frequency and to stop the square wave respectively. The details of the functions are discussed in the following section;
tone()
The function tone is used to generate a square wave at the required, with a required frequency and also for a required period of time. The function basically has three parameters of which the first one indicates the pin number at which the wave can be generated, the second one is the frequency of the square wave and the third parameter is the time duration until which the wave should continue. The prototype of the function is given as follows;
tone ( pin_number, frequency, duration );
As an example to generate a square wave at a pin number 8, with a frequency 1KHz and for a duration 5 seconds the following statement can be used.
tone ( 8, 1000, 5000 );
When the wave is required to present at the particular pin until it is stopped by the noTone() function call the following statement can be used;
tone ( 8, 1000 );
noTone()
The function noTone can be used to stop the square wave exist in the pin number at which it has been initiated by the tone() function call. The function has a parameter which is the pin number where the wave has to be stopped. As an example the function can be used to stop the wave generated at the pin number 8 as shown in the following;
noTone(8);
[/nextpage][nextpage title=”Circuit Diagram” ]