0% found this document useful (0 votes)
3 views

Introduction to Arduino

The lab manual for ROBT 206 introduces students to Arduino microcontrollers, covering essential components like breadboards, LEDs, and resistors, as well as safety precautions for lab work. Students will learn to program the Arduino using the Arduino IDE and complete exercises such as creating an SOS signal in Morse code and designing traffic control systems. The manual emphasizes hands-on experimentation with digital inputs and outputs, encouraging collaboration in small groups.

Uploaded by

Yelnur
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Introduction to Arduino

The lab manual for ROBT 206 introduces students to Arduino microcontrollers, covering essential components like breadboards, LEDs, and resistors, as well as safety precautions for lab work. Students will learn to program the Arduino using the Arduino IDE and complete exercises such as creating an SOS signal in Morse code and designing traffic control systems. The manual emphasizes hands-on experimentation with digital inputs and outputs, encouraging collaboration in small groups.

Uploaded by

Yelnur
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

ROBT 206 – Microcontrollers with Lab– Lab Manual

……………………………………………………………………………………………………...

Lab 1: Introduction to Arduino

Objectives:

• To learn about Arduino board, breadboard, LEDs, resistors and wires


• To test basic digital output, a “hello world” program
• To exercise your knowledge with other designs and codes
• To test basic digital input/outputs
• To use the input and output pins experimentally
• To learn about pushbuttons

Equipment:
• Arduino board
• Breadboard
• LEDs
• Resistors
• Pushbutton
• Jumper wires

Size of the working group:


● 3 students per group (12 groups in total, limited to the number of workstations in the lab)
Required Safety Precautions:
● Wear appropriate clothes and non-conductive shoes. No excessive jewelry to prevent
electric shock.
● No drinks or food, wet clothes, rags or backpacks on or near computers, equipment or
circuitry. Keep them in a special designated drawers under the tables.
● Always read safety procedures, datasheets or manuals that come with most electronic
equipment and components you are about to test or work on.
● Do not connect power to a circuit until you are done wiring it and recheck the work. Ask
the TA or Lab Instructor for the verification.
● If any of the components is broken, replace it. Return broken component to TA or Lab
Instructor.
● Always remove power from a circuit before connecting alligator clips.
● Do not leave your circuit unattended with power on.
● Return all equipment and components used to their original place. Clean your workplace
from any trash you left.
ROBT 206 – Microcontrollers with Lab– Lab Manual
……………………………………………………………………………………………………...
Arduino Microcontroller Board

• An open source popular microcontroller board


• Started in 2005 in Ivrea, Italy
• Name “Arduino” means “strong friend”
• Free C language based programming environment
• A low-cost computer that you can program – simply connect to computer with a USB
cable or power it with a AC-to-DC
adapter
• Based on the AVR ATmega328
microcontroller
• 14 digital input/output pins
• 6 analog inputs
• a 16 MHz crystal oscillator,
• a USB connection,
• a power jack,
• an ICSP header,
• a reset button.
• 32 kBytes Flash program memory
• 2 kByte SRAM, 1 kByte EEPROM
• 16 MHz clock speed

Figure 1. Arduino UNO

The cool thing about the Arduino is you can program it to interact with the real world. The
Arduino can connect to and interact with a variety of sensors and actuators which allow you to
monitor what is happening around you, and to control things like motors, relays, and servos. To
do this though, you need to be able to connect components to the Arduino. Prototyping is most
easily done by using a breadboard.

Breadboard

A breadboard is also referred to as a solderless breadboard or a plugboard. It is used to build


temporary circuits for testing or to experiment new circuit ideas. It has many holes, which can
be used to plug in resistors, capacitors, inductors, integrated circuits (ICs), and etc. A typical
ROBT 206 – Microcontrollers with Lab– Lab Manual
……………………………………………………………………………………………………...
breadboard is shown in Figure 2. The backside of the breadboard has strips of metal connecting
the holes on the front side. The holes connected by a same metal strip form one common node
in a circuit. Different components at a given node are connected by pushing in a corresponding
end of each component into holes connected to the same node.

It is important to understand how the little holes on the breadboard are connected. This graphic
will help you understand which holes are connected and which are not. To connect two leads
together, they should be plugged into the same column on the breadboard. It does not matter
which column, as long as the two leads are in the same column.

Figure 2. A breadboard

