Introduction to Arduino
Introduction to Arduino
……………………………………………………………………………………………………...
Objectives:
Equipment:
• Arduino board
• Breadboard
• LEDs
• Resistors
• Pushbutton
• Jumper wires
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
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 programming language is standard C (but simplified). The program space contains: Global
Space and Function Space:
Global
setufunction
loopfunction
Global
Main Functions
setup()
loop()
● Continuous loop function (similar to while(1))
ROBT 206 – Microcontrollers with Lab– Lab Manual
……………………………………………………………………………………………………...
● Takes and returns no arguments
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
……………………………………………………………………………………………………...
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 7. A simple electronic circuit with one Resistor (R1) and 1 LED (LED1).
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.
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}
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”).
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.
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.
Design the system and write the corresponding code and test your setup at the end.
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
……………………………………………………………………………………………………...
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 Input
LOW HIGH
Tiny switches
Always
conne
whe
pushed
Pushbutton
ROBT 206 – Microcontrollers with Lab– Lab Manual
……………………………………………………………………………………………………...
Switch to Volts: Positive Logic
Using digitalRead()
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
……………………………………………………………………………………………………...
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
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):
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.
Verified: Time:
______________________________________________________________________________
Verified: Time:
______________________________________________________________________________
Verified: Time:
______________________________________________________________________________
Verified: Time:
______________________________________________________________________________
Clean your workstation and return all used lab supplies to their original place!
Verified: Time: