23CSE102HackathonReferenceMaterial
23CSE102HackathonReferenceMaterial
Circuit Diagram
Code
int IRSensor = 9; // connect IR sensor module to Arduino pin D9
int LED = 13; // connect LED to Arduino pin 13
void setup(){
Serial.begin(115200); // Init Serial at 115200 Baud Rate.
Serial.println("Serial Working"); // Test to check if serial is working or not
pinMode(IRSensor, INPUT); // IR Sensor pin INPUT
pinMode(LED, OUTPUT); // LED Pin Output
}
void loop(){
int sensorStatus = digitalRead(IRSensor); // Set the GPIO as Input
if (sensorStatus == 1) // Check if the pin high or not
{
// if the pin is high turn off the onboard Led
digitalWrite(LED, LOW); // LED LOW
Serial.println("Motion Detected!"); // print Motion Detected! on the serial monitor window
}
else {
//else turn on the onboard LED
digitalWrite(LED, HIGH); // LED High
Serial.println("Motion Ended!"); // print Motion Ended! on the serial monitor window
}
}
Ultrasonic Sensor Interface
Circuit Diagram
Code
const int trigPin = 4;
const int echoPin = 5;
long duration;
int distance;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm”);
delay(1000);
}
PIR Sensor Interface
Circuit Diagram
Code
const int pirPin = 5;
void setup() {
pinMode(pirPin, INPUT);
Serial.begin(9600);
delay(20000);
}
void loop() {
int pirState = digitalRead(pirPin);
if (pirState == HIGH) {
Serial.println("Motion detected!”);
delay(1000);
}
else {
Serial.println("No motion detected.");
}
}
Potentiometer Interface
Circuit Diagram
Code
#define POTENTIOMETER_PIN A0
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println(analogRead(POTENTIOMETER_PIN));
delay(100);
}
Gas Sensor MQ2 Interfacing
Circuit Diagram
Code
// Sensor pin A0 analog Input
#define sensorPin A0
void setup() {
Serial.begin(9600);
digitalWrite(ledPin, LOW);
}
void loop() {
Serial.print("Analog output: “);
Serial.println(analogRead(sensorPin));
delay(500);
}
Reading Input from Serial Monitor
Code
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println("Enter your name.");
while (Serial.available() == 0)
{ //Wait for user input }
name = Serial.readString(); //Reading the Input string from Serial port.
Serial.println("Enter your Moblie No.");
while (Serial.available() == 0) {}
Mobile = Serial.readString();
Serial.println("Enter your Address.");
while (Serial.available() == 0) {}
Address = Serial.readString();
Serial.println("Enter your Email.");
while (Serial.available() == 0) {}
Email = Serial.readString();
Serial.println("-------------------------"); //Showing the details
Serial.println("YOUR NAME:" + name);
Serial.println("YOUR MOBILE NO:" + Mobile);
Serial.println("YOUR ADDRESS:" + Address);
Serial.println("YOUR EMAIL:" + Email);
Serial.println("Thanks You...");
Serial.println("");
while (Serial.available() == 0) {}
}
LED Interface
Circuit Diagram
Code
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
Code
//Define all the input pins of the motor driver
const int motor1input1=2;
const int motor1input2=3;
const int motor2input1=4;
const int motor2input2=5;
void setup() {
Serial.begin(9600);
//Set baud rate for serial communication
//Set all the pin as OUTPUT
pinMode(motor1input1,OUTPUT);
pinMode(motor1input2,OUTPUT);
pinMode(motor2input1,OUTPUT);
pinMode(motor2input2,OUTPUT);
//Set all the pin to Logic HIGH to initially turn off the motor
digitalWrite(motor1input1,HIGH);
digitalWrite(motor1input2,HIGH);
digitalWrite(motor2input1,HIGH);
digitalWrite(motor2input2,HIGH);
}
void loop() {
digitalWrite(motor1input1,LOW);
//Turn first motor in clockwise direction
//digitalWrite(motor1input2,LOW);
// Motor A connections
int ENA= 9;
int IN1= 8;
int IN2 = 7;
void setup()
{
pinMode(ENA, OUTPUT); // Set ENA as OUTPUT
pinMode(IN1, OUTPUT); // Set IN1 as OUTPUT
pinMode(IN2, OUTPUT); // Set IN2 as OUTPUT
Code
#include <Servo.h>
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}