Arduino Labs
HANDS ON EXPERIMENTS
Lab # 1
TASK-1A
LED Blinking
The ‘Hello World!’ of Arduino, it’s the inbuilt Blinking LED!
HARDWARE REQUIREMENTS
• Arduino
• Solderless breadboard
ARDUINO CODE
void setup() {
// put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT);
void loop() {
}
ARDUINO CODE
void setup() {
// put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT);
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH
is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by
making the voltage LOW
delay(1000);
}
External LED
TASK-1B
LED Blinking
Using a breadboard and arduino, Light up an LED using pin # 7.
HARDWARE REQUIREMENTS
• Arduino
• LED
• Wires
• Solderless breadboard
CONNECTION DIAGRAM
ARDUINO CODE
int led = 7;
//This assigns the name ‘led’ the integer 7. Now every time we write
‘led’ in our code, Arduino will interpret that as 7.
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}
Lab # 2
TASK
Create a push button circuit. When the button is pressed, the
LED should light up.
Use arduino only for power and ground.
HARDWARE REQUIREMENTS
• Solderless breadboard
• Wires
• LED
• Resistor
• Push buttons x 1
CONNECTION DIAGRAM
VARIABLES/FUNCTIONS
int led=2;
void setup()
{
pinMode(led,OUTPUT);
}
void loop()
{
digitalWrite(led, HIGH);
}
Lab # 3
Traffic Lights
TASK
• Build traffic lights program using Red Green and Yellow
Leds
• Use three Leds and toggle them with the time delay of
1second.
• Use digital Arduino pins for the program
Circuit Diagram
Code
int led_g=8; digitalWrite(led_y, HIGH);
int led_y=4; // delay(1000); // Wait for 1000
int led_r=2; millisecond(s)
void setup() digitalWrite(led_g, LOW);
{ pinMode(led_g, OUTPUT); digitalWrite(led_r, LOW);
pinMode(led_y, OUTPUT); delay(1000); // Wait for 1000
millisecond(s)
pinMode(led_r, OUTPUT);
}
digitalWrite(led_r, HIGH);
void loop()
// delay(1000); // Wait for 1000
{ millisecond(s)
digitalWrite(led_g, HIGH); digitalWrite(led_y, LOW);
//delay(1000); // Wait for 1000 millisecond(s) digitalWrite(led_g, LOW);
digitalWrite(led_y, LOW); delay(1000); // Wait for 1000
digitalWrite(led_r, LOW); millisecond(s)
delay(1000); // Wait for 1000 millisecond(s) }
Lab # 4
TASK
• Build & Program Your Arduino Ultrasonic Sensor
• A sensor is fixed at the back of your car which acts as a
rear view camera. Using the ultrasonic sensor, create a
circuit that acts as a rear view camera i.e. it measures the
distance and lights up a green LED if the distance is in a
safe zone w.r.t threshold defined. Similarly when the car
approaches the threshold an orange LED lights up and
finally when the distance is the least, Red LED is on.
HARDWARE REQUIREMENTS
• Arduino Uno REV 3
• Ultrasonic Sensor
• Jumper Wires
• LEDs – Red Orange Yellow
• Buzzer
COMPONENT DETAILS
Pins
VCC: +5VDC
Trig : Trigger (OUTPUT)
Echo: Echo (INPUT)
GND: GND
CONNECTION DIAGRAM
ARDUINO CODE
// defines pins numbers
const int trigPin = 4;
const int echoPin = 2;
const int led_r =7;
const int led_y =8;
const int led_g =12;
const int buzzer =9;
// defines variables
long duration;
int distance;
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_y, OUTPUT);
pinMode(led_g, OUTPUT);
pinMode(buzzer,OUTPUT);
[Link](9600); // Starts the serial communication
}
ARDUINO CODE
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);
// 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
[Link]("Distance: ");
[Link](distance);
ARDUINO CODE
if (distance <= 50 && distance > 35)
{ // This is where the LED On/Off happens
digitalWrite(led_g,HIGH); // When the green condition is
met, the R and Y LED should turn off
digitalWrite(led_y,LOW);
digitalWrite(led_r,LOW);
}
if (distance <= 35 && distance > 20)
{ // This is where the LED On/Off happens
digitalWrite(led_y,HIGH); // When the yellow condition
is met, the Green and red LED should turn off
digitalWrite(led_g,LOW);
digitalWrite(led_r,LOW);
}
if (distance <= 20 && distance > 0)
{ // This is where the LED On/Off happens
digitalWrite(led_r,HIGH); // When the Red
condition is met, the Green LED should turn off
digitalWrite(led_g,LOW);
digitalWrite(led_y,LOW);
tone(buzzer, 1000);
delay(1000);
noTone(buzzer);
delay(1000);
}
if (distance >= 55 || distance <= 0)
{
[Link]("Out of range");
digitalWrite(led_r,HIGH); // When the Red
condition is met, the Green LED should turn off
digitalWrite(led_g,LOW);
digitalWrite(led_y,LOW);
[Link](distance);
[Link](" cm");
delay(500);
}