LAB 2
digitalRead
Read the value from a specified digital pin and the result is either HIGH
or LOW.
Syntax:
digitalRead(pin);
Parameter:
pin – the number of the digital pin wanted to read on Arduino Uno
Sample usage:
Sets pin 13 to the same value as pin 7, declared as an input.
int val = 0;
void setup(){
pinMode(7, INPUT); //sets the digital pin 7 as Input
pinMode(13, OUTPUT); //sets the digital pi n 13 as output
}
void loop(){
val = digital Read(7); // read the input value i n digital pin 7
digitalWrite(13, val ); // sets the digital pi n 13 val ue
}
analogWrite()
Writes an analog value (PWM wave) to a pin. Can be used to light a
LED at varying brightnesses or drive a motor at various speeds. After a
call to analogWrite(), the pin will generate a steady rectangular wave
of the specified duty cycle until the next call to analogWrite() (or a call
to digitalRead() or digitalWrite()) on the same pin.
Syntax:
analogWrite(pin, value)
Parameter:
pin – the number of the analog pin wanted to read on Arduino Uno
ACTIVITY 1.1 BLINKING AN LED WHEN SWITCH IS ON
COMPONENTS REQUIRED
• 1 LED
• 1 SPST/SPDT SWITCH/ DIP SWITCH
This will turn on and off an
LED connected at any digital
pin for duration of 1 second
when the switch is ON-based
on what pin it is connected
ACTIVITY 1.2: LED SEQUENCE BASED ON SWITCH
SETTINGS
COMPONENTS REQUIRED
• Arduino UNO Switch No. is ON Output
• 4 SPST/SPDT SWITCH OR DIP 10101010
1
SWITCH 01010101
00001111
• 8 LED’S 2
11110000
11001100
3
Make a circuit connection of 00110011
Arduino and LEDs that will 10101010
01010101
perform the following sequence
00001111
of light continuously based on 4
11110000
the following setting of 11001100
switches: 00110011
ACTIVITY 1.3: PIANO
COMPONENTS REQUIRED
• Arduino UNO
• Push Buttons X 8
• Small Piezo Buzzer (or a small
speaker)
• Connecting Wires
• Breadboard
• Power Supply