100% found this document useful (1 vote)
4K views

Arduino Cheat Sheet

This document provides a cheat sheet on Arduino programming concepts including basic program structure, control structures, operators, data types, variables, arrays, functions, libraries, and input/output functions. The program structure uses setup() and loop() functions. Control structures include if/else statements, while loops, for loops, and switch cases. Common operators, data types, variables, and arrays are described. Built-in functions cover math, random numbers, serial communication, and more. Libraries allow extended functionality like I2C, EEPROM, and servo control. Input/output covers digital/analog pins, PWM, interrupts and more.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
4K views

Arduino Cheat Sheet

This document provides a cheat sheet on Arduino programming concepts including basic program structure, control structures, operators, data types, variables, arrays, functions, libraries, and input/output functions. The program structure uses setup() and loop() functions. Control structures include if/else statements, while loops, for loops, and switch cases. Common operators, data types, variables, and arrays are described. Built-in functions cover math, random numbers, serial communication, and more. Libraries allow extended functionality like I2C, EEPROM, and servo control. Input/output covers digital/analog pins, PWM, interrupts and more.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Arduino Programming Cheat Sheet

Structure & Flow


Basic Program Structure
void setup() {
// Runs once when sketch starts
}
void loop() {
// Runs repeatedly
}
Control Structures
if (x < 5) { ... } else { ... }
while (x < 5) { ... }
for (int i = 0; i < 10; i++) { ... }
// Exit a loop immediately
break;
continue; // Go to next iteration
switch (var) {
case 1:
...
break;
case 2:
...
break;
default:
...
}
return x; // x must match return type
return;
// For void return type
Function Definitions
<ret. type> <name>(<params>) { ... }
e.g. int double(int x) {return x*2;}

Operators

Primary source: Arduino Language Reference


http://arduino.cc/en/Reference/

Built-in Functions

Libraries

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

Pin Input/Output
Digital I/O - pins 0-13 A0-A5
pinMode(pin,
[INPUT, OUTPUT, INPUT_PULLUP])
int digitalread(pin)
digitalWrite(pin, [HIGH, LOW])

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)

Analog In - pins A0-A5


int analogRead(pin)
analogReference(
[DEFAULT, INTERNAL, EXTERNAL])

Random Numbers
randomSeed(seed) // long or int
long random(max) // 0 to max-1
long random(min, max)

Serial - comm. with PC or via RX/TX


begin(long speed) // Up to 115200
end()
int available() // #bytes available
int read()
// -1 if none available
int peek()
// Read w/o removing
flush()
print(data)
println(data)
write(byte)
write(char * string)
write(byte * data, size)
SerialEvent() // Called if data rdy

Compound Operators
++ increment
-- decrement
+= compound addition
-= compound subtraction
*= compound multiplication
/= compound division
&= compound bitwise and
|= compound bitwise or

PWM Out - pins 3 5 6 9 10 11


analogWrite(pin, value)

Bits and Bytes


lowByte(x)
highByte(x)
bitRead(x, bitn)
bitWrite(x, bitn, bit)
bitSet(x, bitn)
bitClear(x, bitn)
bit(bitn) // bitn: 0=LSB 7=MSB

SoftwareSerial.h - comm. on any pin


SoftwareSerial(rxPin, txPin)
begin(long speed) // Up to 115200
listen()
// Only 1 can listen
isListening() // at a time.
read, peek, print, println, write
// Equivalent to Serial library

Type Conversions
char(val)
byte(val)
int(val)
word(val)
long(val)
float(val)

EEPROM.h - access non-volatile memory


byte read(addr)
write(addr, byte)
EEPROM[index] // Access as array

Bitwise Operators
&
bitwise and
^
bitwise xor
<< shift left

|
~
>>

bitwise or
bitwise not
shift right

Pointer Access
& reference: get a pointer
* dereference: follow a pointer

Advanced I/O
tone(pin, freq_Hz)
tone(pin, freq_Hz, duration_ms)
noTone(pin)
shiftOut(dataPin, clockPin,
[MSBFIRST, LSBFIRST], value)
unsigned long pulseIn(pin,
[HIGH, LOW])
Time
unsigned long millis()
// Overflows at 50 days
unsigned long micros()
// Overflows at 70 minutes
delay(msec)
delayMicroseconds(usec)

External Interrupts
attachInterrupt(interrupt, func,
[LOW, CHANGE, RISING, FALLING])
detachInterrupt(interrupt)
interrupts()
noInterrupts()

7
~6
~5
4
~3
2
TX1
RX0

AREF
GND
13
12
~11
~10
~9
8

DIGITAL (PWM~)

L
TX
RX

ARDUINO UNO
ON
ICSP
1

persists between calls


in RAM (nice for ISR)
read-only
in flash

ATmega382:
16MHz, 32KB Flash (program),
2KB SRAM, 1KB EEPROM

DC in
sugg. 7-12V
limit 6-20V

POWER

ANALOG IN

SDA
SCL

Arrays
int myPins[] = {2, 4, 8, 3, 6};
// Array of 6 ints
int myInts[6];
myInts[0] = 42; // Assigning first
// index of myInts
myInts[6] = 12; // ERROR! Indexes
// are 0 though 5

Wire.h - IC communication
// Join a master
begin()
begin(addr) // Join a slave @ addr
requestFrom(address, count)
beginTransmission(addr) // Step 1
send(byte)
// Step 2
send(char * string)
send(byte * data, size)
// Step 3
endTransmission()
int available() // #bytes available
byte receive() // Get next byte
onReceive(handler)
onRequest(handler)

WWW.ARDUINO.CC - Made in Italy

A0
A1
A2
A3
A4
A5

Qualifiers
static
volatile
const
PROGMEM

RESET

IOREF
RESET
3.3V
5V
GND
GND
Vin

Strings
char str1[8] =
{'A','r','d','u','i','n','o','\0'};
// Includes \0 null termination
char str2[8] =
{'A','r','d','u','i','n','o'};
// Compiler adds null termination
char str3[] = "Arduino";
char str4[8] = "Arduino";

Numeric Constants
123
decimal
0b01111011 binary
0173
octal - base 8
0x7B
hexadecimal - base 16
123U
force unsigned
123L
force long
123UL
force unsigned long
123.0
force floating point
1.23e6
1.23*10^6 = 1230000

SCL
SDA

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+38 - 3.4028e+38
double currently same as float
void
i.e., no return value

int1
int0

Variables, Arrays, and Data

Servo.h - control servo motors


attach(pin, [min_uS, max_uS])
write(angle) // 0 to 180
writeMicroseconds(uS)
// 1000-2000; 1500 is midpoint
int read()
// 0 to 180
bool attached()
detach()

by Mark Liton
Adapted from:
- Original: Gavin Smith
- SVG version: Frederic Dufourg
- Arduino board drawing: Fritzing.org

You might also like