Week 1
Week 1
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
1|Page
ACTIVITY 2: TRAFFIC LIGHTS USING ARDHUINO BOARD AND BREADTH
BOARD (CODE)
void setup() {
pinMode(RED_LED, OUTPUT);
pinMode(YELLOW_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
}
void loop() {
digitalWrite(RED_LED, HIGH);
delay(5000);
digitalWrite(RED_LED, LOW);
digitalWrite(YELLOW_LED, HIGH);
delay(2000);
digitalWrite(YELLOW_LED, LOW);
digitalWrite(GREEN_LED, HIGH);
delay(5000);
digitalWrite(GREEN_LED, LOW);
2|Page
DAY 2: TUESDAY
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
void loop() {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
Serial.println("NORMAL");
digitalWrite(ledPin, LOW);
else {
Serial.println("ABNORMAL");
digitalWrite(ledPin, HIGH);
delay(1000);
3|Page
ACTIVITY 2: LED GLOWS AS THE TEMPERATURE INCREASES
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
void loop() {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
int brightness;
brightness = 50;
brightness = 128;
} else {
brightness = 255;
analogWrite(ledPin, brightness);
delay(1000);
4|Page
DAY 3: WEDNESDAY
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
void loop() {
Serial.println(ldrValue);
if (ldrValue < 500) {
digitalWrite(ledPin, HIGH);
Serial.println("Light ON (Dark)");
} else {
digitalWrite(ledPin, LOW);
}
delay(1000);
5|Page
ACTIVITY 2: LCD DISPLAY (SERIES)
#include <Wire.h>
#include "DFRobot_LCD.h"
DFRobot_LCD lcd(16,2);
void setup() {
lcd.init();
lcd.print("hello, world!");
delay(1000);
}
void loop() {
lcd.setCursor(0, 1);
lcd.print(millis()/1000);
delay(100);
6|Page
DAY 4: THURSDAY
7|Page
ACTIVITY 2: MOTION DETECTOR TO DISPLAY “INTRUDER DETECTED”
(PIR SENSOR)
#include <Wire.h>
#include "DFRobot_LCD.h"
void setup() {
Wire.begin();
lcd.init();
lcd.print("");
delay(2000);
lcd.clear();
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
void loop() {
if (motionDetected != prevState) {
lcd.clear();
lcd.setCursor(0, 0);
if (motionDetected) {
lcd.print("INTRUDER DETECTED!");
} else {
digitalWrite(ledPin, LOW);
prevState = motionDetected;
8|Page
}
delay(500);
DAY 5: FRIDAY
ACTIVITY 1: MOTION DETECTOR USING IR SENSOR
#include <Wire.h>
#include "DFRobot_LCD.h"
#define PIR_PIN 2
#define LED_PIN 13
void setup() {
pinMode(PIR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
lcd.init();
lcd.setCursor(0, 0);
lcd.print("System Ready...");
delay(2000);
lcd.clear();
}
9|Page
void loop() {
lastMotionTime = millis();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(alertMsg);
digitalWrite(LED_PIN, HIGH);
for (int i = 0; i < alertMsg.length(); i++) {
lcd.scrollDisplayLeft();
delay(300);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("INTRUDER ALERT!");
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("No Motion");
digitalWrite(LED_PIN, LOW);
lastMotionState = motion;
delay(100); // Small delay to prevent too fast polling
}
10 | P a g e
ACTIVITY 2: RUN TWO SENSORS IR AND LM35
#include <Wire.h>
#include "DFRobot_LCD.h"
#define IR_PIN 2
#define LED_PIN 13
#define LM35_PIN A0
const int colorR = 255;
const int colorG = 0;
const int colorB = 0;
DFRobot_LCD lcd(16, 2);
unsigned long lastMotionTime = 0;
unsigned long motionCooldown = 5000;
bool lastMotionState = LOW;
void setup() {
pinMode(IR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
lcd.init();
lcd.setRGB(colorR, colorG, colorB);
lcd.setCursor(0, 0);
lcd.print("System Ready...");
delay(2000);
lcd.clear();
}
void loop() {
int motion = digitalRead(IR_PIN);
float temperature = analogRead(LM35_PIN) * (5.0 / 1023.0) * 100;
if (motion == HIGH && lastMotionState == LOW && (millis() - lastMotionTime > motionCooldown)) {
lastMotionTime = millis();
String alertMsg = "LIGHT ON! ";
lcd.clear();
11 | P a g e
lcd.setCursor(0, 0);
lcd.print(alertMsg);
digitalWrite(LED_PIN, HIGH);
for (int i = 0; i < alertMsg.length(); i++) {
lcd.scrollDisplayLeft();
delay(300);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("INTRUDER DETECTED!");
}
else if (motion == LOW && lastMotionState == HIGH)
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("");
digitalWrite(LED_PIN, LOW);
}
lastMotionState = motion;
lcd.setCursor(0, 1);
lcd.print("");
lcd.print(temperature, 1);
lcd.print(" C");
if (temperature < 50) {
lcd.print(" NORMAL TEMP");
} else {
lcd.print("ABNORMAL ");
}
delay(100); // Small delay to prevent too fast polling
}
12 | P a g e