Arduino Basics - Complete Reference Guide Cheat Sheet

Last Updated: November 21, 2025

Digital Pins

Function Description
pinMode(pin, INPUT) pinMode(7, INPUT); // Set pin 7 as input
pinMode(pin, OUTPUT) pinMode(13, OUTPUT); // Set pin 13 as output
pinMode(pin, INPUT_PULLUP) pinMode(2, INPUT_PULLUP); // Enable internal pull-up resistor
digitalWrite(pin, HIGH) digitalWrite(13, HIGH); // Set pin to 5V
digitalWrite(pin, LOW) digitalWrite(13, LOW); // Set pin to 0V
digitalRead(pin) int state = digitalRead(7); // Returns HIGH or LOW

Analog Pins

Function Description
analogRead(pin) int value = analogRead(A0); // Read 0-1023
analogWrite(pin, value) analogWrite(9, 128); // PWM output 0-255
analogReference(type) analogReference(EXTERNAL); // Set voltage reference
Voltage to value // 0V = 0, 5V = 1023 (10-bit resolution)
Value to voltage float voltage = (analogRead(A0) * 5.0) / 1023.0;

PWM Pins (Uno: 3, 5, 6, 9, 10, 11)

Technique Example
LED dimming analogWrite(9, 128); // 50% brightness
Motor speed analogWrite(10, 200); // ~78% speed
Fade effect for(int i=0; i<=255; i++) { analogWrite(9, i); delay(10); }
Servo control #include <Servo.h>
servo.attach(9); servo.write(90);

Core Functions

Function Description
delay(ms) delay(1000); // Pause for 1 second
delayMicroseconds(us) delayMicroseconds(500); // Pause 500 microseconds
millis() unsigned long time = millis(); // Time since start
micros() unsigned long time = micros(); // Microseconds
map(value, from, to) int out = map(analogRead(A0), 0, 1023, 0, 255);
constrain(x, min, max) int val = constrain(reading, 0, 100);
random(min, max) int dice = random(1, 7); // 1-6
randomSeed(seed) randomSeed(analogRead(0)); // Initialize random

Serial Communication

Function Example
Begin serial Serial.begin(9600); // 9600 baud rate
Print text Serial.print("Hello"); // No newline
Print with newline Serial.println("Hello"); // With newline
Print variable Serial.println(sensorValue);
Print formatted Serial.print("Temp: "); Serial.print(temp); Serial.println("C");
Print hex Serial.println(value, HEX);
Check available if(Serial.available() > 0) { /* read data */ }
Read byte int inByte = Serial.read();
Read string String str = Serial.readString();

Common Sensors

Sensor Code Example
Temperature (DHT11) #include <DHT.h>
DHT dht(2, DHT11);
float temp = dht.readTemperature();
Ultrasonic (HC-SR04) digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
PIR Motion int motion = digitalRead(pirPin); // HIGH when motion detected
Light (LDR) int light = analogRead(A0); // Lower = brighter
Sound sensor int sound = analogRead(A1); // Amplitude level
Potentiometer int value = analogRead(A2); // 0-1023
Button int buttonState = digitalRead(buttonPin);
IR Receiver #include <IRremote.h>
if(IrReceiver.decode()) { Serial.println(IrReceiver.decodedIRData.command); }

Project Examples - Blink LED

Step Code
Setup void setup() { pinMode(13, OUTPUT); }
Loop void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}

Project Examples - Button Control

Component Code
Setup const int buttonPin = 2;
const int ledPin = 13;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
Loop void loop() {
int buttonState = digitalRead(buttonPin);
if(buttonState == LOW) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}

Project Examples - Temperature Monitor

Stage Code
Include library #include <DHT.h>
Initialize DHT dht(2, DHT11);
Setup void setup() {
Serial.begin(9600);
dht.begin();
}
Read & display void loop() {
float temp = dht.readTemperature();
float humidity = dht.readHumidity();
Serial.print("Temp: ");
Serial.print(temp);
Serial.print("C Humidity: ");
Serial.print(humidity);
Serial.println("%");
delay(2000);
}

Math & Logic Functions

Function Example
min(x, y) int smaller = min(10, 20); // Returns 10
max(x, y) int larger = max(10, 20); // Returns 20
abs(x) int positive = abs(-5); // Returns 5
pow(base, exp) float result = pow(2, 3); // 2^3 = 8
sqrt(x) float root = sqrt(16); // Returns 4.0
sin/cos/tan float sine = sin(radians);

Interrupts

Function Description
attachInterrupt attachInterrupt(digitalPinToInterrupt(2), ISR_function, RISING);
detachInterrupt detachInterrupt(digitalPinToInterrupt(2));
Modes LOW, CHANGE, RISING, FALLING
ISR example volatile bool flag = false;
void ISR_function() { flag = true; }

Arrays & Strings

Type Example
Array declaration int values[5] = {1, 2, 3, 4, 5};
Array access int first = values[0];
String creation String text = "Hello";
String concatenation String full = text + " World";
String length int len = text.length();
String comparison if(text.equals("Hello")) { }
💡 Pro Tips:
  • Digital pins 0 & 1 are used for Serial communication - avoid using them
  • PWM pins are marked with ~ symbol on Arduino boards
  • Use INPUT_PULLUP for buttons to avoid external resistors
  • millis() is better than delay() for non-blocking code
  • Always check sensor readings for NaN before using them
  • Use const int for pin numbers to make code more readable
  • Add 0.1uF capacitor near sensors to reduce noise
  • Common ground is required when connecting external power supplies
← Back to Data Science & ML | Browse all categories | View all cheat sheets