ENCYCLOPEDIA OF RADIO ELECTRONICS AND ELECTRICAL ENGINEERING Arduino. Operations of analog input-output, work with a sound. Encyclopedia of radio electronics and electrical engineering Encyclopedia of radio electronics and electrical engineering / Radio amateur designer Although digital input-output operations allow solving a wide range of tasks, the presence of a built-in analog-to-digital converter (ADC) in the microcontroller of the Arduino board and the ability to output analog signals using pulse-width modulation (PWM) provide work with analog sensors and all kinds of actuators, affecting the object in proportion to the control signal. Strictly speaking, in output mode, all Arduino port lines can only transmit discrete signals that have only two states. But the microcontroller is able to change these states very quickly, generating rectangular pulses. If you apply these pulses to any device that has inertial properties, then it will behave as if the voltage applied to it is constant, equal to the average value of the pulse, and changes smoothly, and not jumps between high and low logic levels. In PWM mode, the port generates a pulse signal of constant frequency and variable duty cycle (this is the ratio of the pulse repetition period to their duration). Often, instead of the duty cycle, they operate with the reciprocal of it - the duty cycle, which can be changed from 0 (no pulses) to 100% (pulses follow, merging, without pauses). Therefore, although the output voltage is either high or low at any given moment, its average value is proportional to the duty cycle. If you connect a regular multimeter to this output, it will show this value (of course, if the pulse frequency is high enough). In Arduino UNO, outputs D3, D5, D6, D9, D10 and D11 can work in PWM mode. Usually on the board they are marked with the signs "~" or the abbreviations "PWM". It should be noted that Arduino boards of other modifications may have more or less such outputs. In the simplest case, PWM can be used to control the brightness of an LED. This device is practically inertialess, but human vision has enough inertia that a sequence of fast LED flashes is perceived as a continuous glow with a duty factor-dependent brightness. PWM-capable digital outputs are configured to use this mode by default, so you do not need to call the pinMode() function to make them work in this mode. To set the duty cycle of a PWM signal, there is a standard analogWrite(N, M) function, where N is the pin number, M is a number proportional to the required duty cycle. It must be between 0 and 255, with 0 being zero duty cycle (constant low output), 255 being 100% duty cycle (constant high output). Timing diagrams of the output voltage for some values of M and, accordingly, the duty cycle Kz are shown in fig. 1.
For example, consider the table given in Table. 1 program that gradually increases the brightness of the LED connected to digital output D9, and then gradually decreases it. It is based on the examples3.AnalogFading standard example provided with the Arduino IDE. The enumeration of the values of the pulse duty cycle is implemented here with the help of the for loop operators already considered in [1]. Table 1. To receive analog signals from external devices, the Arduino has inputs A0-A5, which are set to the desired state by default, so no additional initialization is required. The ADC built into the Arduino UNO generates 10-bit binary codes and converts the input voltage ranging from 0 to +5 V to an integer from 0 to 1023 (210-1). The analogRead(N) function is used to read the conversion result, where N is the number of the analog input. A variety of sensors can be connected to the Arduino analog inputs, the output voltage of which is proportional to the measured value (variable resistors, thermistors, photoresistors, etc.). However, it must be remembered that only from 0 to +5 V can be applied to the analog input. If the output voltage of the sensor lies in a different interval or is of negative polarity, the signal must first be placed in the specified interval. The analog input is polled at a frequency of less than 10 kHz [2], which may not be sufficient to analyze some rapidly changing signals. The presence of analog inputs allows you to turn the Arduino into a simple digital voltmeter that measures a constant voltage from 0 to +5 V and transmits the measurement result to a computer. To do this, just download the program shown in Table 2 into the Arduino. XNUMX. Table 2 Please note that the constants in the program are the ADC reference voltage Uref (in millivolts) and the coefficient for converting the ADC output code into voltage Ku. The coefficient value is calculated by dividing the specified reference voltage by 1023. The coefficient is usually a fractional value, so the Ki constant is of type float (floating point number). The constant Uref has the same type for the correct calculation of the coefficient. Since there are only constants on the right side of the formula, it is not the microcontroller that calculates the coefficient when executing the program, but the compiler itself at the stage of its translation. All this allows you to increase the accuracy of the voltmeter by measuring the exact value of the reference voltage at the Uref pin of the Arduino board with a multimeter and writing it into the program by assigning the constant Uref. Other ways to improve the accuracy of analog-to-digital conversion can be found in [3, 4]. When the program in question is running, the TX LED blinks on the board, signaling the transfer of information through the serial port. The RX LED is off because the computer is not transmitting anything in response. The built-in Arduino IDE terminal displays the received information (Fig. 2) - the results of measuring the voltage of the galvanic battery 3332.
Arduino can give not only light, but also sound signals. To do this, it is necessary to connect a piezo sound emitter, for example ZP-1, to one of its outputs (Fig. 3).
To work with sound, a special function tone(N, F, T) is provided, where N is the pin number on which rectangular pulses will be generated; F - sound frequency, Hz; T - sound duration, ms. The last parameter is optional. In its absence, the sound will be continuous. To turn it off, the noTone(N) function is provided. Of course, a piezoceramic sound emitter can hardly be called a high-quality playback device, and the signal generated by the microcontroller has a rectangular shape, nevertheless, the use of these functions allows you to play simple melodies. An example is given in Table. 3. This is a slightly modified example 02.Digital oneMelody program included with the Arduino IDE. Since it is inconvenient to manually set the frequency of each note of the melody, the file pitches.h is connected to the program in its header using the #include directive. This operation is tantamount to including the full text of this file into the program. In this case, it contains a list of note names that can be played and their frequencies. Table 3 The sound emitter must be connected to output D8. For the program, a melody is a sequence of constants of the same type (frequency values), which are conveniently combined into an array - a numbered list of similar elements. When declaring an array, you must either enumerate all of its elements, or specify their total number. Note that the numbering of array elements always starts from zero. In this example, two arrays are used: int melody[] contains the names of the melody notes, int note Durations[] - their duration in milliseconds. To access an element of an array, its name is specified with a sequence number enclosed in square brackets. To be able to easily change the number of notes in a melody, it is calculated using the sizeof(V) functions, which return the number of bytes occupied by its argument (variable or their array) in the microcontroller's memory. In this case, the melody array is 16 bytes long and its int elements are two bytes long. Therefore, the Note variable gets the value 8, and that is how many times the body of the for loop will be repeated, playing the notes one by one. If you add several notes to the melody[] array, the Note value will change accordingly. Just remember to pad the noteDurations[] array with the durations of these notes. Since the melody is played once, all the operations necessary for this are placed inside the setup() function. To re-execute, you need to reset the microcontroller by pressing the RESET button on the Arduino board. The Arduino programs discussed in the article can be downloaded from ftp://ftp.radio.ru/pub/2016/09/aninout.zip. Literature
Author: D. Lekomtsev See other articles Section Radio amateur designer. Read and write useful comments on this article. Latest news of science and technology, new electronics: The world's tallest astronomical observatory opened
04.05.2024 Controlling objects using air currents
04.05.2024 Purebred dogs get sick no more often than purebred dogs
03.05.2024
Other interesting news: ▪ Road signs with E Ink displays ▪ Volkswagen Golf new generation ▪ Brain implant translates thoughts into words News feed of science and technology, new electronics
Interesting materials of the Free Technical Library: ▪ section of the site Electricity for beginners. Article selection ▪ Rosinante article. Popular expression ▪ article What are the similarities between a calendar and a deck of cards? Detailed answer ▪ article White root. Legends, cultivation, methods of application ▪ article Little heart on LEDs. Encyclopedia of radio electronics and electrical engineering ▪ article Monetary fantasy. Focus Secret
Leave your comment on this article: All languages of this page Home page | Library | Articles | Website map | Site Reviews www.diagram.com.ua |