0% found this document useful (0 votes)
9 views

Coding Arduino FYP2

The document provides an Arduino code for controlling a UVC light using an ultrasonic sensor and an LCD display. It includes setup instructions for connecting the Arduino, ultrasonic sensor, relay module, and LCD, as well as the logic for triggering sanitization based on detected distance. The code displays messages on the LCD during operation and ensures the UVC light is activated when an object is within 10 cm of the sensor.

Uploaded by

Nur insyirah
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)
9 views

Coding Arduino FYP2

The document provides an Arduino code for controlling a UVC light using an ultrasonic sensor and an LCD display. It includes setup instructions for connecting the Arduino, ultrasonic sensor, relay module, and LCD, as well as the logic for triggering sanitization based on detected distance. The code displays messages on the LCD during operation and ensures the UVC light is activated when an object is within 10 cm of the sensor.

Uploaded by

Nur insyirah
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
You are on page 1/ 3

Coding Arduino

#include <Wire.h>

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD at address 0x27 with 16 columns and
2 rows

const int trigPin = 9; // Ultrasonic sensor trigger pin


const int echoPin = 8; // Ultrasonic sensor echo pin
const int relayPin = 7; // Relay control pin
long duration; // Time taken by pulse to travel back
int distance; // Distance calculated based on duration

void setup() {
pinMode(trigPin, OUTPUT); // Set trigPin as an output
pinMode(echoPin, INPUT); // Set echoPin as an input
pinMode(relayPin, OUTPUT); // Set relayPin as an output
lcd.init(); // Initialize the LCD
lcd.print("Place Hand Near"); // Initial message
delay(2000); // Wait 2 seconds to give user time to read
message
}

void loop() {
// Generate an ultrasonic pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Measure the duration of the pulse in microseconds


duration = pulseIn(echoPin, HIGH);

// Calculate the distance (in cm) based on pulse duration


distance = duration * 0.034 / 2;

// Check if an object is close enough to trigger the UVC light


if (distance <= 10) {
lcd.clear();
lcd.print("Sanitizing..."); // Display message during sanitization
digitalWrite(relayPin, LOW); // Turn UVC light ON (assuming relay is
active low)
delay(10000); // Keep UVC light on for 10 seconds
digitalWrite(relayPin, HIGH); // Turn UVC light OFF
lcd.clear();
lcd.print("Sanitization Done"); // Inform that sanitization is complete
delay(2000); // Display message for 2 seconds
lcd.clear();
lcd.print("Place Hand Near"); // Reset display message
}

delay(500); // Small delay before next distance check


}
Connection circuit

1. Arduino Uno

• Power Supply: Connect the solar power supply to the Arduino’s VIN or 5V pin
(ensure the solar output is appropriate for the Arduino).

2. Ultrasonic Sensor (HC-SR04)

• VCC: Connect to the Arduino's 5V pin.


• GND: Connect to the Arduino's GND pin.
• Trig: Connect to digital pin 9 on the Arduino.
• Echo: Connect to digital pin 8 on the Arduino.

3. Relay Module

• IN: Connect to digital pin 7 on the Arduino (this controls the relay).
• VCC: Connect to the Arduino's 5V pin.
• GND: Connect to the Arduino's GND pin.

4. LCD 16x2 (with I2C)

• VCC: Connect to the Arduino's 5V pin.


• GND: Connect to the Arduino's GND pin.
• SDA: Connect to the Arduino's A4 pin.
• SCL: Connect to the Arduino's A5 pin.

5. UVC Light Power Connection

• Power Supply (9V Battery):


o Positive Terminal: Connect to the COM (Common) terminal of the relay.
This is because the relay acts as a switch, and you want to connect the positive
supply to the common contact, which will then provide power to the UVC
light when the relay is activated.
o Negative Terminal: Connect to the negative terminal of the UVC light. This
completes the circuit for the UVC light.

6. Relay Connection

• Relay NO (Normally Open): Connect to the positive terminal of the UVC light.
• Relay COM (Common): Connect to the positive terminal of the 9V battery. The
relay will close (connect) when activated, allowing current to flow from the 9V
battery to the UVC light.

You might also like