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

Arduino Robotics Revise

Uploaded by

Shanon Malzeba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
208 views

Arduino Robotics Revise

Uploaded by

Shanon Malzeba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 122

ARDUINO PROGRAMMING

1
ROBOT -  also called as droid is a machine especially one
programmable by a computer capable of carrying out a complex
series of actions automatically.

2
∞ Robots can be guided by an external control device or the
control may be embedded within
∞ Robots may be constructed to take on human form but
most robots are machines designed to perform a task with
no regard to how they look
∞ Robots can be autonomous or semi-autonomous
LAWS OF ROBOTICS Law 1: A robot may not injure a human
being or through inaction, allow a human
being to come to harm

Law 2: A robot must obey orders given to


it by human beings, except where such
orders would conflict with a higher order
law

Law 3: A robot must protect its own


existence as long as such protection does
Asimov proposed three not conflict with a higher order law
“Laws of Robotics” and later
added the “zeroth law”
4
CHARACTERISTICS
OF ROBOTICS

SENSING – to be able to sense its surroundings


MOVEMENT – to be able to move around its environment
ENERGY – to be able to have power
INTELLIGENCE – a robot needs some kind of "smarts."

5
Things to be Consider
Before Creating a
Robot

 Objective
 Planning / Architecture
 Designing / Creativeness
 Function / Purpose
 Program / Algorithm
 Source of Power / Energy
 Input and Output Devices
 Capabilities and Limitation

6
IDEAL TASK FOR
ROBOTS

Robots are usually applied Four D’s :


Dirty
Dangerous
Dull and
Difficult for human to perform

7
ARDUINO

?
ARDUINO is an open-source electronics platform based on easy-to-use
hardware and software.  
MICROCONTROLLER

!
MICROCONTROLLER -  (or MCU, short for microcontroller unit) is a
small computer (SoC) on a single integrated circuit containing a processor core,
memory, and programmable input/output peripherals.

ARDUINO is a single-board microcontroller to make using electronics in


multidisciplinary projects more accessible.
Arduino PARTS OF THE ARDUINO UNO MICROCONTROLLER UNIT
UNO 1. Power IN (Barrel Jack) - It can be used with either a 9V or 12V supply
2. Power IN (USB Port) - Provides power and communicates with the board when
plugged into a computer via USB.
3. LED (RX: Receiving) - It blinks when receiving a data.
4. LED (TX: Transmitting) - It blinks when transmitting a data.
5. LED (Troubleshooting) - It blinks when the program is running properly.
6. Pins (Digital, Analog, Ground) - It can be used for input, output, power and ground.
 Pin 0 – 13
 Analog Output Pins / PWM Pins
 Pin ~ 3, ~5, ~6, ~9, ~10 and ~11
 7. LED (ON/OFF) - It indicates if there is a power.
8. Reset Button - It manually reset the Arduino that make your code to restart.
9. Pins (Analog In, Power In, Ground Power Out, Reset) - It can be used for input, output,
power and ground.
 Pin A0 – A5
 Power Pin
o Reset
o +3.3 V
o 5V
o Ground
o VIN (Voltage/Power IN)
BREADBOARD

!
BREADBOARD is used to create circuits and connect different sensors
and actuators to the Arduino
board it also refers to a solderless breadboard.
Connection

1 – Serial connection from positive 1 to 30 is used to connect the horizontal


pins which is called power rails. The connection on the blue line rail is
different from the red line rail.
 
2 – Serial connection from A1, B1, C1, D1 and E1 is used to connect the
vertical pins
3 – Trench serves a very important purpose, if a dual in-line package or
DIP chip is used, there is a separation of connection is made so that if
connection is made to each side of the IC there will be no interference
between the functionality of the leg on the opposite side.

12
Connection

13
Arduino

PROGRAMMING INTERFACE
PROGRAM ARDUINO IDE (INTEGRATED DEVELOPMENT ENVIRONMENT)

1. Menu Bar
2. Toolbar
3. Verify Button
4. Upload Button
5. New File Button
6. Open Button
7. Save Button
8. Serial Monitor
9. Status Bar

15
INTERFACE SETTING UP THE ARDUINO INTEGRATED DEVELOPMENT
ENVIRONMENT FOR ARDUINO UNO

SELECTING THE
MICROCONTROLLER
To pick the correct microcontroller board
click Arduino/Genuino Uno by clicking
Tools>Board on the Menu Bar Menu Bar

16
SELECTING THE PORT
Select the port on where the
microcontroller board is connected by
clicking Port/COM5(Arduino/Genuino
Uno)

17
SKETCH – SAMPLE CODE

void setup( ) {

// put your setup code here, to run once:


}

void loop( ) {

// put your main code here, to run


repeatedly: }

18
Arduino

Sample Coding and Wiring


LED “Light Emitting Diode”
A semiconductor diode that converts electric energy into electromagnetic radiation at a
visible and near infrared frequencies when its pn junction is forward biased.

1 – Short pin to be connected to GND (ground)


