You are on page 1of 14

EXPERIMENT #2 ARDUINO DIGITAL APPLICATIONS AND CONTROL

STRUCTURES

LEARNING OUTCOMES:
After completing this experiment the students will be able to:
1. Apply the millis() function on a Arduino program.
2. Implement a debounce to validate a button press
3. Use edge detection in reading analog data.
4. Demonstrate INPUT_PULLUP application in Arduino.
5. Implement the different control structures available in the Arduino programming
language.

EXPERIMENT 2A. BLINK WITHOUT DELAY


COMPONENTS:
1. Arduino board
2. 1 x LED
3. 1 x 220- resistor

CIRCUIT & SCHEMATIC DIAGRAM:

Figure 1. Circuit diagram of Experiment 2A Figure 2: Schematic diagram of experiment 2A.

PROCEDURE
1. Attach a 220-ohm resistor to pin 13 of your Arduino board.
2. Connect the positive side of the LED to the resistor, and the negative side of the LED to
the ground.
3. Plug your Arduino board to your computer and start the Arduino program.
4. Choose the Arduino board that you are you using in the Tools > Boards menu, then
choose the COM port you are using in the Tools > Serial Port menu.

*Image and other sources are taken from arduino.cc

NCP 516 EMBEDDED SYSTEMS LABORATORY 1|Page


5. Encode and verify the program below, if no errors occurred download the program into
your Arduino board.

CODE:
const int ledPin = 13; if(currentMillis -
previousMillis > interval) {
int ledState = LOW; previousMillis = currentMillis;
long previousMillis = 0;
long interval = 1000; if (ledState == LOW)
ledState = HIGH;
void setup() { else
pinMode(ledPin, OUTPUT); ledState = LOW;
}
digitalWrite(ledPin, ledState);
void loop() }
{ }
unsigned long currentMillis = millis();

EXPERIMENT 2B. EDGE DETECTION


COMPONENTS:
1. Arduino board
2. 1 x 10k resistor
3. 1 x Breadboard
4. 1 x Tactile switch

CIRCUIT & SCHEMATIC DIAGRAM:

Figure 3. Circuit diagram of Experiment 2B Figure 4. Circuit diagram of Experiment 2B

PROCEDURE:
1. Connect three wires to the Arduino board.

*Image and other sources are taken from arduino.cc

NCP 516 EMBEDDED SYSTEMS LABORATORY 2|Page


2. The first goes from one leg of the pushbutton through a pull-down resistor (here 10k) to
ground. The second goes from the corresponding leg of the pushbutton to the 5 volt
supply. The third connects to a digital I/O pin 2 of the Arduino board which reads the
button's state.
3. Plug your Arduino board to your computer and start the Arduino program.
4. Choose the Arduino board that you are you using in the Tools > Boards menu, then
choose the COM port you are using in the Tools > Serial Port menu.
5. Encode and verify the program below, if no errors occurred download the program into
your Arduino board.

