Arduino

How to Use AnalogWrite() Function | Arduino Reference

To operate different devices with Arduino there are different functions available that can be used to program the microcontroller. We can call such functions as input and output functions as they play a crucial role in controlling the devices attached to the Arduino board. One of these functions is the AnalogWrite() function and we have discussed the functionality of the function briefly in this guide.

What is analogWrite function

From the function name we can assume that it writes some value, and this value will be in the range of 0 to 255. In other words, we can say that this function is mainly used to control any analog devices attached to the Arduino by assigning value to the analog pin of Arduino to which that respective device is attached.

The range 0 to 255 is the duty cycle of the square wave generated for the analog devices or in other words we can say that the resolution for analogWrite() function is 8 bits. For using this function, we have to follow the syntax given below:

analogWrite(pin, value, frequency);

To use the analogWrite() function there are mainly three arguments:

Pin: The digital pin number of Arduino on which the device is connected.

Value: The value that is to be assigned to the pin of Arduino either HIGH or LOW.

Frequency: This is an optional argument for the analogWrite() function through which we can give frequency of the wave form and by default the frequency of the square wave is 500Hz.

How we can use analogWrite() function in Arduino

Using the analogwrite() function, we can control almost every analog device by connecting it with an Arduino board. To demonstrate how we can use this function in Arduino we have given some examples of how this function can be used effectively to control the analog devices.

Controlling the brightness of LED using the analogWrite() function

We can use the analogWrite() function to control the brightness of the LED by assigning its pin the duty cycle value which will in turn either increase the value of brightness or decrease the brightness of the LED. So to control the LED brightness we have decremented the value of 5 from 255  till the value becomes zero. So, we have given the Arduino code below which change  the brightness of the LED using the analogWrite() function:

int led = 3;    //  Arduino pin for LED
int value = 0;  // variable that will store the brightness value
int brightness_value  = 5;  // variable in which have maximum value of brightness
void setup() {
 // working mode for LED
  pinMode(led, OUTPUT);
}
void loop() {
  // giving the LED the brightness value
    analogWrite(led, value);
// at each iteration add the value of brightness to the maximum brightness  
  value = value + brightness_value;
// if the value is between the duty cycle then decrement in the maximum brightness of LED
  if if (value <= 0 || value >= 255) {
    brightness_value = -brightness_value;
  }
  delay(30);
}

The operation of above code can be seen below:

Controlling the speed of dc motor using digitalWrite() function using potentiometer

Another device that we can control using the analogWrite() function is the speed of the DC motor and we have controlled it by giving it the values using the potentiometer.  To give an idea of how we can control the speed of DC motor using the analogwrite() function we have given the Arduino code below:

int POT;/* To store the potentiometer value */
int value;/* to store the scalarized value of for 10 bits to 8 bits resolution */
 void setup()  
  {    
   pinMode(A1,INPUT); /* working mode for potentiometer*/  
   pinMode(A0,OUTPUT);  /* working mode of motor */
  }  
  void loop()  
  {  
  POT=analogRead(A1); /* getting the value of potentiometer*/
  value=map(POT,0,1024,0,255);   /* changing the data resolution from 10 bit to 8 bit */
  analogWrite(A0,value); /* giving the duty cycle value to the motor */
  }

To control the motor speed using the potentiometer we have first converted the values of the potentiometer that range from 0 to 1023 to range from 0 to 255 using the map function. Next, we have given the scalarized value to the DC motor and in this way, we have controlled the speed of the motor.

The operation of above code can be seen below:

Conclusion

To configure the analog devices with Arduino there are some dedicated analog functions that can be used for this purpose. One of the analog functions is the analogWrite() function that is mainly used to assign the values to the analog devices. So, we have described the functionality of the analogWrite() function in detail along with the two examples that show how you can use the analogWrite() function for the analog devices.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.