4 - 1 Arduino Basic Functions - 1
4 - 1 Arduino Basic Functions - 1
[Digital I/O]
Description
Configures the specified pin to behave either as an input or an output. See the Digital Pins page for details
on the functionality of the pins.
As of Arduino 1.0.1, 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
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
}
analogRead()
[Analog I/O]
Description
Reads the value from the specified analog pin. Arduino boards contain a multichannel, 10-bit analog to
digital converter. This means that it will map input voltages between 0 and the operating voltage(5V or 3.3V)
into integer values between 0 and 1023. On an Arduino UNO, for example, this yields a resolution between
readings of: 5 volts / 1024 units or, 0.0049 volts (4.9 mV) per unit. See the table below for the usable pins,
operating voltage and maximum resolution for some Arduino boards.
The input range can be changed using analogReference(), while the resolution can be changed (only for Zero,
Due and MKR boards) using analogReadResolution().
On ATmega based boards (UNO, Nano, Mini, Mega), it takes about 100 microseconds (0.0001 s) to read an
analog input, so the maximum reading rate is about 10,000 times a second.
Syntax
analogRead(pin)
Parameters
pin: the name of the analog input pin to read from (A0 to A5 on most boards, A0 to A6 on MKR boards, A0 to
A7 on the Mini and Nano, A0 to A15 on the Mega).
Returns
The analog reading on the pin. Although it is limited to the resolution of the analog to digital converter (0-
1023 for 10 bits or 0-4095 for 12 bits). Data type: int.
Example Code
int analogPin = A3; // potentiometer wiper (middle terminal) connected to analog pin 3
// outside leads to ground and +5V
int val = 0; // variable to store the value read
void setup() {
Serial.begin(9600); // setup serial
}
void loop() {
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
}
analogWrite()
[Analog I/O]
Description
Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightnesses or drive a
motor at various speeds. After a call to analogWrite(), the pin will generate a steady rectangular wave of the
specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite()) on
the same pin.
Syntax
analogWrite(pin, value)
Parameters
pin: the Arduino pin to write to. Allowed data types: int.
value: the duty cycle: between 0 (always off) and 255 (always on). Allowed data types: int.
Returns
Nothing
Example Code
Sets the output to the LED proportional to the value read from the potentiometer.
void setup() {
pinMode(ledPin, OUTPUT); // sets the pin as output
}
void loop() {
val = analogRead(analogPin); // read the input pin
analogWrite(ledPin, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0
to 255
}
Serial
[Communication]
Description
Used for communication between the Arduino board and a computer or other devices. All Arduino boards
have at least one serial port (also known as a UART or USART), and some have several.
On Uno, Nano, Mini, and Mega, pins 0 and 1 are used for communication with the computer. Connecting
anything to these pins can interfere with that communication, including causing failed uploads to the board.
You can use the Arduino environment’s built-in serial monitor to communicate with an Arduino board. Click
the serial monitor button in the toolbar and select the same baud rate used in the call to begin().
Serial communication on pins TX/RX uses TTL logic levels (5V or 3.3V depending on the board). Don’t connect
these pins directly to an RS232 serial port; they operate at +/- 12V and can damage your Arduino board.
To use these extra serial ports to communicate with your personal computer, you will need an additional
USB-to-serial adaptor, as they are not connected to the Mega’s USB-to-serial adaptor. To use them to
communicate with an external TTL serial device, connect the TX pin to your device’s RX pin, the RX to your
device’s TX pin, and the ground of your Mega to your device’s ground.
Control Structures
for
[Control Structure]
Description
The for statement is used to repeat a block of statements enclosed in curly braces. An increment counter is
usually used to increment and terminate the loop. The for statement is useful for any repetitive operation,
and is often used in combination with arrays to operate on collections of data/pins.
Syntax
for (initialization; condition; increment) {
// statement(s);
}
Parameters
Example Code
// Dim an LED using a PWM pin
int PWMpin = 10; // LED in series with 470 ohm resistor on pin 10
void setup() {
// no setup needed
}
void loop() {
for (int i = 0; i <= 255; i++) {
analogWrite(PWMpin, i);
delay(10);
}
}
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
do...while
[Control Structure]
Description
The do…while loop works in the same manner as the while loop, with the exception that the condition is tested
at the end of the loop, so the do loop will always run at least once.
Syntax
do {
// statement block
} while (condition);
Parameters
Example Code
int x = 0;
do {
delay(50); // wait for sensors to stabilize
x = readSensors(); // check the sensors
} while (x < 100);
switch...case
[Control Structure]
Description
Like if statements, switch case controls the flow of programs by allowing programmers to specify different
code that should be executed in various conditions. In particular, a switch statement compares the value of a
variable to the values specified in case statements. When a case statement is found whose value matches
that of the variable, the code in that case statement is run.
The break keyword exits the switch statement, and is typically used at the end of each case. Without a break
statement, the switch statement will continue executing the following expressions ("falling -through") until a
break, or the end of the switch statement is reached.
Syntax
switch (var) {
case label1:
// statements
break;
case label2:
// statements
break;
default:
// statements
break;
}
Parameters
var: a variable whose value to compare with various cases. Allowed data types: int, char.
label1, label2: constants. Allowed data types: int, char.
Returns
Nothing
Example Code
switch (var) {
case 1:
//do something when var equals 1
break;
case 2:
//do something when var equals 2
break;
default:
// if nothing else matches, do the default
// default is optional
break;
}