Menu English Ukrainian russian Home

Free technical library for hobbyists and professionals Free technical library


ENCYCLOPEDIA OF RADIO ELECTRONICS AND ELECTRICAL ENGINEERING
Free library / Schemes of radio-electronic and electrical devices

Arduino. Operations of analog input-output, work with a sound. Encyclopedia of radio electronics and electrical engineering

Free technical library

Encyclopedia of radio electronics and electrical engineering / Radio amateur designer

Comments on the article Comments on the article

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.

Arduino. Analog I/O Operations, Audio Operations
Rice. 1. Timing diagrams of the output voltage

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.

Arduino. Analog I/O Operations, Audio Operations

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

Arduino. Analog I/O Operations, Audio Operations

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. Analog I/O Operations, Audio Operations
Rice. 2. Program window

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).

Arduino. Analog I/O Operations, Audio Operations
Rice. 3. Connecting a piezo sound emitter

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

Arduino. Analog I/O Operations, Audio Operations

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

  1. Lekomtsev D. Arduino. Digital I/O operations. - Radio, 2016, No. 8, p. 51-54.
  2. Analog measurements with Arduino. - URL: robotosha.ru/arduino/analog-measurements-arduino.html.
  3. Arduino Language Reference. Analog I/O - analogReference(). - URL: arduino.cc/en/Reference/AnalogReference.
  4. analogReference() function. - URL: arduino.ru/Reference/AnalogReference.

Author: D. Lekomtsev

See other articles Section Radio amateur designer.

Read and write useful comments on this article.

<< Back

Latest news of science and technology, new electronics:

The world's tallest astronomical observatory opened 04.05.2024

Exploring space and its mysteries is a task that attracts the attention of astronomers from all over the world. In the fresh air of the high mountains, far from city light pollution, the stars and planets reveal their secrets with greater clarity. A new page is opening in the history of astronomy with the opening of the world's highest astronomical observatory - the Atacama Observatory of the University of Tokyo. The Atacama Observatory, located at an altitude of 5640 meters above sea level, opens up new opportunities for astronomers in the study of space. This site has become the highest location for a ground-based telescope, providing researchers with a unique tool for studying infrared waves in the Universe. Although the high altitude location provides clearer skies and less interference from the atmosphere, building an observatory on a high mountain poses enormous difficulties and challenges. However, despite the difficulties, the new observatory opens up broad research prospects for astronomers. ... >>

Controlling objects using air currents 04.05.2024

The development of robotics continues to open up new prospects for us in the field of automation and control of various objects. Recently, Finnish scientists presented an innovative approach to controlling humanoid robots using air currents. This method promises to revolutionize the way objects are manipulated and open new horizons in the field of robotics. The idea of ​​controlling objects using air currents is not new, but until recently, implementing such concepts remained a challenge. Finnish researchers have developed an innovative method that allows robots to manipulate objects using special air jets as "air fingers". The air flow control algorithm, developed by a team of specialists, is based on a thorough study of the movement of objects in the air flow. The air jet control system, carried out using special motors, allows you to direct objects without resorting to physical ... >>

Purebred dogs get sick no more often than purebred dogs 03.05.2024

Caring for the health of our pets is an important aspect of the life of every dog ​​owner. However, there is a common assumption that purebred dogs are more susceptible to diseases compared to mixed dogs. New research led by researchers at the Texas School of Veterinary Medicine and Biomedical Sciences brings new perspective to this question. A study conducted by the Dog Aging Project (DAP) of more than 27 companion dogs found that purebred and mixed dogs were generally equally likely to experience various diseases. Although some breeds may be more susceptible to certain diseases, the overall diagnosis rate is virtually the same between both groups. The Dog Aging Project's chief veterinarian, Dr. Keith Creevy, notes that there are several well-known diseases that are more common in certain breeds of dogs, which supports the notion that purebred dogs are more susceptible to disease. ... >>

Random news from the Archive

Eco-friendly plastic from fish waste 10.04.2021

Bioplastics could be used for almost the same purposes as conventional polyurethane, says team leader Francesca Kerton of Memorial University of Newfoundland (MUN).

Plastic based on fish waste is environmentally friendly. In addition, the invention solves the problem of recycling these food waste. After all, fish heads, bones, skin and intestines, usually sent to landfill, can be turned into a harmless, biodegradable material.

The novelty will replace polyurethane, which today can be found everywhere: in shoes, clothes, refrigerators, building materials, etc. At the same time, polyurethane is obtained from crude oil and phosgene, it has a large carbon footprint and slowly decays.

To produce the new material, the researchers used fat extracted from pieces of salmon left over from industrial processing.

Chemists have developed a method for converting fish oil into a polyurethane-like polymer. First, they added oxygen to the fat to form epoxides. The epoxides were then combined with carbon dioxide. And the resulting molecules were combined with amines containing nitrogen, resulting in a new material.

The new plastic has no fish smell.

Other interesting news:

▪ Road signs with E Ink displays

▪ Volkswagen Golf new generation

▪ Solar power plant on a roll

▪ Brain implant translates thoughts into words

▪ The coldest place in space

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:

Name:


Email (optional):


A comment:





All languages ​​of this page

Home page | Library | Articles | Website map | Site Reviews

www.diagram.com.ua

www.diagram.com.ua
2000-2024