Module 6
Getting started with Arduino
Quick-reference revision notes for parents.
6.1 Getting started — first circuit (LED)
- An Arduino is a small programmable microcontroller board with input/output (I/O) pins.
- It runs simple C/C++ programs called sketches.
Every sketch has two main functions:
void setup() {
// runs once when the board starts
pinMode(13, OUTPUT);
}
void loop() {
// runs over and over forever
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
pinMode(pin, OUTPUT)— set the pin's direction.digitalWrite(pin, HIGH/LOW)— turn the pin ON or OFF (5V or 0V).delay(ms)— pause for that many milliseconds.
6.2 Breadboards and buttons
- A breadboard is a reusable board with hidden wires that connect rows of holes — you push components in instead of soldering.
- A button connects a pin to either +5V or GND when pressed.
- Use a pull-down or pull-up resistor so the pin reads a clear HIGH or LOW.
int buttonPin = 2;
void setup() {
pinMode(buttonPin, INPUT);
}
void loop() {
if (digitalRead(buttonPin) == HIGH) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
}
6.3 Latching buttons using variables
A latch remembers a state — e.g. one press turns the LED on, the next turns it off, even though the button is only momentarily pushed.
int ledState = LOW;
void loop() {
if (digitalRead(buttonPin) == HIGH) {
ledState = !ledState; // flip
digitalWrite(13, ledState);
delay(300); // debounce
}
}
Variables in Arduino C: int, bool, float, String. Always declare with a type.
6.4 Analogue input and output
Analogue input
analogRead(A0)returns a value from 0 to 1023.- Used with sensors that give a varying voltage (e.g. light sensor, temperature sensor, potentiometer).
Serial.begin(9600)+Serial.println(value)sends data back to the computer to view in the Serial Monitor.
Analogue output (PWM)
- Arduino digital pins can only be HIGH or LOW. PWM (Pulse Width Modulation) fakes analogue output by switching very fast — average voltage is between 0V and 5V.
analogWrite(pin, value)withvalue0–255 — used to dim LEDs, control motor speed, etc.- Only works on certain pins (marked with ~).
6.5 Subroutines
Pull repeated code into a function, just like in Python:
void blink(int pin, int times) {
for (int i = 0; i < times; i++) {
digitalWrite(pin, HIGH);
delay(200);
digitalWrite(pin, LOW);
delay(200);
}
}
void loop() {
blink(13, 3);
delay(1000);
}
voidmeans the function doesn't return a value.- Parameters allow the same code to do different things.
6.6 Making a useful system
Combine inputs, outputs and logic to build something real.
- Temperature sensor — outputs a voltage that depends on temperature; read with
analogRead. - LDR (light sensor) — resistance changes with light; in a divider circuit, gives a varying analogue voltage.
- Motors need more current than the Arduino can give directly — use a transistor (e.g. NPN) as a switch, with the motor powered separately.
Example project — automatic light
Read the LDR. If it's dark (analogRead value below threshold), turn the LED on. Otherwise turn it off. A simple "smart" night light.
Quick reference
- Sketch shape:
setup()runs once,loop()runs forever pinMode,digitalRead/Write,analogRead,analogWrite,delay- analogRead: 0–1023 (10-bit). analogWrite (PWM): 0–255
- Use a transistor + separate supply for motors
- Subroutines:
void name(params) { … }