2 – Longer pin to be connected to any of the digital pins (pins 2 – 13
Resistor

Resistance, it blocks the flow of electricity.

If the resistance value is big, it flows a little of electricity


If the resistance value is small, it flows a lot of electricity
LED  LIGHT EMITTING DIODE - LED
In short, LEDs are like tiny light bulbs. However, LEDs require a lot less power to light
up by comparison.

1 – short pin to be connected to GND


(ground)
2 – longer pin to be connected to any of
the digital pins (pins 2 – 19)

23
­int LED = 2;
/* int means integer, LED is a variable that will be used to represent the device connected on the
microcontroller and assigning 2 as the value for pin 2*/
void setup( ) {
// put your setup code here, to run once:
pinMode(LED, OUTPUT);
/* pinMode is a function that prepares ALL the pins used in the microcontroller. Variable LED is the same as
2 thus, pin 2 will be used as an OUTPUT device */
}
void loop( ) {
// put your main code here, to run repeatedly:
digitalWrite(LED, HIGH);
/* digitalWrite ( ) is the function in controlling digital device, inside the parenthesis ( ) are the pin that needs
to be controlled and it can be set either HIGH or LOW */
delay(3000);
/* delay ( ) is the function in controlling digital device, inside the parenthesis ( ) are the pin that needs to be
controlled and it can be set either HIGH or LOW */
digitalWrite(LED, LOW);
delay(2000);
}
24
ALTERNATE
BLINKING LED int LED1 = 7;
int LED2 = 8;

void setup( ) {
pinMode (LED1, OUTPUT); //Pin 7 is defined as output
pinMode (LED2, OUTPUT);//Pin 8 is defined as output
}
void loop( ) {
digitalWrite (LED1, HIGH);//turn on the LED on pin 7
delay(1000); //wait for 1000 milliseconds
digitalWrite (LED1, LOW) ; //turn off the LED on pin 7
digitalWrite (LED2, HIGH); //turn on the LED on pin 8
delay(1000); //wait for 1000 milliseconds
digitalWrite (LED2, LOW); //turn off the LED on pin 8
}

25
int LED1 = 2; void loop( ) { digitalWrite(LED6, HIGH);
Controlling int LED2 = 3; digitalWrite(LED1, HIGH); delay(3000);
8 LED int LED3 = 4; delay(3000);
digitalWrite(LED1, LOW);
digitalWrite(LED6, LOW);
delay(2000);
int LED4 = 5;
delay(2000); digitalWrite(LED7, HIGH);
int LED5 = 6;
digitalWrite(LED2, HIGH); delay(3000);
int LED6 = 7; digitalWrite(LED7, LOW);
delay(3000);
int LED7 = 8; digitalWrite(LED2, LOW); delay(2000);
int LED8 = 9; delay(2000); digitalWrite(LED8, HIGH);
void setup( ) { digitalWrite(LED3, HIGH); delay(3000);
pinMode(LED1, OUTPUT); delay(3000); digitalWrite(LED8, LOW);
digitalWrite(LED3, LOW); delay(2000);
pinMode(LED2, OUTPUT);
delay(2000); }
pinMode(LED3, OUTPUT);
digitalWrite(LED4,HIGH);
pinMode(LED4, OUTPUT);
delay(3000);
pinMode(LED5, OUTPUT);
digitalWrite(LED4, LOW);
pinMode(LED6, OUTPUT);
delay(2000);
pinMode(LED7, OUTPUT);
digitalWrite(LED5, HIGH);
pinMode(LED8, OUTPUT);
delay(3000);
} digitalWrite(LED5, LOW);
delay(2000);
26
LED with
Fade Effect
int LED= 3;
int brightness=0;
int fading=5; //”fading” sets up the speed of the fading.
void setup() {
pinMode (LED, OUTPUT); //The pin with the LED is supposed to be an
output.
}
void loop() {
analogWrite(LED, brightness);
brightness=brightness + fading;
delay(25);
if(brightness==0 || brightness== 255) {
fading= -fading;
}
}

27
Stoplight int ledPin1 = 2; // red
int ledPin2 = 4; // yellow
int ledPin3 = 6; // green

void setup() {
pinMode(ledPin1,OUTPUT);
pinMode(ledPin2,OUTPUT);
pinMode(ledPin3,OUTPUT);
}
//go
void loop() { digitalWrite(ledPin3, HIGH);
//stop digitalWrite(ledPin1,LOW);
digitalWrite(ledPin1, HIGH); digitalWrite(ledPin2,LOW);
digitalWrite(ledPin2,LOW); delay(8000);
digitalWrite(ledPin3,LOW);
delay(5000); //ready
digitalWrite(ledPin2, HIGH);
//ready digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2, HIGH); digitalWrite(ledPin3,LOW);
digitalWrite(ledPin1,LOW); delay(3000);
digitalWrite(ledPin3,LOW); }
delay(3000);
28
Serial
Monitor

 SERIAL MONITOR is used for communication between the


Arduino board and a computer or other devices

OPENING UP THE SERIAL MONITOR


After you have uploaded the sketch onto your Arduino, click on
the right-most button on the toolbar in the Arduino IDE. The
button is circled below

29
Serial void setup () {
Monitor Serial.begin (9600); // Sets the data rate in bits per second
(baud) for serial data transmission
Serial.println ("Serial connection established...\n\n Please enter
text above and press send.\n");
}
void loop () {
String temp = Serial.readString(); /*This is a LOCAL variable, it
will store any text we send TO the Arduino FROM the PC.*/
if (temp != NULL) { //If the variable is NOT empty run the next
instructions
Serial.print ("You sent the string: "); //Print the string
Serial.print (temp); //then print the text we sent to the Arduino
Serial.println ("."); //Finally print a fullstop. This is data FROM
the Arduino TO the PC.
}
//If there is no text entered, the Arduino will continue
processing from here…But as there are no further instructions,
it will return to the top and start again. If you were to put
something here such as a Serial.print () command, it would fire
constantly until you provide a string of text. Feel free to try it!
}
30
Switch

Electrical device having two states, on or off (open or close). Ideally having zero
impedance when closed and infinite impedance when open.
int pbValue;

Push Button /* int means integer, pbValue is a variable that will be used to hold the
value that the tact switch will send to the microcontroller unit */
void setup( ) {
pinMode(3, INPUT);
/* pinMode is a function that prepares ALL the pins used in the
microcontroller. 2 is the digital pin used to connect the tact switch and
it’s considered as an INPUT device */
Serial.begin(9600);
// Sets the data rate in bits per second (baud) for serial data
transmission
}
void loop( ) {
pbValue = digitalRead(3);
// digitalRead will basically get the data from pin 2 and assign that value
to variable pbValue.
Serial.println(pbValue);
// prints over the Serial Monitor the value assigned to pbValue
// Serial.println(“pbValue”); - this code will display the word “pbValue”
} 32
LED with
push button
int PinButton1 = 2;
int PinLed = 9;

void setup()
{
pinMode(PinButton1, INPUT);
digitalWrite(PinButton1, HIGH); // turn on pull-up !!
pinMode(PinLed, OUTPUT);
}
void loop()
{
if(digitalRead(PinButton1) == LOW){
digitalWrite(PinLed, HIGH);
}
else{
digitalWrite(PinLed, LOW);
}
}

33
3 LED with Control Button
void loop()
{
if(digitalRead(PinButton1) == LOW){
digitalWrite(PinLed1, HIGH);
}
int PinButton1 = 2; else{
int PinButton2 = 3; digitalWrite(PinLed1, LOW);
int PinButton3 = 4; }
int PinLed1 = 9; //red //2
int PinLed2 = 10; //yellow {
int PinLed3 = 11; //green if(digitalRead(PinButton2) == LOW){
digitalWrite(PinLed2, HIGH);
void setup() }
{ else{
pinMode(PinButton1, INPUT); digitalWrite(PinLed2, LOW);
digitalWrite(PinButton1, HIGH); // turn on pull-up !! }
pinMode(PinButton2, INPUT); //3
digitalWrite(PinButton2, HIGH); // turn on pull-up !! {
pinMode(PinButton3, INPUT); if(digitalRead(PinButton3) == LOW){
digitalWrite(PinButton3, HIGH); // turn on pull-up !! digitalWrite(PinLed3, HIGH);
pinMode(PinLed1, OUTPUT); }
pinMode(PinLed2, OUTPUT); else{
pinMode(PinLed3, OUTPUT); digitalWrite(PinLed3, LOW);
} }
}}}

34
  Potentiometer

A potentiometer is a three-terminal resistor with a sliding or rotating contact that


forms an adjustable voltage divider. If only two terminals are used, one end and the
wiper, it acts as a variable resistor or rheostat.
Potentiometer

int varPin = A0;


int val = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Serial connection established..\n Adjust the Potentiometer to
see the value change!");
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print("Potentiometer Value: ");
val = analogRead(varPin);
Serial.println(val);
}
36
LED with Potentiometer Control Blink

int potpin = A0;


int led = 13;
int val = 0;

void setup() {

pinMode (led, OUTPUT);


Serial.begin (9600);
}

void loop() {

val = analogRead(potpin);
Serial.println (val);
analogWrite(led, val);
delay (10);

digitalWrite (led,HIGH);
delay(val);
digitalWrite (led,LOW);
delay(val);
}
37
LED with Potentiometer Control Fade

int led = 9;
int potpin = 0;

void setup() {

pinMode (led, OUTPUT);

}
void loop() {
int val = analogRead(potpin);
val = map (val, 1, 1024, 1, 255);
analogWrite (led, val);
}

38
Buzzer

A buzzer or beeper is an audio signaling device, which may


be mechanical, electromechanical, or piezoelectric (piezo for short). 
Buzzer Active Buzzer
Alarm Sound

int buzzer=13; // initialize digital IO pin that controls the buzzer

