Bird
Raised Fist0
Arduinoprogramming~10 mins

How Arduino code maps to AVR hardware - Visual Walkthrough

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - How Arduino code maps to AVR hardware
Write Arduino Sketch
Arduino IDE Compiles Code
Code Converted to AVR Machine Code
Upload to AVR Microcontroller
AVR Executes Machine Code
Hardware Pins & Peripherals Act
Physical Output (LED, Motor, Sensor)
Arduino code is written, compiled into machine code, uploaded to the AVR chip, which then runs it to control hardware pins and peripherals.
Execution Sample
Arduino
void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}
This code turns the built-in LED on pin 13 on and off every second.
Execution Table
StepActionArduino FunctionAVR Hardware EffectOutput
1Setup runs oncepinMode(13, OUTPUT)Configures pin 13 as outputPin 13 ready to send signals
2Loop startdigitalWrite(13, HIGH)Sets pin 13 voltage HIGHLED turns ON
3Waitdelay(1000)CPU waits 1000 msLED stays ON
4Loop continuesdigitalWrite(13, LOW)Sets pin 13 voltage LOWLED turns OFF
5Waitdelay(1000)CPU waits 1000 msLED stays OFF
6Loop repeatsBack to digitalWrite(13, HIGH)Pin 13 voltage HIGH againLED turns ON again
💡 Loop runs forever, toggling LED ON and OFF every second
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6
Pin 13 StateLOW (default)HIGHLOWHIGH
Key Moments - 3 Insights
Why does pinMode(13, OUTPUT) only run once?
Because setup() runs once at the start to configure hardware; see execution_table row 1 where pin 13 is set as output before the loop.
How does digitalWrite affect the physical LED?
digitalWrite changes the voltage on pin 13, turning the LED ON or OFF as shown in rows 2 and 4 of the execution_table.
What does delay(1000) do in terms of hardware?
delay(1000) pauses the CPU for 1000 milliseconds, keeping the LED state stable during that time, as seen in rows 3 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of pin 13 after step 4?
AHIGH
BLOW
CUndefined
DFlashing
💡 Hint
Check the 'Pin 13 State' in variable_tracker after step 4.
At which step does the LED turn ON for the first time?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
See execution_table row 2 where digitalWrite(13, HIGH) sets the LED ON.
If delay(1000) was changed to delay(500), how would the execution_table change?
AThe LED would toggle every 500 ms instead of 1000 ms
BThe LED would stay ON permanently
CThe LED would never turn OFF
DNo change in LED timing
💡 Hint
Look at the delay steps 3 and 5 controlling LED timing.
Concept Snapshot
Arduino code is written in sketches with setup() and loop().
The IDE compiles this code into AVR machine code.
This machine code runs on the AVR microcontroller.
Functions like pinMode and digitalWrite control hardware pins.
Hardware pins control physical devices like LEDs.
Delays pause CPU to keep states stable.
Full Transcript
This visual execution shows how Arduino code maps to AVR hardware. First, the Arduino sketch is written with setup() and loop() functions. The Arduino IDE compiles this code into machine code that the AVR microcontroller understands. When uploaded, the AVR runs this code. For example, pinMode(13, OUTPUT) configures pin 13 as an output pin. Then digitalWrite(13, HIGH) sets the pin voltage high, turning on the LED connected to pin 13. The delay(1000) pauses the CPU for 1 second, keeping the LED on. Then digitalWrite(13, LOW) turns the LED off, followed by another delay. This loop repeats forever, blinking the LED on and off every second. Variables like pin 13 state change accordingly. Key moments include understanding setup runs once, digitalWrite controls voltage, and delay pauses CPU. The quiz tests understanding of pin states and timing changes. This shows the step-by-step mapping from Arduino code to physical hardware action.

Practice

(1/5)
1. What does the Arduino digitalWrite() function do in relation to the AVR hardware?
easy
A. It sets a specific pin on the AVR chip to HIGH or LOW voltage.
B. It reads the voltage level from a pin on the AVR chip.
C. It configures the clock speed of the AVR chip.
D. It resets the AVR chip to its initial state.

Solution

  1. Step 1: Understand the purpose of digitalWrite()

    The digitalWrite() function is used to control output pins on the Arduino board.
  2. Step 2: Map function to AVR hardware action

    It changes the voltage level on a specific pin of the AVR chip to either HIGH (5V) or LOW (0V).
  3. Final Answer:

    It sets a specific pin on the AVR chip to HIGH or LOW voltage. -> Option A
  4. Quick Check:

    digitalWrite() controls pin voltage = D [OK]
