0% found this document useful (0 votes)
17 views13 pages

Lab Ele551

The document is a lab module for an embedded system design course at Universiti Teknologi Mara, focusing on Arduino programming and interfacing. It outlines various laboratory exercises that teach students to control LEDs, motors, servos, and piezo elements using Arduino, along with coding examples and tasks. The module aims to equip students with practical skills in electronic circuit design and programming using modern engineering tools.

Uploaded by

2023248588
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views13 pages

Lab Ele551

The document is a lab module for an embedded system design course at Universiti Teknologi Mara, focusing on Arduino programming and interfacing. It outlines various laboratory exercises that teach students to control LEDs, motors, servos, and piezo elements using Arduino, along with coding examples and tasks. The module aims to equip students with practical skills in electronic circuit design and programming using modern engineering tools.

Uploaded by

2023248588
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

FACULTY OF ELECTRICAL

STUDENT KIT
TM
ENGINEERING LAB MODULE
UNIVERSITI TEKNOLOGI MARA

Program: EE241 Sem : 6


Ownership : Faculty of Electrical Engineering
FKE Doc.ID : Date Issued : 2014

Embedded System Design and Interfacing


COURSE CODE: ELE551

Authors

Mohd Faizul bin Md Idros


Prepared by : Date : Dec 2015
Abdul Hadi Abdul Razak

st
1 Revision : Date :

nd
2 Revision : Date :

Endorsement by Center of Studies

Chair : Date :

Signature :

1
CIRCUIT SIMULATION USING ORCAD-PSCPICE ELE551

MODULE OUTCOMES:

Upon completion of this experiment, students should have the:

MO1 : Ability to use the techniques, skills and modern engineering tools necessary for electronic
engineering practices
MO2 : Ability to design electronic circuit and controlled by microcontroller
MO3 : Ability to apply C language to perform the specific task of operation. .

BACKGROUND:

An embedded system is a special-purpose computer system designed to perform certain dedicated


functions. It is usually embedded as part of a complete device including hardware and mechanical parts. An
embedded system is one that has computer hardware with software embedded in it as one of its
components. We can define an embedded system as “A microprocessor based system that does not look
like a computer”. Due to their compact size, low cost and simple design aspects made embedded systems
very popular and encroached into human lives and have become indispensable. They are found
everywhere from kitchen ware to space craft.

Arduino is an open source electronics prototyping platform composed of a microcontroller, a programming


language, and an IDE. It is also a tool for making interactive applications, designed to simplify this task for
beginners but still flexible enough for experts to develop complex projects. This is the programming step of
using Arduino and Intel galelio;

1. declare ‘variables’.
• For example, if I had a sensor and a button I wanted to connect to my Arduino, I would start with
declaring two variables, like this:
• int mySensor = 0;
• int myButton = 2;
2. add what is called the setup.
• The next step is to add the setup. The setup is the name of a function that always needs to be
included inside a sketch.
void setup(){
//enter commands here
}
3. add the loop.
• Next up we have the loop function. This is where the action happens. This is the part of the sketch
where
• the Arduino spends most of its time while it’s powered up.
• To declare the loop, you write:
void loop()
//enter commands here
}
PRELIMINARY WORK:

In this laboratory, student will learn about the Arduino development environment and some of the board
choices that student can use to support the Atmel chip family. Some of the hardware details about the
boards were discussed to help you decide which board to use while you learn about programming the board
using Arduino C. You then downloaded the Arduino development environment and installed the IDE. As a
check on the IDE installation, you loaded the sample Blink program from the examples supplied with the
IDE, compiled, and downloaded it to the board to verify that everything was installed correctly. You are now
ready to start learning Arduino C. Figure below shows the step of installing Arduino software.

,
Step 1: Download the software
Goto http://arduino.cc/en/Main/Software
And download the software for your operating
system

Step 2: Unzip the Software


