Sub topic 2
The Microcontroller at the Center
This chapter assumes that you know basic voltage, current, and resistance, can use the Arduino IDE, and understand the setup() and loop() structure of a sketch. It also assumes that you can read a simple breadboard circuit diagram.
What the microcontroller does
The microcontroller is the programmable computer on the Arduino Uno. It is the large integrated circuit in the center of the board, the ATmega328P. An integrated circuit is a small electronic component that contains many connected circuits inside one package.
The microcontroller runs your sketch. It reads signals, makes decisions, and changes output signals. For example, it can read whether a button is pressed and then turn an LED on.
The board adds useful support around the microcontroller. It provides a clock, power connections, a reset circuit, and a USB connection. The microcontroller remains the part that executes your program and controls the electrical pins.
flowchart TD
A[Sketch in program memory] --> B[Microcontroller executes instructions]
B --> C[Read input pins]
B --> D[Run calculations and decisions]
B --> E[Set output pins]
C --> B
D --> B
E --> F[LED, motor driver, sensor, or other circuit]The microcontroller's working areas
The ATmega328P uses several kinds of memory:
- Flash memory stores the compiled sketch when you upload it. It keeps its contents after power is removed.
- SRAM stores temporary data while the sketch runs, such as variables and function call information. Its contents disappear when power is removed.
- EEPROM stores small amounts of data that should survive a reset or power loss.
The microcontroller also contains hardware peripherals. A peripheral is a built-in circuit that performs a specific task. Examples include timers, analog-to-digital converters, and serial communication hardware. Arduino functions provide simpler access to many of these features.
The microcontroller has a clock. The clock produces regular electrical pulses that coordinate instruction execution and timing. The Uno's clock runs at 16 MHz, or 16 million cycles per second. Functions such as delay() and timer-based features depend on this clock.
A sketch controlling the microcontroller
This sketch configures the built-in LED and repeatedly changes its output state:
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}When the board starts, it runs setup() once. The call to pinMode() tells the microcontroller to drive the selected pin as an output. The board then calls loop() repeatedly. digitalWrite() makes the output signal HIGH or LOW, and delay(500) pauses for about 500 milliseconds.
The exact voltage represented by HIGH depends on the board's electrical design. On an Uno, the microcontroller operates with 5 V logic. The program controls signals, but the connected circuit still must obey the pin's voltage and current limits.
Reset and program execution
A reset stops the current execution and starts the microcontroller's startup process again. The Uno briefly resets when you open a USB serial connection in many setups. After reset, the bootloader checks whether a new sketch should be uploaded. If no upload begins, it starts the existing sketch.
Exercise
Which part of the Uno stores the running sketch's instructions, and which part stores temporary variables?
Show answer
Flash memory stores the sketch's instructions. SRAM stores temporary variables while the sketch runs.