0% found this document useful (0 votes)
7 views29 pages

Arduino Beginner To Pro Guide

Arduino programing for beginners to pro

Uploaded by

kevinayim32
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views29 pages

Arduino Beginner To Pro Guide

Arduino programing for beginners to pro

Uploaded by

kevinayim32
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

■ ARDUINO

Complete Beginner to Pro Guide


For GCE Industrial Computing — TVE Advanced & Intermediate Level

■ CHAPTER TOPIC

Chapter 1 What is Arduino? — Hardware explained

Chapter 2 Setting Up the Arduino IDE

Chapter 3 Your First Program — Blink an LED

Chapter 4 Understanding the Code Structure

Chapter 5 Digital Input & Output (Buttons & LEDs)

Chapter 6 Analogue Signals & Sensors

Chapter 7 Control Structures (if, else, loops)

Chapter 8 Variables, Data Types & Operators

Chapter 9 Functions — Writing Clean Code

Chapter 10 Serial Monitor — Debugging Tool

Chapter 11 Interfacing — Controlling High-Voltage Devices

Chapter 12 Real Exam Projects (Full Programs)

Chapter 13 Common Errors & How to Fix Them

Chapter 14 Quick Reference Card

■ How to use this guide: Read from Chapter 1 to 14 in order. Every chapter builds on the last. Type
every code example yourself — don't just read it. That is how you learn Arduino.
CHAPTER 1 — WHAT IS ARDUINO?

1.1 What is Arduino in Simple Words?


Imagine a very small, cheap computer that you can programme to control real-world devices — lights,
motors, sensors, buzzers, even robots. That is Arduino.

Arduino is an open-source electronics platform made up of two things:

• A small circuit board (the hardware) with a microcontroller chip on it


• A software program (IDE) you use on your computer to write and upload code to the board

Think of it like this: The Arduino board is the brain. You write instructions on your computer, send them
to the board, and the board follows those instructions to control whatever you connected to it.

1.2 What Can Arduino Do?


Application What Arduino Controls

Door Alarm Detects door opening with a limit switch → turns ON a lamp

Intruder Alert PIR sensor detects motion → activates a buzzer or alarm

Automatic Lighting LDR sensor detects darkness → switches lights ON

Conveyor Belt Button starts motor; limit switch stops it automatically

Temperature Monitor Reads temperature and turns fan/heater ON or OFF

Watering System Soil moisture sensor → controls water pump

Traffic Lights Controls red/yellow/green LEDs with timed delays

Robot Car Controls motors to move forward, backward, turn left/right

1.3 The Arduino UNO Board — Every Part Explained


The Arduino UNO is the most popular and the one used in your GCE exams. Let's go through every part
of the board:

Part / Label What It Is What It Does

ATmega328P chip The microcontroller (the brain) Runs your programme. Has CPU, RAM, Flash memory all in one chip

USB Port (Type B) Connection to your computer Used to upload your programme AND power the board during develop

Power Jack (barrel) For a 7–12V external power supply


Powers the board when not connected to a computer

Digital pins 0–13 14 digital input/output pins Can be set as INPUT (read signals) or OUTPUT (send signals). Each

Pin 13 (built-in LED) Special digital pin Has a small LED built into the board connected to it — good for testing

Pins 3,5,6,9,10,11 (~) PWM pins (marked with ~) Can simulate analogue output (vary voltage between 0–5V) using Puls