Hint: digitalWrite sets pin voltage HIGH or LOW [OK]
Common Mistakes:
  • Confusing digitalWrite() with digitalRead()
  • Thinking it resets the chip
  • Assuming it changes clock speed
2. Which of the following is the correct syntax to set pin 13 as an output in Arduino code?
easy
A. pinMode(OUTPUT, 13);
B. pinMode(13, OUTPUT);
C. digitalWrite(13, OUTPUT);
D. digitalRead(13, OUTPUT);

Solution

  1. Step 1: Identify correct function and parameters for pin mode

    The function to set pin mode is pinMode(), which takes the pin number first, then the mode.
  2. Step 2: Match correct parameter order

    The correct order is pinMode(pin, mode); so pinMode(13, OUTPUT); is correct.
  3. Final Answer:

    pinMode(13, OUTPUT); -> Option B
  4. Quick Check:

    pinMode(pin, mode) sets pin direction = A [OK]
Hint: pinMode(pin, OUTPUT) sets pin as output [OK]
Common Mistakes:
  • Swapping parameters in pinMode()
  • Using digitalWrite() to set pin mode
  • Using digitalRead() incorrectly
3. Consider this Arduino code snippet:
pinMode(8, OUTPUT);
digitalWrite(8, HIGH);
int val = digitalRead(8);

What will be the value of val after running this code?
medium
A. Undefined behavior
B. 0
C. 1
D. Compilation error

Solution

  1. Step 1: Analyze pin mode and write operations

    Pin 8 is set as OUTPUT and then set to HIGH voltage.
  2. Step 2: Understand digitalRead() on an OUTPUT pin

    Reading a pin set as OUTPUT returns the value last set by digitalWrite() since PIN register reflects the output pin voltage, which is HIGH (1).
  3. Final Answer:

    1 -> Option C
  4. Quick Check:

    digitalRead() on OUTPUT pin returns 1 = A [OK]
Hint: digitalRead on OUTPUT pin returns the written value (1) [OK]
Common Mistakes:
  • Assuming digitalRead returns 0 on output pin
  • Thinking digitalRead cannot read output pins
  • Thinking code causes error
4. This Arduino code is intended to blink an LED on pin 13, but it doesn't work:
void setup() {
  digitalWrite(13, HIGH);
  pinMode(13, OUTPUT);
}
void loop() {
  digitalWrite(13, LOW);
  delay(1000);
  digitalWrite(13, HIGH);
  delay(1000);
}

What is the main problem?
medium
A. digitalWrite() cannot be used on pin 13
B. delay() cannot be used in loop()
C. setup() function is missing
D. pinMode() must be called before digitalWrite() in setup()

Solution

  1. Step 1: Check order of pin setup in setup()

    Pin mode must be set before writing to the pin to ensure proper hardware configuration.
  2. Step 2: Identify incorrect sequence

    The code calls digitalWrite(13, HIGH); before pinMode(13, OUTPUT);, which can cause the pin not to behave as expected.
  3. Final Answer:

    pinMode() must be called before digitalWrite() in setup() -> Option D
  4. Quick Check:

    Set pinMode before digitalWrite = C [OK]
Hint: Always set pinMode before digitalWrite [OK]
Common Mistakes:
  • Calling digitalWrite before pinMode
  • Thinking delay() is invalid
  • Assuming pin 13 is special and can't be used
5. You want to toggle an LED connected to pin 7 every 500ms using direct AVR port manipulation for speed. Which code snippet correctly maps Arduino pin 7 to AVR PORTD and toggles it?
hard
A. DDRD |= (1 << DDD6); PORTD ^= (1 << PORTD6);
B. DDRB |= (1 << DDB7); PORTB ^= (1 << PORTB7);
C. DDRC |= (1 << DDC7); PORTC ^= (1 << PORTC7);
D. DDRD |= (1 << DDD7); PORTD ^= (1 << PD7);

Solution

  1. Step 1: Identify Arduino pin 7 AVR port and bit

    On Arduino Uno, pin 7 maps to PORTD bit 6 (PD6), not bit 7.
  2. Step 2: Set pin 7 as output and toggle it

    Setting DDRD bit 6 to 1 configures pin 7 as output. Toggling PORTD bit 6 flips the pin state.
  3. Final Answer:

    DDRD |= (1 << DDD6); PORTD ^= (1 << PORTD6); -> Option A
  4. Quick Check:

    Pin 7 = PD6 toggle = D [OK]
Hint: Pin 7 is PD6; set DDRD and toggle PORTD bit 6 [OK]
Common Mistakes:
  • Using wrong port (PORTB or PORTC) for pin 7
  • Setting wrong bit number
  • Confusing DDRx and PORTx registers