Arduino 89
Arduino 89
If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the
corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW.
If the pin is configured as an INPUT, digitalWrite() will enable (HIGH) or disable (LOW) the
internal pullup on the input pin. It is recommended to set the pinMode() to INPUT_PULLUP to
enable the internal pull-up resistor. See the Digital Pins tutorial for more information.
If you do not set the pinMode() to OUTPUT, and connect an LED to a pin, when calling
digitalWrite(HIGH), the LED may appear dim. Without explicitly setting pinMode(), digitalWrite()
will have enabled the internal pull-up resistor, which acts like a large current-limiting resistor.
Syntax
digitalWrite(pin, value)
Parameters
pin: the Arduino pin number.
value: HIGH or LOW.
Returns
Nothing
Example Code
The code makes the digital pin 13 an OUTPUT and toggles it by alternating between HIGH and
LOW at one second pace.
void setup() {
pinMode(13, OUTPUT); // sets the digital pin 13 as output
}
void loop() {
digitalWrite(13, HIGH); // sets the digital pin 13 on
delay(1000); // waits for a second
digitalWrite(13, LOW); // sets the digital pin 13 off
delay(1000); // waits for a second
}
Notes and Warnings
The analog input pins can be used as digital pins, referred to as A0, A1, etc. The exception is the
Arduino Nano, Pro Mini, and Mini’s A6 and A7 pins, which can only be used as analog inputs.
It is possible to enable the internal pullup resistors with the mode INPUT_PULLUP. Additionally,
the INPUT mode explicitly disables the internal pullups.
Syntax
pinMode(pin, mode)
Parameters
pin: the Arduino pin number to set the mode of.
mode: INPUT, OUTPUT, or INPUT_PULLUP. See the Digital Pins page for a more complete
description of the functionality.
Returns
Nothing
Example Code
The code makes the digital pin 13 OUTPUT and Toggles it HIGH and LOW
void setup() {
pinMode(13, OUTPUT); // sets the digital pin 13 as output
}
void loop() {
digitalWrite(13, HIGH); // sets the digital pin 13 on
delay(1000); // waits for a second
digitalWrite(13, LOW); // sets the digital pin 13 off
delay(1000); // waits for a second
}
Notes and Warnings
The analog input pins can be used as digital pins, referred to as A0, A1, etc.
It is possible to enable the internal pullup resistors with the mode INPUT_PULLUP. Additionally,
the INPUT mode explicitly disables the internal pullups.
Syntax
pinMode(pin, mode)
Parameters
pin: the Arduino pin number to set the mode of.
mode: INPUT, OUTPUT, or INPUT_PULLUP. See the Digital Pins page for a more complete
description of the functionality.
Returns
Nothing
Example Code
The code makes the digital pin 13 OUTPUT and Toggles it HIGH and LOW
void setup() {
pinMode(13, OUTPUT); // sets the digital pin 13 as output
}
void loop() {
digitalWrite(13, HIGH); // sets the digital pin 13 on
delay(1000); // waits for a second
digitalWrite(13, LOW); // sets the digital pin 13 off
delay(1000); // waits for a second
}
Notes and Warnings
The analog input pins can be used as digital pins, referred to as A0, A1, etc.
Example Code
The brackets may be omitted after an if statement. If this is done, the next line (defined by the
semicolon) becomes the only conditional statement.
if (x > 120)
digitalWrite(LEDpin, HIGH);
if (x > 120) {
digitalWrite(LEDpin1, HIGH);
digitalWrite(LEDpin2, HIGH);
}
// all are correct
Notes and Warnings
The statements being evaluated inside the parentheses require the use of one or more operators
shown below.
Comparison Operators:
x == y (x is equal to y)
x != y (x is not equal to y)
x < y (x is less than y)
x > y (x is greater than y)
x <= y (x is less than or equal to y)
x >= y (x is greater than or equal to y)
Beware of accidentally using the single equal sign (e.g. if (x = 10) ). The single equal sign is the
assignment operator, and sets x to 10 (puts the value 10 into the variable x). Instead use the double
equal sign (e.g. if (x == 10) ), which is the comparison operator, and tests whether x is equal to 10
or not. The latter statement is only true if x equals 10, but the former statement will always be true.
This is because C++ evaluates the statement if (x=10) as follows: 10 is assigned to x (remember that
the single equal sign is the (assignment operator)), so x now contains 10. Then the 'if' conditional
evaluates 10, which always evaluates to TRUE, since any non-zero number evaluates to TRUE.
Consequently, if (x = 10) will always evaluate to TRUE, which is not the desired result when using
an 'if' statement. Additionally, the variable x will be set to 10, which is also not a desired action.
Reference > Language > Structure > Control structure > Else
else
[Control Structure]
Description
The if…else allows greater control over the flow of code than the basic if statement, by allowing
multiple tests to be grouped. An else clause (if at all exists) will be executed if the condition in the if
statement results in false. The else can proceed another if test, so that multiple, mutually exclusive
tests can be run at the same time.
Each test will proceed to the next one until a true test is encountered. When a true test is found, its
associated block of code is run, and the program then skips to the line following the entire if/else
construction. If no test proves to be true, the default else block is executed, if one is present, and
sets the default behavior.
Note that an else if block may be used with or without a terminating else block and vice versa. An
unlimited number of such else if branches are allowed.
Syntax
if (condition1) {
// do Thing A
}
else if (condition2) {
// do Thing B
}
else {
// do Thing C
}
Example Code
Below is an extract from a code for temperature sensor system