Analogue pins A0–A5 6 analogue input pins Can read variable voltage (0–5V) and convert to numbers 0–1023 (usi

5V pin Power output Provides 5V to power external sensors and components


Part / Label What It Is What It Does

3.3V pin Power output Provides 3.3V for components that need lower voltage

GND pins Ground (0V) The negative/reference connection — every circuit must connect to GN

RESET button Restart button Restarts your programme from the beginning

TX / RX LEDs Communication indicators Blink when data is being sent (TX) or received (RX) via serial

ICSP header Programming header Used by advanced users to programme the chip directly

Vin pin Voltage input Accepts 7–12V when using external power (not USB)

1.4 How Electricity Flows in an Arduino Circuit


Before writing any code, you must understand this basic rule:

■ ELECTRICITY RULE: Current always flows from a HIGH voltage (+) point to a LOW voltage (GND /
0V) point. In Arduino circuits, when a digital pin is HIGH = 5V, current flows OUT of the pin through
your component to GND. When LOW = 0V, no current flows.

Key Voltage Levels:

Signal Voltage Meaning

HIGH 5V (5 volts) ON — current flows — LED lights up — relay activates

LOW 0V (0 volts) OFF — no current — LED off — relay deactivated

Analogue A0–A5 range 0V to 5V Any value between 0 and 5 volts — read as 0 to 1023

1.5 What is a Microcontroller? (Simple Explanation)


A microcontroller is a tiny complete computer on a single chip. The ATmega328P on your Arduino UNO
contains:

• CPU — the processor that runs your code instructions


• Flash Memory (32KB) — where your uploaded programme is stored permanently (like a hard drive)
• SRAM (2KB) — temporary memory used while the programme runs (like RAM)
• EEPROM (1KB) — special memory that keeps data even when power is off
• I/O Pins — the digital and analogue pins you connect components to
• ADC — Analogue to Digital Converter (reads real-world voltages)
• Timers — internal clocks for timing and PWM

■ This is why the ATmega328P is called a microcontroller and NOT a microprocessor — it has
EVERYTHING built in on one chip, whereas a microprocessor (like in your laptop) needs external
RAM, storage, and I/O chips to work.
CHAPTER 2 — SETTING UP THE ARDUINO IDE

2.1 What is the Arduino IDE?


IDE stands for Integrated Development Environment. It is the software you install on your computer
where you write your Arduino code. Think of it as a special notepad for writing Arduino programmes that
can also send your code to the board.

2.2 How to Download and Install the Arduino IDE


Step Action What You See / Do

Step 1 Open a web browser Go to: [Link]/en/software

Step 2 Download the IDE Click 'Windows Installer' (or Mac/Linux depending on your OS). Choose the free version

Step 3 Run the installer Double-click the downloaded .exe file. Click 'I Agree' on licence.

Step 4 Install components Keep all boxes ticked (especially USB driver). Click Install.

Step 5 Finish installation Click Close when done. Find 'Arduino IDE' on your desktop.

Step 6 Open the IDE Double-click the Arduino IDE icon. Wait for it to load.

Step 7 Connect your board Plug Arduino UNO into your computer using the USB cable.

Step 8 Select the board In IDE: Tools → Board → Arduino AVR Boards → Arduino UNO

Step 9 Select the port In IDE: Tools → Port → select the COM port that appears (e.g. COM3)

Step 10 Ready! You are now ready to write and upload programmes!

2.3 Understanding the Arduino IDE Screen


When you open the Arduino IDE, here is what you see and what each part does:

Area / Button Location What It Does

✔ Verify (tick button) Top toolbar — left Checks your code for errors WITHOUT uploading to board. Always ve

→ Upload (arrow button) Top toolbar Compiles AND sends your code to the Arduino board via USB

■ New (page icon) Top toolbar Creates a new empty sketch (programme)

■ Open (folder icon) Top toolbar Opens a saved sketch file (.ino)

■ Save (disk icon) Top toolbar Saves your current sketch to your computer

Serial Monitor (magnifier) Top right corner Opens a window to see messages from your Arduino (very useful for d

Code editor area Middle large white area Where you type your programme code

Message area (black bottom) Bottom of screen Shows compilation messages, errors and success/fail status

Board & Port indicator Bottom right corner Shows which board and port is currently selected

Sketch name Top of code area The name of your current programme file (.ino)
2.4 Understanding Compilation and Uploading
Compilation: When you click Verify/Upload, the IDE first converts your human-readable code (C/C++) into
machine code (binary instructions) that the ATmega328P microcontroller can understand. This process is
called compilation.

Uploading: After successful compilation, the IDE sends the machine code through the USB cable to the
Arduino board. The code is stored in the Flash memory of the ATmega328P. Once uploaded, the Arduino
will run your programme every time it is powered on — even without being connected to the computer.

■ TIP: If you get an error during upload, check: (1) Is the correct board selected? (2) Is the correct port
selected? (3) Is the USB cable properly connected? A blue/green LED on the board should flash during
upload.

2.5 Your First Look at a Blank Sketch


When you open a new sketch, you see this default code:

void setup() { // put your setup code here, to run once: } void loop() { // put your
main code here, to run repeatedly: }

The two words in green (// ...) are comments. Comments are notes for the programmer — Arduino
completely ignores them when running. You write comments to explain what your code does.

The two functions setup() and loop() are REQUIRED in every Arduino programme. You cannot delete
them. We will learn exactly what they do in Chapter 4.
CHAPTER 3 — YOUR FIRST PROGRAMME: BLINK AN LED

3.1 What We Will Build


The classic first Arduino project: make the built-in LED on pin 13 blink ON and OFF every 1 second. This
teaches you the basic structure of every Arduino programme.

3.2 What You Need


• 1× Arduino UNO board
• 1× USB cable (to connect to computer)
• (Optional) 1× LED + 220Ω resistor if you want an external LED
• Arduino IDE installed on your computer

3.3 The Complete Blink Programme — Type This Exactly


// ============================================ // PROGRAMME: Blink the LED on Pin
13 // AUTHOR: Your Name // DATE: Today's date //
============================================ void setup() { // setup() runs ONCE
when Arduino turns on pinMode(13, OUTPUT); // set pin 13 as output } void loop() {
// loop() runs FOREVER after setup finishes digitalWrite(13, HIGH); // turn LED ON
(5V) delay(1000); // wait 1 second (1000ms) digitalWrite(13, LOW); // turn LED OFF
(0V) delay(1000); // wait 1 second }

3.4 What Each Line Does — Line by Line Explanation


Line of Code What It Means in Simple English

// PROGRAMME: Blink... This is a COMMENT — just a note, Arduino ignores it. Use // for single line comments

void setup() { Start of the setup function. The { opens a block of code

// setup() runs ONCE... Another comment explaining what setup does

pinMode(13, OUTPUT); Tell pin 13 to work as an OUTPUT (it will send signals out). The semicolon ; ends every

} Close the setup() function. Every { must have a matching }

void loop() { Start of the loop function — this runs forever and ever

digitalWrite(13, HIGH); Set pin 13 to HIGH = 5V = ON. Current flows. LED lights up.

delay(1000); PAUSE everything for 1000 milliseconds = 1 second. LED stays ON for 1 second.

digitalWrite(13, LOW); Set pin 13 to LOW = 0V = OFF. No current. LED turns off.

delay(1000); Pause for another 1 second. LED stays OFF for 1 second.

} Close the loop() function. Now it loops back to the top of loop() and repeats forever.

3.5 How to Upload and Test


Step Action

1 Type the code exactly as shown above into your Arduino IDE

2 Click the ✔ Verify button — check for any red error messages in the bottom black area

3 If no errors, click the → Upload button

4 Wait for the IDE to say 'Done uploading' in the bottom area

5 Look at pin 13 LED on your Arduino board — it should blink ON for 1 second, OFF for 1 second

6 To make it blink faster, change 1000 to 200 (0.2 seconds) and upload again

7 To make it blink slower, change 1000 to 3000 (3 seconds) and upload again

■ CONGRATULATIONS! If the LED is blinking, you just wrote and uploaded your first Arduino
programme! This is the same basic structure used in every programme, no matter how complex.

3.6 Challenge: Make Your Own Blink Pattern


Try changing the delay times to make an SOS morse code pattern (3 short, 3 long, 3 short):
void setup() { pinMode(13, OUTPUT); } void loop() { // S = three short blinks
digitalWrite(13, HIGH); delay(200); digitalWrite(13, LOW); delay(200);
digitalWrite(13, HIGH); delay(200); digitalWrite(13, LOW); delay(200);
digitalWrite(13, HIGH); delay(200); digitalWrite(13, LOW); delay(500); // O = three
long blinks digitalWrite(13, HIGH); delay(600); digitalWrite(13, LOW); delay(200);
digitalWrite(13, HIGH); delay(600); digitalWrite(13, LOW); delay(200);
digitalWrite(13, HIGH); delay(600); digitalWrite(13, LOW); delay(500); // S = three
short blinks again digitalWrite(13, HIGH); delay(200); digitalWrite(13, LOW);
delay(200); digitalWrite(13, HIGH); delay(200); digitalWrite(13, LOW); delay(200);
digitalWrite(13, HIGH); delay(200); digitalWrite(13, LOW); delay(2000); // long
pause before repeating }
CHAPTER 4 — UNDERSTANDING THE CODE STRUCTURE

4.1 The Two Required Functions


Every single Arduino programme MUST have exactly these two functions. Without them, your code will not
compile.

THE SETUP() FUNCTION


• Runs exactly ONCE when the Arduino powers on or is reset
• Used to configure/initialise things before the main programme starts
• Typical things done in setup(): set pin modes, start serial communication, initialise variables

void setup() { // This code runs ONE TIME only when Arduino starts pinMode(2,
INPUT); // configure pin 2 as input pinMode(13, OUTPUT); // configure pin 13 as
output [Link](9600); // start serial monitor at 9600 baud }

THE LOOP() FUNCTION


• Runs continuously / forever after setup() finishes
• This is where your main programme logic goes
• It repeats over and over as long as the Arduino has power
• Think of it as: read sensors → make decisions → control outputs → repeat

void loop() { // This code runs REPEATEDLY forever // Read a sensor → Make a
decision → Control output → Repeat int sensorValue = digitalRead(2); // read button
on pin 2 if (sensorValue == HIGH) { // if button pressed digitalWrite(13, HIGH); //
turn LED on } else { digitalWrite(13, LOW); // turn LED off } }

4.2 The Order of Execution (What Happens Step by Step)


Order What Happens

1 You plug in power (or press RESET button)

2 Arduino runs any global variable declarations at the very top of the code

3 Arduino runs setup() — ONE TIME — top to bottom

4 Arduino enters loop() and runs it top to bottom

5 When loop() reaches the closing }, it immediately jumps BACK to the start of loop()

6 loop() runs again... and again... and again... forever until power is removed

4.3 Comments — Your Code Notes


Comments are text in your code that Arduino completely ignores. They are there for YOU (and other
people reading your code) to understand what the code does. ALWAYS write comments — especially in
an exam!

// This is a SINGLE LINE comment — everything after // is ignored /* This is a


MULTI-LINE comment It can span many lines Everything between /* and */ is ignored */
int ledPin = 13; // you can put a comment at the END of a line too
4.4 Semicolons — The Most Important Rule
In Arduino/C++ code, every statement (instruction) must end with a semicolon ; Forgetting a
semicolon is the most common beginner mistake and causes a compilation error.

// CORRECT — semicolons at end of each statement: pinMode(13, OUTPUT); // ✓ correct


digitalWrite(13, HIGH); // ✓ correct delay(1000); // ✓ correct // WRONG — missing
semicolons (will cause error): pinMode(13, OUTPUT) // ✗ ERROR! digitalWrite(13,
HIGH) // ✗ ERROR! // NOTE: Function DEFINITIONS do NOT have semicolons: void setup()
{ // ✓ no semicolon after { ... } // ✓ no semicolon after }

4.5 Curly Braces — Opening { and Closing }


Curly braces group lines of code together into a block. Every { MUST have a matching }. If you forget one,
you get a compilation error.

void setup() { // { opens the setup block pinMode(13, OUTPUT); } // } closes the
setup block — MUST be here! void loop() { // { opens the loop block if (x == 1) { //
{ opens the if block digitalWrite(13, HIGH); } // } closes the if block } // }
closes the loop block

■ TIP: In the Arduino IDE, click on any { or } and the matching partner will be highlighted. This helps
you find missing brackets!
CHAPTER 5 — DIGITAL INPUT & OUTPUT

5.1 The Three Digital Functions You Must Know


Function Syntax What It Does

pinMode() pinMode(pin, mode); Configures a pin as INPUT or OUTPUT — ALWAYS done in setup

digitalWrite() digitalWrite(pin, value); Sets an OUTPUT pin to HIGH (5V/ON) or LOW (0V/OFF)

digitalRead() digitalRead(pin); Reads an INPUT pin — returns HIGH or LOW

5.2 pinMode() — Setting Pin Direction


Before using any pin, you MUST tell Arduino whether it is an INPUT or OUTPUT using pinMode(). Always
put this in setup().

void setup() { pinMode(2, INPUT); // pin 2 receives signals (button, sensor)


pinMode(7, INPUT); // pin 7 also receives signals pinMode(13, OUTPUT); // pin 13
sends signals (LED, relay) pinMode(8, OUTPUT); // pin 8 also sends signals }

INPUT_PULLUP: There is also a special mode called INPUT_PULLUP. Use this with buttons when you
want the pin to read HIGH by default and LOW when button is pressed (uses Arduino's built-in resistor).

pinMode(2, INPUT_PULLUP); // pin reads HIGH normally, LOW when button pressed

5.3 digitalWrite() — Controlling Output Pins


// Turn LED ON: digitalWrite(13, HIGH); // pin 13 = 5V = ON // Turn LED OFF:
digitalWrite(13, LOW); // pin 13 = 0V = OFF // Can use 1 instead of HIGH, and 0
instead of LOW: digitalWrite(13, 1); // same as HIGH digitalWrite(13, 0); // same as
LOW

5.4 digitalRead() — Reading Input Pins


digitalRead() checks whether a pin is receiving HIGH (5V) or LOW (0V). We save the result in a variable to
use it in decisions.

int buttonState; // create a variable to store the reading buttonState =


digitalRead(2); // read pin 2 and store result // Or do it in one line: int
buttonState = digitalRead(2);

5.5 Project: Button Controls LED


Circuit: Connect a push button to pin 2 (other side to GND). Connect an LED through a 220Ω resistor to
pin 13 (other side to GND). When you press the button, LED turns ON. When released, LED turns OFF.

// ============================================= // PROJECT: Button controls an LED


// Button → Pin 2 (with pull-up resistor to 5V) // LED → Pin 13 → 220 ohm resistor
→ GND // ============================================= int buttonPin = 2; // button
connected to digital pin 2 int ledPin = 13; // LED connected to digital pin 13 int
buttonState; // variable to store button reading void setup() { pinMode(buttonPin,
INPUT); // button = input pinMode(ledPin, OUTPUT); // LED = output } void loop() {
buttonState = digitalRead(buttonPin); // read the button if (buttonState == HIGH) {
// if button is pressed (HIGH) digitalWrite(ledPin, HIGH); // turn LED ON } else {
// if button not pressed digitalWrite(ledPin, LOW); // turn LED OFF } }

5.6 Multiple LEDs and Buttons


You can control multiple pins at the same time — just declare and configure all of them:

// Multiple LEDs controlled by multiple buttons void setup() { pinMode(2, INPUT); //


button 1 pinMode(3, INPUT); // button 2 pinMode(12, OUTPUT); // LED 1 pinMode(13,
OUTPUT); // LED 2 } void loop() { // Button 1 controls LED 1 if (digitalRead(2) ==
HIGH) { digitalWrite(12, HIGH); } else { digitalWrite(12, LOW); } // Button 2
controls LED 2 if (digitalRead(3) == HIGH) { digitalWrite(13, HIGH); } else {
digitalWrite(13, LOW); } }
CHAPTER 6 — ANALOGUE SIGNALS & SENSORS

6.1 Digital vs Analogue — What is the Difference?


Type Values Example

Digital Only TWO values: HIGH (5V) or LOW (0V) Button pressed or not, LED on or off

Analogue ANY value between 0V and 5V continuously Temperature (25.3°C), light level, volume

6.2 analogRead() — Reading Analogue Sensors


Arduino UNO has 6 analogue input pins: A0, A1, A2, A3, A4, A5. These pins can read any voltage
between 0V and 5V. The built-in ADC (Analogue to Digital Converter) converts that voltage to a number
between 0 and 1023.

Formula: Voltage = (analogRead value / 1023) × 5V


int sensorValue = analogRead(A0); // read voltage on pin A0 (0 to 1023) // Examples
of what different readings mean: // 0 = 0V (sensor at minimum) // 512 = 2.5V (sensor
at middle) // 1023 = 5V (sensor at maximum)

6.3 analogWrite() — Simulating Analogue Output (PWM)


Arduino digital pins cannot truly output variable voltages, but PWM pins (~) can simulate it by rapidly
switching ON and OFF. This is called PWM — Pulse Width Modulation. analogWrite() accepts a value
from 0 to 255 (0 = fully OFF, 255 = fully ON).

PWM pins on Arduino UNO: pins 3, 5, 6, 9, 10, 11 (marked with ~ on the board)
// analogWrite only works on PWM pins (3,5,6,9,10,11) analogWrite(9, 0); // LED
completely OFF analogWrite(9, 128); // LED at HALF brightness (50%) analogWrite(9,
255); // LED at FULL brightness (100%) // Gradually increase brightness (fade in
effect): for (int brightness = 0; brightness <= 255; brightness++) { analogWrite(9,
brightness); delay(10); // wait 10ms between each step }

6.4 Common Sensors Used in Exam Questions


Sensor Type Output What It Detects Typical Connection

PIR (Passive Infrared) Motion Digital HIGH/LOWHuman body heat / movement


Digital pin (e.g., D2)

LDR (Light Dependent Resistor)


Light Analogue or Digital
Brightness/darkness A0–A5 or digital pin

Limit Switch Contact Digital HIGH/LOWPhysical touch / end of travel


Digital pin (e.g., D3)

Push Button Manual Digital HIGH/LOWManual press by person Digital pin (e.g., D2)

Temperature Sensor (LM35)


Temperature Analogue voltage Air/surface temperature A0–A5

Ultrasonic (HC-SR04) Distance Digital pulse Distance to an object Two digital pins (TRIG, ECHO)

Soil Moisture Sensor Moisture Analogue or Digital


Water content in soil A0–A5 or digital pin
6.5 Project: LDR Automatic Light (Analogue Sensor)
Circuit: LDR connected to A0 (voltage divider with a fixed resistor). LED on pin 9 (PWM). When dark →
LED bright. When bright → LED dim.

// Automatic light using LDR sensor // LDR on A0, LED on pin 9 (PWM) int ldrPin =
A0; // LDR connected to analogue pin A0 int ledPin = 9; // LED connected to PWM pin
9 void setup() { pinMode(ledPin, OUTPUT); // LED is output [Link](9600); //
start serial monitor } void loop() { int lightLevel = analogRead(ldrPin); // read
light (0-1023) [Link](lightLevel); // print to serial monitor // Map: dark
(low reading) = bright LED, light = dim LED int ledBrightness = map(lightLevel, 0,
1023, 255, 0); analogWrite(ledPin, ledBrightness); // set LED brightness delay(100);
// small delay for stability }

The map() function re-maps a number from one range to another. map(value, fromLow, fromHigh,
toLow, toHigh). In the example above: map(lightLevel, 0, 1023, 255, 0) converts 0→255 and 1023→0
(inverts the reading so dark = bright LED).
CHAPTER 7 — CONTROL STRUCTURES (if, else, loops)

7.1 The if Statement — Making Decisions


The if statement lets your Arduino make a decision. If the condition is TRUE, run the code inside. If
FALSE, skip it.

// Basic if statement: if (condition) { // code runs ONLY if condition is TRUE } //


Example: if (digitalRead(2) == HIGH) { digitalWrite(13, HIGH); // turn LED on if
button pressed }

7.2 The if-else Statement


// if-else: do one thing OR another if (condition) { // runs if condition is TRUE }
else { // runs if condition is FALSE } // Example — PIR sensor alarm: if
(digitalRead(pirPin) == HIGH) { // motion detected digitalWrite(buzzerPin, HIGH); //
alarm ON } else { // no motion digitalWrite(buzzerPin, LOW); // alarm OFF }

7.3 The if-else if-else Chain


For multiple conditions, chain them together with else if:

// Check temperature level and react int temp = analogRead(A0); // read temperature
sensor if (temp > 800) { // Very hot digitalWrite(redLED, HIGH);
digitalWrite(greenLED, LOW); } else if (temp > 400) { // Warm digitalWrite(redLED,
LOW); digitalWrite(greenLED, HIGH); } else { // Cool digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW); }

7.4 Comparison Operators — Used in Conditions


Operator Meaning Example Result

== Equal to x == 5 True if x is exactly 5

!= Not equal to x != 5 True if x is anything except 5

> Greater than x>3 True if x is 4, 5, 6...

< Less than x < 10 True if x is 9, 8, 7...

>= Greater than or equal to x >= 60 True if x is 60, 61, 62...

<= Less than or equal to x <= 100 True if x is 100, 99, 98...

■■ VERY IMPORTANT: Use == (double equals) to COMPARE. Use = (single equals) to ASSIGN a
value. if (x = 5) is WRONG. if (x == 5) is CORRECT.

7.5 Logical Operators — Combining Conditions


Operator Symbol Meaning Example

AND && BOTH conditions must be true if (x > 0 && x < 100)
Operator Symbol Meaning Example

OR || AT LEAST ONE condition must be trueif (x == 1 || x == 2)

NOT ! Reverses the condition if (!buttonPressed)

7.6 The for Loop — Repeat a Fixed Number of Times


// Blink LED 5 times using a for loop for (int i = 0; i < 5; i++) { // i starts at 0,
increases by 1 each time, // stops when i reaches 5 digitalWrite(13, HIGH);
delay(200); digitalWrite(13, LOW); delay(200); } // After loop: LED blinked exactly
5 times // Structure of for loop: // for (start; condition; increment) { // code to
repeat // }

7.7 The while Loop — Repeat While Condition is True


// Keep alarm ON while motion is detected while (digitalRead(pirPin) == HIGH) {
digitalWrite(buzzerPin, HIGH); // alarm stays on delay(100); } // When PIR goes LOW,
while loop exits digitalWrite(buzzerPin, LOW); // alarm off // While loop with
counter: int count = 0; while (count < 10) { digitalWrite(13, HIGH); delay(100);
digitalWrite(13, LOW); delay(100); count++; // count = count + 1 }
CHAPTER 8 — VARIABLES, DATA TYPES & OPERATORS

8.1 What is a Variable?


A variable is a named storage location in memory that holds a value. Think of it like a labelled box: you put
a value in the box, give the box a name, and later you can read or change the value inside.

// Creating a variable: dataType variableName = startingValue; // Examples: int


ledPin = 13; // integer variable named ledPin, storing value 13 int buttonState = 0;
// integer, starts at 0 float temperature = 0.0; // decimal number variable

8.2 Data Types in Arduino


Data Type Size Range / What It Stores Example

int 2 bytes Whole numbers: -32,768 to 32,767 int count = 0;

long 4 bytes Large whole numbers: -2 billion to +2 billion


long bigNum = 100000;

float 4 bytes Decimal numbers (6-7 decimal places) float temp = 36.6;

double 4 bytes Larger decimal numbers double pi = 3.14159;

char 1 byte A single character char grade = 'A';

String Variable A sequence of characters (text) String name = "Arduino";

boolean 1 byte true or false only boolean isOn = true;

byte 1 byte Whole number 0 to 255 byte brightness = 128;

8.3 Where to Declare Variables


Variables can be declared in two places, and this affects where they can be used:

// GLOBAL variables — declared OUTSIDE all functions // Can be used ANYWHERE in your
programme int buttonPin = 2; // global — accessible in setup() AND loop() int ledPin
= 13; // global int counter = 0; // global void setup() { pinMode(buttonPin, INPUT);
// can use buttonPin here pinMode(ledPin, OUTPUT); } void loop() { // LOCAL variable
— declared INSIDE a function // Can ONLY be used inside this loop() function int
reading = digitalRead(buttonPin); // local variable counter++; // can use global
counter here }

8.4 Arithmetic Operators


Operator Symbol Example Result

Addition + x = 5 + 3; x=8

Subtraction - x = 10 - 4; x=6

Multiplication * x = 3 * 4; x = 12

Division / x = 10 / 2; x=5
Operator Symbol Example Result

Modulo (remainder) % x = 10 % 3; x = 1 (remainder of 10÷3)

Increment (+1) ++ x++; x increases by 1

Decrement (-1) -- x--; x decreases by 1

8.5 Assignment Operators (Shortcuts)


x = 10; // assign value 10 to x x += 5; // same as: x = x + 5 → x becomes 15 x -= 3;
// same as: x = x - 3 → x becomes 12 x *= 2; // same as: x = x * 2 → x becomes 24 x
/= 4; // same as: x = x / 4 → x becomes 6 x++; // same as: x = x + 1 → x becomes 7
x--; // same as: x = x - 1 → x becomes 6

8.6 Constants — Values That Never Change


// Use 'const' to declare a constant (never changes) const int LED_PIN = 13; //
naming convention: UPPER_CASE for constants const int BUTTON_PIN = 2; const float PI
= 3.14159; // Arduino also has built-in constants: // HIGH = 1 (pin voltage high) //
LOW = 0 (pin voltage low) // INPUT = 0 (pin direction) // OUTPUT = 1 (pin direction)
// true = 1, false = 0
CHAPTER 9 — FUNCTIONS — WRITING CLEAN CODE

9.1 What is a Function?


A function is a named block of code that performs a specific task. Instead of writing the same code
multiple times, you write it once in a function and call it by name whenever needed. This makes your code
cleaner, shorter and easier to understand.

You already know two functions: setup() and loop(). Now you will learn to create your own!

9.2 Creating Your Own Function


// Function DEFINITION — write this BELOW loop() void blinkLED(int pin, int times) {
// function name and parameters for (int i = 0; i < times; i++) { digitalWrite(pin,
HIGH); delay(300); digitalWrite(pin, LOW); delay(300); } } void setup() {
pinMode(13, OUTPUT); } void loop() { blinkLED(13, 3); // CALL the function: blink
pin 13, 3 times delay(2000); blinkLED(13, 5); // CALL again: blink 5 times
delay(2000); }

9.3 Functions That Return a Value


// A function that returns an int value int readButton(int pin) { int state =
digitalRead(pin); return state; // send this value back to whoever called the
function } void loop() { int btn = readButton(2); // receive the returned value if
(btn == HIGH) { digitalWrite(13, HIGH); } } // Function that returns true/false
boolean isMotionDetected(int pirPin) { return (digitalRead(pirPin) == HIGH); } void
loop() { if (isMotionDetected(7)) { digitalWrite(8, HIGH); // turn alarm ON } }

■ TIP: Use void before a function name if it does NOT return a value. Use int, float, boolean etc. if it
DOES return something.
CHAPTER 10 — SERIAL MONITOR (Your Debugging Tool)

10.1 What is the Serial Monitor?


The Serial Monitor is a window in the Arduino IDE that lets your Arduino board send text messages back to
your computer screen. It is the most important tool for debugging (finding and fixing problems in your
code).

Think of it as a walkie-talkie between your Arduino and your computer. You can see sensor values,
variable values, and messages printed from your code.

10.2 How to Use the Serial Monitor


Step Action

1 In setup(): write [Link](9600); — this starts serial communication at 9600 baud

2 In your code: use [Link]() to print messages or values

3 Upload the code to the Arduino

4 In Arduino IDE: click the Serial Monitor icon (top right) or press Ctrl+Shift+M

5 Make sure the baud rate in Serial Monitor (bottom right dropdown) matches your [Link]() value (9600)

6 You will see your messages appearing in real time!

10.3 Serial Functions


[Link](9600); // MUST be in setup() — sets communication speed
[Link](value); // print a value WITHOUT moving to new line
[Link](value); // print a value AND move to next line // Examples:
[Link]('Hello Arduino!'); // prints text [Link](1234); // prints a
number int sensorValue = analogRead(A0); [Link]('Sensor reading: '); // print
label [Link](sensorValue); // print the number on same line

10.4 Example: Monitoring a Button and Sensor


// Print button state and sensor value every 500ms void setup() {
[Link](9600); // start serial monitor pinMode(2, INPUT); // button } void
loop() { int btnState = digitalRead(2); int lightLevel = analogRead(A0);
[Link]('Button: '); [Link](btnState); // 1 or 0 [Link](' Light:
'); [Link](lightLevel); // 0 to 1023 delay(500); // update every 0.5 seconds
}

■ EXAM USE: In your GCE practical exam, the serial monitor is used to verify your sensors are
working correctly before or during testing.
CHAPTER 11 — INTERFACING (Controlling Real Devices)

11.1 Why You Need Interfacing — The Problem


Your Arduino UNO can only output 5V maximum and 40mA per pin. This is enough to light a small LED
but NOT enough to power:

• A 220V AC fan or lamp (mains electricity!)


• A DC motor (needs more current than Arduino can provide)
• A high-power buzzer or alarm
• Any device that needs more than 5V or more than 40mA

■■ WARNING: NEVER connect 220V mains electricity directly to an Arduino pin. It will DESTROY
your Arduino instantly and could seriously injure you. Always use a relay or other interfacing circuit for
mains voltage.

11.2 The RELAY — Most Important Interfacing Device


A relay is an electrically controlled mechanical switch. It has two completely separate circuits inside it:

• Control side (LOW voltage): Connected to Arduino — receives the 5V signal. When the Arduino
sends HIGH, an electromagnet inside the relay activates.
• Switch side (HIGH voltage): Connected to your 220V lamp, fan, motor etc. When the
electromagnet activates, it closes/opens the mechanical switch.

Relay Pin / Terminal Connect To Purpose

IN (control pin) Arduino digital output pin (e.g., D8) Arduino sends HIGH to activate relay

VCC Arduino 5V pin Powers the relay module

GND Arduino GND pin Ground reference

COM (Common) One wire of mains circuit The common terminal of the switch

NO (Normally Open) Other wire of load Circuit OPEN when relay OFF; CLOSED when relay ON

NC (Normally Closed) Alternative connection Circuit CLOSED when relay OFF; OPEN when relay ON

In exam questions, use NO (Normally Open) for your load connections. This means: relay OFF → load
OFF. Relay ON → load ON. This is the safest and most common configuration.

11.3 How to Wire a Relay to Arduino (Concept)


The relay acts as the 'middleman' between Arduino and the 220V device:

Connection From To

Power to relay module Arduino 5V pin Relay VCC pin

Ground Arduino GND pin Relay GND pin

Control signal Arduino digital output pin (e.g., D8) Relay IN pin

Mains power 220V Live wire Relay COM terminal


Connection From To

Load connection Relay NO terminal One terminal of lamp/motor

Mains return Other terminal of lamp/motor 220V Neutral wire

11.4 Other Interfacing Devices


Device What It Is When to Use It

Transistor (NPN BC547) Electronic switch using current Controlling small DC loads (mini motors, buzzers) up to a few hundred

Motor Driver (L298N) H-bridge IC for motor control Controlling speed and direction of DC motors (conveyor, robot)

Optocoupler (4N35) Light-based isolation componentElectrically isolating Arduino from sensitive or noisy circuits

Relay Module (5V) Pre-built relay with transistor driver


Easiest way to switch 220V AC devices — used in all exam questions

ULN2003 Darlington 8-channel transistor array Driving multiple loads (stepper motors, multiple relays)

11.5 Voltage Level of Arduino Digital Output


This is a common exam question — the answer you must remember:

■ The digital output signal from Arduino UNO is 5V (HIGH) or 0V (LOW). This 5V signal is used to
control the relay's IN pin, which then safely switches the high-voltage load on or off.
CHAPTER 12 — REAL EXAM PROJECTS (Complete Solutions)

12.1 HOW TO APPROACH EVERY ARDUINO EXAM QUESTION


Step What To Do

Step 1: Read carefully Identify the INPUT devices (sensors, buttons) and OUTPUT devices (lamp, buzzer, motor, LED)

Step 2: Note the pin numbersThe question tells you which pin each device is connected to — note them down

Step 3: Understand the logicWhen sensor is HIGH → what happens? When LOW → what happens? Write it in English first

Step 4: Write the algorithm List the steps in plain English before coding

Step 5: Code it Write setup() to configure pins, write loop() with if/else for the logic

Step 6: Verify Click Verify in IDE to check for errors, fix any red error messages

Step 7: Upload and test Upload, observe behaviour, fix any logical errors

12.2 PROJECT A: DOOR MONITORING SYSTEM


Scenario: A limit switch is fitted to a door and connected to Arduino pin D3. A lamp is connected as output
via relay on pin D8. External power supply used. When door OPENS → limit switch HIGH → lamp ON.
When door CLOSES → switch LOW → lamp OFF.

Algorithm (Step-by-Step Instructions):


1. START
2. Configure pin 3 as INPUT (limit switch)
3. Configure pin 8 as OUTPUT (relay/lamp)
4. Read the state of the limit switch (pin 3)
5. IF limit switch is HIGH (door open) THEN: set pin 8 HIGH (lamp ON)
6. ELSE (door closed): set pin 8 LOW (lamp OFF)
7. Go back to Step 4 (repeat forever)

// ================================================ // PROJECT A: Arduino Door


Monitoring System // INPUT: Limit switch on digital pin D3 // OUTPUT: Lamp via relay
on digital pin D8 // Logic: Door open (HIGH) → Lamp ON // Door closed (LOW) → Lamp
OFF // ================================================ int limitSwitchPin = 3; //
limit switch connected to D3 int lampRelayPin = 8; // relay controlling lamp on D8
int switchState; // variable to hold switch reading void setup() {
pinMode(limitSwitchPin, INPUT); // limit switch = INPUT pinMode(lampRelayPin,
OUTPUT); // relay/lamp = OUTPUT [Link](9600); // for debugging } void loop() {
switchState = digitalRead(limitSwitchPin); // read switch if (switchState == HIGH) {
// Door is OPEN — limit switch activated digitalWrite(lampRelayPin, HIGH); // turn
lamp ON [Link]('Door OPEN - Lamp ON'); } else { // Door is CLOSED — limit
switch released digitalWrite(lampRelayPin, LOW); // turn lamp OFF
[Link]('Door CLOSED - Lamp OFF'); } }

Why is a relay used between Arduino and the lamp?


The relay is used because the lamp operates on 220V AC mains electricity, which is far too dangerous and
too much voltage for the Arduino pin (which outputs only 5V). The relay electrically separates the Arduino
(5V) circuit from the mains (220V) circuit, allowing the Arduino to safely switch the lamp ON and OFF.

12.3 PROJECT B: INTRUDER ALERT SYSTEM (PIR + BUZZER)


Scenario: PIR motion sensor on D2 (input). 220V AC alarm buzzer via relay on D8 (output). PIR HIGH =
motion detected → buzzer ON. PIR LOW = no motion → buzzer OFF.
// ================================================ // PROJECT B: Intruder Alert —
PIR Sensor + Buzzer // INPUT: PIR motion sensor on pin D2 // OUTPUT: Buzzer/alarm
via relay on pin D8 // Logic: Motion detected (HIGH) → Buzzer ON // No motion (LOW)
→ Buzzer OFF // ================================================ int pirPin = 2; //
PIR sensor on digital pin 2 int buzzerPin = 8; // relay/buzzer on digital pin 8 void
setup() { pinMode(pirPin, INPUT); // PIR sensor = INPUT pinMode(buzzerPin, OUTPUT);
// buzzer/relay = OUTPUT [Link](9600); [Link]('Intruder Alert System
Ready'); } void loop() { int motionDetected = digitalRead(pirPin); // read PIR if
(motionDetected == HIGH) { digitalWrite(buzzerPin, HIGH); // ALARM ON
[Link]('MOTION DETECTED! ALARM ON!'); } else { digitalWrite(buzzerPin, LOW);
// alarm off [Link]('No motion. System monitoring.'); } delay(500); // check
every 0.5 seconds }

12.4 PROJECT C: AUTOMATIC LIGHTING CONTROL (LDR + LAMP)


Scenario: Digital light sensor (LDR) on pin D2. 220V lamp via relay on pin D13. Sensor LOW = insufficient
light → lamp ON. Sensor HIGH = sufficient light → lamp OFF.

// ================================================ // PROJECT C: Automatic Lighting


— LDR + Lamp // INPUT: Light sensor (LDR) on digital pin D2 // OUTPUT: 220V lamp via
relay on pin D13 // Logic: Insufficient light (LOW) → lamp ON // Sufficient light
(HIGH) → lamp OFF // ================================================ int
lightSensorPin = 2; // LDR connected to pin D2 int lampRelayPin = 13; // relay/lamp
connected to D13 void setup() { pinMode(lightSensorPin, INPUT); // LDR = INPUT
pinMode(lampRelayPin, OUTPUT); // lamp/relay = OUTPUT [Link](9600); } void
loop() { int sensorOutput = digitalRead(lightSensorPin); // read LDR if
(sensorOutput == LOW) { // Insufficient light — it is dark
digitalWrite(lampRelayPin, HIGH); // turn lamp ON [Link]('DARK - Lamp ON');
} else { // Sufficient light — it is bright digitalWrite(lampRelayPin, LOW); // turn
lamp OFF [Link]('LIGHT - Lamp OFF'); } delay(200); }

12.5 PROJECT D: CONVEYOR BELT SYSTEM (START BUTTON + LIMIT


SWITCH)
Scenario: START push button on D2. Limit switch on D3. Relay/motor driver on D8 (motor). Indicator LED
on D9. Press START → motor runs + LED ON. Limit switch triggered → motor stops + LED OFF.

// ================================================ // PROJECT D: Conveyor Belt


Start-Stop System // INPUT: START button D2, Limit switch D3 // OUTPUT: Motor/relay
D8, Indicator LED D9 // Logic: START pressed → motor ON + LED ON // Limit switch
pressed → motor OFF + LED OFF // ================================================
int startButtonPin = 2; // START push button on D2 int limitSwitchPin = 3; // limit
switch on D3 int motorRelayPin = 8; // motor relay/driver on D8 int indicatorLEDPin
= 9; // indicator LED on D9 void setup() { pinMode(startButtonPin, INPUT); // button
= INPUT pinMode(limitSwitchPin, INPUT); // limit switch = INPUT
pinMode(motorRelayPin, OUTPUT); // motor = OUTPUT pinMode(indicatorLEDPin, OUTPUT);
// LED = OUTPUT [Link](9600); [Link]('Conveyor System Ready'); } void
loop() { int startState = digitalRead(startButtonPin); // read START int limitState
= digitalRead(limitSwitchPin); // read limit if (startState == HIGH) { // START
button pressed — run motor digitalWrite(motorRelayPin, HIGH); // motor ON
digitalWrite(indicatorLEDPin, HIGH); // LED ON [Link]('Motor RUNNING - LED
ON'); } if (limitState == HIGH) { // Limit switch triggered — item reached end
digitalWrite(motorRelayPin, LOW); // motor STOP digitalWrite(indicatorLEDPin, LOW);
// LED OFF [Link]('Limit reached - Motor STOPPED'); } }

12.6 PROJECT E: POULTRY FARM LIGHTING (LDR + BUZZER + LAMP)


Scenario: Light sensor on D2 (input). Buzzer on D13, Lamp via relay on D8. Buzzer on D13 connected to
pin 13, Lamp relay on D8, LDR on D2. Sensor LOW → buzzer ON (alert). Sensor HIGH → buzzer OFF.
// ================================================ // PROJECT E: Poultry Farm
Lighting System // Buzzer on pin 13, Light sensor (LDR) on pin 2 // Logic: LOW
sensor (dark) → buzzer ON // HIGH sensor (bright) → buzzer OFF //
================================================ int lightSensorPin = 2; // LDR
connected to D2 int buzzerPin = 13; // buzzer connected to D13 void setup() {
pinMode(lightSensorPin, INPUT); // LDR = INPUT pinMode(buzzerPin, OUTPUT); // buzzer
= OUTPUT [Link](9600); } void loop() { int sensorState =
digitalRead(lightSensorPin); if (sensorState == LOW) { // Insufficient light
detected digitalWrite(buzzerPin, HIGH); // buzzer ON (alert) [Link]('Low
light - Buzzer ALERTING'); } else { // Sufficient light digitalWrite(buzzerPin,
LOW); // buzzer OFF [Link]('Sufficient light - Buzzer OFF'); } delay(100); }
CHAPTER 13 — COMMON ERRORS & HOW TO FIX THEM

13.1 Compilation Errors (Red errors in IDE)


Error Message Cause Fix

expected ';' before... Missing semicolon at end of a statement


Add ; at the end of the flagged line

'xxx' was not declared in this scope Variable used before being declared,
Declare
or spelling
the variable
mistake
at top, check spelling (case sensitive!)

expected '}' at end of input Missing closing curly brace } Count { and } — add the missing }

a function-definition is not allowed here


Function defined inside another Move
function
your custom function OUTSIDE and BELOW loop()

'pinMode' was not declared Usually means setup() is missingCheck


or misspelled
void setup() { is correctly written

stray '\' in program Illegal character in code Remove any special characters you may have accidentally type

expected ')' before... Missing closing parenthesis Find the ( that has no matching )

call of overloaded 'xxx' is ambiguousWrong data type used in a function


Check the correct data types for the function parameters

13.2 Upload Errors


Error Cause Fix

avrdude: ser_open(): can't open Wrong


device COM port selected or Arduino
Checknot
Tools → Port and select the correct COM port
connected

No device found on COMx Arduino not recognised by computer


Try a different USB cable; try a different USB port on computer

Sketch too big Your code is larger than the Arduino's


Reduce 32KB
codeflash
size;memory
remove unused variables and functions

programmer is not responding Board type wrong Check Tools → Board → Arduino UNO is selected

timeout communicating with programmer


Upload started before Arduino was
Press
ready
the Reset button on Arduino, then immediately click Upload

13.3 Logical Errors (Code runs but does the wrong thing)
These are the trickiest errors because the IDE does not flag them. You have to find them yourself using
the Serial Monitor.

Symptom Likely Cause Fix

LED never turns ON Wrong pin number; pinMode setCheck


as INPUT
pin numbers
instead ofmatch
OUTPUT
your wiring; check pinMode(pin, OUTPU

LED always ON, never OFF Logic inverted (== LOW when should
Checkbe
your
== if/else
HIGH)conditions; swap HIGH and LOW

Button press does nothing Button wired to wrong pin; not reading
Check correct
your wire
pinconnections match your pin numbers in code

Sensor reads 0 always Sensor not powered correctly; wrong


Checkpin
5Vused
and GND connections; verify correct analogue pin

Programme seems to freeze Infinite loop or very long delay() Check your while loop conditions; reduce delay values

Using = instead of == Assignment instead of comparison


In if statements, always use == to compare values

13.4 Golden Debugging Rules


1. ADD [Link]() EVERYWHERE to print what is happening at each step
2. Check your WIRING first — most problems are connection mistakes
3. Make sure you selected the right BOARD (Arduino UNO) and PORT in Tools menu
4. Click VERIFY before UPLOAD to catch code errors first
5. Check ALL your { } are matched — each { needs a }
6. Check ALL your ; are present at the end of every statement
7. Variable names are CASE SENSITIVE: ledPin ≠ LedPin ≠ LEDPIN
8. If completely stuck, start with a simple blink sketch and add one thing at a time
CHAPTER 14 — QUICK REFERENCE CARD

14.1 All Key Functions at a Glance


Function Syntax Returns Where Used

pinMode() pinMode(pin, INPUT/OUTPUT); nothing setup()

digitalWrite() digitalWrite(pin, HIGH/LOW); nothing loop()

digitalRead() digitalRead(pin); HIGH or LOW loop()

analogRead() analogRead(A0); 0 to 1023 loop()

analogWrite() analogWrite(pin, 0-255); nothing loop() — PWM pins only

delay() delay(milliseconds); nothing anywhere

millis() millis(); ms since start anywhere

[Link]() [Link](9600); nothing setup()

[Link]() [Link](value); nothing anywhere

[Link]() [Link](value); nothing anywhere

map() map(val,fromL,fromH,toL,toH); mapped value anywhere

constrain() constrain(val, min, max); constrained value anywhere

abs() abs(value); absolute value anywhere

max() max(a, b); larger value anywhere

min() min(a, b); smaller value anywhere

random() random(min, max); random int anywhere

14.2 Pin Modes & Values Reference


Value/Constant Numeric Value Meaning

HIGH 1 5V — pin is ON, output 5V, or input reads >3V

LOW 0 0V — pin is OFF, output 0V, or input reads <1.5V

INPUT 0 Pin configured to receive signals from outside

OUTPUT 1 Pin configured to send signals out

INPUT_PULLUP 2 Input with internal 47kΩ pull-up resistor (default HIGH)

true 1 Boolean true — condition is satisfied

false 0 Boolean false — condition is not satisfied

A0–A5 14–19 Analogue pins — use analogRead() on these

14.3 Programme Template — Copy This for Every New Exam Project
// ================================================ // PROGRAMME NAME: [write name
here] // INPUT DEVICE: [e.g., PIR sensor on D2] // OUTPUT DEVICE: [e.g., Buzzer on
D8] // LOGIC: [describe what it should do] //
================================================ // --- GLOBAL VARIABLES AND PIN
DEFINITIONS --- int inputPin = 2; // [change pin number] int outputPin = 8; //
[change pin number] // --- SETUP: Runs ONE TIME when Arduino starts --- void setup()
{ pinMode(inputPin, INPUT); // configure input pinMode(outputPin, OUTPUT); //
configure output [Link](9600); // start serial monitor } // --- LOOP: Runs
REPEATEDLY forever --- void loop() { int sensorState = digitalRead(inputPin); //
read input if (sensorState == HIGH) { // [condition 1] — what to do when HIGH
digitalWrite(outputPin, HIGH); [Link]('[describe action]'); } else { //
otherwise — what to do when LOW digitalWrite(outputPin, LOW);
[Link]('[describe action]'); } delay(100); // small stabilisation delay }

14.4 Exam Marks Allocation Guide


Task in Exam Marks Key Points to Score Full Marks

State voltage of digital output from Arduino


2 marksUNO Answer: 5V (HIGH) — always say 5V not just 'high voltage'

State type of control system + reason


4 marks Closed-loop because it uses feedback from a sensor to control output. Open-

State one interfacing device for 220V


2 marks Relay — always answer relay for 220V AC control questions

Write an Arduino programme 12 marks Full marks: correct setup() with all pinModes, correct loop() with correct if/else

Identify input and output devices 4 marks Input = sensor (PIR, LDR, limit switch, button). Output = lamp, buzzer, LED, m

Give one function of Arduino board 2 marks Processes sensor inputs and controls output devices according to the program

14.5 The ONE Page Summary You Must Memorise


WHAT HOW

Start every programme void setup() { } and void loop() { }

Configure a pin pinMode(pin, INPUT); or pinMode(pin, OUTPUT); in setup()

Turn something ON digitalWrite(pin, HIGH);

Turn something OFF digitalWrite(pin, LOW);

Read a button/sensor int state = digitalRead(pin);

Make a decision if (state == HIGH) { ... } else { ... }

Pause/wait delay(1000); // 1000ms = 1 second

Read analogue sensor int val = analogRead(A0); // returns 0–1023

Variable voltage output analogWrite(pin, 128); // 0–255, PWM pins only

Print for debugging [Link](9600); in setup, [Link]() in loop

Control 220V device Use a RELAY module between Arduino and the mains device

Arduino output voltage 5V (HIGH) / 0V (LOW)


WHAT HOW

Analogue pins label A0, A1, A2, A3, A4, A5

ATmega328P chip does CPU + RAM + Flash + I/O all on ONE chip

setup() runs ONCE at startup

loop() runs CONTINUOUSLY / FOREVER

■ You now have everything you need to understand and write Arduino
programmes for your GCE exam. Remember: PRACTICE is the key. Type every
code example, make changes, observe what happens. The more you write, the
better you get. GOOD LUCK! You've got this! ■

You might also like