First steps with hardware on an Arduino Uno: blinking an LED, reading sensors, dimming with PWM, and reacting with interrupts. Press play on any sketch to watch it happen.
1 Digital output: setup and loop
An Arduino Uno is a small board built around the ATmega328P microcontroller, running at 16 MHz and 5 V. There is no operating system. Your setup() runs once, then loop() runs forever.
setup(): runs once at power-up. Configure your pins here.
loop(): runs repeatedly until the power goes off.
digitalWrite: sets a pin to 5 V (HIGH) or 0 V (LOW).
Resistor: always put one in series with an LED, or you can damage both the LED and the pin.
Rule of thumb: delay() stops everything else. Use millis() once your program does more than one job.
2 Analog input and PWM
The real world is not just on or off. An ADC turns a voltage into a number, and PWM fakes an in-between output by switching a pin on and off very quickly.
analogRead: the Uno has a 10-bit ADC, so 0 V to 5 V becomes 0 to 1023.
Voltage: V = reading × 5 / 1023.
analogWrite: PWM from 0 to 255 on the pins marked ~.
Duty cycle: the share of time the pin is on. 50% looks half as bright.
void loop() {
int raw = analogRead(A0); // 0 to 1023
int brightness = map(raw, 0, 1023, 0, 255);
analogWrite(9, brightness); // PWM on pin 9
}
Signal flow diagram
1/4 Turn the knob halfway. The voltage on A0 is now 2.5 V.
Rule of thumb: Use map() to convert between ranges, such as 0 to 1023 into 0 to 255.
3 Interrupts and debouncing
Polling means checking a button over and over, and a slow loop can miss a quick press. An interrupt makes the hardware stop your code the instant something happens, run a short function, and carry on.
attachInterrupt: on the Uno, pins 2 and 3 can trigger an interrupt.
ISR: interrupt service routine. Keep it very short, with no delay() or Serial.
volatile: marks variables shared with an ISR, so the compiler always reads them fresh.
Bouncing: a mechanical button contact chatters for a few milliseconds and looks like many presses.