void setup()
{
pinMode (buzzer, OUTPUT);// set pin mode as “output”
}
void loop()
{
digitalWrite(buzzer, HIGH); // produce sound
delay (500);
digitalWrite(buzzer, LOW);
delay (500);

40
Buzzer Active Buzzer
Changing Sound

int buzzer=3; // initialize digital IO pin that controls the buzzer

void setup() {

pinMode (buzzer, OUTPUT);


}
void loop() {

analogWrite (buzzer, random(0,255)); //Send a random PWM signal to pin


13

delay(500); //wait half a second before changing pitch


}

41
Buzzer Passive Buzzer
Melody Sound

int buzzer=13; // initialize digital IO pin that controls the buzzer

void setup() {

pinMode(buzzer, OUTPUT);
}
void loop() {
tone (buzzer, random (500, 1500); //Send a random PWM signal to pin 13
delay(300); //wait half a second before changing pitch
}

42
Passive Buzzer int buzzerPin = 3;//the buzzer pin attach to
int fre;//set the variable to store the frequence value
Fire Alarm void setup()
{
pinMode(buzzerPin,OUTPUT);
}

void loop()
{
for(int i = 200;i <= 800;i++) //frequence loop from 200 to
800
{
tone(3,i); //turn the buzzer on
delay(5); //wait for 5 milliseconds
}
delay(4000); //wait for 4 seconds on highest frequence
for(int i = 800;i >= 200;i--)//frequence loop from 800
downto 200
{
tone(3,i);
delay(10);
}}
int buzzer = 2;//the pin of the active buzzer
Passive Buzzer void setup()
{
Error Sound pinMode(buzzer,OUTPUT);//initialize the buzzer pin as an output
}
void loop()
{
unsigned char i,j;
while(1)
{
//output an frequency
for(i=0;i<80;i++)
{
digitalWrite(buzzer,HIGH);
delay(1);//wait for 1ms
digitalWrite(buzzer,LOW);
delay(1);//wait for 1ms
}
//output another frequency
for(i=0;i<100;i++)
{
digitalWrite(buzzer,HIGH);
delay(2);//wait for 2ms
digitalWrite(buzzer,LOW);
delay(2);//wait for 2ms
}}}
44
Buzzer with Button

int buzzer = 7;//the pin of the active buzzer


int inPin = 12; //button//
int val;

void setup( )
{
pinMode(buzzer,OUTPUT); //initialize the buzzer pin as an output
pinMode (inPin, INPUT); //button//
}

void loop( ){
val = digitalRead (inPin);
if (val == LOW) {

digitalWrite(buzzer,HIGH);

}else{
digitalWrite(buzzer,LOW);
}
}

45
activity Buzzer with LED with Button control
int buzzer = 7;//the pin of the active buzzer
int PinButton1 = 12; //button//
int PinLed1 = 10; //led//

void setup()
{
pinMode(PinButton1, INPUT);
digitalWrite(PinButton1, HIGH); // turn on pull-up !!
pinMode(PinLed1, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop(){
if(digitalRead(PinButton1) == LOW){
digitalWrite(PinLed1, HIGH);
}
else{
digitalWrite(PinLed1, LOW);
// buzzer
if(digitalRead(PinButton1) == HIGH){
digitalWrite(buzzer, HIGH);
delay (1);
digitalWrite(buzzer, LOW);
delay (1);
}
else{
digitalWrite(buzzer, LOW);
}
}} 46
7 Segment

SSD or seven-segment indicator, is a form of electronic display device for displaying


decimal numerals that is an alternative to the more complex dot matrix displays.
//1
int pinA = 2;
digitalWrite (pinA, HIGH);
int pinB = 3;
Segment int pinC = 4;
digitalWrite (pinB, LOW);
digitalWrite (pinC, LOW);
int pinD = 5;
digitalWrite (pinD, HIGH);
int pinE = 6;
digitalWrite (pinE, HIGH);
int pinF = 7;
digitalWrite (pinF, HIGH);
int pinG = 8;
digitalWrite (pinG, HIGH);
delay(1000);
void setup() {
//2
pinMode (pinA,OUTPUT);
digitalWrite (pinA, LOW);
pinMode (pinB,OUTPUT);
digitalWrite (pinB, LOW);
pinMode (pinC,OUTPUT);
digitalWrite (pinC, HIGH);
pinMode (pinD,OUTPUT);
digitalWrite (pinD, LOW);
pinMode (pinE,OUTPUT);
digitalWrite (pinE, LOW);
pinMode (pinF,OUTPUT);
digitalWrite (pinF, HIGH);
pinMode (pinG,OUTPUT);
digitalWrite (pinG, LOW);
}
delay(1000);
void loop() {
//3
//0
digitalWrite (pinA, LOW);
digitalWrite (pinA, LOW);
digitalWrite (pinB, LOW);
digitalWrite (pinB, LOW);
digitalWrite (pinC, LOW);
digitalWrite (pinC, LOW);
digitalWrite (pinD, LOW);
digitalWrite (pinD, LOW);
digitalWrite (pinE, HIGH);
digitalWrite (pinE, LOW);
digitalWrite (pinF, HIGH);
digitalWrite (pinF, LOW);
digitalWrite (pinG, LOW);
digitalWrite (pinG, HIGH);
delay(1000);
delay(1000); 49
//7
//4
digitalWrite (pinA, LOW);
digitalWrite (pinA, HIGH);
digitalWrite (pinB, LOW);
digitalWrite (pinB, LOW);
digitalWrite (pinC, LOW);
digitalWrite (pinC, LOW);
digitalWrite (pinD, HIGH);
digitalWrite (pinD, HIGH);
digitalWrite (pinE, HIGH);
digitalWrite (pinE, HIGH);
digitalWrite (pinF, HIGH);
digitalWrite (pinF, LOW);
digitalWrite (pinG, HIGH);
digitalWrite (pinG, LOW);
delay(1000);
delay(1000);
//8
//5
digitalWrite (pinA, LOW);
digitalWrite (pinA, LOW);
digitalWrite (pinB, LOW);
digitalWrite (pinB, HIGH);
digitalWrite (pinC, LOW);
digitalWrite (pinC, LOW);
digitalWrite (pinD, LOW);
digitalWrite (pinD, LOW);
digitalWrite (pinE, LOW);
digitalWrite (pinE, HIGH);
digitalWrite (pinF, LOW);
digitalWrite (pinF, LOW);
digitalWrite (pinG, LOW);
digitalWrite (pinG, LOW);
delay(1000);
delay(1000);
//9
//6
digitalWrite (pinA, LOW);
digitalWrite (pinA, LOW);
digitalWrite (pinB, LOW);
digitalWrite (pinB, HIGH);
digitalWrite (pinC, LOW);
digitalWrite (pinC, LOW);
digitalWrite (pinD, LOW);
digitalWrite (pinD, LOW);
digitalWrite (pinE, HIGH);
digitalWrite (pinE, LOW);
digitalWrite (pinF, LOW);
digitalWrite (pinF, LOW);
digitalWrite (pinG, LOW);
digitalWrite (pinG, LOW);
delay(1000);
delay(1000);
} 50
Traffic light with Count down Series
void loop() { //green //3
int pinA = 2; //red digitalWrite (ledG, HIGH); digitalWrite (pinA, LOW);
int pinB = 3; digitalWrite (ledR, HIGH); //8 digitalWrite (pinB, LOW);
int pinC = 4; //5 digitalWrite (pinA, LOW); digitalWrite (pinC, LOW);
int pinD = 5; digitalWrite (pinA, LOW); digitalWrite (pinB, LOW); digitalWrite (pinD, LOW);
int pinE = 6; digitalWrite (pinB, HIGH); digitalWrite (pinC, LOW); digitalWrite (pinE, HIGH);
int pinF = 7; digitalWrite (pinC, LOW); digitalWrite (pinD, LOW); digitalWrite (pinF, HIGH);
digitalWrite (pinD, LOW); digitalWrite (pinE, LOW);
int pinG = 8; digitalWrite (pinG, LOW);
digitalWrite (pinE, HIGH); digitalWrite (pinF, LOW); delay(1000);
int ledR = 9; digitalWrite (pinF, LOW); digitalWrite (pinG, LOW);
int ledY = 10; //2
digitalWrite (pinG, LOW); delay(1000); digitalWrite (pinA, LOW);
int ledG = 11; delay(1000); //7 digitalWrite (pinB, LOW);
//4 digitalWrite (pinA, LOW); digitalWrite (pinC, HIGH);
void setup() { digitalWrite (pinA, HIGH); digitalWrite (pinB, LOW); digitalWrite (pinD, LOW);
digitalWrite (pinB, LOW); digitalWrite (pinC, LOW); digitalWrite (pinE, LOW);
pinMode (pinA, OUTPUT); digitalWrite (pinC, LOW); digitalWrite (pinD, HIGH); digitalWrite (pinF, HIGH);
digitalWrite (pinD, HIGH); digitalWrite (pinE, HIGH);
pinMode (pinB, OUTPUT); digitalWrite (pinG, LOW);
digitalWrite (pinE, HIGH); digitalWrite (pinF, HIGH); delay(1000);
pinMode (pinC, OUTPUT); digitalWrite (pinF, LOW); digitalWrite (pinG, HIGH);
pinMode (pinD, OUTPUT); //1
digitalWrite (pinG, LOW); delay(1000); digitalWrite (pinA, HIGH);
pinMode (pinE, OUTPUT); delay(1000); //6 digitalWrite (pinB, LOW);
pinMode (pinF, OUTPUT); //3 digitalWrite (pinA, LOW); digitalWrite (pinC, LOW);
pinMode (pinG, OUTPUT); digitalWrite (pinA, LOW); digitalWrite (pinB, HIGH); digitalWrite (pinD, HIGH);
pinMode (ledR, OUTPUT); digitalWrite (pinB, LOW); digitalWrite (pinC, LOW); digitalWrite (pinE, HIGH);
pinMode (ledY, OUTPUT); digitalWrite (pinC, LOW); digitalWrite (pinD, LOW); digitalWrite (pinF, HIGH);
digitalWrite (pinD, LOW); digitalWrite (pinE, LOW); digitalWrite (pinG, HIGH);
pinMode (ledG, OUTPUT);
digitalWrite (pinE, HIGH); digitalWrite (pinF, LOW); delay(1000);
digitalWrite (pinF, HIGH); digitalWrite (pinG, LOW); digitalWrite (ledG, LOW);
} digitalWrite (pinG, LOW); delay(1000); delay(500);
delay(1000); //5 }
//2 digitalWrite (pinA, LOW);
digitalWrite (pinA, LOW); digitalWrite (pinB, HIGH);
digitalWrite (pinB, LOW); digitalWrite (pinC, LOW);
digitalWrite (pinC, HIGH); digitalWrite (pinD, LOW);
digitalWrite (pinD, LOW); digitalWrite (pinE, HIGH);
digitalWrite (pinE, LOW); digitalWrite (pinF, LOW);
digitalWrite (pinF, HIGH); digitalWrite (pinG, LOW);
digitalWrite (pinG, LOW); delay(1000);
delay(1000); //4
//1 digitalWrite (pinA, HIGH);
digitalWrite (pinA, HIGH); digitalWrite (pinB, LOW);
digitalWrite (pinB, LOW); digitalWrite (pinC, LOW);
digitalWrite (pinC, LOW); digitalWrite (pinD, HIGH);
digitalWrite (pinD, HIGH); digitalWrite (pinE, HIGH);
digitalWrite (pinE, HIGH); digitalWrite (pinF, LOW);
digitalWrite (pinF, HIGH); digitalWrite (pinG, LOW);
digitalWrite (pinG, HIGH); delay(1000);
delay(1000);
digitalWrite (ledR, LOW);
delay(500); 51
RGB MODULE

RGB MODULE to use an RGB (Red, Green, Blue) LED to create many colors and effects
in their projects
RGB int RED_PIN = 9;
int GREEN_PIN = 10;
// Blue (turn just the blue LED on):
digitalWrite(RED_PIN, LOW);

Module int BLUE_PIN = 11; digitalWrite(GREEN_PIN, LOW);


digitalWrite(BLUE_PIN, HIGH);
void setup() delay(1000);
{
pinMode(RED_PIN, OUTPUT); // Yellow (turn red and green on):
7 CHANGING COLORS pinMode(GREEN_PIN, OUTPUT); digitalWrite(RED_PIN, HIGH);
pinMode(BLUE_PIN, OUTPUT); digitalWrite(GREEN_PIN, HIGH);
} digitalWrite(BLUE_PIN, LOW);
void loop() delay(1000);
{
// Off (all LEDs off): // Cyan (turn green and blue on):
digitalWrite(RED_PIN, LOW); digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW); digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW); digitalWrite(BLUE_PIN, HIGH);
delay(1000); delay(1000);

// Red (turn just the red LED on): // Purple (turn red and blue on):
digitalWrite(RED_PIN, HIGH); digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW); digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW); digitalWrite(BLUE_PIN, HIGH);
delay(1000); delay(1000);

// Green (turn just the green LED // White (turn all the LEDs on):
on): digitalWrite(RED_PIN, HIGH);
digitalWrite(RED_PIN, LOW); digitalWrite(GREEN_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH); digitalWrite(BLUE_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW); delay(1000);
delay(1000); }
53
RGB Changing Random int rPin = 9;
Module Colors int gPin = 10;
int bPin = 11;

