IR/Slot Sensor Interface
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
String name = "";
String Mobile = "";
String Address = "";
String Email = "";
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);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
DC Motor Interface
Circuit Diagram
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);
//uncomment this to rotate first motor in anti-clockwise direction
digitalWrite(motor2input1,LOW);
//Turn second motor in anti-clockwise direction
//digitalWrite(motor2input2,LOW);
//uncomment this to rotate second motor in anti-clockwise direction
}
DC Motor Speed Control
Circuit Diagram
Code
// 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
digitalWrite(IN1, LOW); // Make the Arduino Pin 8 Low
digitalWrite(IN2, LOW); // Make the Arduino Pin 7 Low
}
void loop()
{
// Rotate Motor Clockwise for 4 Seconds
analogWrite(ENA, 255); // Enable the L293D IC
digitalWrite(IN1, HIGH); // Make PIN 8 of the Arduino High
digitalWrite(IN2, LOW); // Make PIN 7 of the Arduino LOW
delay(4000); // wait for 4 Sec
// Rotate Motor counter-Clockwise for 4 Seconds
digitalWrite(IN1, LOW); // Make PIN 8 of the Arduino LOW
digitalWrite(IN2, HIGH);// Make PIN 7 of the Arduino HIGH
delay(4000);
// Stop Rotating the Motor
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
delay(1000);
// Turn on motors
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
// Accelerate from minimum to maximum speed
for (int i = 0; i < 256; i++) {
analogWrite(ENA, i);
delay(20);
}
// Decelerate from maximum speed to minimum
for (int i = 255; i >= 0; --i) {
analogWrite(ENA, i);
delay(20);
}
// Now turn off motors
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
delay(1000);
}
Servo Motor Interface
Circuit Diagram
Code
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
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
}
}