It is also noticed that some common nodes are longer than most of the five-hole nodes. They are
typically used for power supply connections or for those nodes to which many components are
connected. A jumper wire can also be used to combine two nodes into one. It is recommended
to use black wires for ground and red (or other colors if there are multiple voltages (logic
signals) needed. Keep the jumper wires short and flat on the board, so that the circuit doesn't
look cluttered. Route jumper wires around the chips, so that it makes it easy to change the chips.
You could trim (cut small parts of) or bend the resistor/capacitor/inductor lead, so that they will
fit in snugly and won't get pulled out by accident. A wire should be used to connect the probe of
an oscilloscope onto the breadboard, since the probe connection might loosen the existing
connection of your components.

Arduino IDE

The software can be downloaded from: https://www.arduino.cc/en/software. You will be using


Arduino IDE to program the Arduino Board. Let’s take a look at the IDE in the figure below:
ROBT 206 – Microcontrollers with Lab– Lab Manual
……………………………………………………………………………………………………...

Figure 3. Arduino’s Program space.

(1) Code entry area


This is where you’ll type in the code for your sketch.
(2) The console
This is where you’ll see status messages when uploading your code to the board.
(3) Verify button
Click this button to verify that your code has no errors. If your code does have errors,
they will be displayed in the console.
(4) Upload button
Click this button to verify the code and upload it to the board if there are no
errors.
(5) New button
This creates a new sketch in the current window.
(6) Open button
This will allow you to open a saved sketch from your sketchbook folder, the location on
your computer where the Arduino IDE saves your sketches.
ROBT 206 – Microcontrollers with Lab– Lab Manual
……………………………………………………………………………………………………...
(7) Save button
This will allow you to save the sketch in the current window. You’ll be prompted to enter
a file name if you haven’t already saved the file.
(8) Serial monitor
This button opens the serial monitor, which lets you send and receive information between your
board and your computer.

The programming language is standard C (but simplified). The program space contains: Global
Space and Function Space:

• Global space contains variables and function definitions


• Function space contains variables and calls to functions

Global
setufunction
loopfunction
Global

Arduino’s standard library:

Main Functions

setup()

● Used for one-time function calls (initializing pins/serial ports)


● Takes and returns no arguments

loop()
● Continuous loop function (similar to while(1))
ROBT 206 – Microcontrollers with Lab– Lab Manual
……………………………………………………………………………………………………...
● Takes and returns no arguments

Lots of useful functions

• pinMode() – set a pin as input or output


• digitalWrite() – set a digital pin high/low
• digitalRead() – read a digital pin’s state
• analogRead() – read an analog pin
• analogWrite() – write an “analog” PWM value
• delay() – wait an amount of time
• millis() – get the current time
Control functions

if … else if … else
• Executes code depending on the evaluation of a conditional while
• Loop which iterates while evaluated conditional is true
For
• Loop which iterates for a set number of iterations switch … case …
break …

default
•Executes code depending on whether a value is equal to a specific comparison value

return
•Returns a specified value upon completion of a function continue
ROBT 206 – Microcontrollers with Lab– Lab Manual
……………………………………………………………………………………………………...

Select what Arduino you have under the Tools menu.

Figure 4. Select what Arduino to connect to.

And select the COM port of the Arduino UNO board:


ROBT 206 – Microcontrollers with Lab– Lab Manual
……………………………………………………………………………………………………...
Figure 5. Select COM6 port.

Arduino Board Sketches:

The Arduino code that is written to the Arduino Board in order to perform some action is called a
Sketch. After this we will be using Arduino code and sketch interchangeably. To see what a
Galileo Sketch looks like go to File and press New to create a new sketch.

The first thing to notice is that there are two separate blocks of code enclosed within curly
brackets. One starts with void setup() and the other starts with void loop(). Every Arduino
sketch you write will have both of these.

The block of code that starts with void setup() is the setup function. When your sketch starts, the
Arduino Board will execute each line of code within the setup function, starting with the first line
and then working its way down. It will then move onto the loop function, which is all the code in
the curly brackets after void loop(). The Arduino Board will repeatedly execute the code in the
loop function over and over again until power is shut down or the reboot or reset button is
pressed. Again, it will execute the code in the order it is written (see the Fig. 4):

Figure 6. Arduino Board sketch


ROBT 206 – Microcontrollers with Lab– Lab Manual
……………………………………………………………………………………………………...

The ‘Hello World!’ of Arduino, it’s the Blinking LED!


In most programming languages, the first program you write prints “hello world" to the screen.
Since an Arduino board doesn't have a screen, we blink an LED instead. The boards are designed
to make it easy to blink an LED using digital pin 13. Some (like the Diecimila and LilyPad) have
the LED built-in to the board. On most others (like the Mini and BT), there is a 1 KB resistor on
the pin, allowing you to connect an LED directly. Now try connecting an LED using digital pin
13. After that try to connect an LED to another digital pin, for that you should use an external
resistor.

Figure 7. A simple electronic circuit with one Resistor (R1) and 1 LED (LED1).

For this circuit you will need:


1) LED = Light-Emitting Diode. Electricity only flows one way in a diode, so it needs a
“current limiting” resistor, or it burns out. The LED has two legs: a longer and a shorter
leg. Connect a longer leg to the
2) Resistor
3) Breadboard
4) Arduino board
5) Wires
ROBT 206 – Microcontrollers with Lab– Lab Manual
……………………………………………………………………………………………………...