int rVal = 0;
int gVal = 0;
int bVal = 0; //Set the values to 0 to begin with
void setup() {
pinMode (gPin, OUTPUT);
pinMode (bPin, OUTPUT);
pinMode (rPin, OUTPUT);
}
void loop() {
analogWrite(rPin, rVal);
analogWrite(bPin, bVal);
analogWrite(gPin, gVal); //Apply PWM output to each leg of the
RGB LED, with the value stored in the corresponding variable.
rVal = random(0,255);
gVal = random(0,255);
bVal = random(0,255); //Randomise the variables to get a random
color each time
delay(500); //Delay before changing colors, so we can see each
change.
}

54
4 DIGITAL TUBE

4 DIGITAL TUBE is a chip to form a 4 digit numerical display module that


are sometimes called “Digital Tube”
4 Digit int pinA = 2; void loop() {

Display
int pinB = 3; digitalWrite(D1, HIGH); // OFF
int pinC = 4; digitalWrite(D2, LOW); // ON
int pinD = 5; digitalWrite(D3, LOW); // ON
int pinE = 6; digitalWrite(D4, LOW); //ON
int pinF = 7; //0
int pinG = 8; digitalWrite(pinA, HIGH); //ON
int d1 = 9; digitalWrite(pinB, HIGH); //ON
int d2 = 10; digitalWrite(pinC, HIGH); //ON
int d3 = 11; digitalWrite(pinD, HIGH); //ON
int d4 = 12; digitalWrite(pinE, HIGH); //ON
digitalWrite(pinF, HIGH); //ON
void setup() { digitalWrite(pinG, LOW); //OFF
delay(1000);
pinMode (pinA, OUTPUT);
pinMode (pinB, OUTPUT); digitalWrite(D1, LOW);
pinMode (pinC, OUTPUT); digitalWrite(D2, HIGH);
pinMode (pinD, OUTPUT); digitalWrite(D3, LOW);
pinMode (pinE, OUTPUT); digitalWrite(D4, LOW);
pinMode (pinF, OUTPUT); //1
pinMode (pinG, OUTPUT); digitalWrite(pinA, LOW);
pinMode (d1, OUTPUT); digitalWrite(pinB, HIGH);
pinMode (d2, OUTPUT); digitalWrite(pinC, HIGH);
pinMode (d3, OUTPUT); digitalWrite(pinD, LOW);
pinMode (d4, OUTPUT); digitalWrite(pinE, LOW);
} digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
delay(1000);

56
4 Digit //2
digitalWrite(pinA, HIGH);
//5
digitalWrite(pinA, HIGH);
Display digitalWrite(pinB, HIGH);
digitalWrite(pinC, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH); digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH); digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW); digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH); digitalWrite(pinG, HIGH);
delay(1000); delay(1000);
//6
digitalWrite(D1, LOW); digitalWrite(pinA, HIGH);
digitalWrite(D2, LOW); digitalWrite(pinB, LOW);
digitalWrite(D3, LOW); digitalWrite(pinC, HIGH);
digitalWrite(D4, HIGH); digitalWrite(pinD, HIGH);
//3 digitalWrite(pinE, HIGH);
digitalWrite(pinA, HIGH); digitalWrite(pinF, HIGH);
digitalWrite(pinB, HIGH); digitalWrite(pinG, HIGH);
digitalWrite(pinC, HIGH); delay(1000);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, LOW); //7
digitalWrite(pinF, LOW); digitalWrite(pinA, HIGH);
digitalWrite(pinG, HIGH); digitalWrite(pinB, HIGH);
delay(1000); digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
delay(1000);

57
4 Digit
Display digitalWrite(D1, LOW);
digitalWrite(D2, LOW);
digitalWrite(D3, LOW);
digitalWrite(D4, LOW);

//8
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
delay(1000);

//9
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
delay(1000);
}