Unzip arduino-00 -win.zip ( Recommended Path,
c:\Program Files\ - version #)

Step 3: Shortcut Icon


Open c:\program files\arduino-00 ( Right Click
Arduino.exe (send to>Desktop (create shortcut) )
\ - version #)

Step 4: Plug In Your Arduino


Plug your Arduino in:
using the included USB cable, plug your Arduino
board into a free USB port Wait for a box to pop up

Step 5: Add new Hardware


Skip searching the internet
(click the next box when prompted to do so)
Install from a Specific destination
(click “Install from a list or specific location
(Advanced)) Choose the Location c:\program
files\arduino-00 \drivers\FTDI USB Drivers\
Finished

Now students can try the example of the simple programming from File > Examples menu.
LABORATORY 1 : OUTPUT PORT INTERFACING

LED’s (light emitting diodes) are used in all sorts of clever things which is why we have included them in this
kit. We will start off with something very simple, turning one on and off, repeatedly, producing a pleasant
blinking effect. To get started grab the parts listed below, pin the layout sheet to your breadboard and then
plug everything in. Once the circuit is assembled you'll need to upload the program. To do this plug the
Arduino board into your USB port. Then select the proper port in Tools > Serial Port > (the comm. port of
your Arduino). Next upload the program by going to File > Upload to I/O Board (ctrl+U). Finally bask in
the glory and possibility that controlling lights offers.

Parts; Schematic;

i) Breadboard sheet
ii) 560Ω resistor
iii) LED
iv) Wire

Coding;

/*
* Blink
*
* The basic Arduino example. Turns an LED on for one second,
* then off for one second, and so on... We use pin 13 because,
* depending on your Arduino board, it has either a built-in LED
* or a built-in resistor so that you need only an LED.
*
* http://www.arduino.cc/en/Tutorial/Blink
*/
int ledPin = 13; // LED connected to digital pin 13
void setup() // run once, when the sketch starts
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
void loop() // run over and over again
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // waits for a second

Task

Along with digital (on/off) control the Arduino can control some pins in an analog (brightness) fashion. Demonstrate
clearly the different level of LED brightness of LEDs can be adjusted.
Lab 2: MULTIPLE OUTPUT PORT INTERFACING

We have caused one LED to blink, now its time to up the stakes. Lets connect eight. We'll also have an
opportunity to stretch the Arduino a bit by creating various lighting sequences. This circuit is also a nice
setup to experiment with writing your own programs and getting a feel for how the Arduino works. Along
with controlling the LEDs we start looking into a few simple programming methods to keep your programs
small.

for()loops - used when you want to run a piece of code several times.
arrays[] - used to make managing variables easier (its a group of variables)

Modify code from Lab1 by adding a loop instruction to make the all the LED’s blinking follows the
information from the coding.
____________________________________________________________________________________
Loops;
{
int delayTime = 100; // the time (in milliseconds) to pause between LEDs
//make smaller for quicker switching and larger for slower

//Turn Each LED on one after another


for(int i = 0; i <= 7; i++){
digitalWrite(ledPins[i], HIGH); //Turns on LED #i each time this runs i
delay(delayTime); //gets one added to it so this will repeat
} //8 times the first time i will = 0 the final
//time i will equal 7;

//Turn Each LED off one after another


for(int i = 7; i >= 0; i--){ //same as above but rather than starting at 0 and counting up
//we start at seven and count down
digitalWrite(ledPins[i], LOW); //Turns off LED #i each time this runs i
delay(delayTime); //gets one subtracted from it so this will repeat
} //8 times the first time i will = 7 the final
//time i will equal 0;
}
___________________________________________________________________________________

Task
1. Normal traffic light systems have a RED, YELLOW AND BLUE color. Demonstrate the sequence of
a traffic light system using 3 different colors of LEDs.
Lab 3: TRANSISTOR AND MOTOR

The Arduino's pins are great for directly controlling small electric items like LEDs. However, when dealing
with larger items (like a toy motor or washing machine), an external transistor is required. A transistor is
incredibly useful. It switches a lot of current using a much smaller current. A transistor has 3 pins. For a
negative type (NPN) transistor you connect your load to collector and the emitter to ground. Then when a
small current flows from base to the emitter a current will flow through the transistor and your motor will
spin (this happens when we set our Arduino pin HIGH). There are literally thousands of different types of
transistors, allowing every situation to be perfectly matched. Transistor 2N222A is chosen rather common
general purpose transistor. The important factors in our case are that it’s maximum voltage (40 v) and its
maximum current (600 milliamp) are both high enough for our toy motor

Write code
void loop() // run over and over again
{
motorOnThenOff();
}

void motorOnThenOff(){
int onTime = 2500; //the number of milliseconds for the motor to turn on
int offTime = 1000; //the number of milliseconds for the motor to turn off

digitalWrite(motorPin, HIGH); //turns the motor On


delay(onTime); waits for onTime milliseconds
//
digitalWrite(motorPin, LOW); // turns the motor Off
delay(offTime); // waits for offTime milliseconds
}

Motor troubleshooting in case motor is not working

1) Different manufacturers produce the same transistor with different pin assignments. Try turning the
transistor 180 degrees.

2) The transistor will get warm, in most cases this is okay, if you are worried about the temperature
turn the circuit off for a bit and let it cool down.

3) Sometimes the Arduino board will disconnect from the computer. Try un-plugging and then re-
plugging it into your USB port.

Task

We played with the Arduino's ability to control the brightness of an LED earlier now we will use the same feature
to control the speed of motorusing Pulse Width Modulation (PWM). Demonstrate the same technique of
controlling the blinking LEDs to control the speed of DC motor.
Lab 4: A SINGLE SERVO

Spinning a motor is good fun but when it comes to projects where motion control is required they tend to
leave us wanting more. They are mass produced, widely available and cost anything from a couple of dollars
to hundreds. Inside is a small gearbox (to make the movement more powerful) and some electronics (to
make it easier to control). A standard servo is positionable from 0 to 180 degrees. Positioning is controlled
through a timed pulse, between 1.25 milliseconds (0 degrees) and 1.75 milliseconds (180 degrees) (1.5
milliseconds for 90 degrees). Timing varies between manufacturer. If the pulse is sent every 25-50
milliseconds the servo will run smoothly. One of the great features of the Arduino is it has a software
library that allows you to control two servos (connected to pin 9 or 10) using a single line of code.

Write code
#include <Servo.h>
Servo myservo; //create servo object to control a servo
int pos = 0; //variable to store the servo position
void setup() {
myservo.attach(9); //attaches the servo on pin 9 to the servo object
}
void loop() {
for(pos = 0; pos < 180; pos += 1) //goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); //tell servo to go to position in variable 'pos'
delay(15); //waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) //goes from 180 degrees to 0 degrees
{
myservo.write(pos); //tell servo to go to position in variable 'pos'
delay(15); //waits 15ms for the servo to reach the position
}
}

Pre-caution

1) Even with colour wires it is still shockingly easy to plug a servo in backwards. This might be the
case of servo not twisting.
2) some servos do break when plugged in backwards if still not working.
3) If servo broken, don't worry they are only a few dollars, plus taking one apart is good fun (you can
try running the motor directly with a transistor).

Task

Now, use potentiometer (CIRC08) to demonstrate how to control the servo by controlling the pulse directly.
LAB 5: PIEZO ELEMENTS

To this point we have controlled light, motion, and electrons, Lets tackle sound next. But sound is an
analog phenomena, how will our digital Arduino cope? We will once again rely on its incredible speed which
will let it mimic analog behavior. To do this, we will attach a piezo element to one of the Arduino's digital
pins. A piezo element makes a clicking sound each time it Vis pulsed with current. If we pulse it at the right
frequency (ie. quickly enough) these clicks will run together to produce notes. Lets get to experimenting
with it and get your Arduino playing 'Twinkle Twinkle Little Star'.

Pre-caution

Given the size and shape of the piezo element it is easy to miss the right holes on the breadboard. If no
sound, try double checking its placement.

If you can't Think while the melody is playing, just pull up the piezo element whilst you think, upload your
program then plug it back in.

