Arduino Hardware Setup and Programming
Arduino Hardware Setup and Programming
INTRODUCTION
/*
Blink
Turns an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
void setup() {
// Initialize the digital pin as an output
pinMode(LED_PIN, OUTPUT);
}
void loop() {
// Turn the LED on
digitalWrite(LED_PIN, HIGH);
// Wait for one second
delay(1000);
// Turn the LED off
digitalWrite(LED_PIN, LOW);
// Wait for one second
delay(1000);
}
Breakdown of the Sketch Components
1. Header Comment Block
/*
Blink
Turns an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
Purpose: Provides a description of the sketch.
Details: Enclosed between /* and */, this multi-line comment explains what
the program does and includes any pertinent information.
2. Library Inclusion
#include <Arduino.h> // Include the core Arduino library
Purpose: Incorporates external libraries that provide additional
functionality.
Details: The #include directive adds the specified library to the sketch. In
this case, <Arduino.h> is the core library that includes essential functions
for Arduino programming.
3. Global Variable Declaration
// Define a constant for the LED pin
const int LED_PIN = 13;
Purpose: Declares global variables or constants accessible throughout the
sketch.
Details: const int LED_PIN = 13; defines a constant integer LED_PIN with
a value of 13, representing the pin number connected to the LED.
4. setup() Function
void setup() {
// Initialize the digital pin as an output
pinMode(LED_PIN, OUTPUT);
}
Purpose: Configures initial settings; runs once when the sketch starts.
Details: The setup() function is called once when the program starts. Here,
pinMode(LED_PIN, OUTPUT); sets the LED pin as an output.
5. loop() Function
cpp
CopyEdit
void loop() {
// Turn the LED on
digitalWrite(LED_PIN, HIGH);
// Wait for one second
delay(1000);
// Turn the LED off
digitalWrite(LED_PIN, LOW);
// Wait for one second
delay(1000);
}
Purpose: Contains the main code to be executed repeatedly.
Details: The loop() function runs continuously after setup(). In this
example:
digitalWrite(LED_PIN, HIGH); turns the LED on.
delay(1000); pauses the program for 1000 milliseconds (1 second).
digitalWrite(LED_PIN, LOW); turns the LED off.
Another delay(1000); pauses the program for another second
before repeating the cycle.
Summary
This basic "Blink" sketch demonstrates the fundamental structure of an Arduino
program, comprising:
Comments: For documentation and code clarity.
Library Inclusions: To extend functionality.
Global Variables/Constants: For configuration and easy maintenance.
setup() Function: For initialization.
loop() Function: For the main logic that runs repeatedly.
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH);
digitalWrite(buzzerPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);
delay(1000);
}
5. Click "Start Simulation" → Observe LED and buzzer alternately turning on/off.
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
float temp = dht.readTemperature();
Serial.print("Temperature: ");
Serial.print(temp);
Serial.println(" °C");
delay(2000);
}
4. Click "Start Simulation" → Check Serial Monitor for temperature readings.
void setup() {
pinMode(sensorPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int state = digitalRead(sensorPin);
if (state == LOW) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
delay(500);
}
5. Click "Start Simulation" → LED should turn on when an object is detected.
Test Components:
Each Group will prepare the following materials and components:
Laptop/ desktop/ android phone
Arduino Board (LAFVIN R3 CH340)
Breadboard
USB Cable
Jumper Wires (M-M Dupont Wire)
Resistors (Assorted)
LEDs (RGB and Individual Red, Yellow, Green)
Passive and Active Buzzers
Potentiometer (10K)
Sensors:
DHT11 Temperature and Humidity Sensor
Soil Moisture Sensor
Water Level Sensor
Sound Sensor Module
Object Avoidance Module
Photoresistor (LDR)
Thermistor
Tilt Switch
Displays and Switches:
7-Segment Display (1-Digit)
Button Switches (Assorted Colors)
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH);
digitalWrite(buzzerPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);
delay(1000);
}
#define DHTPIN 2
#define DHTTYPE DHT11
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
float temp = dht.readTemperature();
Serial.print("Temperature: ");
Serial.print(temp);
Serial.println(" °C");
delay(2000);
}
void setup() {
pinMode(sensorPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int state = digitalRead(sensorPin);
if (state == LOW) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
delay(500);
}
void setup() {
Serial.begin(9600);
dht.begin();
pinMode(buzzer, OUTPUT);
}
void loop() {
float temp = dht.readTemperature();
Serial.print("Temperature: ");
Serial.print(temp);
Serial.println(" °C");
void setup() {
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(buzzer, OUTPUT);
Serial.begin(9600);
}
void loop() {
int moisture = analogRead(sensorPin);
Serial.print("Soil Moisture: ");
Serial.println(moisture);
void setup() {
pinMode(buzzer, OUTPUT);
Serial.begin(9600);
}
void loop() {
int level = analogRead(sensorPin);
Serial.print("Water Level: ");
Serial.println(level);
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
int lightValue = analogRead(ldrPin);
buttonState = digitalRead(buttonPin);
Serial.print("Light Value: ");
Serial.println(lightValue);
void loop() {
int state = digitalRead(sensorPin);
if (state == LOW) {
digitalWrite(ledPin, HIGH);
digitalWrite(buzzer, HIGH);
} else {
digitalWrite(ledPin, LOW);
digitalWrite(buzzer, LOW);
}
delay(500);
}
Final Project Implementation - 100 Points (1hr)
Criteria 20 Points 15 Points 10 Points 5 Points 0 Points Score
Correct All Minor Some Major Compone
Wiring & compone wiring wiring wiring nts are
Connectio nts are errors, but mistakes, issues, not wired
ns (20 correctly the requiring requiring properly,
pts) wired and system moderate significant and the
neatly still debuggin interventio system
arranged, functions g to n to work. does not
with no with slight function function.
loose adjustmen properly.
connectio ts.
ns.
Functional The The The The The
ity of the system system system system system
System works functions works functions does not
(20 pts) exactly as with minor partially poorly work at
intended inconsiste but lacks and does all.
and ncies but some not meet
meets all meets expected key
project most features. requireme
requireme requireme nts.
nts. nts.
Code The code The code The code The code The code
Efficiency is well- is is has is
& Logic structured functional somewhat multiple unorganiz
(15 pts) , with only inefficient inefficienci ed,
optimized, minor or es and is contains
and inefficienci contains difficult to numerous
follows es or redundant read. errors, or
best redundant /unnecess does not
practices lines. ary parts. run.
with no
unnecess
ary lines.
Debuggin The The The The The
g& student student student student student is
Troublesh efficiently can requires struggles unable to
ooting (15 identifies debug some help significant troublesh
pts) and fixes most with ly to oot or fix
any errors issues debuggin debug errors,
with independ g and and even with
minimal ently with troublesh needs assistanc
assistanc minimal ooting substantia e.
e. guidance. errors. l
guidance.
Project Uses at Use three Uses Uses only Uses only
Complexit least compone three two one or no
y (15 pts) three nts compone compone functional
compone effectively nts but nts or has compone
nts and and lacks an nts.
includes meets all additional incomplet
additional requireme functionali e setup.
features nts. ty or
beyond creativity.
the basic
requireme
nts.
Time Complete Complete Complete It takes Does not
Managem s the s the s the significant complete
ent (15 project project project ly longer the
pts) within the within the but to project
allotted 30 time slightly complete within the
minutes frame but exceeds the given
with time with the given project. time.
to spare minimal time.
for testing time for
and testing.
improvem
ents.