Arduino Cheat Sheet
Arduino Cheat Sheet
http://arduino.cc/en/Reference/
Bitwise Operators
& (bitwise and)
^ (bitwise xor)
<< (shift left)
| (bitwise or)
~ (bitwise not)
>> (shift right)
Time
unsigned long millis()
// overflows at 50 days
unsigned long micros()
// overflows at 70 minutes
delay(msec)
delayMicroseconds(usec)
RESET
External Interrupts
attachInterrupt(interrupt, func,
[LOW, CHANGE, RISING, FALLING])
detachInterrupt(interrupt)
interrupts()
noInterrupts()
DIGITAL (PWM~)
L
TX
RX
ARDUINO UNO
ON
ICSP
Arrays
int myInts[6]; // array of 6 ints
int myPins[]={2, 4, 8, 3, 6};
int mySensVals[6]={2, 4, -8, 3, 2};
myInts[0]=42; // assigning first
// index of myInts
myInts[6]=12; // ERROR! Indexes
// are 0 though 5
Pointer Access
& (reference: get a pointer)
* (dereference: follow a pointer)
Strings
char S1[8] =
{'A','r','d','u','i','n','o'};
// unterminated string; may crash
char S2[8] =
{'A','r','d','u','i','n','o','\0'};
// includes \0 null termination
char S3[]="Arduino";
char S4[8]="Arduino";
by Mark Liffiton
ATmega382:
16MHz, 32KB Flash (prog.),
2KB SRAM, 1KB EEPROM
DC in
sugg. 7-12V
limit 6-20V
POWER
ANALOG IN
SDA
SCL
A0
A1
A2
A3
A4
A5
Qualifiers
static
volatile
const
PROGMEM
Constants
HIGH | LOW
INPUT | OUTPUT
true | false
143
(Decimal)
0173
(Octal - base 8)
0b11011111 (Binary)
0x7B
(Hexadecimal - base 16)
7U
(force unsigned)
10L
(force long)
15UL
(force long unsigned)
10.0
(force floating point)
2.4e5
(2.4*10^5 = 240000)
Advanced I/O
tone(pin, freqhz)
tone(pin, freqhz, duration_ms)
noTone(pin)
shiftOut(dataPin, clockPin,
[MSBFIRST,LSBFIRST], value)
unsigned long pulseIn(pin,
[HIGH,LOW])
Random Numbers
randomSeed(seed) // long or int
long random(max)
long random(min, max)
IOREF
RESET
3.3V
5V
GND
GND
Vin
Data types
void
boolean
char
int
long
unsigned
byte
unsigned
word
unsigned
float
double
Math
min(x, y)
max(x, y)
abs(x)
sin(rad)
cos(rad)
tan(rad)
sqrt(x)
pow(base, exponent)
constrain(x, minval, maxval)
map(val, fromL, fromH, toL, toH)
int1
int0
Compound Operators
++ (increment)
-- (decrement)
+= (compound addition)
-= (compound substraction)
*= (compound multiplication)
/= (compound division)
&= (compound bitwise and)
|= (compound bitwise or)
Pin Input/Output
Libraries
7
~6
~5
4
~3
2
TX1
RX0
Control Structures
if (x < 5) { ... } else { ... }
while (x < 5) { ... }
do { ... } while ( x < 5);
for (int i = 0; i < 10; i++) { ... }
break; // exit a loop immediately
continue; // go to next iteration
switch (myVar) {
case 1:
...
break;
case 2:
...
break;
default:
...
}
return x; // just return; for voids
General Operators
= (assignment operator)
+ (add)
- (subtract)
* (multiply)
/ (divide)
% (modulo)
== (equal to)
!= (not equal to)
< (less than) > (greater than)
<= (less than or equal to)
>= (greater than or equal to)
&& (and)
|| (or)
! (not)
Built-in Functions
AREF
GND
13
12
~11
~10
~9
8
Operators
SCL
SDA
Adapted from:
- Original by Gavin Smith
- SVG version by Frederic Dufourg
- Arduino board drawing
original by Fritzing.org