How to use digitalWrite Arduino Command

Hey Friends! Hope you are doing well. Today, I'll discuss the details on How to use digitalWrite Arduino Command. The digitalWrite command in Arduino is used for writing the status of the digital Pin. The Pin assigned to this command must be an OUTPUT pin so that it can transfer data to other components like LEDs, motors, and actuators and use them as per your needs and requirements. Arduino boards have always been a great choice for both experts and newbies, as they come with built-in peripheral functions, and no need of external components is required to drive automation and develop some basic functions on the relevant project. Basic Arduino Software is used as a compiler and no separate burner is required to burn the required code into the board. You can simply plug the board with the computer through a USB cable and burn and compile the program by a single click on the software.

How to use digitalWrite Arduino Command

  • digitalWrite Arduino Command is used to write the status of digital Pins, and can make them either HIGH or LOW. The Pin needs to be an OUTPUT Pin.
  • We are working on Arduino UNO, that comes with 14 digital I/O pins and 6 analog pins. You can set these pins as an input or output using pinMode based on your technical requirements.
  • These pins are labeled as 0 to 13 on the board including two serial pins called Tx and Rx appearing at the start and marked as 1 and 0 pins respectively.
  • We set Pins as an INPUT when we want to read or receive data from some component like sensors. And making pins as an OUTPUT will require a digitalWrite function to write or send data to the required device like LEDs, motors etc.
  • When any pin is set an as OUTPUT using the pinMode Arduino Command, it will appear as HIGH or LOW depending on the voltage applying on the pin. For voltage above 3.3V it will appear as HIGH and for GND (ground) it will appear as LOW on the software.
  • Before connecting an LED to pin, make sure to set pinMode as OUTPUT when you call the digitalWrite function, otherwise LED won't be flashing with clear bright light.
Syntax for digitalWrite Arduino
  • Here’s the syntax used for digitalWrite Arduino command:

digitalWrite(pin, value);

where:
  • The "pin" defines the Arduino pin number used. It has to be an OUTPUT Pin.
  • And "value" defines if the pin will be HIGH or LOW.
  • For example:

digitalWrite(8, HIGH);

Note: 
  • It is worth mentioning here that digitalWrite command doesn’t store or return any value unlike most of the commands used in Arduino that help in storing some value.
Difference between analogWrite and digitalWrite
Both terms analogWrite and digitalWrite are used for same purpose i.e. sending data from Arduino, but:
  • The former is used to send data in analog form i.e. 0V - 5V.
  • The later only defines the HIGH and LOW value of the desired pin where HIGH is an indication, pin is getting 5V and LOW is an indication pin is set to ground or zero voltage.
That's all for today. I hope you have found this article informative. If you are unsure or have any question, you can ask me in the comment section below. I'd love to help you according to the best of my expertise. You are most welcome to keep us updated with your valuable feedback and suggestion - they help us provide you quality work so you keep coming back every now and then. Thanks for reading the article.

How to use pinMode Arduino Command

Hi Friends! Hope you are doing well. In this post, I'll uncover the details on How to use pinMode Arduino Command. The pinMode defines the Arduino Pins, if they are used as an input or output. The INPUT_PULLUP is another option achieved by pinMode, that is mainly used to place a virtual pull-up resistor to the input pins. We have started Arduino Tutorials for Beginners quite a while now for the newbies, who are really interested to get a hands-on experience with Arduino. Generally, Arduino is known as a Microcontroller, but it is a step ahead of it. The PIC microcontrollers require some basic circuit to start with but Arduino brings revolution in the automation industry by removing the need of developing any basic circuit. Although Atmega328 is the Microcontroller used in Arduino UNO. Also, the burner is required to burn the program in PIC Microcontrollers, while there is no need to connect separate burner with the Arduino - simply plug it with the computer through a USB cable and start playing with it. In this tutorial, we will thoroughly discuss what is pinMode and how to use it in the Arduino module. Let's jump right in and explore what is this about and everything you need to know.

How to use pinMode in Arduino

  • The Arduino Board comes with GPIO (general purpose input output) pins that can be used in two ways i.e. input, output.
  • pinMode Arduino Command is used to define the operation of these Input/output pins, there are three types of modes that can be assigned using this command and are named as:
    • OUTPUT.
    • INPUT.
    • INPUT_PULLUP.
  • There are 14 digital and 6 analog pins in the module that mainly depend on the pinMode for setting up their mode of operation as an input or output.
  • In this post we mainly discuss the Arduino UNO, that is based on ATmega328 microcontroller, however, you can use other modules like Pro Mini, Mega or Leonardo as per your needs and requirements. The pinMode works same in the module no matter what type of Arduino version you are using.
Syntax for pinMode Arduino
Here's the syntax for our pinMode Arduino command:

pinMode(pin#, mode);

where:
  • Pin defines the Arduino pin number used.
  • There are three types of modes that can be assigned to pins of Arduino, which are:
    • OUTPUT
    • INPUT
    • INPUT_PULLUP
Let's use Pin # 8 of Arduino and assign all possible modes to it:

pinMode(8, OUTPUT);

pinMode(8, INPUT);

pinMode(8, INPUT_PULLUP);

Note: 
  • It is important to note that, unlike most of the functions used in the C code for Arduino module, this pinMode doesn't store or return any value.
  • You have to use any one of these three modes at a time.
Modes of pinMode Arduino
  • In the previous section, we have discussed the basic syntax of pinMode, and I hope you have pretty much got the basic idea behind it.
  • The only thing worth mentioning here is the difference between INPUT and INPUT_PULLUP.
  • So, here's a simple code where I have made Pin # 8 as an INPUT and read its status on Serial Monitor.
int Pin = 8;

int Status = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(Pin, INPUT);          
}

void loop()
{
  Status = digitalRead(Pin);       
  
  if(Status == HIGH)
  {
    Serial.println("HIGH");
  }

  if(Status == LOW)
  {
    Serial.println("LOW");
  }
 
}
  • Let's have a look at the Serial Monitor:
  • While taking the above image, Pin # 8 was in open state and we are getting just random values.
  • We are getting these random values i.e. HIGH, LOW because our Pin#8 is neither connected to +5V nor GND.
  • Arduino seems confused here, and we can remove this confusion by simply changing INPUT to INPUT_PULLUP.
  • As we run the Serial Monitor, we will get something shown below:
  • You can see how we are getting HIGH value only, while the pin is still in open state.
  • We can conclude, when we have nothing on our INPUT pin then INPUT_PULLUP will make the pin HIGH.
Difference between Read and Write
There are two ways to send or receive data. You can either define the pin as an input that helps in reading the data from an external device like sensors. Or you can define pin as an output that helps in writing and sending a command to LEDs, motors or actuators for executing the desired functions. That's all for today. I hope you have found this post informative as per your needs and requirements and can easily use this pinMode Arduino Command. If you are feeling skeptical or have any question, you can ask me in the comment section below. I'll try and help you according to the best of my expertise. Thanks for reading the article.
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir