Arduino Lab Book BBACA
Arduino Lab Book BBACA
Diagram:
Program /Code:
a. Single LED blink
/* Assignment 1. Demonstration of LED Blink
*/
# define LED 6
// the setup function runs once when you press reset or power the
board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED, OUTPUT);
}
b. LED Array
int ledPin0 = 2;
int ledPin1 = 3;
int ledPin2 = 4;
int ledPin3 = 5;
int ledPin4 = 6;
int ledPin5 = 7;
int ledPin6 = 8;
int ledPin7 = 9;
void setup() {
pinMode(ledPin0, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
pinMode(ledPin7, OUTPUT);
}
void loop() {
digitalWrite(ledPin0, HIGH);
delay(50);
digitalWrite(ledPin0, LOW);
delay(50);
digitalWrite(ledPin1, HIGH);
delay(50);
digitalWrite(ledPin1, LOW);
delay(50);
digitalWrite(ledPin2, HIGH);
delay(50);
digitalWrite(ledPin2, LOW);
delay(50);
digitalWrite(ledPin3, HIGH);
delay(50);
digitalWrite(ledPin3, LOW);
delay(50);
digitalWrite(ledPin4, HIGH);
delay(50);
digitalWrite(ledPin4, LOW);
delay(50);
digitalWrite(ledPin5, HIGH);
delay(50);
digitalWrite(ledPin5, LOW);
delay(50);
digitalWrite(ledPin6, HIGH);
delay(50);
digitalWrite(ledPin6, LOW);
delay(50);
digitalWrite(ledPin7, HIGH);
delay(50);
digitalWrite(ledPin7, LOW);
delay(50);
digitalWrite(ledPin7, HIGH);
delay(50);
digitalWrite(ledPin7, LOW);
delay(50);
digitalWrite(ledPin6, HIGH);
delay(50);
digitalWrite(ledPin6, LOW);
delay(50);
digitalWrite(ledPin5, HIGH);
delay(50);
digitalWrite(ledPin5, LOW);
delay(50);
digitalWrite(ledPin4, HIGH);
delay(50);
digitalWrite(ledPin4, LOW);
delay(50);
digitalWrite(ledPin3, HIGH);
delay(50);
digitalWrite(ledPin3, LOW);
delay(50);
digitalWrite(ledPin2, HIGH);
delay(50);
digitalWrite(ledPin2, LOW);
delay(50);
digitalWrite(ledPin1, HIGH);
delay(50);
digitalWrite(ledPin1, LOW);
delay(50);
digitalWrite(ledPin0, HIGH);
delay(50);
digitalWrite(ledPin0, LOW);
delay(50);
}
Assignment No. 2: Demonstration of interfacing of switch & LED.
/* Switch (Push button and LED - Turns on and off a light emitting
diode(LED) connected to digital pin 2, when pressing a pushbutton
attached to pin 3.
*/
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 3; // the number of the pushbutton pin
const int ledPin = 2; // the number of the LED pin
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
a. Single LED fade in and fade out b. LED Array fade in and fade out
Procedure:
1. Connect the Arduino Uno board to the PC through USB cable.
2. Start Arduino IDE from PC and open new Sketch and the program/code
for that and save as LEDfade.
3. Verify/Compile the program and Upload the code into Arduino Uno.
4. Make necessary connections according diagram.
5. Observe the output running on the board.
6. Repeat the procedure for other programs to implement your ideas and
programming coding skill.
Program:
Procedure:
1. Connect the Arduino Uno board to the PC through USB cable.
2. Start Arduino IDE from PC and open new Sketch and the
program/code for that and save as trafficlight.
3. Verify/Compile the program and Upload the code into Arduino
Uno.
4. Make necessary connections according diagram.
5. Observe the output running on the board.
6. Repeat the procedure for other programs to implement your ideas
and programming coding skill.
Program:
void setup() {
// Declaring all the LED's as output
for (int i = 0; i < 3; i++) {
pinMode(signal1[i], OUTPUT);
pinMode(signal2[i], OUTPUT);
pinMode(signal3[i], OUTPUT);
pinMode(signal4[i], OUTPUT);
}
}
void loop() {
// Making Green LED at signal 1 and red LED's at other signal HIGH
digitalWrite(signal1[2], HIGH);
digitalWrite(signal1[0], LOW);
digitalWrite(signal2[0], HIGH);
digitalWrite(signal3[0], HIGH);
digitalWrite(signal4[0], HIGH);
delay(redDelay);
// Making Green LED at signal 1 LOW and making yellow LED at signal 1 HIGH for 2 seconds
digitalWrite(signal1[1], HIGH);
digitalWrite(signal1[2], LOW);
delay(yellowDelay);
digitalWrite(signal1[1], LOW);
// Making Green LED at signal 2 and red LED's at other signal HIGH
digitalWrite(signal1[0], HIGH);
digitalWrite(signal2[2], HIGH);
digitalWrite(signal2[0], LOW);
digitalWrite(signal3[0], HIGH);
digitalWrite(signal4[0], HIGH);
delay(redDelay);
// Making Green LED at signal 2 LOW and making yellow LED at signal 2 HIGH for 2 seconds
digitalWrite(signal2[1], HIGH);
digitalWrite(signal2[2], LOW);
delay(yellowDelay);
digitalWrite(signal2[1], LOW);
// Making Green LED at signal 3 and red LED's at other signal HIGH
digitalWrite(signal1[0], HIGH);
digitalWrite(signal2[0], HIGH);
digitalWrite(signal3[2], HIGH);
digitalWrite(signal3[0], LOW);
digitalWrite(signal4[0], HIGH);
delay(redDelay);
// Making Green LED at signal 3 LOW and making yellow LED at signal 3 HIGH for 2 seconds
digitalWrite(signal3[1], HIGH);
digitalWrite(signal3[2], LOW);
delay(yellowDelay);
digitalWrite(signal3[1], LOW);
// Making Green LED at signal 4 and red LED's at other signal HIGH
digitalWrite(signal1[0], HIGH);
digitalWrite(signal2[0], HIGH);
digitalWrite(signal3[0], HIGH);
digitalWrite(signal4[2], HIGH);
digitalWrite(signal4[0], LOW);
delay(redDelay);
// Making Green LED at signal 4 LOW and making yellow LED at signal 4 HIGH for 2 seconds
digitalWrite(signal4[1], HIGH);
digitalWrite(signal4[2], LOW);
delay(yellowDelay);
digitalWrite(signal4[1], LOW);
}
Assignment No. 5: Demonstration of interfacing of 7 segment display
Procedure:
1. Connect the Arduino Uno board to the PC through USB cable.
2. Start Arduino IDE from PC and open new Sketch and the
program/code for that and save as Seven_segment.
3. Verify/Compile the program and Upload the code into Arduino
Uno.
4. Make necessary connections according diagram.
5. Observe the output running on the board.
6. Repeat the procedure for other programs to implement your ideas
and programming coding skill.
Program:
#include "SevSeg.h"
SevSeg sevseg;
void setup()
{
//Set to 1 for single digit display
byte numDigits = 1;
sevseg.setBrightness(90);
}
void loop()
{
//Display numbers one by one with 2 seconds delay
for(int i = 0; i < 10; i++)
{
sevseg.setNumber(i);
sevseg.refreshDisplay();
delay(2000);
}
}
Assignment No. 6: Demonstration of up and down counter. (Counting 0 to
9 using any display).
Aim: To demonstrate up and down counter application using Arduino Uno
Objectives:
1. To understand the basics of LED working.
2. To develop & write simple LED blink program using Arduino Uno
platform and demonstrate it.
3. To implement your ideas and programming coding skill using LED
Arrays
Software and hardware required:
1. Arduino IDE
2. FCT make Arduino Circuit Board
Diagram:
Procedure:
1. Connect the Arduino Uno board to the PC through USB cable.
2. Start Arduino IDE from PC and open new Sketch and the
program/code for that and save as counter.
3. Verify/Compile the program and Upload the code into Arduino
Uno.
4. Make necessary connections according diagram.
5. Observe the output running on the board.
6. Repeat the procedure for other programs to implement your ideas
and programming coding skill.
Program:
#include "SevSeg.h"
SevSeg sevseg;
void setup()
{
//Set to 1 for single digit display
byte numDigits = 1;
sevseg.setBrightness(90);
}
void loop()
{
//Display numbers one by one with 2 seconds delay
for(int i = 0; i < 10; i++)
{
sevseg.setNumber(i);
sevseg.refreshDisplay();
delay(2000);
}
}
Assignment No. 7: Demonstration of interfacing of speaker (audio) with
Arduino Uno.
Aim: To demonstrate speaker (audio) application using Arduino Uno
Objectives:
1. To understand the basics of Speaker/Buzzer working.
2. To develop & write speaker program using Arduino Uno platform
and demonstrate it.
3. To implement your ideas and programming coding skill using
Speaker
Software and hardware required:
1. Arduino IDE
2. FCT make Arduino Circuit Board
Diagram:
Procedure:
1. Connect the Arduino Uno board to the PC through USB cable.
2. Start Arduino IDE from PC and open new Sketch and the
program/code for that and save as speaker.
3. Verify/Compile the program and Upload the code into Arduino
Uno.
4. Make necessary connections according diagram.
5. Observe the output running on the board.
6. Repeat the procedure for other programs to implement your ideas
and programming coding skill.
Program:
/*
Speaker (Jan Gan Mam )
*/
int speakerPin = 7; //buzzer is connected to Pin 7 of the Board.
void playNote(char note, int duration) { //Assigning high time for the
notes
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 956, 851, 758, 716, 638, 568, 1014, 478 };
void setup() {
pinMode(speakerPin, OUTPUT); // pin 7 as an OutPut Pin
}
Procedure:
1. Connect the Arduino Uno board to the PC through USB cable.
2. Start Arduino IDE from PC and open new Sketch and the
program/code for that and save as IR_sensor.
3. Verify/Compile the program and Upload the code into Arduino
Uno.
4. Make necessary connections according diagram.
5. Observe the output running on the board.
6. Repeat the procedure for other programs to implement your ideas
and programming coding skill.
Program:
//IR sensor interface
int val = 0 ;
#define IR 6
#define Lamp 5
#define RED 4
#define GREEN 3
#define BUZ 2
void setup()
{
Serial.begin(9600); // sensor baud rate
pinMode(IR,INPUT); // IR sensor output pin connected
pinMode(RED,OUTPUT); // LED
pinMode(GREEN,OUTPUT); // LED
pinMode(BUZ,OUTPUT); // BUZZER
pinMode(Lamp,OUTPUT); // Lamp
}
void loop()
{
val = digitalRead(IR); // IR sensor output pin connected
Serial.println(val); // see the value in serial monitor in Arduino IDE
delay(500);
if(val == 1 )
{
digitalWrite(Lamp,LOW); // Lamp OFF
digitalWrite(RED,HIGH); // LED ON
digitalWrite(GREEN,LOW); // LED OFF
digitalWrite(BUZ,LOW); // BUZZER OFF
}
else
{
digitalWrite(Lamp,HIGH); // Lamp ON
digitalWrite(RED,LOW); // LED OFF
digitalWrite(GREEN,HIGH); // Green LED ON
digitalWrite(BUZ,HIGH); // BUZZER ON
}
}
Assignment No. 9: Demonstration of relay control.
Program:
1. Relay Progrm
// Arduino Relay Control Code
#define relay 11
#define interval 1000
void setup() {
// put your setup code here, to run once:
pinMode(relay, OUTPUT);
}
void loop()
{
// put your main code here, to run repeatedly:
digitalWrite(relay, HIGH);
delay(interval);
digitalWrite(relay, LOW);
delay(interval);
}
*/
#define Lamp 5
#define RED 4
#define GREEN 3
#define BUZ 2
char switchstate;
/*declaring that there is a variable called switchstate, which will
hold a character value. This is due to programming of the app, which
will send a text value to arduino. If we use 'int' instead of
'char' the code will not work properly.*/
void setup() {//Here the code only runs once.
Serial.begin(9600);
/*To start serial communication at a rate of 9600 bits per second. This
is the default rate anyways.*/
pinMode(Lamp, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(RED, OUTPUT);
pinMode(BUZ, OUTPUT);
//Declaring that the LED is an output.
}
void loop() {//This code repeats. This is our main code.
if (Serial.available() > 0) {
delay(50);
//code to be executed only when Serial.available()>0
/*Serial.available>0 is to check if there is any reading from the
HC-05 Bluetooth module.*/
switchstate = Serial.read();
while(Serial.available() > 0) {
Serial.read();
}
/*The character we had declared earlier is now being assigned a value-
the value of whatever Serial.read() is.*/
digitalWrite(Lamp, LOW);
digitalWrite(GREEN, LOW);
digitalWrite(RED, LOW);
digitalWrite(BUZ, LOW);
if (switchstate == 'g') {
digitalWrite(GREEN, HIGH);
} else if (switchstate == 'r') {
digitalWrite(RED, HIGH);
} else if (switchstate == 'l') {
digitalWrite(Lamp, HIGH);
} else if (switchstate == 'b') {
digitalWrite(BUZ, HIGH);
delay(1000);
digitalWrite(BUZ, LOW);
}
}
}
Assignment No. 10: Demonstration of servo motor.
void loop() {
// reads the value of the potentiometer (value between 0 and 1023)
int analogValue = analogRead(A0);
}
void loop() {
// put your main code here, to run repeatedly:
int motorSpeed = analogRead(A0)/10;
if( motorSpeed > 100)
motorSpeed = 100;
if(digitalRead(motorDirection)==HIGH)
{
phaseA();
delay(motorSpeed);
phaseB();
delay(motorSpeed);
phaseC();
delay(motorSpeed);
phaseD();
delay(motorSpeed);
}
else
{
phaseD();
delay(motorSpeed);
phaseC();
delay(motorSpeed);
phaseB();
delay(motorSpeed);
phaseA();
delay(motorSpeed);
}
}
void phaseA()
{
// phase_a 0x0a
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
}
void phaseB()
{
// phase_b 0x06
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
}
void phaseC()
{
// phase_c 0x05
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
}
void phaseD()
{
// phase_d 0x09
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
}
Assignment No. 12: Demonstration of “Hello” message on LCD Display.
lcd.setCursor(0,0);
lcd.print("*Hello*");
lcd.setCursor(0,1);
lcd.print("College of BBA(CA)");
delay(1000); //Delay of 1 second for ease of viewing in serial monitor
}
Assignment No. 13: Demonstration of reading analog voltage
*/
#include<LiquidCrystal.h>
//pins of the LCD. (RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(6, 7, 5, 4, 3, 2);
void setup()
{
pinMode(Red_LED, OUTPUT);
pinMode(Green_LED, OUTPUT);
pinMode(BUZ, OUTPUT);
pinMode(sensor,INPUT); // Configuring pin A1 as input
Serial.begin(9600);
lcd.begin(16,2);
delay(500);
}
void loop()
{
char sign = 223;
vout=analogRead(sensor);
tempc = (vout*500)/1023;
//vout=vout* 0.48828125;
lcd.setCursor(0,0);
lcd.print("***Tempeature***");
lcd.setCursor(0,1);
lcd.print(tempc);
lcd.print(sign);
lcd.print("C ");
lcd.print(" ");
lcd.print((tempc*1.8)+32);
lcd.print(sign);
lcd.print("F ");
delay(1000); //Delay of 1 second for ease of viewing in serial monitor
#include<LiquidCrystal.h>
//pins of the LCD. (RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(6, 7, 5, 4, 3, 2);
void setup()
{
pinMode(Red_LED, OUTPUT);
pinMode(Green_LED, OUTPUT);
pinMode(BUZ, OUTPUT);
lcd.setCursor(0,0);
lcd.print("*Light Intensity*");
lcd.setCursor(0,1);
lcd.print(vout);
delay(1000); //Delay of 1 second for ease of viewing in serial monitor
if (vout <100)
{
digitalWrite(Red_LED, HIGH);
digitalWrite(Green_LED, LOW);
digitalWrite(BUZ, HIGH);
}
else
{
digitalWrite(Green_LED, HIGH);
digitalWrite(Red_LED, LOW);
digitalWrite(BUZ, LOW);
}
}