Arduino Basic Tutorial: What is digital output and digital input?
Description:
In Today’s article, I would like to talk about Arduino’s digital output and digital input. In this article, we will discuss the following topics
- Arduino Digital Output and Digital Input Pin
- mode setting
- Arduino digital output
- Arduino digital input
Amazon Links:
*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!
Arduino Digital Output and Digital Input Pin
The external view of Arduino UNO R3 is shown below.
Digital 0 to digital 13 is shown on the right side of the board are the digital output and digital input pins.
Digital generally refers to binary values of 1 and 0.
In Arduino, the equivalent of “1” is the high voltage ” 5V ” and is expressed as ” High “.
Also, the equivalent of “0” is the low voltage ” 0V ” and is expressed as ” Low “.
Arduino mode setting
Digital output and digital input are assigned to the same pin.
How do you distinguish between output and input even though they are on the same pin? I think so.
The following command appears there.
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 13 (pin number 13) as an output pin.
pinMode( 13, OUTPUT );
Arduino digital output
The digital output corresponding to the terminals from digital 0 to 13 is shown in the external view.
” 5V ” and ” 0V ” can be output from the specified pin .
By connecting the parts you want to operate to each terminal, you can control by giving “5V” and “0V”.
For example, it is possible to control LEDs to turn on and off, motor rotation, and stop.
The control commands to be used are as follows.
digitalWrite(pin_number, value)
Pin_number: Specify the pin number to use
Value: Specify HIGH or LOW.
HIGH means 5V, LOW means 0V output.
Example Output 5V to digital 13 (pin number 13).
digitalWrite( 13, HIGH );
In my upcoming lessons, I will explain this function by controlling the blinking of the Leds using digitalWrite().
Arduino digital input
As with digital output, digital input corresponds to digital terminals 0 to 13 in the external view.
Determines whether the voltage applied to the specified pin number is ” 5V ” or ” 0V “.
As a result, “HIGH” and “LOW” are returned as return values.
*Actually, even if the voltage is lower than 5V or higher than 0V, “HIGH” and “LOW” are judged.
This is because the Arduino itself has a level for judging, and if it is higher than that, it judges it as “HIGH”, and if it is lower than it, it judges it as “LOW”.
Generally, it is called “threshold”
In the case of Arduino, the threshold for judging HIGH is set to approximately 2.7V, and the threshold for judging LOW is set to approximately 2.1V. Therefore, if it is higher than 2.7V, it is “HGIH”, and if it is lower than 2.1V, it is “LOW”. What is between 2.7V and 2.1V? However, when setting the threshold, add hysteresis to avoid unstable operation.
Hysteresis will be explained on another occasion. A possible use case would be to know if a switch is on or off. I want to know the status of the external equipment!
Moreover, the judgment is enough with two values of high or low! It means to use it when you say.
The control commands to be used are as follows.
digitalRead(pin_number)
Pin_number: Specify the pin number you want to read the value
Return value: HIGH or LOW
Example Output 5V to digital 13 (pin_number 13).
Since this is a read command, a variable to store the read value is required.
Here it is val.
int defines an integer type and the range of possible values is -32768 to 32767.
int val;
val = digitalRead(13);