Professional Documents
Culture Documents
Arduino Basics
Arduino Basics
DESIGN LABORATORY 1
EMBEDDED C PROGRAMMING & ARDUINO BASICS
OBJECTIVES
At the end of the laboratory activities the student should be able to:
Create, modify and debug programs written in Embedded C.
Interface system components to Arduino
Design and create a system using Arduino
ACTIVITY 1
CODES AND SETUPS
1. Write a program to display the sum of the numbers from 1 to 500 to the
serial monitor.
void setup() {
Serial.begin(9600);
long sum;
for(int i = 1; i <= 500; i++) {
sum += i;
}
Serial.println(sum);
}
void loop() {
}
2. Write a program to display the first 20 terms of the Fibonacci Series starting
from 0, 1, 1, 2 and so on.
int numbers[20];
void setup() {
Serial.begin(9600);
for(int i = 0; i < 20; i++){
if (i < 2) {
numbers[i] = i;
} else {
numbers[i] = numbers[i-2] + numbers[i-1];
}
}
for(int i = 0; i < 20; i++){
Serial.println(numbers[i]);
}
}
void loop() {
}
ACTIVITY 2
BLINKING LIGHTS
1. Explain every instruction on the blink program and explain how does the
program work.
The first 2 lines of code instantiate pin numbers into a
variables of personal choice for easier identification.
The pinMode() method in the setup sets the identified pins as
to what role they are going to take. In this case, the pins are
set to display output.
Inside the loop function, the first two digitalWrite() methods
light up led1 and turns off led 2 and this is in a 1000 ms
delay time followed by the lighting up of led 2 and led 1 is
turned off for another 1000 ms delay time. Algorithms in the
loop function recur infiinitely.
3. Modify the code, this time use at least 4 LEDs. You are free to program your
own light sequence.
int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;
const int leds[4] = {led1, led2, led3, led4};
void setup(){
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
}
void loop() {
for(int i = 0; i < 4; i++){
digitalWrite(leds[i], HIGH);
delay(500);
}
}
DESIGN PROBLEM
Design and implement a system that uses any of the following components
below. Explain, what problems are you trying to solve by your design?
MATERIALS
Microcontroller
7 Segment Display
(Common Cathode)
Breadboard
Push Button
Connecting Wires
SCHEMATIC DIAGRAM
BREADBOARD DIAGRAM
int e = 2;
int d = 3;
int g = 4;
int c = 5;
int f = 6;
int a = 7;
int b = 8;
const int segs[7] = { a, b, c, d, e, f, g };
const byte numbers[10] = { 0b1000000, 0b1111001, 0b0100100, 0b10110000, 0b10011001, 0b10010010, 0b0000010, 0b1111000, 0b0000000,
0b10010000 };
void setup() {
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
}
void loop() {
for (int i = 9; i >= 0; i--) {
delay(1000); l
ightSegments(numbers[i]);
}
}
void lightSegments(byte number) {
for (int i = 0; i < 7; i++) {
int bit = bitRead(number, i);
digitalWrite(segs[i], bit);
}
}
ACTUAL OUTPUT
LEARNING EXPERIENCES
During the laboratory, it was also discovered that different LED require
varying values of resistors. Furthermore, this laboratory activity provided us
with the basics of using Arduino including its basic syntax. The use of Arduino
has also opened our minds into its real life applications which can benefit man.