Figure 8. The schematic.


Step-by-Step Instructions:

1. Connect one of your Arduino Board GND pins to the one of the long power rails on
your breadboard - this will be your ground rail.
2. Connect the short leg of your LED to this same ground rail on the breadboard and
connect the long leg to a row on the breadboard.
3. Connect a wire from the 5-volt pin to one side of the 220 Ohm (red-red-brown-gold)
resistor.
4. Plug your Arduino Board into your computer with a USB cable.
5. Open up the Arduino IDE.
6. Copy and paste it from below.
7. Click the verify button on the top left. It should turn orange and then back to blue.
8. Click the upload button. It will also turn orange and then blue once the sketch has
finished uploading to your Arduino Board.
9. Open up the serial monitor window.
10. See how the LED reacts.

You need to use the following sketch:

int led = 13;

void setup() {
pinMode(led, OUTPUT);
}

void loop() {
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}

Pre-Lab Exercise to be done in a simulation individually: SOS signal


ROBT 206 – Microcontrollers with Lab– Lab Manual
……………………………………………………………………………………………………...
Write an Arduino sketch that sends SOS signals in Morse code via an LED and upload it to the
Arduino Board.

In Morse code, the SOS (Save Our Souls) signal is composed of three (3) short signals, three (3)
long signals and three (3) short signals. So if a dot “.” is a short signal and underscore “_” is a
long signal SOS will look like “. . . _ _ _ . . .”. Choose the lengths of the signals as you prefer,
but make sure that the long signals are at least three times longer than the short signals. Make
sure that the separate signals are distinguishable (You should be able to see 3 separate short
signals instead of a long one when you send an “S”).

Lab Exercises to be done in groups:

Exercise 1: SOS signal


Repeat the SOS signal exercise using the real components in the lab.

Exercise 2: a simple traffic control system for pedestrians


Assume that you have traffic lights (with Red, Green and Yellow lights) for pedestrians. The aim
is to control the traffic using these lights. We will have only the following situations:
- The red light is on and the others are off. In this case cars can pass and pedestrians cannot
cross the street.
- The yellow light is on, meaning that it is the transition time; both cars and pedestrians
should wait for the stable light situation.
- The green light is on, meaning that the cars cannot pass and it is safe for the pedestrians
to cross the street.

It is also possible to use only two lights (red and green) and instead of the third one (yellow
light), use the blinking of the others.

Exercise 3: a simple traffic control system for a junction

Assume that you have two pairs of traffic lights (with Red, Green and Yellow lights) located
in different directions of the junction. One pair is placed on the east-west and the other pair is
placed in the south-north direction.

The control system is working based only on time, meaning that there is no sensor for sensing
the presence of the pedestrians . The red, yellow and green lights will be turned on and off in a
sequential way, with a pre-specified time interval for each direction of the junction.

Obviously, the lights on both directions should be synchronized with each other (for example,
when the green light is turned on in the east-west direction, the red light should be turned on
in the south-north direction).

Therefore there will be only the following situations (“a” and “b” refer to east-west and
ROBT 206 – Microcontrollers with Lab– Lab Manual
……………………………………………………………………………………………………...
south-north directions, respectively):

- The red light is on and the other two are off in direction “a”, meaning that the cars cannot
pass in this direction and they need to stop. In this case it is safe for the pedestrians to cross
the street in direction “a”. At the same time, for direction “b”, the green light will be on for a
given amount of time and at the end the green light will go off and the yellow light will be on.
This means that the situation is going to change. When the yellow light is on, cars and
pedestrians are not allowed to pass and cross the street.

