Common Arduino Commands
When you first start programming you’ll find that you’ll have a difficult time just remembering what some of the common commands and constructs are. Fortunately, the Arduino comes with some very thorough reference pages that contain precisely this information. It can be found here:
http://arduino.cc/en/Reference/HomePage
You also find the reference section by going to the Help Menu->Reference in your Arduino Environment. To begin with, you’ll be using a fairly small set of commands. I’ve included the most commonly used ones below.
pinMode(pin, mode)
Description
Configures the specified pin to behave either as an input or an output. See the reference page below.
Parameters
pin: the number of the pin whose mode you wish to set. (int)
mode: either INPUT or OUTPUT.
digitalWrite(pin, value)
Description
Ouputs either HIGH or LOW at a specified pin.
Parameters
pin: the pin number
value: HIGH or LOW
digitalRead(pin)
What it does
Reads the value from a specified pin, it will be either HIGH or LOW.
Parameters
pin: the number of the pin you want to read.
int analogRead(pin)
Description
Reads the value from the specified analog pin. The Arduino board contains a 6 channel (8 channels on the mini), 10-bit analog to digital converter. This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023. This yields a resolution between readings of: 5 volts / 1024 units or, .0049 volts (4.9 mV) per A/D unit.
It takes about 100 us (.0001 s) to read an analog input, so the maximum reading rate is about 10000 times a second.
Parameters
int pin
AnalogRead() accepts one integer specifying the number of the pin to read. Values between 0 and 5 are valid on most boards, and between 0 and 7 on the mini.
Note
Analog pins default to inputs and unlike digital ones, do not need to be declared as INPUT nor OUTPUT
analogWrite(pin, value)
Description
Writes an analog value (PWM wave) to a pin. On newer Arduino boards (including the Mini and BT) with the ATmega168 chip, this function works on pins 3, 5, 6, 9, 10, and 11. Older USB and serial Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.
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 wave until the next call to analogWrite (or a call to digitalRead or digitalWrite on the same pin).
Parameters
pin: the pin to write to.
value: the duty cycle: between 0 and 255. A value of 0 generates a constant 0 volts output at the specified pin; a value of 255 generates a constant 5 volts output at the specified pin. For values in between 0 and 255, the pin rapidly alternates between 0 and 5 volts – the higher the value, the more often the pin is high (5 volts). For example, a value of 64 will be 0 volts three-quarters of the time, and 5 volts one quarter of the time; a value of 128 will be at 0 half the time and 255 half the time; and a value of 192 will be 0 volts one quarter of the time and 5 volts three-quarters of the time.
delay(ms)
Description
Pauses your program for the amount of time (in miliseconds) specified as parameter.
Parameters
ms: the number of milliseconds to pause (there are 1000 milliseconds in a second)
Serial.begin(int speed)
Description
Sets the data rate in bits per second (baud) for serial data transmission. For communicating with the computer, use one of these rates: 300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200. You can, however, specify other rates – for example, to communicate over pins 0 and 1 with a component that requires a particular baud rate.
Parameters
int datarate, in bits per second (baud)
Returns
nothing
Example:
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
int Serial.available()
Description
Get the number of bytes (characters) available for reading over the serial port.
Parameters
None
Returns
The number of bytes are available to read in the serial buffer, or 0 if none are available. If any data has come in, Serial.available() will be greater than 0. The serial buffer can hold up to 128 bytes.
Example
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print(“I received: “);
Serial.println(incomingByte, DEC);
}
}
int Serial.read()
Description
Reads incoming serial data.
Parameters
None
Returns
an int, the first byte of inocming serial data available (or -1 if no data is available).
Example
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print(“I received: “);
Serial.println(incomingByte, DEC);
}
}
Serial.print(data)
Description
Prints data to the serial port.
Parameter
data: all integer types, including char
Syntax
This command can take many forms:
Serial.print(b) with no format specified, prints b as a decimal number in an ASCII string. For example,
int b = 79;
Serial.print(b);
prints the ASCII string “79″.
Serial.print(b, DEC) prints b as a decimal number in an ASCII string. For example,
int b = 79;
Serial.print(b, DEC);
prints the string “79″.