The code is written so you can easily add your own songs, check out the code below to get started.

note frequency period timeHigh

c 261 Hz 3830 1915


d 294 Hz 3400 1700
e 329 Hz 3038 1519
f 349 Hz 2864 1432
g 392 Hz 2550 1275
a 440 Hz 2272 1136
b 493 Hz 2028 1014
C 523 Hz 1912 956

The timing for each note is calculated based on variables, as such we can tweak the sound of each note or the timing.
To change the speed of the melody you need to change only one line,

int tempo = 300; ---> int tempo = (new #)

Change it to a larger number to slow the melody down, or a smaller number to speed it up.

Tuning the notes:

If you are worried about the notes being a little out of tune this can be fixed as well. The notes have been calculated
based on a formula in the comment block at the top of the program. But to tune individual notes just adjust their values
in the tones[] array up or down until they sound right. (each note is matched by its name in the names[] (array ie. c =
1915 ) char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };

int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

Task

The program is pre-set to play 'Twinkle Twinkle Little Star'. Change the code to get another song according to your own
creativity.
LAB 6: BUTTON PRESSING WITH PUSHBUTTONS

Up to this point we have focused entirely on outputs, time to get our Arduino to listen, watch and feel. We'll
start with a simple pushbutton. Wiring up the pushbutton is simple. There is one component, the pull up
resistor, that might seem out of place. This is included because an Arduino doesn't sense the same way we
do (ie button pressed, button unpressed). Instead it looks at the voltage on the pin and decides whether it
is HIGH or LOW. The button is set up to pull the Arduino's pin LOW when it is pressed, however, when the
button is unpressed the voltage of the pin will float (causing ccasional errors). To get the Arduino to reliably
read the pin as HIGH when the button is unpressed, we add the pull up resistor.

Coding

*/
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
}
void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, LOW); // turn LED OFF
} else {
digitalWrite(ledPin, HIGH); // turn LED ON
}
}

Caution

If light not turning On, Give it a 90 degree twist and see if it starts working. The pushbutton is square and
because of this it is easy to put it in the wrong way.

If light not fading, it could be a bit of a silly mistake we constantly made, when you switch from simple on
off to fading remember to move the LED wire from pin 13 to pin 9.

No worries about underwhelmed because these circuits are all super stripped down to make
playing with the components easy, but once you throw them together the sky is the limit.

Task

Lets use the buttons to control an analog signal. To do this you will need to change the wire connecting the LED from
pin 13 to pin 9, also change this in code.

int ledPin = 13; ----> int ledPin = 9;

By referring Lab 6, modify the coding to have a simple piano using 7 switches for different note a,b,c,d,e,f,g.
LAB 7: TWISTING POTENTIOMETERS

Along with the digital pins the Arduino has it also has 6 pins which can be used for analog input. These
inputs take a voltage (from 0 to 5 volts) and convert it to a digital number between 0 (0 volts) and 1024 (5
volts) (10 bits of resolution). A very useful device that exploits these inputs is a potentiometer (also called a
variable resistor). When it is connected with 5 volts across its outer pins the middle pin will read some value
between 0 and 5 volts dependent on the angle to which it is turned (ie. 2.5 volts in the middle). We can
then use the returned values as a variable in our program.

Coding

int potPin = 2; // select the input pin for the potentiometer


int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
}
void loop() {
val = analogRead(potPin); // read the value from the sensor
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(val); // stop the program for some time
digitalWrite(ledPin, LOW); // turn the ledPin off
delay(val); // stop the program for some time
}

Sporadically Working - This is most likely due to a slightly dodgy connection with
the potentiometer's pins. This can usually be conquered by taping the potentiometer down

The Control is Backward - There are two ways to fix this, either switch the red and black wires connected to the
potentiometer, or turn the potentiometer around. (sorry sometimes the factory ships us a backwards potentiometer)

Still Backward - You can try operating the circuit upside down. Sometimes this helps.

Task

Change the loop() code to control the LED brightness according to the analog value from the potentiometer.
LAB 8: PHOTO RESISTORS