58
4 Digit #include <Wire.h> void loop() {
ENTER A
Display NUMBER DIGIT
int aPin = 2;
int bPin = 3;
for (int i=0; i<timer; i++)
{
int cPin = 4; digitalWrite(D1, LOW);
int dPin = 5; digitalWrite(D2, HIGH);
int ePin = 6; digitalWrite(D3, HIGH);
int fPin = 7; digitalWrite(D4, HIGH);
int gPin = 8; pickNumber(4);
int D1 = 9; delay(1);
int D2 = 10; digitalWrite(D1, HIGH);
int D3 = 11; digitalWrite(D2, LOW);
int D4 = 12; digitalWrite(D3, HIGH);
int timer=1000; digitalWrite(D4, HIGH);
pickNumber(3);
void setup() { delay(1);
digitalWrite(D1, HIGH);
pinMode(aPin, OUTPUT); digitalWrite(D2, HIGH);
pinMode(bPin, OUTPUT); digitalWrite(D3, HIGH);
pinMode(cPin, OUTPUT); digitalWrite(D4, LOW);
pinMode(dPin, OUTPUT); pickNumber(1);
pinMode(ePin, OUTPUT); delay(1);
pinMode(fPin, OUTPUT); digitalWrite(D1, HIGH);
pinMode(gPin, OUTPUT); digitalWrite(D2, HIGH);
pinMode(D1, OUTPUT); digitalWrite(D3, LOW);
pinMode(D2, OUTPUT); digitalWrite(D4, HIGH);
pinMode(D3, OUTPUT); pickNumber(2);
pinMode(D4, OUTPUT); delay(1);
} }
}
59
4 Digit void pickNumber(int x){
void one()
{
ENTER A
Display NUMBER DIGIT
switch(x){
case 1: one(); break;
digitalWrite( aPin, LOW);
digitalWrite( bPin, HIGH);
case 2: two(); break; digitalWrite( cPin, HIGH);
case 3: three(); break; digitalWrite( dPin, LOW);
case 4: four(); break; digitalWrite( ePin, LOW);
case 5: five(); break; digitalWrite( fPin, LOW);
case 6: six(); break; digitalWrite( gPin, LOW);
case 7: seven(); break; }
case 8: eight(); break; void two()
case 9: nine(); break; {
default: zero(); break; digitalWrite( aPin, HIGH);
} digitalWrite( bPin, HIGH);
} digitalWrite( cPin, LOW);
digitalWrite( dPin, HIGH);
void clearLEDs() digitalWrite( ePin, HIGH);
{ digitalWrite( fPin, LOW);
digitalWrite( 2, LOW); // A digitalWrite( gPin, HIGH);
digitalWrite( 3, LOW); // B }
digitalWrite( 4, LOW); // C void three()
digitalWrite( 5, LOW); // D {
digitalWrite( 6, LOW); // E digitalWrite( aPin, HIGH);
digitalWrite( 7, LOW); // F digitalWrite( bPin, HIGH);
digitalWrite( 8, LOW); // G digitalWrite( cPin, HIGH);
} digitalWrite( dPin, HIGH);
digitalWrite( ePin, LOW);
digitalWrite( fPin, LOW);
digitalWrite( gPin, HIGH);
}
60
void four() void seven()
4 Digit { {
ENTER A digitalWrite( aPin, LOW); digitalWrite( aPin, HIGH);
Display NUMBER DIGIT
digitalWrite( bPin, HIGH);
digitalWrite( cPin, HIGH);
digitalWrite( bPin, HIGH);
digitalWrite( cPin, HIGH);
digitalWrite( dPin, LOW); digitalWrite( dPin, LOW);
digitalWrite( ePin, LOW); digitalWrite( ePin, LOW);
digitalWrite( fPin, HIGH); digitalWrite( fPin, LOW);
digitalWrite( gPin, HIGH); digitalWrite( gPin, LOW);
} }
void five() void eight()
{ {
digitalWrite( aPin, HIGH); digitalWrite( aPin, HIGH);
digitalWrite( bPin, LOW); digitalWrite( bPin, HIGH);
digitalWrite( cPin, HIGH); digitalWrite( cPin, HIGH);
digitalWrite( dPin, HIGH); digitalWrite( dPin, HIGH);
digitalWrite( ePin, LOW); digitalWrite( ePin, HIGH);
digitalWrite( fPin, HIGH); digitalWrite( fPin, HIGH);
digitalWrite( gPin, HIGH); digitalWrite( gPin, HIGH);
} }
void six() void nine()
{ {
digitalWrite( aPin, HIGH); digitalWrite( aPin, HIGH);
digitalWrite( bPin, LOW); digitalWrite( bPin, HIGH);
digitalWrite( cPin, HIGH); digitalWrite( cPin, HIGH);
digitalWrite( dPin, HIGH); digitalWrite( dPin, HIGH);
digitalWrite( ePin, HIGH); digitalWrite( ePin, LOW);
digitalWrite( fPin, HIGH); digitalWrite( fPin, HIGH);
digitalWrite( gPin, HIGH); digitalWrite( gPin, HIGH);
} }
61
4 Digit
ENTER A void zero()
Display NUMBER DIGIT
{
digitalWrite( aPin, HIGH);
digitalWrite( bPin,HIGH);
digitalWrite( cPin, HIGH);
digitalWrite( dPin, HIGH);
digitalWrite( ePin, HIGH);
digitalWrite( fPin, HIGH);
digitalWrite( gPin, LOW);
}

62
Diodes

LDR (LIGHT DEPENDENT RESISTOR) itself varies its resistance based on the amount
of light that hits the surface, we can measure the resistance and use that to determine
the level of light present.
LDR SENSOR  
int inPin = A0; //Pin the sensor is connected to
int sensorVal = 0; //Variable to store sensor data
 
void setup () {
 
Serial.begin(9600);
Serial.println("Serial Communication started...\n");
}
 
void loop () {
 
sensorVal = analogRead(inPin);
 
//analogRead will read the voltage on the pin
specified and return it as a value between 0 and
1024.
 
Serial.println(sensorVal);
 
//Print the sensor reading to the serial window so
we can view the data.
}

64
int ledpin = 13;
LED WITH LDR SENSOR int ldrpin = A0;

void setup() {

pinMode(ledpin, OUTPUT);
pinMode(ldrpin, INPUT);
Serial.begin(9600);// set baud rate at “9600”
}
void loop() {

int ldrStatus = analogRead(ldrpin);

// check if the ldr status is <=300


//if it is , the LED is HIGH

if (ldrStatus <=300){
digitalWrite(ledpin, HIGH); //turn LED on
Serial.println("LDR is DARK,LED is ON");

}
else{
digitalWrite(ledpin,LOW); //turn LED off
Serial.println("-----------------");
}
}
65
void loop() {
ldrValue = analogRead(ldrPin);
const int ldrPin = A0; Serial.println(ldrValue);
const int ledPin0 = 8;
const int ledPin1 = 9;
const int ledPin2 = 10; if (ldrValue < ldrlevel) {
const int ledPin3 = 11; digitalWrite(buzzer, HIGH);
Light Level Sensor const int ledPin4 = 12;
const int buzzer = 13;
}
else if (ldrValue < ldrlevel0) {
digitalWrite(ledPin4, HIGH);
int ldrValue = 0; }
int ldrlevel=400; // level sensitivity LDR else if (ldrValue < ldrlevel1) {
int ldrlevel0=500; // level sensitivity LDR digitalWrite(ledPin4, LOW);
int ldrlevel1=600; // level sensitivity LDR digitalWrite(ledPin3, HIGH);
int ldrlevel2=700; // level sensitivity LDR }
int ldrlevel3=800; // level sensitivity LDR else if (ldrValue < ldrlevel2) {
int ldrlevel4=900; // level sensitivity LDR digitalWrite(ledPin3, LOW);
digitalWrite(ledPin2, HIGH);
void setup() { }
else if (ldrValue < ldrlevel3) {
Serial.begin(9600); digitalWrite(ledPin2, LOW);
pinMode(ledPin0, OUTPUT); digitalWrite(ledPin1, HIGH);
pinMode(ledPin1, OUTPUT); }
pinMode(ledPin2, OUTPUT); else if (ldrValue < ldrlevel4) {
pinMode(ledPin3, OUTPUT); digitalWrite(ledPin1, LOW);
pinMode(ledPin4, OUTPUT); digitalWrite(ledPin0, HIGH);
pinMode(buzzer, OUTPUT); }
} else {
digitalWrite(ledPin0, LOW);
digitalWrite(buzzer, LOW);
}
66
}
void loop() {
ldrValue = analogRead(ldrPin);
const int ldrPin = A0; Serial.println(ldrValue);
const int ledPin0 = 8;
const int ledPin1 = 9;
const int ledPin2 = 10; if (ldrValue < ldrlevel) {
const int ledPin3 = 11; digitalWrite(buzzer, HIGH);
Light Level Sensor const int ledPin4 = 12;
const int buzzer = 13;
}
else if (ldrValue < ldrlevel0) {
digitalWrite(ledPin4, HIGH);
int ldrValue = 0; }
int ldrlevel=400; // level sensitivity LDR else if (ldrValue < ldrlevel1) {
int ldrlevel0=500; // level sensitivity LDR digitalWrite(ledPin4, LOW);
int ldrlevel1=600; // level sensitivity LDR digitalWrite(ledPin3, HIGH);
int ldrlevel2=700; // level sensitivity LDR }
int ldrlevel3=800; // level sensitivity LDR else if (ldrValue < ldrlevel2) {
int ldrlevel4=900; // level sensitivity LDR digitalWrite(ledPin3, LOW);
digitalWrite(ledPin2, HIGH);
void setup() { }
else if (ldrValue < ldrlevel3) {
Serial.begin(9600); digitalWrite(ledPin2, LOW);
pinMode(ledPin0, OUTPUT); digitalWrite(ledPin1, HIGH);
pinMode(ledPin1, OUTPUT); }
pinMode(ledPin2, OUTPUT); else if (ldrValue < ldrlevel4) {
pinMode(ledPin3, OUTPUT); digitalWrite(ledPin1, LOW);
pinMode(ledPin4, OUTPUT); digitalWrite(ledPin0, HIGH);
pinMode(buzzer, OUTPUT); }
} else {
digitalWrite(ledPin0, LOW);
digitalWrite(buzzer, LOW);
}
67
}
TILT SWITCH

TILT SWITCH OR MERCURY SWITCH is an electrical switch that opens and closes


