🚀 Lesson 8: Your First Complete Projects
⏱️ Estimated time: 45 minutes | Difficulty: Intermediate
Time to Build
Now that you know the basics, let's combine everything to build something cool! We'll look at a small project that uses inputs and outputs together.
Project 1: Alarm System
Ultrasonic sensor triggers buzzer when something is too close.
int trigPin = 9, echoPin = 10, buzzer = 8;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
digitalWrite(trigPin, LOW); delayMicroseconds(2);
digitalWrite(trigPin, HIGH); delayMicroseconds(10);
digitalWrite(trigPin, LOW);
float distance = pulseIn(echoPin, HIGH) * 0.034 / 2;
if (distance < 20) { // Less than 20cm
tone(buzzer, 1000); // Buzzer ON
} else {
noTone(buzzer); // Buzzer OFF
}
delay(100);
}
Project 2: Smart Night Light
LED turns on automatically when it gets dark.
int ldrPin = A0, ledPin = 9;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int light = analogRead(ldrPin);
Serial.print("Light: "); Serial.println(light);
if (light < 300) { // Dark
analogWrite(ledPin, 255); // Full brightness
} else if (light < 600) { // Dim
analogWrite(ledPin, 100); // Low brightness
} else { // Bright
analogWrite(ledPin, 0); // OFF
}
delay(200);
}
Project 3: Temperature Monitor with LED Warning
int tempPin = A0;
int greenLED = 11, yellowLED = 10, redLED = 9;
void setup() {
Serial.begin(9600);
pinMode(greenLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(redLED, OUTPUT);
}
void loop() {
float voltage = analogRead(tempPin) * (5.0 / 1024.0);
float tempC = (voltage - 0.5) * 100.0;
// Reset LEDs
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, LOW);
digitalWrite(redLED, LOW);
if (tempC < 25) {
digitalWrite(greenLED, HIGH); // Cool
} else if (tempC < 35) {
digitalWrite(yellowLED, HIGH); // Warm
} else {
digitalWrite(redLED, HIGH); // Hot!
}
Serial.print(tempC); Serial.println(" °C");
delay(1000);
}
✅ Quick Quiz
❓ What function plays a tone on a buzzer?
❓ What stops the buzzer?
❓ Why combine sensors with outputs?