- The green light is on in direction “a”, meaning that cars are allowed to pass the junction
in this direction. On the other hand, in direction “b”, the red light is turned on for a while,
and at the end, the yellow light is turned on to show the transition time.
- The yellow light is on in direction “a”, meaning that the transition time is there, the cars
and pedestrians are not allowed to cross the street. On the other hand, in direction “b”, the
red light would be on, preventing the cars from passing the street.

Here is an illustration to help you understand the pattern:

Design the system and write the corresponding code and test your setup at the end.

Inputs and Outputs


Like other hardware development boards, the Arduino Board reads inputs and can control
outputs.

An input brings information from the physical world into the board’s microcontroller. It can be
as simple as the state of a button or switch but can also be the position of a dial or slider like you
see on a sound mixing board. Sensors can also be used as inputs to read information from the
physical world. There are plenty of sensors to choose from including temperature, light level,
sound level, acceleration, and much more (see Fig. 1).
ROBT 206 – Microcontrollers with Lab– Lab Manual
……………………………………………………………………………………………………...

Figure 1. Some typical sensors.

An output is how a development board like the Arduino Board can affect the physical world. It
can be as simple as a light emitting diode, or LED, which glows when electrical current runs
through it. An LED might indicate whether the device is turned on, or if there’s an error (a
blinking red LED would be perfect for that). Outputs could also be motors that drive wheels on a
robot, a text display for the temperature, or a speaker that plays musical tones (see Fig. 2).
Figure 2. Some typical output components

Digital and Analog Input/Output


● Digital I/O – only two values: on/off
● Digital can be thought of as only two bins (zero and one)
● Analog I/O – many values
● Computers don’t really do analog
● So they fake it, with quantization
ROBT 206 – Microcontrollers with Lab– Lab Manual
……………………………………………………………………………………………………...
Quantization - breaking up the analog range requires more complex circuitry to
into bins. The number of bins is the implement.
resolution. More bins = higher accuracy, but

Digital Input

● Most inputs you’ll use are variations of switches.


● Switches make or break a connection
● But Arduino needs to see a voltage
● Specifically, a “HIGH” (5 volts) or a “LOW” (0 volts)

LOW HIGH

Tiny switches

Always
conne
whe
pushed
Pushbutton
ROBT 206 – Microcontrollers with Lab– Lab Manual
……………………………………………………………………………………………………...
Switch to Volts: Positive Logic

● Digital inputs can “float” between 0 and 5 volts


● Resistor “pulls down” input to ground (0 volts)
● Pressing switch sets input to 5 volts
● Press is HIGH
● Release is LOW

Switch to Volts: Inverted Logic

● Resistor pulls up input to 5 volts


● Switch sets input to 0 volts
● But now the sense is inverted
● Press is LOW
● Release is HIGH
“pull-up”

Using digitalRead()

● int buttonPin = 7; assign pin#7 to button_pin


● In setup():
○ use pinMode(buttonPin,INPUT) to make pin an input
● In loop():
○ use digitalRead(buttonPin) to get switch position
● If you are doing many tests, use a variable to hold the output value of
digitalRead().
● e.g. val = digitalRead(buttonPin)
ROBT 206 – Microcontrollers with Lab– Lab Manual
……………………………………………………………………………………………………...

Step-by-Step Instructions:

1. Connect one of your Arduino Board GND pins to the one of the long power rails on
your breadboard - this will be your ground rail.
2. Connect the short leg of your LED to this same ground rail on the breadboard and
connect the long leg to a row on the breadboard.
3. Connect the 220 Ohm resistor from pin 13 to the same row the long leg of your LED is
attached to.
4. Place the push button on the breadboard
5. Connect a jumper wire from the 5-volt pin to one side of the pushbutton.
6. Connect a jumper wire from pin 7 to the other side of the pushbutton.
7. Connect one side of the 10K (brown-black-orange) resistor from the ground rail on the
breadboard and the other side to the pushbutton - on the same side that pin 7 connects.
8. Plug your Arduino Board into your computer with a USB cable.
9. Open up the Arduino IDE.
10. Either download this sketch here, or copy and paste it below from the page.
11. Click the verify button on the top left. It should turn orange and then back to blue.
12. Click the upload button. It will also turn orange and then blue once the sketch has
finished uploading to your Arduino Board.
13. Open up the serial monitor window.
14. Press the button a couple times and see how the LED at pin 13 reacts.
ROBT 206 – Microcontrollers with Lab– Lab Manual
……………………………………………………………………………………………………...

