You are on page 1of 1

Primary source: Arduino Language Reference

Arduino Programming Cheat Sheet http://arduino.cc/en/Reference/

Structure & Flow Operators Built-in Functions Libraries


Basic Program Structure General Operators Pin Input/Output Math Serial (communicate with PC or via RX/TX)
void setup() { = (assignment operator) Digital I/O (pins: 0-13 A0-A5) min(x, y) max(x, y) abs(x) begin(long Speed) // up to 115200
// runs once when sketch starts + (add) - (subtract) sin(rad) cos(rad) tan(rad) end()
pinMode(pin,[INPUT, OUTPUT])
} * (multiply) / (divide) sqrt(x) pow(base, exponent) int available() // #bytes available
int digitalread(pin) byte read() // -1 if none available
void loop() { % (modulo) digitalWrite(pin, value) constrain(x, minval, maxval)
byte peek()
// runs repeatedly == (equal to) != (not equal to) // Write HIGH to an input to map(val, fromL, fromH, toL, toH) flush()
} < (less than) > (greater than) // enable pull-up resistors print(myData)
<= (less than or equal to) Analog In (pins: 0-5) Random Numbers println(myData)
Control Structures >= (greater than or equal to) randomSeed(seed) // long or int write(myBytes)
int analogRead(pin)
if (x < 5) { ... } else { ... } && (and) || (or) ! (not) SerialEvent() // called if data rdy
analogReference( long random(max)
while (x < 5) { ... } [DEFAULT, INTERNAL, EXTERNAL]) long random(min, max)
do { ... } while ( x < 5); Compound Operators SoftwareSerial (serial comm. on any pins)
PWM Out (pins: 3 5 6 9 10 11)
for (int i = 0; i < 10; i++) { ... } ++ (increment) (#include <softwareSerial.h>)
analogWrite(pin, value) Bits and Bytes
break; // exit a loop immediately -- (decrement) SoftwareSerial(rxPin, txPin)
lowByte(x) highByte(x)
continue; // go to next iteration += (compound addition) begin(long Speed) // up to 115200
bitRead(x, bitn)
switch (myVar) { -= (compound substraction) Advanced I/O listen() // Only 1 can listen
bitWrite(x, bitn, bit)
case 1: *= (compound multiplication) tone(pin, freqhz) isListening() // at a time.
bitSet(x, bitn)
... /= (compound division) tone(pin, freqhz, duration_ms) read, peek, print, println, write
bitClear(x, bitn)
break; &= (compound bitwise and) noTone(pin) // all like in Serial library
bit(bitn) // bitn: 0=LSB 7=MSB
case 2: |= (compound bitwise or) shiftOut(dataPin, clockPin,
... [MSBFIRST,LSBFIRST], value) EEPROM (#include <EEPROM.h>)
break; Type Conversions
unsigned long pulseIn(pin, byte read(intAddr)
default: Bitwise Operators char() byte()
[HIGH,LOW]) write(intAddr, myByte)
... & (bitwise and) | (bitwise or) int() word()
} ^ (bitwise xor) ~ (bitwise not) long() float()
return x; // just return; for voids << (shift left) >> (shift right) Servo (#include <Servo.h>)
Time
External Interrupts attach(pin, [min_uS, max_uS])
unsigned long millis()
write(angle) // 0 to 180
// overflows at 50 days attachInterrupt(interrupt, func,
writeMicroseconds(uS)
Variables, Arrays, and Data unsigned long micros()
// overflows at 70 minutes
[LOW, CHANGE, RISING, FALLING])
detachInterrupt(interrupt)
// 1000-2000; 1500 is midpoint
int read() // 0 to 180
delay(msec) interrupts()
Data types Constants bool attached()
delayMicroseconds(usec) noInterrupts()
void HIGH | LOW detach()
boolean (0, 1, true, false) INPUT | OUTPUT
char (e.g. 'a' -128 to 127) true | false Wire (I²C comm.) (#include <Wire.h>)

int1
int0
int (-32768 to 32767) 143 (Decimal)

SCL
SDA
begin() // join a master
long (-2147483648 to 2147483647) 0173 (Octal - base 8) begin(addr) // join a slave @ addr
unsigned char (0 to 255) 0b11011111 (Binary) requestFrom(address, count)
byte (0 to 255) 0x7B (Hexadecimal - base 16)

2
GND
13
12
~11
~10
~9

~6
~5

~3
AREF

TX→1
RX←0
beginTransmission(addr) // Step 1
unsigned int (0 to 65535) 7U (force unsigned) RESET DIGITAL (PWM~) send(myByte) // Step 2
word (0 to 65535) 10L (force long) send(char * mystring)
unsigned long (0 to 4294967295) 15UL (force long unsigned) L
float (-3.4028e+38 to 3.4028e+38) 10.0 (force floating point)
ARDUINO UNO send(byte * data, size)
endTransmission() // Step 3
double (currently same as float) 2.4e5 (2.4*10^5 = 240000)
TX ON
RX int available() // #bytes available
ICSP byte receive() // get next byte
Qualifiers 1 onReceive(handler)
Pointer Access
static (persists between calls) onRequest(handler)
& (reference: get a pointer)
volatile (in RAM (nice for ISR))
* (dereference: follow a pointer)
const (make read only)
PROGMEM (in flash) WWW.ARDUINO.CC - Made in Italy

Arrays
Strings ATmega382:
16MHz, 32KB Flash (prog.),
by Mark Liffiton
char S1[8] =
int myInts[6]; // array of 6 ints {'A','r','d','u','i','n','o'}; 2KB SRAM, 1KB EEPROM
int myPins[]={2, 4, 8, 3, 6}; // unterminated string; may crash Adapted from:
DC in POWER ANALOG IN
int mySensVals[6]={2, 4, -8, 3, 2}; char S2[8] = - Original by Gavin Smith
IOREF
RESET
3.3V

sugg. 7-12V
GND
GND
Vin
myInts[0]=42; // assigning first {'A','r','d','u','i','n','o','\0'}; - SVG version by Frederic Dufourg
5V

A0
A1
A2
A3
A4
A5
limit 6-20V
// index of myInts // includes \0 null termination
myInts[6]=12; // ERROR! Indexes char S3[]="Arduino"; - Arduino board drawing
original by Fritzing.org
SDA
SCL
// are 0 though 5 char S4[8]="Arduino";

You might also like