Whilst getting input from a potentiometer can be useful for human controlled experiments, what do we use
when we want an environmentally controlled experiment? We use exactly the same principles but instead of
a potentiometer (twist based resistance) we use a photo resistor (light based resistance). The Arduino
cannot directly sense resistance (it senses voltage) so we set up a voltage divider (http://tinyurl.com/2sunta
). The exact voltage at the sensing pin is calculable, but for our purposes (just sensing relative light) we can
experiment with the values and see what works for us. A low value will occur when the sensor is well lit
while a high value will occur when it is in darkness.

Figure 8

Coding

/*
* loop() - this function will start after setup
* finishes and then repeat
*/

void loop()
{
int lightLevel = analogRead(lightPin); //Read the light level
lightLevel = map(lightLevel, 0, 900, 0, 255);
//adjust the value 0 to 900 to
lightLevel = constrain(lightLevel, 0, 255);
//make sure the value is betwween 0 and 255
analogWrite(ledPin, lightLevel); //write the value
}

Pre-caution

LED remain dark - This is a mistake we continue to make time and time again, if only they could make an
LED that worked both ways. Pull it up and give it a twist.

Changes in Ligh - Given that the spacing of the wires on the photo-resistor is not standard, it is easy
to misplace it. Double check its in the right place.

Task

Modify the code to have a system that can control the brightness of LED according to the received light
from the photo resistor.
LAB 9: TEMPERATURE SENSOR

In this session, student will apply the coding to measure temperature using serial
communication in Arduino. To do this we'll use a rather complicated IC (integrated circuit)
hidden in a package identical to our 2N222A transistors. It has three pins ground, signal
and +5 volts, and is easy to use. It outputs 10 millivolts per degree centigrade on the
signal pin (to allow measuring temperatures below freezing there is a 500 mV offset eg. 25
C = 750 mV, 0 C = 500mV). To convert this from the digital value to degrees we will use
some of the Arduino's maths abilities. Then to display it we'll use one of the IDE's rather
powerful features, the debug window. We'll output the value over a serial connection to
display on the screen. Let's get to it. One extra note, this circuit uses the Arduino IDE's
serial monitor. To open this, first upload the program then click the button which looks
like a square with an antenna.

Coding
void loop() // run over and over
again
{
float temperature =
getVoltage(temperaturePin); //getting the voltage reading from the temperature sensor
temperature = (temperature - .5) * 100;//converting from 10 mv
//per degree wit 500 mV offset to degrees ((volatge - 500mV) times 100)
Serial.println(temperature); //printing the result
delay(1000); //waiting a second
}

float getVoltage(int pin){


return (analogRead(pin) * .004882814);//converting from a 0 to 1024 digital range

Task
Upload the sketch turn on the serial monitor, then change the speed from 9600 baud to 115200 baud in the pull down
menu. Here, you are transmitting data 12 times faster.
LAB 10: CONTROLLING RELAY FOR LARGER LOADS

In this lab, we combine what we learned about using transistors in lab 03 to control a
relay. A relay is an electrically controlled mechanical switch. Inside the little plastic box is
an electromagnet that, when energized, causes a switch to trip (often with a very
satisfying clicking sound). You can buy relays that vary in size from a quarter of the size
of the one in this kit up to as big as a fridge, each capable of switching a certain amount
of current. They are immensely fun because there is an element of the physical to them.
While all the silicon we've played with to this point is fun sometimes you just want to wire
up a hundred switches to control something magnificent. Relays give you the ability to
dream it up then control it with your Arduino.

Coding

Use the same code for LED blinking in Lab01. The output port connected to the terminal base of transistor.

Task

Modify the code and circuit to control the direction of spin of a DC motor in order to reverse the direction of
current flow through it. To do this manually we reverse the leads. To do it electrically we require something
called an h-bridge. It can be constructed using either four transistors (2 PNP and 2 NPN) or using one DPDT
relay. To control the motor's direction, wire up the following circuit.

You might also like