This example code can


be found in File ->
Examples- >Digital-
>Button

const int BUTTON = 7;


const int LED = 3;
int state = 0;

void setup()
{
pinMode(BUTTON, INPUT);
pinMode(LED, OUTPUT);
}

void loop()
{
state = digitalRead(BUTTON);

if (state == HIGH)
{
digitalWrite(LED, HIGH);
}
else{
digitalWrite(LED, LOW);
} }
ROBT 206 – Microcontrollers with Lab– Lab Manual
……………………………………………………………………………………………………...
Exercise 4. Switch debouncing

What is a Switch Bounce?


One of the major problems encountered when using push buttons and switches in digital
electronics projects is the problem of bouncing. When we press a button once it may register
twice and when we press it four times in a row, for instance, it may register just twice. This
occurrence is due to a property of switches known as bounciness which is as a result of the
physical property of the switches. The left-hand image below shows a simple push switch with a
pull-up resistor. The right hand image shows the trace at the output terminal, Vout, when the
switch is pressed. As can be seen, pressing the switch does not provide a clean edge. If this signal
was used as an input to a digital counter sensitive to the rising edge of the pulse, for example,
you'd get multiple counts rather than the expected single count.

Note that the same can also occur on the release for a switch.

The problem is that the contacts within the switch don't make contact cleanly, but actually
slightly 'bounce'. The bounce is quite slow, so you can recreate the trace, and the problem quite
easily

.
The switch debouncing can be done both in hardware and software. In this lab, the aim is to
implement it in the software. The basic idea is to sample the switch signal at a regular interval
and filter out any glitches. There are a couple of approaches to achieving this, one of which is
used here. Let’s assume a switch circuit like that shown in the explanation of switch bounce: a
simple push switch with a pull-up resistor. You can follow step by step instructions provided
above to implement the software debouncing:
You need to use the following sketch (can be found in File -> Examples->Digital->Debounce):

const int buttonPin = 2; //This is the buttons read pin


ROBT 206 – Microcontrollers with Lab– Lab Manual
……………………………………………………………………………………………………...
const int ledPin = 13; // This is the LED output pin

int ledState = HIGH; //Variable for current LED state


int buttonState; //Variable for current button reading
int lastButtonState = LOW;//Variable for last know button reading

unsigned long lastDebounceTime = 0; //last time the pin was toggled, used
to keep track of time
unsigned long debounceDelay = 50; //the debounce time which user sets
prior to run

void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState);
}

void loop() {
//read the button pin, if pressed will be high, if not pressed will be
low
int reading = digitalRead(buttonPin);
//in the case that the reading is not equal to the last state set the
last debounce time to current millis time
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
//check the difference between current time and last registered button
press time, if it's greater than user defined delay then change the LED
state as it's not a bounce
if ((millis()-lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
//set the LED
digitalWrite(ledPin, ledState);
//save the reading. next time through the loop the state of the reading
will be known as the lastButtonState
lastButtonState = reading;
}
ROBT 206 – Microcontrollers with Lab– Lab Manual
……………………………………………………………………………………………………...
Exercise 5: Multiple switches

Use this circuit design to build a circuit. Control one LED’s state with two buttons. Please refer
to the button debouncing task.
ROBT 206 – Microcontrollers with Lab– Lab Manual
……………………………………………………………………………………………………...

LAB 1
VERIFICATION SHEET
Submit this page to your Lab Instructor or TA at the end of the lab session.

Name(s): Section #: Pre-lab verification (10 pts):


1.
2.
3.

Exercise 1: SOS (18 pts)

Verified: Time:
______________________________________________________________________________

Exercise 2: a simple traffic control system for pedestrians (18 pts)

Verified: Time:
______________________________________________________________________________

Exercise 3: a simple traffic control system for a junction (18 pts)


Verified: Time:
______________________________________________________________________________

Exercise 4: switch debouncing (18 pts)

Verified: Time:
______________________________________________________________________________

Exercise 5: multiple switches (18 pts)

Verified: Time:
______________________________________________________________________________

Clean your workstation and return all used lab supplies to their original place!

Verified: Time:

You might also like