a circuit when a small amount of the liquid metal mercury connects metal electrodes
to close the circuit. When it is tilted a small amount of the liquid metal mercury makes
contact with metal electrodes to close the circuit
TILT SWITCH

int tpin = 7;

void setup() {

Serial.begin(9600);
Serial.println("Serial Established..\nTilt board to continue.");
pinMode (tpin, INPUT); //Set pin 7 to input for reading the sensor.
}
void loop() {

if (digitalRead(tpin) == true)
{
Serial.println("Tilted!");
delay (1000);
}
else {
Serial.println("Upright!");
delay (1000);
}
}
69
int tpin = 7;
Tilt switch with LED int led = 2;

void setup() {
pinMode (led, OUTPUT);
Serial.begin(9600);
Serial.println("Serial Established..\nTilt board to continue.");
pinMode (tpin, INPUT); //Set pin 7 to input for reading the
sensor.
}
void loop() {
if (digitalRead(tpin) == true)

{
digitalWrite(led, HIGH);
Serial.println ("Tilted!");
delay (1000);
}
else {
digitalWrite(led, LOW);
Serial.println ("Upright!");
delay (1000);
}
}

70
LCD

LCD (liquid crystal display) is the technology used for displays in notebook and
other smaller computers.
LCD DISPLAY Display Text #include <LiquidCrystal.h>
void loop() {
lcd.print(" hello ");
lcd.setCursor(0, 1);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); lcd.print(" world.. ");
delay (1000);
void setup() {
lcd.begin(16, 2); lcd.clear();
lcd.print(" jomar cruz"); lcd.print(" blink cursor");
lcd.setCursor(0, 1); lcd.blink();
lcd.print(" welcome"); delay (2000);
delay(2000);
lcd.clear(); lcd.clear();
lcd.print("Arduino Projects"); lcd.print("NO Blink Cursor");
lcd.setCursor(0, 1); lcd.noBlink();
lcd.print(" Training"); delay (2000);
delay(2000);
lcd.clear(); lcd.clear();
lcd.print(" SAMPLE"); lcd.print(" line cursor");
lcd.setCursor(0, 1); lcd.cursor();
lcd.print(" LCD DISPLAY"); delay (1000);
delay(2000); lcd.clear();
lcd.clear(); lcd.print(" NO cursor");
lcd.noCursor();
} delay (1000);
lcd.clear();
} 72
#include <LiquidCrystal_I2C.h> lcd.print(" Hello ");
1602 LCD LiquidCrystal_I2C lcd(0x27,16,2); lcd.setCursor(0, 1);
lcd.print(" world.. ");
void setup() {
lcd.init(); //initialize the lcd delay (1000);
lcd.backlight(); //open the lcd.clear();
backlight
} lcd.print(" Hello Everyone");
void loop() lcd.setCursor(0, 1);
{ lcd.print(" Welcome");
lcd.begin(16, 2); delay(2000);
lcd.print(" Hello Everyone");
lcd.setCursor(0, 1); lcd.clear();
lcd.print(" Welcome"); lcd.print("TechFactors Inc.");
delay(2000); lcd.blink();
lcd.clear(); delay (2000);
lcd.print("Arduino Projects"); lcd.clear();
lcd.setCursor(0, 1);
lcd.print(" Training"); lcd.print(" I wish you ");
delay(2000); lcd.setCursor(0, 1);
lcd.clear(); lcd.print(" all the best.");
lcd.print(" SAMPLE"); lcd.noCursor();
lcd.setCursor(0, 1); delay (2000);
lcd.print(" LCD DISPLAY"); lcd.clear();
delay(2000);
lcd.clear(); }
73
LIBRARIES

LIBRARIES
It allows you to call functions that are already written, for an example the Stepper
Motor library contains all of the raw code to access the hardware, all we need to do is
initialize it and call the functions. A library does all the heavy lifting so you can use
your modules quickly and easily.
First you should navigate to the
Sketch -> Include Library Menu.

75
Click on Manage Libraries and the
following window will appear:

76
Another step is to add .Zip file to
your Library

First you should navigate to the Sketch


-> Include Library Menu.

77
Locate and select the .Zip File that
will be added to the Arduino Directory
and then click Open.
O n c e d o n e G o t o fi l e - > E x a m p l e M e n u
t h e n l o c a t e d t h e A d d e d z i p fi l e y o u
included.
Servo Motor

A Servomotor is a rotary actuator or linear actuator that allows for precise control
of angular or linear position, velocity and acceleration. It consists of a
suitable motor coupled to a sensor for position feedback.
Servo Motor

#include<Servo.h>
Servo name_servo; // define any servo name

void setup() {

name_servo.attach (9); // define servo signal pin


}
void loop() {

name_servo.write (180); // Turned to 180 degree


delay (3000);
name_servo.write (0); // Turned to 0 degree
delay (2000);
name_servo.write (90); // Turned to 90 degree
delay (2000);
}

80
Servo With push button command
#include<Servo.h>
Servo name_servo; // define any servo name
const int pinA = 10;//button

void setup() {
pinMode (pinA, INPUT); //button//
name_servo.attach (7); // define servo signal pin
}
void loop() {

if (digitalRead(pinA) == HIGH){
name_servo.write (0); // Turned to 0 degree
delay(1000);}

else {
name_servo.write (180); // Turned to 180 degree
}

81
Servo With 2 push button command
#include<Servo.h>
int pos = 0;
Servo servo;

void setup() {
pinMode (2, INPUT);
pinMode (3, INPUT);
servo.attach(9);
}
void loop() {
while (digitalRead(2) == HIGH && pos < 180) {
pos++;
servo.write(pos);
delay(15);
}
while (digitalRead(3) == HIGH && pos > 0) {
pos--;
servo.write(pos);
delay(15);
}
}
82
Servo Motor with Potentiometer

#include <Servo.h> // add servo library

Servo myservo; // create servo object to control a servo

int potpin = 0; // analog pin used to connect the potentiometer


int val; // variable to read the value from the analog pin

void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo
object
}

void loop() {
val = analogRead(potpin);
// reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180);
// scale it to use it with the servo (value between 0 and 180)
myservo.write(val);
// sets the servo position according to the scaled value
delay(15);
// waits for the servo to get there
} 83
Ultrasonic
Sensor

An Ultrasonic sensor is a device that can measure the distance to an object by


using sound waves. It measures distance by sending out a sound wave at a specific
frequency and listening for that sound wave to bounce back.
int trigPin = 9;
Ultrasonic int echoPin = 10;

Sensor long duration;


int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delay(100);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delay(100);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in
microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
} 85
int trigPin = 9;
int echoPin = 10;
Distance Light int LED_R = 11;
identifier int LED_G = 13;
 
// defines variables
long duration;
int distance;
int safetyDistance;
 
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(LED_R, OUTPUT);
pinMode(LED_G, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}
 
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
 
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW); 86
// Reads the echoPin, returns the sound wave travel time in
microseconds
Distance Light duration = pulseIn(echoPin, HIGH);
identifier  
// Calculating the distance
distance= duration*0.034/2;
 
safetyDistance = distance;
if (safetyDistance <= 15){ //safe distance 15 cm
digitalWrite(LED_G, HIGH);
digitalWrite(LED_R, LOW);
}
else{
digitalWrite(LED_G, LOW);
digitalWrite(LED_R, HIGH);
}
 
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}
 

87
const int trigPin = 9;
Ultra Sensor with
ULTRASONIC Buzzer & LED
const int echoPin = 10;
const int buzzer = 11;
SENSOR Distance Alarm const int ledPin = 13;

// defines variables
long duration;
int distance;
int safetyDistance;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds


digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
88
Ultra Sensor with
ULTRASONIC Buzzer & LED // Reads the echoPin, returns the sound wave travel time in
SENSOR Distance Alarm microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance


distance= duration*0.034/2;

safetyDistance = distance;
if (safetyDistance <= 10){
digitalWrite(buzzer, HIGH);
digitalWrite(ledPin, HIGH);
}
else{
digitalWrite(buzzer, LOW);
digitalWrite(ledPin, LOW);
}

// Prints the distance on the Serial Monitor


Serial.print("Distance: ");
Serial.println(distance);
}

89
Servo with ultra sonic distance reader

