This article was translated to English, and was originally published for deviceplus.jp.
With digital input, 5V can be read as HIGH and 0V as LOW. You can determine whether a switch is on based on which state it is in. However, electricity is not only 5V and 0V, but various voltages. For example, one AA battery is 1.5V, and a household outlet is 100V.
Arduino supports analog inputs to read these various voltages. Analog inputs enable you to read the state of electronic components that change gradually. For example, you can use volume to adjust brightness or loudness or a temperature sensor to get the current temperature.
This article is the last in the series. We will explain analog input using volume.
・Converting analog values to digital values
・Analog input in Arduino
・”Volume” that can be used to adjust voltage
・Analog input with volume
Actual voltage is not an exact value such as 1V or 5V, but an irrational number, in which numerical values continue forever after the decimal point, such as 5.01342…V. These values cannot be directly handled by a computer. In electronic circuits, therefore, analog values are converted to digital values using a mechanism called AD converter so that the value can be handled by a computer. Arduino also has an AD converter by default, which can read analog voltages and convert them into digital values.
The AD converter divides the target voltage into several equal parts. For Arduino Uno, voltages can be read in the range of 0-5V. These 0-5V values are divided into 1023 equal parts. The AD converter converts the analog voltage reading to the nearest equal value. If the voltage is 3V, for example, “614” is the closest value. This value can be sent to Arduino for use.
In the program, integer values up to 1023 are used to process the program. However, the input value is not a voltage value. If you want to know the voltage value, you can calculate it as follows:
For example, if the value from the AD converter is “614”, you can obtain about 3V by dividing by 1023 and then multiplying by 5.
With Arduino Uno, you can use six pins for digital input, A0 to A5. By connecting electronic components here for the input, you can read how much voltage is applied. However, pins A4 and A5 are also used for digital communication called I2C. Note that these pins cannot be used for analog input when connecting electronic components such as sensors or displays that use I2C digital communication.
“Volume” is an electronic part that can be used to adjust the sound volume or the lighting brightness. The volume contains a resistive element and terminals moving on the resistive element. The resistance can be changed by moving these terminals.
The volume generally has three terminals. The left and right terminals are attached to both ends of the resistor. The center terminal is designed to move on the resistive element. If you move the center terminal to the right, the resistance between the left and center terminals increases. Conversely, if you move it to the left, the resistance decreases.
In the figure above, the resistance element is linear, but in the rotary volume, the internal resistance element is circular. Although the shapes are different, they basically operate the same way.
The Arduino analog input uses the voltage change for the input. Since the resistance changes in the volume, however, it is not possible to read the volume change by connecting it directly to Arduino. Therefore, the resistance change should be converted to a voltage change. Connect the power supply and GND to the left and right terminals of the resistor. The voltage at the center terminal changes depending on the terminal position. Assume that the left terminal is connected to GND, and the right terminal is connected to a power supply (5V). The voltage increases as the center terminal moves to the right, and it decreases as the terminal moves to the left. The left end is 0V and the right end is 5V, so the value can be changed within the range of 0 to 5V.
If this center terminal is connected to the Arduino analog input terminal, the input can be changed by moving the volume.
Now let’s try analog input using the volume. Prepare the following electronic components.
The volume pod contains the resistance value of the range that can be changed. It is good to use a 10kΩ volume for the Arduino input. The volume will be easier to move, if a knob is attached to the rotation axis of the volume.
Useful for attaching a volume pod. Pinch the volume terminals with the clip-shaped connectors. The linked product is available as a set of two red lines and two black lines.
When you have prepared the components, connect them as shown in the figure below. Since the volume pod cannot be plugged into the breadboard, use an alligator-clip jumper line to connect it directly to Arduino. Make sure that the metal part of the alligator clip does not touch the adjacent clip part.
Write the following program:
To check the state where the analog input pin is connected, specify the target pin in “analogRead()” (line 11). Then, input the voltage of the pin with the value of 0 to 1023. If you want to know the voltage, it is calculated and converted to voltage as in line 13.
Write the program to Arduino and display the serial monitor.
The value digitized by the AD converter is displayed after “Value:”, and the value calculated and converted into voltage is displayed after “Volt:”. You can see that the value changes as the volume is rotated.
In this series, we introduced the usage underlying Arduino. Try operating a variety of electronic components and creating programs based on the content explained in this series. Thank you very much for following this half-year series to the end!