digitalWrite() & how to use digitalWrite function in Arduino

What is the digitalWrite() function in Arduino, and what digitalWrite() 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 digital write in Arduino so let’s begin.

What is digitalWrite() function in Arduino??

So digitalWrite is basically an in-built function used by Arduino to drive a DC motor, stepper motor, servo motor, buzzer, speaker, glowing a led, displaying values of LCD or OLED display, etc. in Arduino you can use the digital pin as input or output. If you are using the digital pin as output then you have to use the digital write function. The digitalWrite function is used to drive or control an external devicedigitalWrite function makes digital pin HIGH & LOW which is used to make any sensor, module, or led turn ON or OFF. Digital pin configures the digital pin as HIGH & LOW. HIGH means 5v and LOW means 0v.

digital write

Digital input/output pins of Arduino are default set as inputs. If we have not set pins as OUTPUT in pin Mode() and we have connected an LED to a pin, then when we call the digital Write (11, HIGH) function inside the void loop, then the LED may appear dim. So without configuring pins in pin Mode(), digital Write() enabled the internal pull-up resistor, which acts like a large current-limiting resistor.

pinMode(11, OUTPUT)

It is mandatory to declare each and every digital pin of Arduino as output in pin mode.

In Arduino, we have 14 digital pins 0 to 13 so we can use the digital write function for digital pins only, we cannot use the digital write function for analog pins of Arduino. We usually connect the signal pin of the sensor to the digital pin of Arduino. We normally don’t use 0 & 1 pins as they are transmitter (TX) & receiver pins (RX) for serial communications. ATmega328P pins provide 40 mA (source/sink) to other external devices. This is enough current to turn an LED with a limiting resistor but not enough current to drive most motors. Devices or motors which draw more current can damage or destroy Arduino pins. To control high voltage high current motors we need some driver circuits to interface between Arduino and motor.

If the Arduino digital pin is configured as INPUT_PULLUP in pin mode, then it will invert the behavior of the INPUT mode, which means HIGH will acts as an OFF (led will off) and LOW will act as an ON (led will on). The digitalWrite() function simply creates an interface control for Arduino with external devices.

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

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 analog write(ledpin,125).

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.

Arduino digitalwrite

digitalWrite(pin, value);

The digital write 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. digital write does not return any values.

 digitalWrite(11, HIGH); 

 digitalWrite(11, LOW);  

Here 11 is the pin of the Arduino and HIGH is a keyword. Keywords are reserved words that are used by Arduino to perform tasks.

Here keyword HIGH means assigning 5v to that digital pin and keyword LOW means assigning 0v to that digital pin.

As we know in the digital systems 1 means HIGH and 0 means LOW. We can use these numeric values also in the digital write function

digitalWrite(11, 1); 

digitalWrite(11, 0);  

So we will look at one basic example of digital write, we have connected one LED to our digital pin which we will turn ON or turn OFF using the digital write function.

arduino digitalwrite led

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;  // set led to pin 11 of Arduino 
void setup()
{
  pinMode(LEDPIN, OUTPUT);     // declare led pin as output
  Serial.begin(115200);        // set a baud rate of 115200
}
void loop()
{
  digitalWrite(LEDPIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  Serial.println("LED turn ON");
  delay(1000);                       // wait for a 1 second
  digitalWrite(LEDPIN, LOW);    // turn the LED off by making the voltage LOW
  Serial.println("LED turn OFF");
  delay(1000);                       // wait for a 1 second
}

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.

In the void loop, we have a passed argument inside the digital write function that is pin number we are passing a variable name as LEDPIN which corresponds to pin no 11 of Arduino and keywords. Here we are continuously turning ON and turning OFF an LED with help of HIGH and LOW keywords. We have introduced a small delay of 1 sec.

digitalwrite function

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 when we have printed the status of the led as “LED ON” and “LED OFF”.

Conclusion –

Today we learn about Arduino digital write, where to use the digital write function, how to use the digital write function, what the digital write function does in Arduino, syntax, and the parameters of the digital write function.

"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