#include <Servo.h>
Servo servo; for (pos = 180; pos >= 0; pos -= 1)
const int trig = 2, echo = 3 ; {
int pos = 0; servo.write(pos);
void setup() delay(50);
{ Serial.println(radar());
Serial.begin(9600); }
pinMode(trig, OUTPUT);
pinMode(echo, INPUT); }
servo.attach(9); long radar(void)
} {
void loop() digitalWrite(trig, HIGH);
{ delayMicroseconds(15);
for (pos = 0; pos <= 180; pos += 1) digitalWrite(trig, LOW);
{ long dur = pulseIn(echo, HIGH);
servo.write(pos); dur = dur / 58;
delay(50); return dur;
Serial.println(radar()); }
}

90
ULTRA SENSOR WITH LCD DISTANCE DISPLAY
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Creates an LCD object.


Parameters: (rs, enable, d4, d5, d6, d7)

const int trigPin = 9;


const int echoPin = 10;
long duration;
int distanceCm, distanceInch;

void setup() {

lcd.begin(16,2); // Initializes the interface to the LCD screen, and


specifies the dimensions (width and height) of the display
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

91
void loop() {

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);


distanceCm= duration*0.034/2;
distanceInch = duration*0.0133/2;

lcd.setCursor(0,0); // Sets the location at which subsequent text


written to the LCD will be displayed
lcd.print("Distance: "); // Prints string "Distance" on the LCD
lcd.print(distanceCm); // Prints the distance value from the sensor
lcd.print(" cm");
delay(10);
lcd.setCursor(0,1);
lcd.print("Distance: ");
lcd.print(distanceInch);
lcd.print("inch");
delay(10);

}
92
SOUND SENSOR

SOUND SENSOR MODULE is a simple microphone, it is used to detect and measure


sound waves that travels through the air they cause everything that come into contact
with it to vibrate.
SOUND
SENSOR int sound = A0;
 
void setup() {
pinMode (sound, INPUT);
Serial.begin (9600);
// initialize serial communication at 9600 bits per
second:

}
 
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(sound);
 
// print out the value you read:
Serial.println (sensorValue);
 
delay(100);
}

94
SOUND Noise
SENSOR int soundSensor=2;
int LED=4;

void setup() {
pinMode(soundSensor,INPUT);
pinMode(LED,OUTPUT);

}
void loop() {

int SensorData=digitalRead(soundSensor);

if(SensorData==1){
digitalWrite(LED,HIGH);
}
else{
digitalWrite(LED,LOW);
}
}

95
SOUND Clap int soundSensor=2;
int LED=4;
SENSOR boolean LEDStatus=false;

void setup() {
pinMode(soundSensor,INPUT);
pinMode(LED,OUTPUT);

void loop() {

int SensorData=digitalRead(soundSensor);
if(SensorData==1){

if(LEDStatus==false){
LEDStatus=true;
digitalWrite(LED,HIGH);
}
else{
LEDStatus=false;
digitalWrite(LED,LOW);
}
}
}
96
JOYSTICK

JOYSTICK is a combination of 2 analog potentiometer and a digital switch.


for the (X, Y) a 2-axis analog output and for (Z) 1 digital output channel button
Joystick
int JoyStick_X = A0; // x
int JoyStick_Y = A1; // y
 
void setup ()
{
pinMode (JoyStick_X, INPUT);
pinMode (JoyStick_Y, INPUT);
 
Serial.begin (9600); // 9600 bps
}
void loop ()
{
int x, y, z;
x = analogRead (JoyStick_X);
y = analogRead (JoyStick_Y);
 
Serial.print (x, DEC);
Serial.print (",");
Serial.println (y, DEC);
delay (1000);
}

98
Servo Motor with Joystick
Joystick
#include <Servo.h>
 
Servo servo1;
 
//define joystick pin (analog)
int joyX = 0; // servo 1
 
// variable to read the value from analog pins
int joyVal;
 
void setup() {
// attach servo pin PWM 3
servo1.attach(3);
}
void loop() {
// read the value of the joystick (between 0-1023)
joyVal = analogRead (joyX);
joyVal = map (joyVal, 0,1023,0,180); // servo value between 0-180
servo1.write(joyVal); // set servo position according to joystick
value
delay (15);
}

99
#include<Servo.h>

Joystick
int pos = 0;
Servo Motor 1 with Joystick Servo servo1, servo2;
const int joyH = 3;
Servo Motor 2 with Button int servoVal;
 
void setup() {
pinMode(2, INPUT);
pinMode(3, INPUT);
servo2.attach(8);
servo1.attach(9);
}
void loop()
{
servoVal = analogRead(joyH);
servoVal = map(servoVal, 0, 1023, 0, 180);
servo2.write(servoVal);
delay(15);
{
while (digitalRead(2) == HIGH && pos < 180) {
pos++;
servo1.write(pos);
delay(15);
}
while (digitalRead(3) == HIGH && pos > 0) {
pos--;
servo1.write(pos);
delay(15);
} } } 100
Transistor

Term derived from “transfer resistor” a semiconductor device that can be used as an
amplifier or as an electronic switch.
TEMPERATURE
SENSOR 

TEMPERATURE SENSOR is often a resistance temperature detector or


thermocouple that measures temperature through an electrical signal
int varPin = A0; //A0 is the analog in pin
Temperature int val = 0; //This will store the current value from the sensor
int oldVal = 0; //This will store the last value from the sensor to
sensor compare.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Serial connection established..\nTemperature
logging started!");
}
void loop() {
// put your main code here, to run repeatedly:
oldVal = val; //Store the last value in the variable
val = analogRead(varPin); //Store the new value in its variable
if (oldVal != val) //If the temperature has changed, tell us the new
temp
{
Serial.print("Temp: ");
Serial.print(5.0 * val * 100 / 1024); //please see note at bottom
regarding this formula.
Serial.println(" Degrees(Celsius)");
}
delay(500); //Wait half a second, then do it again. (This allows the
line to settle between reads. Not really required, but it does
remove a bit of jitter

} 103
FLAME SENSOR

FLAME SENSOR can be used to detect fire source or other light sources of the
wavelength in the range of 760nm - 1100 nm. It is based on the YG1006 sensor which is
a high speed and high sensitive NPN silicon phototransistor. Due to its black epoxy, the
sensor is sensitive to infrared radiation
Flame
Sensor int inPin = A0;
int sensorVal = 0;

void setup() {
Serial.begin(9600);
Serial.println("Serial Communication started...\nReady to detect Flame.");
}

void loop() {
sensorVal = analogRead(inPin);

if (sensorVal < 1000) {


//Flame
Serial.print("Flame: ");
Serial.println(sensorVal);
}
else {
}
}

105
Flame Fire Alarm
Sensor int flame=0;// select analog pin 0 for the sensor
int Beep=9;// select digital pin 9 for the buzzer
int val=0;// initialize variable
void setup()
{
pinMode(Beep,OUTPUT);// set LED pin as “output”
pinMode(flame,INPUT);// set buzzer pin as “input”

Serial.begin(9600);// set baud rate at “9600”


}
void loop()
{
val=analogRead(flame);// read the analog value of the sensor
Serial.println(val);// output and display the analog value
if(val>=100)// when the analog value is larger than 100, the buzzer will activate.
{
digitalWrite(Beep,HIGH);
}else
{
digitalWrite(Beep,LOW);
}
delay(500);
}
106
WATER LEVEL

WATER LEVEL SENSORS are used to detect the level of substances that can flow.
Such substances include liquids, slurries, granular material and powders.
Level measurements can be done inside containers or it can be the level of a river
or lake.
Water Level
Sensor int measurement= A0;
 
void setup()
{
Serial.begin(9600);
}
 
void loop()
{
 
int val = analogRead(A0); // read
input value
Serial.print("Pin Value ");
Serial.println(val);
delay(2000);
}

108
RFID

RFID RADIO-FREQUENCY IDENTIFICATION DEVICE serves the same purpose as a


bar code or a magnetic strip on the back of a credit card or ATM card; it provides a
unique identifier for that object.
RFID

MODULE PIN ARDUINO PIN


VCC (+ Voltage) 3.3v
RST (Reset) 9
GND (Ground) GND
MISO (Master in Slave out) 12
MOSI (Master out Slave in) 11
SCK (Serial Clock) 13
SDA (Serial Data Signal) 10
IRQ NOT CONNECTED

To use the RFID Module, download the latest MFRC522 .Zip file