CODE:
const int buttonPin = 2; Serial.print("number of button
const int ledPin = 13; pushes: ");
Serial.println(buttonPushCounter);
int buttonPushCounter = 0; }
int buttonState = 0; else {
int lastButtonState = 0; Serial.println("off");
void setup() { }
pinMode(buttonPin, INPUT); }
pinMode(ledPin, OUTPUT); lastButtonState = buttonState;
Serial.begin(9600);
} if (buttonPushCounter % 4 == 0) {
void loop() { digitalWrite(ledPin, HIGH);
buttonState = digitalRead(buttonPin); } else {
digitalWrite(ledPin, LOW);
if (buttonState != lastButtonState) { }
if (buttonState == HIGH) { }
buttonPushCounter++;
Serial.println("on");

EXPERIMENT 2C. DEBOUNCE


COMPONENTS:
1. Arduino board
2. 1 x Tactile switch
3. 1 x Breadboard

CIRCUIT & SCHEMATIC DIAGRAM:

*Image and other sources are taken from arduino.cc

NCP 516 EMBEDDED SYSTEMS LABORATORY 3|Page


Figure 5. Circuit diagram of Experiment 2C Figure 6. Circuit diagram of Experiment 2C

PROCEDURE:
1. Connect three wires to the Arduino board. The first goes from one leg of the pushbutton
through a pull-down resistor (here 10k) to ground. The second goes from the
corresponding leg of the pushbutton to the 5 volt supply. The third connects to a digital
I/O pin 2 of the Arduino board which reads the button's state.
2. Plug your Arduino board to your computer and start the Arduino program.
3. Choose the Arduino board that you are you using in the Tools > Boards menu, then
choose the COM port you are using in the Tools > Serial Port menu.
4. Encode and verify the program below, if no errors occurred download the program into
your Arduino board.

CODE:
const int buttonPin = 2;
const int ledPin = 13; void loop() {
intledState = HIGH; int reading = digitalRead(buttonPin);
intbuttonState;
intlastButtonState = LOW; if (reading != lastButtonState) {
lastDebounceTime = millis();
longlastDebounceTime = 0; }
longdebounceDelay = 50;
if ((millis() - lastDebounceTime)
void setup() { >debounceDelay) {
pinMode(buttonPin, INPUT); if (reading != buttonState) {
pinMode(ledPin, OUTPUT); buttonState = reading;
digitalWrite(ledPin, ledState); if (buttonState == HIGH) {
} ledState = !ledState;

*Image and other content are taken from arduino.cc

NCP 516 EMBEDDED SYSTEMS LABORATORY 4|Page


} digitalWrite(ledPin, ledState);
} lastButtonState = reading;
} }

EXPERIMENT 2D. INPUT PULLUP SERIAL


COMPONENTS:
1. Arduino board
2. 1 x Tactile switch
3. 1 x Breadboard

CIRCUIT & SCHEMATIC DIAGRAM:

Figure 7. Circuit diagram of Experiment 2D Figure 8. Circuit diagram of Experiment 2D

PROCEDURE:
1. Connect two wires to the Arduino board. The black wire connects ground to one leg of
the pushbutton. The second wire goes from digital pin 2 to the other leg of the
pushbutton.
2. Plug your Arduino board to your computer and start the Arduino program.
3. Choose the Arduino board that you are you using in the Tools > Boards menu, then
choose the COM port you are using in the Tools > Serial Port menu.
4. Encode and verify the program below, if no errors occurred download the program into
your Arduino board.

CODE:
void setup(){ pinMode(2, INPUT_PULLUP);
Serial.begin(9600); pinMode(13, OUTPUT);

*Image and other content are taken from arduino.cc

NCP 516 EMBEDDED SYSTEMS LABORATORY 5|Page


if (sensorVal == HIGH) {
} digitalWrite(13, LOW);
}
void loop(){ else {
int sensorVal = digitalRead(2); digitalWrite(13, HIGH);
Serial.println(sensorVal); }
}

*Image and other sources are taken from arduino.cc

NCP 516 EMBEDDED SYSTEMS LABORATORY 6|Page


EXPERIMENT 2E. CONDITIONAL STATEMENTS (IF STATEMENT)
COMPONENTS:
1. Arduino board
2. 1 x Potentiometer

CIRCUIT & SCHEMATIC DIAGRAM:

Figure 9. Circuit diagram of Experiment 2E Figure 10. Circuit diagram of Experiment 2E

PROCEDURE:
1. Connect three wires to the Arduino board. The black wire connects ground to leftmost
leg of the potentiometer. The second wire goes from the 5V to the rightmost leg of the
potentiometer and the last wire connects the center leg of the potentiometer to the
analog pin 0 of the Arduino board.
2. Plug your Arduino board to your computer and start the Arduino program.
3. Choose the Arduino board that you are you using in the Tools > Boards menu, then
choose the COM port you are using in the Tools > Serial Port menu.
4. Encode and verify the program below, if no errors occurred download the program into
your Arduino board.

CODE:
const int analogPin = A0; int analogValue = analogRead(analogPin);
const int ledPin = 13; if (analogValue > threshold) {
const int threshold = 400 digitalWrite(ledPin, HIGH);
} else {
void setup() { digitalWrite(ledPin, LOW);
pinMode(ledPin, OUTPUT); }
Serial.begin(9600); Serial.println(analogValue);
} delay(1);
void loop() { }

*Image and other sources are taken from arduino.cc

NCP 516 EMBEDDED SYSTEMS LABORATORY 7|Page


*Image and other sources are taken from arduino.cc

NCP 516 EMBEDDED SYSTEMS LABORATORY 8|Page


EXPERIMENT 2F. CONDITIONAL STATEMENTS (SWITCH STATEMENT)
COMPONENTS:
1. Arduino board
2. 5 x LEDs
3. 5 x 220 resistors
4. 1 x breadboard

CIRCUIT & SCHEMATIC DIAGRAM:

Figure 11. Circuit diagram of Experiment 2F Figure 12. Circuit diagram of Experiment 2F

PROCEDURE:
1. Connect the positive side of each of the LED to the 2, 3, 4, 5 and 6 digital I/O pins of the
Arduino in series through 220 resistors.
2. Plug your Arduino board to your computer and start the Arduino program.
3. Choose the Arduino board that you are you using in the Tools > Boards menu, then
choose the COM port you are using in the Tools > Serial Port menu.
4. Encode and verify the program below, if no errors occurred download the program into
your Arduino board.

CODE:
void setup() { if (Serial.available() > 0) {
Serial.begin(9600); int inByte = Serial.read();
for (int thisPin = 2; thisPin < 7; switch (inByte) {
thisPin++) { case 'a':
pinMode(thisPin, OUTPUT); digitalWrite(2, HIGH);
} break;
} case 'b':
digitalWrite(3, HIGH);
void loop() { break;

*Image and other sources are taken from arduino.cc

NCP 516 EMBEDDED SYSTEMS LABORATORY 9|Page


case 'c': default:
digitalWrite(4, HIGH); for (int thisPin = 2; thisPin < 7;
break; thisPin++) {
case 'd': digitalWrite(thisPin, LOW);
digitalWrite(5, HIGH); }
break; }
case 'e': }
digitalWrite(6, HIGH); }
break;

EXPERIMENT 2G. CONDITIONAL STATEMENTS (SWITCH STATEMENT #2)


COMPONENTS:
1. Arduino board
2. 1 x photoresistor, or another analog sensor
3. 10k resistors
4. 1 x breadboard

CIRCUIT & SCHEMATIC DIAGRAM:

Figure 13. Circuit diagram of Experiment 2G Figure 14. Circuit diagram of Experiment 2G

PROCEDURE:
1. Connect the photoresistor to the analog pin 0 using a voltage divider circuit, A 10k
resistor makes up the other side of the voltage divider, running from analog 0 to ground.
The other side of the photoresistor is connected to the 5V pin of the Arduino board.
2. Plug your Arduino board to your computer and start the Arduino program.
3. Choose the Arduino board that you are you using in the Tools > Boards menu, then
choose the COM port you are using in the Tools > Serial Port menu.
4. Encode and verify the program below, if no errors occurred download the program into
your Arduino board.

*Image and other sources are taken from arduino.cc

NCP 516 EMBEDDED SYSTEMS LABORATORY 10 | P a g e


CODE:
const int sensorMin = 0; Serial.println("dark");
const int sensorMax = 600; break;
case 1:
void setup() { Serial.println("dim");
Serial.begin(9600); break;
} case 2:
Serial.println("medium");
void loop() { break;
int sensorReading = analogRead(A0); case 3:
int range = map(sensorReading, sensorMin, Serial.println("bright");
sensorMax, 0, 3); break;
}
switch (range) { delay(1);
case 0: }

EXPERIMENT 2H. ITERATION STATEMENTS (FOR LOOP STATEMENT)


COMPONENTS:
1. Arduino board
2. 6 x 220 resistors
3. 6 x LEDs
4. breadboard

CIRCUIT & SCHEMATIC DIAGRAM:

Figure 15. Circuit diagram of Experiment 2H Figure 16. Circuit diagram of Experiment 2H

PROCEDURE:
1. Connect the positive side of each of the LED to the 2, 3, 4, 5, 6 and 7 digital I/O pins of
the Arduino in series through 220 resistors.
2. Plug your Arduino board to your computer and start the Arduino program.

*Image and other sources are taken from arduino.cc

NCP 516 EMBEDDED SYSTEMS LABORATORY 11 | P a g e


3. Choose the Arduino board that you are you using in the Tools > Boards menu, then
choose the COM port you are using in the Tools > Serial Port menu.
4. Encode and verify the program below, if no errors occurred download the program into
your Arduino board.

CODE:
int timer = 100; digitalWrite(thisPin, HIGH);
delay(timer);
void setup() { digitalWrite(thisPin, LOW);
for (int thisPin = 2; thisPin < 8; }
thisPin++) {
pinMode(thisPin, OUTPUT); for (int thisPin = 7; thisPin >= 2;
} thisPin--) {
} digitalWrite(thisPin, HIGH);
delay(timer);
void loop() { digitalWrite(thisPin, LOW);
for (int thisPin = 2; thisPin < 8; }
thisPin++) { }

EXPERIMENT 2I. ITERATION STATEMENTS (WHILE LOOP STATEMENT)


COMPONENTS:
1. Arduino board
2. pushbutton or switch
3. photoresistor or another analog sensor
4. 2 x 10k resistors
5. breadboard

CIRCUIT & SCHEMATIC DIAGRAM:

Figure 17. Circuit diagram of Experiment 2I Figure 18. Circuit diagram of Experiment 2I

*Image and other sources are taken from arduino.cc

NCP 516 EMBEDDED SYSTEMS LABORATORY 12 | P a g e


PROCEDURE:
1. Connect your analog sensor on analog input 2 with a 10K resistor to ground. Connect
your button to digital pin, again with a 10K resistor to ground. Connect your LED to
digital pin 9, with a 220 ohm resistor in series.
2. Plug your Arduino board to your computer and start the Arduino program.
3. Choose the Arduino board that you are you using in the Tools > Boards menu, then
choose the COM port you are using in the Tools > Serial Port menu.
4. Encode and verify the program below, if no errors occurred download the program into
your Arduino board.

CODE:
const int sensorPin = A2; sensorValue = map(sensorValue, sensorMin,
const int ledPin = 9; sensorMax, 0, 255);
const int indicatorLedPin = 13; sensorValue = constrain(sensorValue, 0,
const int buttonPin = 2; 255);

int sensorMin = 1023; analogWrite(ledPin, sensorValue);


int sensorMax = 0; }
int sensorValue = 0;
void calibrate() {
void setup() { digitalWrite(indicatorLedPin, HIGH);
pinMode(indicatorLedPin, OUTPUT); sensorValue = analogRead(sensorPin);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT); if (sensorValue > sensorMax) {
} sensorMax = sensorValue;
}
void loop() {
while (digitalRead(buttonPin) == HIGH) { if (sensorValue < sensorMin) {
calibrate(); sensorMin = sensorValue;
} }
digitalWrite(indicatorLedPin, LOW); }
sensorValue = analogRead(sensorPin);

EXPERIMENT 2J. ARRAYS


COMPONENTS:
1. Arduino board
2. 6 x 220 resistors
3. 6 x LEDs
4. breadboard

*Image and other sources are taken from arduino.cc

NCP 516 EMBEDDED SYSTEMS LABORATORY 13 | P a g e


CIRCUIT & SCHEMATIC DIAGRAM:

Figure 19. Circuit diagram of Experiment 2J Figure 20. Circuit diagram of Experiment 2J

PROCEDURE:
1. Connect the positive side of each of the LED to the 2, 3, 4, 5, 6 and 7 digital I/O pins of
the Arduino in series through 220 resistors.
2. Plug your Arduino board to your computer and start the Arduino program.
3. Choose the Arduino board that you are you using in the Tools > Boards menu, then
choose the COM port you are using in the Tools > Serial Port menu.
4. Encode and verify the program below, if no errors occurred download the program into
your Arduino board.

CODE:
int timer = 100; for (int thisPin = 0; thisPin < pinCount;
int ledPins[] = {2, 7, 4, 6, 5, 3}; thisPin++) {
int pinCount = 6; digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
void setup() { digitalWrite(ledPins[thisPin], LOW);
for (int thisPin = 0; thisPin < pinCount; }
thisPin++) { for (int thisPin = pinCount - 1;
pinMode(ledPins[thisPin], OUTPUT); thisPin >= 0; thisPin--) {
} digitalWrite(ledPins[thisPin], HIGH);
} delay(timer);
digitalWrite(ledPins[thisPin], LOW);
void loop() { }
}

*Image and other sources are taken from arduino.cc

NCP 516 EMBEDDED SYSTEMS LABORATORY 14 | P a g e