Arduino Pro Sheet
20+ Essential Hardware & Logic Snippets
01. Standard Blink
The fundamental test to ensure your board is alive.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
02. Hardware Debouncing
Software-level filter for mechanical button noise.
const int pin = 2;
unsigned long lastDebounce = 0;
int state = HIGH;
void loop() {
int read = digitalRead(pin);
if (read != lastDebounce) {
lastDebounce = millis();
if (read == LOW) { /* Action on press */ }
}
}
03. Sensor Mapping (ADC to PWM)
Translating 10-bit input (0-1023) to 8-bit output (0-255).
void loop() {
int sensor = analogRead(A0);
int output = map(sensor, 0, 1023, 0, 255);
analogWrite(9, output);
}
04. Multitasking (Millis)
Running code without blocking execution with delay().
unsigned long pTime = 0;
void loop() {
if (millis() - pTime >= 1000) {
pTime = millis();
// Execute every second
}
}
05. Ultrasonic Distance
Precise distance measurement using pulse timing.
long getDistance(int t, int e) {
digitalWrite(t, LOW); delayMicroseconds(2);
digitalWrite(t, HIGH); delayMicroseconds(10);
digitalWrite(t, LOW);
return pulseIn(e, HIGH) * 0.034 / 2;
}
06. Persistent Memory
Saving settings after power-off using EEPROM.update (prevents wear).
#include <EEPROM.h>
void save(int addr, byte val) {
EEPROM.update(addr, val); // Writes only if value changed
}
07. External Interrupts
Capturing high-speed external events instantly.
void setup() {
attachInterrupt(digitalPinToInterrupt(2), isr, FALLING);
}
void isr() { /* Keep it fast! */ }
08. Servo Sweep
Controlling position using the Servo library.
#include <Servo.h>
Servo myservo;
void setup() { myservo.attach(9); }
void loop() { myservo.write(90); } // 0 to 180 degrees
09. I2C Scanner
Utility to find the hex address of connected sensors.
#include <Wire.h>
void setup() {
Wire.begin(); Serial.begin(9600);
for(byte i=8; i<127; i++) {
Wire.beginTransmission(i);
if(Wire.endTransmission()==0) Serial.println(i, HEX);
}
}
10. Direct Port Access
Bypassing digitalWrite for 10x faster switching speed.
void setup() { DDRB |= B00100000; } // Set Pin 13 as Output
void loop() {
PORTB |= B00100000; // High
PORTB &= ~B00100000; // Low
}
11. Watchdog Reset
Auto-rebooting the board if code freezes.
#include <avr/wdt.h>
void setup() { wdt_enable(WDTO_2S); }
void loop() { wdt_reset(); // Must be called every < 2s }
12. DHT11 / DHT22 Reading
Using the DHT library to fetch environmental data.
#include "DHT.h"
DHT dht(2, DHT22);
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
}
13. Switch-Case State Machine
The cleanest way to handle system states (Idle, Run, Alarm).
enum State {IDLE, RUN};
State curr = IDLE;
void loop() {
switch(curr) {
case IDLE: if(btn) curr = RUN; break;
case RUN: execute(); break;
}
}
14. Custom PWM Frequency
Modifying timers to remove audible coil whine from motors.
void setup() {
// Set Timer 1 for 31kHz PWM on pins 9, 10
TCCR1B = (TCCR1B & 0b11111000) | 0x01;
}
15. Simple Data Smoothing
Average filter to remove noise from analog sensors.
float smooth = 0;
void loop() {
int raw = analogRead(A0);
smooth = (raw * 0.1) + (smooth * 0.9); // Low-pass filter
}
16. Stepper Pulse Control
Driving a stepper motor using Step/Dir signals.
void step() {
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
}
17. Check Available RAM
Utility to prevent stack overflow on Nano/Uno boards.
int freeRam() {
extern int __heap_start, *__brkval;
int v;
return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int)__brkval);
}
18. Saving RAM (F Macro)
Moving static strings to Flash memory to save precious RAM.
void setup() {
Serial.println(F("This string is stored in FLASH!"));
}
19. Internal 1.1V Reference
Better precision for low-voltage sensor measurements.
void setup() {
analogReference(INTERNAL); // 1.1V on Uno/Nano
}
20. High Speed Serial
Standard setup for high-speed telemetry and ESP8266 talk.
void setup() {
Serial.begin(115200);
while(!Serial); // Wait for USB connection
}
