Verification: 52bc5e43554438b2
Arduino Basic Tutorial

Arduino Basic Tutorial: Analog output pins in Arduino

Description:

Analog output pins are a feature of the Arduino microcontroller board that allows it to output an analog signal, or a continuously variable voltage, to control devices such as motors or lights. So In this article, we will learn about what is an analog output and which pins are used as an analog output. so in this article, we will discuss the following topics

Arduino analog output pin

PWM (pulse modulation)

mode setting

analog output




Amazon Links:

Arduino Uno:

Mega 2560:

Arduino Nano

12v Adaptor:

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

What is analog output?

Analog output is a continuous signal that varies over a certain range. It is typically used to represent physical quantities such as temperature, pressure, or voltage. In contrast to digital output, which can only take on discrete values, analog output can take on any value within a certain range. For example, an analog output might be used to control the position of a servo motor or to adjust the volume of a speaker. Analog output is typically produced by devices such as sensors or analog-to-digital converters, and it is often used in control systems and other applications where precise, continuous control is required.



Arduino analog output pins

The external view of Arduino UNO R3 is shown below. Digital 3, 5, 6, 9, 10, 11 are analog output pins. As you can see in the below image

arduino analog output pins

Even though it’s an analog output pin, you can output any voltage to the analog pins!

It does not mean that···.

Because the UNO’s output pin does not support analog output. It is just a pseudo-analog output. That method is PWM (Pulse Width Modulation).



PWM (pulse modulation)

PWM is a method that creates a pseudo voltage between 0V and 5V (0V and 3.3V) while periodically switching between 0V and 5V (0V and 3.3V).

For example, it is used when you want to change the brightness of the LED.

Suppose we have a waveform that repeats periodically. The longer the period at 5V, the brighter the LED. If the period of 0V is long, the LED lights dimly.

In fact, it only changes the time the LED is energized, not the voltage. However, to the human eye, the brightness appears different. A waveform example is shown below.

1/500 second is the time conversion of PWM frequency 490Hz.
*Actually 1/490 second, about 2 ms (1/500)

arduino analog output pins

For example, the middle waveform above has 50% ON time and 50% OFF time. By averaging this voltage, 50% voltage can be obtained as shown below.

5V x 0.5 + 0V x 0.5 = 2.5V

In this way, pseudo analog output is produced by changing the ratio of ON time and OFF time. The ratio of this ON/OFF time is called duty.



Arduino Analog Output pin mode setting

When using analog output, it is necessary to first specify the output with pinMode().

pinMode (pin_number, mode)

Pin_number: Specify the pin number to be used

Mode: Output if OUTPUT is specified, input if INPUT is specified

Example Specify digital 3 (pin number 3) as an output pin.

pinMode( 3, OUTPUT );

Setting pinMode() can be omitted when analogWrite() is used.

analog output PWM frequency

Analog output corresponds to digital 3, 5, 6, 9, 10, and 11 pins are shown in the above image. From the specified pin, you can output the ” power supply voltage ” and ” 0V ” PWM signal to be used.
The power supply voltage is 5V, 3.3V Also, the PWM frequency is as follows depending on the pin.

Pin number (digital) PWM frequency [Hz]
3,9,10,11 490 (about 0.002 seconds ⇒ about 2 ms)
5,6 980 (about 0.001 seconds ⇒ about 1 ms)

By connecting the parts that you want to operate to each pin, you can control them by giving PWM signals. The control commands to be used are as follows.

analogWrite (pin_number, value)

Pin_number: Specify the pin number to be used

Value: Duty ratio (0 to 255 * integer)

Unfortunately, the value part is 0-255 instead of %. The correspondence is as follows.

%display value (duty ratio)
0 0
50 128
100 255




If you want to calculate it, you can calculate it as follows. *For 5V

If you want to find the value of the argument from the voltage you will use the following formula

255 ÷ 5V × Voltage you want to output [V]

*Round the calculated value to an integer

If you want to obtain the argument value from the output ratio [%]

255 × output ratio [%] × 100

*Round the calculated value to an integer

Example Output 2V to digital 3 (pin number 3).

Argument calculation: 255 ÷ 5V × 2V = 102

analogWrite( 3, 102 );



Here is a simple sample code.

//sample code

int OutPutPWM_pin = 3 ;

 int i = 0 ;

 float V_Out = 2 ; 

void setup(){

pinMode( OutPutPWM_pin, OUTPUT );

i = 255 / 5 * ( int )V_Out;

}

void loop(){

analogWrite( OutPutPWM_pin, i );

}

Feel free to comment if you have any questions or concerns.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button