Home / Learn Chip Programming / Arduino Functions vs Register Programming
Arduino Functions vs Register Programming
See what digitalWrite, analogRead and millis really do, and when to leave them.
In shortSee what digitalWrite, analogRead and millis really do, and when to leave them.
Theory in plain words
What it is
Arduino functions are convenient wrappers: they check pin tables, timers and modes. Direct registers are smaller and faster but board specific.
Why we use it
You can start fast with Arduino, then speed up hot loops or save flash with registers. Employers value knowing both.
How it works
Use Arduino API for application logic and registers for time-critical paths (bit-banging, fast ISRs, tiny chips).
Where it is used
- Fast bit-banged protocols
- ATtiny with little flash
- Debugging mysterious Arduino timing
Cost comparison
Speed of a toggling loopTry it Yourself
Registers make toggling 10 to 20 times faster, which matters for software serial, WS2812 and fast sampling.
// Arduino API
void setup() { pinMode(13, OUTPUT); }
void loop() { digitalWrite(13, HIGH); delay(500); digitalWrite(13, LOW); delay(500); }
// Registers (avr-gcc, no Arduino)
#include <avr/io.h>
#include <util/delay.h>
int main(void) { DDRB |= (1 << PB5); for (;;) { PINB = (1 << PB5); _delay_ms(500); } } // writing 1 to PINx toggles| Arduino call | Register equivalent |
|---|---|
| pinMode(p, OUTPUT) | DDRx |= (1 << n) |
| digitalWrite(p, HIGH) | PORTx |= (1 << n) |
| digitalRead(p) | (PINx >> n) & 1 |
| analogRead(A0) | ADMUX, ADCSRA |= (1< |
| analogWrite(9, v) | OCR1A = v with Timer1 PWM |
| millis() | Timer0 overflow counter |
| Serial.begin(9600) | UBRR0, UCSR0B, UCSR0C |
Quick quiz
1. Writing 1 to PINx on newer AVRs...
A handy toggle trick.
2. Why is digitalWrite slow?
Generic code costs cycles.