Arduino Robotics Revise
Arduino Robotics Revise
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
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
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.
!
BREADBOARD is used to create circuits and connect different sensors
and actuators to the Arduino
board it also refers to a solderless breadboard.
Connection
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( ) {
void loop( ) {
18
Arduino
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
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
void setup() {
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() {
}
void loop() {
int val = analogRead(potpin);
val = map (val, 1, 1024, 1, 255);
analogWrite (led, val);
}
38
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
void setup() {
41
Buzzer Passive Buzzer
Melody Sound
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
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
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);
// 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
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() {
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
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
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() {
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
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
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);
safetyDistance = distance;
if (safetyDistance <= 10){
digitalWrite(buzzer, HIGH);
digitalWrite(ledPin, HIGH);
}
else{
digitalWrite(buzzer, LOW);
digitalWrite(ledPin, LOW);
}
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>
void setup() {
91
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
}
92
SOUND SENSOR
}
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
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
} 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);
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”
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
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
117
#include <Stepper.h>
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);
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