What is analogWrite() function in Arduino??

What is the analogWrite() function in Arduino, how to use analogWrite() & what analogWrite() function does in Arduino programming are some questions that may arise in your mind while working with Arduino.

Hello guys! Today we going to see about analog write in Arduino so let’s begin.

What is AnalogWrite()??

In the last chapter, we learn about digitalWrite(), where we can only give commands 0v or 5v (HIGH or LOW) to other connected devices to Arduino means we can only turn ON or turn OFF. analogWrite is nothing to do with analog pins of Arduino. analogWrite() functions are used in cases where we have to control the brightness of the led, how fast objects are moving, pressures, motor speed control motors in cars, etc. these pins which give accessibility to use this functionality are called PWM (pulse width modulation) pins. PWM pins give accessibility to control and adjust output power.

analog write

In Arduino PWM pins are represented by the ~ symbol on the Arduino board. All pins of Arduino do not have PWM properties only digital pin 3,5,6,10,11 are PWM pin in Arduino. We don’t have liberty or freedom to control its pin output power at what value we want to set it. So it's analogWrite we can give any value ranging from 0 to 255 inside analogWrite() as a parameter. analogWrite corresponds to the duty cycle means power deliver to the load.

analogwrite example

analogWrite(pin, value)

analogWrite function has two parameters. To execute this function we have to pass arguments in it.

Arguments are non-thing but actually real value that we are passing in a function. analogWrite does not return any values.

 analogWrite(11, 255) 

 analogWrite(11, 0)  

Here 11 is the pin of the Arduino and 255 is an integer value.

Here integer value 255 means assigning 5v to that digital pin and integer value 0 means assigning 0v to that digital pin.

We can assign any integer value between 0 to 255 which represents different voltages levels. By calculating the duty cycle we can set our desired voltages level of 1.2v, 3v, 3.6v, etc. through different integer values corresponding to it that will be passed through analogWrite(). 

 

analogwrite pwm

If the power deliver to the load is less then less power will be given to the load to operate. If the power delivered to the load is more then more power will be given to the load to operate. The duty cycle is refer to the width of the pulse to drive the output. The duty cycle is corresponding to an integer value from 1 to 1023 which will be a parameter for the analogWrite() function.

For example 25% duty cycle means 25/100 * 255 = 64 means integer 64 value will be pass through analogWrite(ledpin, 64).

 For 50% duty cycle means 50/100 * 255 = 127 means integer 127 value will be pass through analogWrite(ledpin, 127).

For 75% duty cycle means 75/100 * 255 = 191 means integer 191 value will be pass through analogWrite(ledpin, 191).

What is the difference between digitalWrite() and analogWrite() in Arduino??

digitalWrite() is used with digital pins (0 to 13) of Arduino to configure these pins for an output operation. digitalWrite() only can drive the output ON or OFF, which means HIGH or LOW. We cannot have access to control the output power. In digitalWrite() we can assign only 5v (ON) or 0v (OFF). digitalWrite(ledpin, HIGH) is considered as turn ON or logic 1 whereas digitalWrite(ledpin, LOW) is considered as turn OFF or logic 0.

analogWrite() has no relation with Arduino analog pins. analogWrite() functions are not accessible by all digital pins. Only a digital pin having PWM output can access the analogWrite() function. the digital pin having the ~ symbol on Arduino boards is the PWM pin. By using analogWrite() we can easily controlled the output power between 0v to 5v example 1.5v or 2.5v etc. whereas in digitalWrite() we can only set the output voltage 1 (5v) or HIGH and 0(0v) or LOW. analogWrite() takes 0 to 255 integer values which is represented from 0 to 5v. 0v means 0 and 5v means 255. If we want to set output voltage means power deliver to the load 2.5v means we have to pass 128 in analogWrite(ledpin, 125).

Simple Automatic LED Light Dimmer using Arduino

analogwrite Arduino

Here is a schematic of the circuit. We have connected the LED at pin number 11 of our Arduino board & negative terminal to the Ground.

Code - 

int Ledpin = 11;
void setup() 
{
 pinMode(Ledpin,OUTPUT);
 Serial.begin(115200);           
}
void loop() 
{
  for (int i=0;i<=255;i++)
  {
    analogWrite(Ledpin,i);
    Serial.println(i);
    delay(50);
  }
  for (int j=255;j>=0;j--)
  {
    analogWrite(Ledpin,j);
    Serial.println(j);
    delay(50);
  }
}

Here we have declared a led pin which is connected at pin number 11 of Arduino. We have declared a baud rate of 115200 and LED pin as OUTPUT.

