You are on page 1of 1

Arduino Programming Cheat Sheet Primary source: Arduino Language Reference

https://arduino.cc/en/Reference/

Structure & Flow Operators Built-in Functions Libraries


Basic Program Structure General Operators Pin Input/Output Math Serial - comm. with PC or via RX/TX
void setup() { = assignment 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 pinMode(pin, sin(rad) cos(rad) tan(rad) end()
} * multiply / divide [INPUT, OUTPUT, INPUT_PULLUP]) sqrt(x) pow(base, exponent) int available() // #bytes available
void loop() { % modulo int digitalRead(pin) constrain(x, minval, maxval) int read() // -1 if none available
// Runs repeatedly == equal to != not equal to digitalWrite(pin, [HIGH, LOW]) map(val, fromL, fromH, toL, toH) int peek() // Read w/o removing
} < less than > greater than flush()
<= less than or equal to Analog In - pins A0-A5 Random Numbers print(data) println(data)
Control Structures >= greater than or equal to int analogRead(pin) randomSeed(seed) // long or int write(byte) write(char * string)
if (x < 5) { ... } else { ... } && and || or analogReference( long random(max) // 0 to max-1 write(byte * data, size)
while (x < 5) { ... } ! not [DEFAULT, INTERNAL, EXTERNAL]) long random(min, max) SerialEvent() // Called if data rdy
for (int i = 0; i < 10; i++) { ... }
break; // Exit a loop immediately Compound Operators PWM Out - pins 3 5 6 9 10 11 SoftwareSerial.h - comm. on any pin
Bits and Bytes
continue; // Go to next iteration ++ increment analogWrite(pin, value) SoftwareSerial(rxPin, txPin)
lowByte(x) highByte(x)
switch (var) { -- decrement begin(long speed) // Up to 115200
bitRead(x, bitn)
case 1: += compound addition listen() // Only 1 can listen
Advanced I/O bitWrite(x, bitn, bit)
... -= compound subtraction isListening() // at a time.
tone(pin, freq_Hz) bitSet(x, bitn)
break; *= compound multiplication read, peek, print, println, write
tone(pin, freq_Hz, duration_ms) bitClear(x, bitn)
case 2: /= compound division // Equivalent to Serial library
noTone(pin) bit(bitn) // bitn: 0=LSB 7=MSB
... &= compound bitwise and
shiftOut(dataPin, clockPin,
break; |= compound bitwise or EEPROM.h - access non-volatile memory
[MSBFIRST, LSBFIRST], value) Type Conversions
default: byte read(addr)
unsigned long pulseIn(pin, char(val) byte(val)
... Bitwise Operators write(addr, byte)
[HIGH, LOW]) int(val) word(val)
} & bitwise and | bitwise or EEPROM[index] // Access as array
return x; // x must match return type long(val) float(val)
^ bitwise xor ~ bitwise not
return; // For void return type << shift left >> shift right Time
Servo.h - control servo motors
unsigned long millis() External Interrupts
attach(pin, [min_uS, max_uS])
Function Definitions Pointer Access // Overflows at 50 days attachInterrupt(interrupt, func,
write(angle) // 0 to 180
<ret. type> <name>(<params>) { ... } & reference: get a pointer unsigned long micros() [LOW, CHANGE, RISING, FALLING])
writeMicroseconds(uS)
e.g. int double(int x) {return x*2;} * dereference: follow a pointer // Overflows at 70 minutes detachInterrupt(interrupt)
// 1000-2000; 1500 is midpoint
delay(msec) interrupts()
int read() // 0 to 180
delayMicroseconds(usec) noInterrupts()
bool attached()
Variables, Arrays, and Data detach()

int1
int0
Data Types Numeric Constants Wire.h - I²C communication

SCL
SDA
boolean true | false 123 decimal begin() // Join a master
char -128 - 127, 'a' '$' etc. 0b01111011 binary begin(addr) // Join a slave @ addr
0173 octal - base 8 requestFrom(address, count)

2
13
12

~9

~6
~5

~3
AREF

TX→1
RX←0
GND

~11
~10
unsigned char 0 - 255
byte 0 - 255 0x7B hexadecimal - base 16 RESET DIGITAL (PWM~) beginTransmission(addr) // Step 1
int -32768 - 32767 123U force unsigned send(byte) // Step 2
L
unsigned int
word
0 - 65535
0 - 65535
123L
123UL
force long
force unsigned long
ARDUINO UNO send(char * string)
send(byte * data, size)
TX ON
long -2147483648 - 2147483647 123.0 force floating point RX endTransmission() // Step 3
unsigned long 0 - 4294967295 1.23e6 1.23*10^6 = 1230000 ICSP int available() // #bytes available
float -3.4028e+38 - 3.4028e+38 1 byte receive() // Get next byte
Qualifiers onReceive(handler)
double currently same as float
static persists between calls onRequest(handler)
void i.e., no return value
volatile in RAM (nice for ISR)
const read-only WWW.ARDUINO.CC - Made in Italy
Strings
PROGMEM in flash ATmega382:
char str1[8] = 16MHz, 32KB Flash (program), by Mark Liffiton
{'A','r','d','u','i','n','o','\0'}; Arrays 2KB SRAM, 1KB EEPROM version: 2017-04-10
// Includes \0 null termination int myPins[] = {2, 4, 8, 3, 6};
DC in source: h�ps://github.com/liffiton/Arduino-Cheat-Sheet/
char str2[8] = int myInts[6]; // Array of 6 ints POWER ANALOG IN
IOREF
RESET

sugg. 7-12V Adapted from:


3.3V

{'A','r','d','u','i','n','o'}; myInts[0] = 42; // Assigning first


GND
GND
Vin
5V

A0
A1
A2
A3
A4
A5
// Compiler adds null termination // index of myInts limit 6-20V - Original: Gavin Smith
char str3[] = "Arduino"; myInts[6] = 12; // ERROR! Indexes - SVG version: Frederic Dufourg
char str4[8] = "Arduino"; // are 0 though 5
SDA
SCL
- Arduino board drawing: Fritzing.org

You might also like