Arduino CSE
Arduino CSE
Arduino
Agenda
• Introduction to Arduino Boards
• Getting started with Arduino IDE
• Arduino Programming and Proteus designs
• Interfacing Output (LED) & Input (Key) devices
• Working with IR Sensor, LDR and its Interfacing
• Serial Communication feature of Arduino
• Working with DHT11/22 and its interfacing
• Ultrasonic Sensor Interfacing
What is Arduino?
• A microcontroller board, contains on-board power supply,
USB port to communicate with PC, and an Atmel
microcontroller chip.
• It simplify the process of creating any control system by
providing the standard board that can be programmed
and connected to the system without the need to any
sophisticated PCB design and implementation.
https://getintopc.com/softwares/3d-cad/proteus-professional-2020-free-download/
Getting Started (Installing Arduino IDE and Setting up Arduino Board)
IDE =
Integrated Development
Environment
http://www.arduino.cc/en/Guide/
Environment
Arduino IDE (Cont..)
Two required functions /
methods / routines:
void setup()
{
// runs once
}
void loop()
{
// repeats
}
Arduino IDE (Cont..)
Your computer
communicates to the
Arduino microcontroller via a
serial port → through a USB-
Serial adapter.
pinMode(pin, mode);
Sets pin to either INPUT or OUTPUT
Eg1. pinMode(13, OUTPUT);
digitalRead(pin);
Reads HIGH or LOW from a pin
Eg3. digitalRead(2);
digitalWrite(pin, value);
Writes HIGH or LOW to a pin
Eg2. digitalWrite(13, HIGH);
Our first Arduino Sketch/Program
/*
* Arduinos ketch to toggle the LED connected to pin-13 with a rate/delay of 1sec
*/
void setup()
{
// put your setup code here, to run once: -->I*
pinMode(13, OUTPUT); //pin-13 configures as o/p -->II
}
void loop()
{
// put your main code here, to run repeatedly: -->1*
digitalWrite(13, HIGH); //HIGH Value or Bunary-1 send to pin-13 -->2
//delay(x); //x-ms second(s) delay -->3*
//delayMicroseconds(y); //y-us second(s) delay -->4*
delay(1000); //1000-milliseconds=1second delay -->5
digitalWrite(13, LOW); //LOW Value or Bunary-1 send to pin-13 -->6
delay(1000); //1000-milliseconds=1second delay -->7
//Toggling rate of led connected to pin-13 is of 1second -->8*
}
Uploading and Running the blink sketch
In Arduino, open up:
File → Examples → 01.Basics → Blink
const int ledPin = 13; // choose the pin for the LED
const int inputPin = 2; // choose the input pin (for a pushbutton)
void setup()
{
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
}
void loop()
{
int val = digitalRead(inputPin); // read input value
if (val == HIGH) // check if the input is HIGH
{
digitalWrite(ledPin, HIGH); // turn LED on if switch is pressed
}
else
{
digitalWrite(ledPin, LOW); // turn LED off
}
}
Proteus design (with external resister)
Using a key/push button with external resistor
The digitalRead function monitors the voltage on the input pin (inputPin),
and it returns a value of HIGH if the voltage is high (5 volts) and LOW if
the voltage is low (0 volts).
Actually, any voltage that is greater than 2.5 volts (half of the voltage
powering the chip) is considered HIGH and less than this is treated as LOW.
If the pin is left unconnected (known as floating) the value returned from
digitalRead is indeterminate (it may be HIGH or LOW, and it cannot be
reliably used).
The resistor shown in Figure shown in previous slide ensures that the
voltage on the pin will be low when the switch is not pressed, because the
resistor “pulls down” the voltage to ground.
When the switch is pushed, a connection is made between the pin and +5
volts, so the value on the pin interpreted by digital Read changes from LOW
to HIGH.
InfraRed (IR) Sensor
Features-
•Can be used for obstacle sensing, fire detection, line sensing, etc
•Input Voltage: 5V DC
•Comes with an easy to use digital output
•Can be used for wireless communication and sensing IR remote
signals
IR Sensor have three Pins
1. VCC = +5V DC
2. GND
3. D0 or OUT (Digital Output)
Arduino sketch & Circuit diagram
/* void loop()
IR Proximity Sensor interface code {
Turns on an LED on when obstacle is if(digitalRead(ProxSensor)==LOW)
detected, else off. //Check the sensor output
*/ {
digitalWrite(13, HIGH); // set the LED on
const int ProxSensor=2; Serial.println("Stop something is ahead!!
"); //Message on Serial Monitor
void setup() }
{ else
// initialize the digital Serial port. {
digitalWrite(13, LOW); // set the LED off
Serial.begin(9600); Serial.println("Path is clear");
// initialize the digital pin as an output. //Message on Serial Monitor
pinMode(13, OUTPUT); }
pinMode(ProxSensor,INPUT); delay(1000); // wait for a second
} }
Proteus design:
Light Dependent Resistor:
• Ultra-low cost
• 3 to 5V power and I/O
• 2.5mA max current use during conversion
(while requesting data)
• Good for 20-80% humidity readings with 5%
accuracy
• Good for 0-50°C temperature readings ±2°C
accuracy
DHT22 Specifications
• Low cost
• 3 to 5V power and I/O
• 2.5mA max current use during conversion
(while requesting data)
• Good for 0-100% humidity readings with 2-5%
accuracy
• Good for -40 to 125°C temperature readings
±0.5°C accuracy
Circuit connections of 3-pin sensor:
Circuit connections of 4-pin sensor:
Prerequisites to work with DHT11/22:
• Before you can use the DHT11 on the Arduino, you’ll need to install the DHTLib library.
• https://github.com/adafruit/DHT-sensor-library or
• https://www.arduino.cc/reference/en/libraries/dht-sensor-library/
• It has all the functions needed to get the humidity and temperature readings from the
sensor.
• It’s easy to install, just download the DHTLib.zip and open up the Arduino IDE.
• Then go to Sketch>Include Library>Add .ZIP Library and select the DHTLib.zip file.
Arduino Sketch:
void loop()
#include <Adafruit_Sensor.h> {
#include "DHT.h" delay(2000);
#define DHTPIN 2 float h = dht.readHumidity();
#define DHTTYPE DHT11 float t = dht.readTemperature();
DHT dht(DHTPIN, DHTTYPE); Serial.print("Humidity: \t" );
Serial.print(h);
void setup()
Serial.print(" %\n");
{
delay(500);
Serial.begin(9600); Serial.print("Temperature: \t");
Serial.println("DHT11 test!"); Serial.print(t);
dht.begin(); Serial.print(" *C \n");
} }
Proteus design:
Ultrasonic Sensor
// Reads the echoPin, returns the sound wave travel time in microsec.
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= (duration*0.034)/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println("cm");
delay(500); //500 m.sec = 0.5 sec
}
pulsein()