Physical computing
Arduino UNO is a tiny computer—the brain inside robots, drones, and 3D printers.
From your first blinking LED to a complete smart farm—one clear pattern connects every project.
Each day adds one new superpower. By Day 5, you combine them all.
Arduino UNO is a tiny computer—the brain inside robots, drones, and 3D printers.
Electricity must travel in a full loop. One break anywhere means nothing works.
void setup() {
pinMode(13, OUTPUT); // Pin 13 = output mode
}
void loop() {
digitalWrite(13, HIGH); // LED ON
delay(1000); // wait 1 second
digitalWrite(13, LOW); // LED OFF
delay(1000); // wait 1 second
}setup() runs once.
loop() repeats forever.
delay(1000) = 1000 ms = 1 second.
Turn OFF the board before connecting wires.
A button is the simplest input. Pressing it lets electricity flow. Use a 10KΩ resistor.
if means: “do this only when the condition is true.”tone(pin, frequency, duration)
void setup() {
pinMode(12, OUTPUT); // buzzer
pinMode(9, INPUT); // button
}
void loop() {
if (digitalRead(9) == HIGH) { // button pressed?
tone(12, 330, 200); // play sound
}
noTone(12);
}pinMode three times: pins 13, 12, and 11.Like a bat: send a sound, wait for its echo, then calculate distance.
Bright → BIG number
Dark → small number
Serial.begin(9600) starts talking. Serial.println(x) shows the value.
int light;
void setup() {
pinMode(13, OUTPUT); // LED
Serial.begin(9600);
}
void loop() {
light = analogRead(A0); // read light (0~1023)
Serial.println(light);
if (light < 300) { // dark?
digitalWrite(13, HIGH); // LED ON
} else {
digitalWrite(13, LOW); // LED OFF
}
delay(200);
}Humans give off heat. When a person moves, the PIR sends HIGH. No motion means LOW—just like a button.
void setup() {
pinMode(7, INPUT); // PIR
pinMode(13, OUTPUT); // LED
pinMode(12, OUTPUT); // buzzer
}
void loop() {
if (digitalRead(7) == HIGH) {
digitalWrite(13, HIGH);
tone(12, 988, 300); // alarm sound!
delay(300);
tone(12, 660, 300);
delay(300);
} else {
digitalWrite(13, LOW);
noTone(12);
}
}PIR needs 30–60 seconds of warm-up after power ON.
The sensor watches the soil 24/7. When dry, Arduino tells a relay to power the pump.
The pump needs more power than an Arduino pin can provide. Arduino is the boss; the relay is the strong worker. You can hear it click.
Pump power comes from the battery, never from the Arduino pin.
Compare readings in dry and wet soil. Your threshold will be unique.
int soil;
void setup() {
Serial.begin(9600);
}
void loop() {
soil = analogRead(A0); // read soil moisture
Serial.println(soil);
delay(500);
}Same logic as Blink—just a pump instead of an LED. If ON/OFF is reversed, swap HIGH and LOW.
int pump = 6;
void setup() {
pinMode(pump, OUTPUT);
}
void loop() {
digitalWrite(pump, HIGH); // pump ON
delay(3000);
digitalWrite(pump, LOW); // pump OFF
delay(3000);
}Soil controls the pump. Tank level controls the warning LED.
int soil;
int pump = 6;
long duration;
int distance;
void setup() {
pinMode(pump, OUTPUT);
pinMode(9, OUTPUT); // Trig
pinMode(8, INPUT); // Echo
pinMode(13, OUTPUT); // warning LED
Serial.begin(9600);
}
void loop() {
// STEP 1: soil -> pump
soil = analogRead(A0);
if (soil > 600) { // dry? use YOUR number!
digitalWrite(pump, HIGH);
} else {
digitalWrite(pump, LOW);
}
// STEP 2: tank level -> warning LED
digitalWrite(9, LOW); delayMicroseconds(2);
digitalWrite(9, HIGH); delayMicroseconds(10);
digitalWrite(9, LOW);
duration = pulseIn(8, HIGH);
distance = duration * 0.034 / 2;
if (distance > 15) { // water low?
digitalWrite(13, HIGH); // warning!
} else {
digitalWrite(13, LOW);
}
Serial.print("Soil: "); Serial.print(soil);
Serial.print(" Tank: "); Serial.println(distance);
delay(500);
}| PART | PIN |
|---|---|
| Soil sensor | A0 |
| Relay / pump | 6 |
| Ultrasonic Trig | 9 |
| Ultrasonic Echo | 8 |
| Warning LED | 13 |
| Optional servo | 5 |
IN → Pin 6 · VCC → 5V · GND → GND
Battery (+) → COM · NO → Pump (+)
Pump (−) → Battery (−)
Servo moves from 0°–180°. Dry soil raises a paper sign. Use Pin 5 because Pin 6 is taken.
myservo.attach(5);Keep them separated. Keep the pump underwater while running—never run it dry.