Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

1/12/24, 4:14 PM Arduino Cheat Sheet.

svg

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) // 0-255 SoftwareSerial(rxPin, txPin)
lowByte(x) highByte(x)
switch (var) { -- decrement begin(long speed) // Up to 115200
bitRead(x, bitn)
case 1: += compound addition Advanced I/O listen() // Only 1 can listen
bitWrite(x, bitn, bit)
... -= compound subtraction tone(pin, freq_Hz, [duration_msec]) isListening() // at a time.
bitSet(x, bitn)
break; *= compound multiplication noTone(pin) read, peek, print, println, write
bitClear(x, bitn)
case 2: /= compound division shiftOut(dataPin, clockPin, // Equivalent to Serial library
bit(bitn) // bitn: 0=LSB 7=MSB
... &= compound bitwise and {MSBFIRST|LSBFIRST}, value)
break; |= compound bitwise or shiftIn(dataPin, clockPin, EEPROM.h - access non-volatile memory
default: {MSBFIRST|LSBFIRST}) Type Conversions
char(val) byte(val) byte read(addr)
... Bitwise Operators unsigned long pulseIn(pin, write(addr, byte)
} {HIGH|LOW}, [timeout_usec]) 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_usec, max_usec])
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 (40mA max per I/O pin) Wire.h - I²C communication

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

AREF
GND
13
12
~11
~10
~9
8

7
~6
~5
4
~3
2
TX→1
RX←0
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 return type: no return value
volatile in RAM (nice for ISR)
const read-only WWW.ARDUINO.CC - Made in Italy
Strings
PROGMEM in flash ATmega328P:
char str1[8] = 16MHz, 32KB Flash (program), by Mark Liffiton
{'A','r','d','u','i','n','o','\0'}; Arrays 2KB SRAM, 1KB EEPROM version: 2021-10-23
// Includes \0 null termination byte myPins[] = {2, 4, 8, 3, 6};
char str2[8] = int myInts[6]; // Array of 6 ints DC in POWER ANALOG IN source: https://github.com/liffiton/Arduino-Cheat-Sheet/
IOREF
RESET

sugg. 7-12V Adapted from:


3.3V

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


GND
GND
Vin

limit 6-20V
5V

A0
A1
A2
A3
A4
A5
// Compiler adds null termination // index of myInts - 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

file:///C:/Users/Harshad/Desktop/Arduino Cheat Sheet.svg 1/1

You might also like