Microcontroller Basics
Read inputs, make decisions in a program, and control outputs using a classic Arduino Uno R3.
A microcontroller reads inputs and controls outputs
A microcontroller reads inputs and controls outputs
A microcontroller is a small computer on a chip, with a processor, memory and connections to the outside world. Its program can read a switch, decide what that input means, and turn an output on or off. The Arduino Uno is a board that supplies the chip with power connections, a USB interface and accessible pins.
This topic uses the classic Uno R3 with an ATmega328P, operating with 5 V logic. Other Arduino boards, including newer Uno models, can have different converters and electrical limits.
The drawing groups functions; it is not a wiring pinout. Check the labels on the actual board. Start with Digital and Analog Signals, then Analog-to-Digital Conversion and Pulse-Width Modulation.
A sketch runs setup once, then repeats loop
A sketch runs setup once, then repeats loop
An Arduino program is called a sketch. It uses C/C++ syntax: rules for names, parentheses, braces and statements. It is a programming language, not ordinary English. Capitalization matters: digitalRead and digitalread are different names.
setup() runs once after startup or reset. loop() repeats while the program runs. This original example turns the built-in LED on for half a second and off for half a second.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}
delay uses milliseconds and pauses normal progress through the sketch. The braces group the statements belonging to each function.
Choose the pin mode before using the pin
Choose the pin mode before using the pin
| Mode | What it does | Typical use |
|---|---|---|
INPUT | Reads a voltage level without enabling the internal pull-up. | A sensor that actively drives a compatible signal; do not leave the input floating. |
OUTPUT | Drives the pin HIGH or LOW. | An LED through a current-limiting resistor, or a control signal to a driver. |
INPUT_PULLUP | Reads an input with an internal resistor pulling it toward the logic supply. | A button connecting the input to ground. |
pinMode(pin, mode) configures the pin. digitalRead(pin) returns HIGH or LOW. On an OUTPUT pin, digitalWrite(pin, HIGH) drives it toward the logic supply; LOW drives it toward ground. These are limited-current signals, not ideal power supplies. On an INPUT pin, digitalWrite instead controls the pull-up.
A button to ground reads LOW when pressed
A button to ground reads LOW when pressed
With INPUT_PULLUP, an open switch reads HIGH. Closing the switch to ground reads LOW. This is called active-low: the action happens when the signal is low.
void setup() {
pinMode(2, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
bool pressed = (digitalRead(2) == LOW);
digitalWrite(LED_BUILTIN, pressed ? HIGH : LOW);
}
The pull-up supplies a small current through the closed switch to ground. The switch sinks that current. A physical switch can bounce between states briefly; counting presses reliably needs debouncing beyond this simple state display.
Pull-up resistance and current
The Uno's internal pull-up is approximately 20–50 kΩ, not a fixed 10 kΩ. Arduino's Uno R3 documentation specifies “20-50k ohm.” At nominal 5 V, a closed switch carries about 0.10–0.25 mA through that resistor, using I = V/R. See Current, Voltage, and Resistance.
Use the official IDE and function reference; Checklist; Practice
Use the official IDE and function reference
Get the IDE from Arduino Software. Use the Arduino language reference to check spelling, arguments and return values. The Arduino getting-started guide introduces the programming environment.
- Confirm the board model and choose its matching board entry in the IDE.
- Select the port belonging to the connected board.
- Verify the sketch to catch syntax errors, then upload it.
- Check that the observed input and output match the program.
Disconnect power before changing wiring. Keep signals within the board's ratings and use a suitable driver for a motor. See Electrical Safety for electrical precautions.