first from the internet then add it up to your Arduino library
return;
Set A RFID READER }
//Show UID on serial monitor
Serial.print("UID tag :");
String content= "";
#include <SPI.h> byte letter;
#include <MFRC522.h> for (byte i = 0; i < mfrc522.uid.size; i++)
{
#define SS_PIN 10 Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
#define RST_PIN 9 Serial.print(mfrc522.uid.uidByte[i], HEX);
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance. content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
void setup() }
{ Serial.println();
Serial.begin(9600); // Initiate a serial communication Serial.print("Message : ");
SPI.begin(); // Initiate SPI bus content.toUpperCase();
mfrc522.PCD_Init(); // Initiate MFRC522 if (content.substring(1) == "XX XX XX XX") //change here the UID of the
Serial.println("Put your card to the reader..."); card/cards that you want to give access
Serial.println(); {
Serial.println("Authorized access");
} Serial.println();
void loop() delay(3000);
{ }
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) else {
{ Serial.println(" Access denied");
return; delay(3000);
} }
// Select one of the cards }
if ( ! mfrc522.PICC_ReadCardSerial())
111
{
Set B-1 RFID READER
void loop()
{
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
#include <SPI.h> return;
#include <MFRC522.h> }
#include <Servo.h> // Select one of the cards
#define SS_PIN 10 if ( ! mfrc522.PICC_ReadCardSerial())
#define RST_PIN 9 {
#define LED_G 5 //define green LED pin return;
#define LED_R 4 //define red LED }
#define BUZZER 2 //buzzer pin //Show UID on serial monitor
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance. Serial.print("UID tag :");
Servo myServo; //define servo name String content= "";
byte letter;
void setup() { for (byte i = 0; i < mfrc522.uid.size; i++)
Serial.begin(9600); // Initiate a serial communication {
SPI.begin(); // Initiate SPI bus Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
mfrc522.PCD_Init(); // Initiate MFRC522 Serial.print(mfrc522.uid.uidByte[i], HEX);
myServo.attach(3); //servo pin content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
myServo.write(0); //servo start position content.concat(String(mfrc522.uid.uidByte[i], HEX));
pinMode(LED_G, OUTPUT); }
pinMode(LED_R, OUTPUT); Serial.println();
pinMode(BUZZER, OUTPUT); Serial.print("Message : ");
noTone(BUZZER); content.toUpperCase();
Serial.println("Put your card to the reader..."); if (content.substring(1) == "XX XX XX XX") //change here the UID of the
Serial.println(); card/cards that you want to give access
}
112
Set B-2 RFID READER {
Serial.println();
Serial.print("Message : ");
content.toUpperCase();
if (content.substring(1) == "XX XX XX XX") //change here
the UID of the card/cards that you want to give access
{
Serial.println("Authorized access");
Serial.println();
delay(500);
digitalWrite(LED_G, HIGH);
tone(BUZZER, 500);
delay(300);
noTone(BUZZER);
myServo.write(180);
delay(5000);
myServo.write(0);
digitalWrite(LED_G, LOW);
}

else {
Serial.println(" Access denied");
digitalWrite(LED_R, HIGH);
tone(BUZZER, 300);
delay(1000);
digitalWrite(LED_R, LOW);
noTone(BUZZER);
}
}
113
RFID Lock System #include <SPI.h>
#include <MFRC522.h>
 
#define SS_PIN 10
#define RST_PIN 9
 
#define LED_G 5 //define green LED pin
#define LED_R 4 //define red LED
#define BUZZER 2 //buzzer pin
MFRC522 mfrc522 (SS_PIN, RST_PIN); // Create
MFRC522 instance.
 
void setup()
{
Serial.begin(9600); // Initiate a serial
communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
 
pinMode(LED_G, OUTPUT);
pinMode(LED_R, OUTPUT);
pinMode(BUZZER, OUTPUT);
noTone(BUZZER);
Serial.println("Put your card to the reader...");
Serial.println();
 
}

114
Lock System
RFID
void loop()
{
if ( ! mfrc522.PICC_IsNewCardPresent()) // Look for new cards
{
return; {
} Serial.println("Authorized access");
if ( ! mfrc522.PICC_ReadCardSerial()) // Select one of the cards Serial.println();
{ delay(500);
return; digitalWrite(LED_G, HIGH);
} tone(BUZZER, 500);
Serial.print("UID tag :"); //Show UID on serial monitor delay(300);
String content= ""; noTone(BUZZER);
byte letter; digitalWrite(LED_G, LOW);
for (byte i = 0; i < mfrc522.uid.size; i++) }
{ else {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); Serial.println(" Access denied");
Serial.print(mfrc522.uid.uidByte[i], HEX); digitalWrite(LED_R, HIGH);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ")); tone(BUZZER, 300);
content.concat(String(mfrc522.uid.uidByte[i], HEX)); delay(1000);
} digitalWrite(LED_R, LOW);
Serial.println(); noTone(BUZZER);
Serial.print("Message : "); }
content.toUpperCase(); }
if (content.substring(1) == "XX XX XX XX")
//change here the UID of the card/cards that you want to give access 115
STEPPER MOTOR

STEPPER MOTOR or step motor or stepping motor is a brushless DC electric motor


that divides a full rotation into a number of equal steps

ZC-A0591 PIN ARDUINO PIN


1N1 8
1N2 9
1N3 10
1N4 11
- GND
+ +5 – 12V
Stepper #define IN1 2 void loop() {
motor #define IN2 3
#define IN3 4 int rtn = 3;
#define IN4 5  
  digitalWrite (2,HIGH);
void setup() { delay (rtn);
  digitalWrite (2,LOW);
pinMode (2,OUTPUT);  
pinMode (3,OUTPUT); digitalWrite (3,HIGH);
pinMode (4,OUTPUT); delay (rtn);
pinMode (5,OUTPUT); digitalWrite (3,LOW);
digitalWrite (2,LOW);  
digitalWrite (3,LOW); digitalWrite (4,HIGH);
digitalWrite (4,LOW); delay (rtn);
digitalWrite (5,LOW); digitalWrite (4,LOW);
}  
  digitalWrite (5,HIGH);
delay (rtn);
digitalWrite (5,LOW);
}

117
#include <Stepper.h>

//How many steps does your motor have


#define STEPS 48

//Start new Stepper class with 48 steps (STEPS defined above)


//Motor is connected to pins 4, 5, 6 & 7
Stepper stepper(STEPS, 4, 5, 6, 7);

//Create starting position for motor / pot


int previous = 0;

void setup()
{
//How fast will we try to move the motor
//If your motor stutters, its too fast so just lower the value
stepper.setSpeed(180);
}

void loop()
{
//Read analog 0 (pot) and map it to the range of the motor
int val = map(analogRead(0), 0, 1023, 48, 0);
//Move motor based off of last recorded position
stepper.step(val - previous);
//Store current position
previous = val;
}

118
DC Motor

A DC motor is any of a class of rotary electrical machines that converts direct
current electrical energy into mechanical energy.
DC MOTOR DC MOTOR WITH 2N700 (MONITORING SPEED AND
RUNNING TIME)
 
int motor = 9;
int x = 0;
 
void setup() {
pinMode(motor, OUTPUT);
Serial.begin (9600);
}
 
void loop() {
 
while (x < 350) { // amount of time
x = x + 25;
analogWrite (motor, x);
delay (1000); // running time
analogWrite (motor, 0);
delay (100);
Serial.println (x);

}
} 120
Knock Sensor
const int sensorPin=0;
const int ledPin= 13;
const int threshold= 100;

void setup()
{
pinMode(ledPin, OUTPUT);
}

void loop()
{
int val= analogRead(sensorPin);
if (val >= threshold)
{
digitalWrite(ledPin, HIGH);
delay(5000);
digitalWrite(ledPin, LOW);
}
else
digitalWrite(ledPin, LOW);
}

122
int UD = 0;
int LR = 0; //Setting up controller//
LCD matrix #include "LedControl.h" // need the library
LedControl lc=LedControl(8,10,9,1); //10 is to CLOCK, 9 = CS, 8=DIN//

void setup() {
Serial.begin(9600);

lc.shutdown(0,false);// turn off power saving, enables display


lc.setIntensity(0,8);// sets brightness (0~15 possible values)
lc.clearDisplay(0);// clear screen

void loop() {
UD = analogRead(A0);
LR = analogRead(A1);
char x_translate = map(LR, 1021, 0, 7, 0); //This maps the values//
char y_translate = map(UD, 1021, 0, 0, 7);

Serial.print("UD = ");
Serial.print(UD, DEC);
Serial.print(", LR = ");
Serial.print(LR, DEC);
Serial.print(", x = ");
Serial.print(x_translate, DEC);
Serial.print(", y = ");
Serial.println(y_translate, DEC);
// not in shutdown mode
123
lc.clearDisplay(0);
ARDUINO
UNO
THANK YOU

Neal Creative © Neal Creative | click & Learn more

You might also like