Primary source: Arduino Language Reference https://arduino.cc/en/Reference/
Arduino Arduino Programming Cheat Sheet Structure & Flow Basic Program Structure void setup() setup() { // Runs once once when sketch starts starts } void loop() loop() { // Runs Runs repeatedly repeatedly } Control Structures if (x if (x < 5) { ... ... } } else { else { ... ... } } while (x while (x < 5) { ... ... } } for (int for (int i = 0; i < 10; i++) { ... ... } } break; break; // Exit a loop immediately continue; continue; // Go to next iteration switch (var) switch (var) { case 1: case 1: ... break; break; case 2: case 2: ... break; break; default: default: ... } return x; return x; // x must match return type return; return ; // For void return type Function Definitions (< params params>) { ... } e.g. int double(int x) {return x*2;}
Operators General Operators = assignment + 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 Compound Operators ++ ++ increment -- -- decrement += compound addition -= compound subtraction *= compound multiplicatio multiplication n /= compound division &= compound bitwise and |= compound bitwise or Bitwise Operators & bitwise and ^ bitwise xor << shift left
| ~ >>
bitwise or bitwise not shift right
Pointer Access & reference: get a pointer * dereference : follow a pointer
Built-in Functions
Libraries
Pin Input/Output Digital I/O I/O - pins 0-13 A0-A5 pinMode(pin, pinMode(pin, [INPUT, OUTPUT, INPUT_PULLUP]) INPUT_PULLUP]) int digitalRead(pin) digitalRead(pin) digitalWrite(pin, digitalWrite(pin, [HIGH, LOW])
Math min(x, min(x, y) max(x, max(x, y) abs(x) abs(x) sin(rad) sin(rad) cos(rad) cos(rad) tan(rad) tan(rad) sqrt(x) sqrt (x) pow(base, pow(base, exponent) constrain(x, constrain(x, minval, maxval) map(val, map(val, fromL, fromH, toL, toH)
Analog In In - pins A0-A5 int analogRead(pin) analogRead(pin) analogReference( analogReference ( [DEFAULT, INTERNAL, EXTERNAL])
Random Numbers randomSeed(seed) randomSeed(seed) // long or int long random(max) random (max) // 0 to max-1 long random(min, random (min, max)
Serial Serial - comm. with PC or via RX/TX begin(long begin(long speed) // Up to 115200 end() end() int available() available() // #bytes available int read int read() () // -1 if none available int peek int peek() () // Read w/o removing flush() flush() print(data) print(data) println(data) println(data) write(byte) write(byte) write(char write(char * string) write(byte write(byte * data, size) SerialEvent() SerialEvent() // Called Called if data rdy rdy
PWM Out Out - pins 3 5 6 9 10 11 analogWrite(pin, analogWrite (pin, value)
Bits and Bytes lowByte(x) lowByte(x) highByte(x) highByte(x) bitRead(x, bitRead(x, bitn) bitWrite(x, bitWrite(x, bitn, bit) bitSet(x, bitSet(x, bitn) bitClear(x, bitClear(x, bitn) bit(bitn) bit(bitn) // bitn: 0=LSB 7=MSB
SoftwareSerial.h SoftwareSerial.h - comm. on any pin SoftwareSerial(rxPin, SoftwareSerial(rxPin, txPin) begin(long begin(long speed) // Up to 115200 listen() listen() // Only 1 can listen isListening() isListening() // at a time. time. read, read, peek, peek , print, print, println, println, write // Equivalent Equivalent to Serial Serial library library
Type Conversions char(val) char(val) byte(val) byte (val) int(val) int(val) word(val) word (val) long(val) long(val) float(val) float(val)
EEPROM.h EEPROM.h - access non-volatile memory byte read(addr) read (addr) write(addr, write(addr, byte) EEPROM[index] // Access as array
Advanced I/O tone(pin, tone(pin, freq_Hz) tone(pin, tone (pin, freq_Hz, duration_ms) noTone(pin) noTone (pin) shiftOut (dataPin, clockPin, [MSBFIRST, LSBFIRST], value) unsigned long pulseIn(pin, pulseIn(pin, [HIGH, LOW]) Time unsigned long millis() millis() // Overflows Overflows at 50 days unsigned long micros() micros() // Overflows at 70 minutes delay(msec) delay(msec) delayMicroseconds(usec) delayMicroseconds(usec)
External Interrupts attachInterrupt(interrupt, attachInterrupt(interrupt, func, [LOW, CHANGE, RISING, FALLING]) detachInterrupt(interrupt) detachInterrupt(interrupt) interrupts() interrupts() noInterrupts() noInterrupts()
Variables, Arrays, and Data Data Types boolean true | false char -128 - 127, 'a' '$' etc. unsigned char 0 - 255 byte 0 - 255 int -32768 - 32767 unsigned int 0 - 65535 word 0 - 65535 long -2147483648 - 2147483647 unsigned long 0 - 4294967295 float -3.4028e+3 8 - 3.4028e+38 double currently same as float void i.e., no return value Strings char str1[8] = {'A','r','d','u','i','n','o','\0'}; // Includes Includes \0 null null termination termination char str2[8] = {'A','r','d','u','i','n','o'}; // Co Comp mpil iler er ad adds ds nu null ll te term rmin inat atio ion n
Numeric Constants 123 decimal 0b01111011 0b01111011 binary 0173 octal - base 8 0x7B 0x7B hexadecimal - base 16 123U 123U force unsigned 123L 123L force long 123UL 123UL force unsigned long 123.0 123.0 force floating point 1.23e 1.23 e6 1.23*10^6 = 1230000 Qualifiers static persists between calls volatile in RAM (nice for ISR) const const read-only PROGMEM in flash Arrays int myPins[] = {2, 4, 8, 3, 6}; int myInts[6]; // Array of 6 ints myInts[0] = 42; // Assigning first // index of myInts
RESET
F D 3 2 1 0 9 E N 1 1 1 1 ~ R G ~ ~ A
8
7 6 5 ~ ~
4 3 ~
DIGITAL (PWM~)
L TX RX
2 1 0 → ← X X T R
ARDUINO UNO ON ICSP 1
ATmega382: 16MHz, 32KB Flash (program), 2KB SRAM, 1KB EEPROM
POWER
F T E E V R S 3 D D n O E . V N N i I R 3 5 G G V
ANALOG IN 0 1 2 3 4 5 A A A A A A
Servo.h Servo.h - control servo motors attach(pin, attach(pin, [min_uS, max_uS]) write(angle) write(angle) // 0 to 180 writeMicroseconds(uS) writeMicroseconds(uS) // 1000-2000; 1500 is midpoint int read() read() // 0 to 180 bool attached() attached() detach() detach() Wire.h Wire.h - I²C communication begin() begin() // Join a master begin(addr) begin(addr) // Join a slave @ addr requestFrom(address, requestFrom(address, count) beginTransmission(addr) beginTransmission(addr) // Step 1 send(byte) send(byte) // Step 2 send(char send(char * string) send(byte send(byte * data, size) endTransmission() endTransmission() // Step 3 int available() available() // #bytes available byte receive() receive() // Get next byte onReceive(handler) onReceive(handler) onRequest(handler) onRequest(handler)
by Mark Liffiton version: 2017-04-10 2017-04-10 source: h�ps://github.com/liffiton/Arduino-Cheat-Sheet/
Adapted from: - Original: Gavin Smith