We want to create an automatic LED dimmer so in the void loop we have to use for-loop. As we know analogWrite() function takes 0-255 integer values which represent the duty cycle (power delivered to the load) in our caseload is LED. For loop will provide values from 0 to 255 in an increment manner which turns LED ON from fade to bright. For making LED bright to fade we have used another for loop in a decrement manner which will provide integer values from 255 to 0.

Light dimmer using the push button and Arduino

light dimmer Arduino

LED dimmer Arduino
This image is created using fritzing


Code - 

int brightness_button = 8;
int Fade_button = 9;
int ledpin=11;
int bright_Value;
int fade_Value;
int brightness = 0;



void setup() {
  pinMode(brightness_button,INPUT);
  pinMode(Fade_button,INPUT);
  pinMode(ledpin,OUTPUT);
  Serial.begin(115200);

}

void loop() {
  bright_Value = digitalRead(brightness_button);
  fade_Value = digitalRead(Fade_button);

if (bright_Value==HIGH)
{
  delay(100);
brightness = brightness + 5; 
if (brightness >= 255)
{
 brightness=255; 
}
Serial.print("brightness Level : ");
Serial.println(brightness);
 analogWrite(ledpin,brightness); 
// brightness++;
}

 if (fade_Value==HIGH) 
{
    delay(100);
    
brightness = brightness - 5; 
if (brightness <= 0)
{
 brightness = 0; 
}
    Serial.print("brightness Level : ");
    Serial.println(brightness);
  analogWrite(ledpin,brightness); 
//  brightness--;
}

 
}

Here we have declared a led pin and two push button which is connected at pin number 11, pin 9, and pin 8 of Arduino. We have initialized two variables for storing the status of two buttons. we have declared the brightness value as 0 by default. We have declared a baud rate of 115200, two push buttons as INPUT, and an LED pin as OUTPUT. 

We want to create an LED Light dimmer using a push button so in the void loop we store the value of the brightness button in the variable name bright value and the fade button in the variable name fade value. If the bright button value is HIGH then we will add +5 in brightness. Initially, we have set the value of brightness as 0 so when we press the brightness button the first time the brightness value will be brightness = 0 + 5 = 5, when we press the brightness button a second time the value of brightness which was updated as 5 previously will become brightness = 5+5 =10 when we again press the brightness button the third time the value of brightness which was updated as 10 previously will become brightness = 10 + 5 = 15. In such a way, the brightness value will be passed in the analogWrite() function such as analogWrite(ledpin, brightness).

When we want to fade the LED then we will same logic instead of incrementing the value of brightness by 5, we will decrement it by -5. When we press the fade button the first time the brightness value will be brightness = 50 - 5 = 45. When we press the fade button a second time the value of brightness which was updated as 45 previously will become brightness = 45 - 5 = 40. In this way, we can increase and decrease the brightness level of an LED.

Light dimmer using potentiometer and Arduino

light dimmer potentiometer

Code - 

int potentiometer_pin = A0;
int potnetiometer_value;
int Ledpin = 8;
int brightness;
void setup()
{
 Serial.begin(115200);
 pinMode(potentiometer_pin,INPUT);
 pinMode(Ledpin,OUTPUT);
}

void loop() 
{
 potnetiometer_value = analogRead(potentiometer_pin);
 // map(value, from low, from high, to low, to high);
  int brightness = map(potnetiometer_value,0,1023,0,255);
  analogWrite(Ledpin,brightness);
  Serial.print("Brightness Levels : ");
 Serial.println(brightness);
 delay(1000);
}

Output - 

LED Brightness Arduino

Now we used the serial monitor to observe the output. To open up the serial monitor, click on Tools > Serial Monitor or use the shortcut (SHIFT + CONTROL + M) You can see LED brightness changes according to the rotating knob of the potentiometer. here we have displayed brightness for a user to view the brightness level. You can see when there is the value of brightness level increases and decreases according to the rotation of knob of potentiometer”. the rotation of the knob can be the clockwise or anti-clockwise direction.

Conclusion –

Today we learn about analogWrite() function in Arduino, where to use analogWrite() function, how to use analogWrite(), what analogWrite() function does in Arduino, syntax and parameters about analogWrite() function. analogWrite() is a very powerful function as it gives users access to control output power. we have also seen some applications of analogWrite() we made a light dimmer using a potentiometer and push buttons.


"I hope you find this IoT blog very helpful to you. In the upcoming lesson, we will see more about IoT sensors till then bye. See you all in my next blog."

Close Menu