Home / Learn Embedded C / Arduino-Style Libraries vs Bare-Metal C

Arduino-Style Libraries vs Bare-Metal C

digitalWrite() vs writing directly to a hardware register — the trade-off explained.

The short versiondigitalWrite() vs writing directly to a hardware register — the trade-off explained.

Theory in plain words

Library call vs direct register accessLibrary call (e.g.digitalWrite)Easy to read and rememberPortable across boards(mostly)Slightly slower (extrafunction calls)Great for learning andprototypingBare-metal registeraccessNeeds you to know the exactregister/bitTied to one specific chipFastest possible, smallestcodeNeeded for tight timing ortiny chips
Library call vs direct register access

The basic idea

Arduino-style functions like digitalWrite()/pinMode() are friendly wrapper functions; bare-metal code writes directly to the chip's hardware registers for full control and speed.

Why this comes up

Beginners usually start with the friendly wrappers to learn concepts fast; professional and performance-critical firmware often drops to bare-metal register access for speed, size or precise timing.

The mechanics

A wrapper function like digitalWrite(13, HIGH) internally does exactly the same register read-modify-write you would do by hand — it is just slower because of extra function-call and lookup overhead.

Where it is used

  • Beginner vs advanced firmware style
  • Code review discussions

Try it

A tiny wrapper function doing the same thing as a direct writeC: edit and run

Both lines end up doing the exact same bit-set operation on the same underlying register — the wrapper just hides the shift-and-OR.

This course teaches both styles

Early lessons use friendly simulated calls like GPIO_WRITE() so you can focus on concepts. Register-level lessons later (and the Chip Families group) show the exact bare-metal register writes those friendly calls stand in for.

Try it: a wrapper function for reading a pin

A tiny digitalRead-style wrapper vs reading the register directlyC: edit and run

Just like digitalWrite(), a digitalRead()-style wrapper is only ever shifting and masking the same register you could read by hand.

Quick check

1. What does a function like digitalWrite() usually do internally?