STUDENT LAB - Manual ETTMM601 PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 61

STUDENTLAB MANUAL: PART2 (MICROCONTROLLER)

PREPARED BY:

ABIZEYE Louis Bonheur

[Microcontroller Laboratory Manual_Preapared by Louis Bonheur Abizeye/RP IPRC TUMBA _2022] Page 1
Table of Contents
INTRODUCTION TO ARDUINO MICROCONTROLLER ........................................................................................... 3
1.1. What is Arduino? ............................................................................................................................................... 3
Introduction ....................................................................................................................................................................... 3
Setup & Installation ........................................................................................................................................................... 3
Experiment 1– Basic Digital Output: Blinking LED ........................................................................................................... 10
Experiment 2 – Basic Digital Output: Dancing LED operation ...................................................................................... 15
Experiment 3 – Basic Digital Output: One Way Traffic Light ........................................................................................ 17
Experiment NO4_Basic Digital Input: Reading Pushbutton ........................................................................................... 19
Experiment NO5_Basic Digital Input: State Change Detection (Edge Detection) for pushbuttons ............................ 21
Experiment NO6_Basic Serial Port Output ................................................................................................................. 24
Experiment NO7 – Basic Serial Port Input/Output: “Hello World” ............................................................................ 26
Experiment 8 – Basic Serial Port Input/Output: “Switch statement with serial input” ............................................... 28
Experiment 9 – Analog Input/Output: Reading Analog Pins and Converting the Input to a Voltage ......................... 30
Experiment 10 – Analog Input: Light Level Indicator with Light Dependent Resistor .............................................. 32
Experiment 11 – Analog Output: Pulse width modulation .......................................................................................... 34
Experiment 12 – Analog I/O: Controlling the dc motor speed .................................................................................... 37
Experiment 13 – Analog Input:: Movement detection system .................................................................................... 40
Experiment 14 – Analog Input: LIGHT AND DARK DETECTION SYSTEM ........................................................ 43
Experiment 15 – Analog Input:: TEMPERATURE CONTROL SYSTEM ................................................................ 46
Experiment 16 – Analog Input:: DISPLAYING MESSAGE ON LIQUID CRYSTAL DISPLAY ........................... 50
Experiment 16: Use of Basic Sensors_distance measurement ..................................................................................... 56
Experiment 17: Basic Use of Actuators_SERVO MOTOR CONTROLL .................................................................. 59

[Microcontroller Laboratory Manual_Preapared by Louis Bonheur Abizeye/RP IPRC TUMBA _2022] Page 2
INTRODUCTION TO ARDUINO MICROCONTROLLER

1.1.What is Arduino?

Introduction
The Arduino (Uno) Board is a micro-controller board that was created to house the ATmega328
chip. The chip is a high performance and low power 8-bit micro-controller that has 23 programmable
I/O lines, 32K bytes of flash memory (of which 0.5KB is already used for the Boot loader), 1k bytes of
EEPROM and 2k bytes of RAM. The Arduino Uno board provides the user with 6 analog input pins, 14
digital I/O pins of which 6 of them can also be used for PWM outputs, a power jack, a USB port, an
ICSP header, a reset button, a small LED connected to digital pin 13, and a 16MHz crystal oscillator
[1]. (See Arduino Uno Component View for more details about the parts.)
In comparison to earlier models, the board uses the Atmega8U2 programmed as a USB-to-serial
converter rather than the FTDI USB-to-serial driver chip.
It is the latest in the Arduino USB board series and the current reference model for the Arduino
platform. The board is fairly easy to use and capable of doing a lot of things.

Setup & Installation


A. Arduino IDE (For Windows)
This part will guide you through the set up and installation process of the Integrated Development
Environment (IDE) that will be used throughout the exercises.
1. Open your default internet browser and access the Arduino website. Download the latest Arduino IDE
version. The software is compatible with Linux, Mac and Windows so just choose the one that matches
your OS. The Arduino download page is at http://arduino.cc/en/Main/Software [2].

Figure 1: A part of Arduino Website’s download page. The current version at this time was 0022. Arduino
allows you to install its IDE on several platforms (see encircled)

[Microcontroller Laboratory Manual_Preapared by Louis Bonheur Abizeye/RP IPRC TUMBA _2022] Page 3
2. After downloading the compressed file, extract its contents to your preferred directory (C:\Program
Files, your Desktop or etc...). Note that the whole folder size is around 200MB when completely
extracted [2].

Figure 2: Screenshot of attempt to extract the zipped Arduino folder. Make sure you have an archive utility such as 7zip or
WinRAR.

3. Congratulations! Arduino IDE is installed on your computer. To use it, just navigate to your main folder
directory and run the Arduino application [2].

Figure 3: Screenshot of what’s inside the Arduino-0022 folder. The application


icon looks like an infinity symbol.

[Microcontroller Laboratory Manual_Preapared by Louis Bonheur Abizeye/RP IPRC TUMBA _2022] Page 4
B.Arduino Uno board driver (For Windows)

This part will guide you through the set-up and installation process of the Arduino Uno board driver for
the device to be recognized by the IDE.

1. Connect the Arduino UNO to the computer via USB Cable (A on fig. 4) [2]. Check if it is properly
connected by observing the green LED labeled ON (B on fig. 4) on the board.

Figure 4: Photo of Arduino Uno board connected to a


Computer. Note that the board’s USB-B port.

2. Wait for Windows to try and install the device’s driver until it fails. Navigate to the Device Manager
through Start > Control Panel > Device Manager. Locate the Arduino Uno Device. Right-click it
to choose Update Driver Software [2].

Figure 5:Screenshot of the Device Manager. The Arduino


Uno should have an exclamation point.

[Microcontroller Laboratory Manual_Preapared by Louis Bonheur Abizeye/RP IPRC TUMBA _2022] Page 5
3. Choose to browse your computer for the driver by clicking Browse my computer for driver software
[2]

Figure 6: Screenshot of the options for searching the device driver.


Choose the second option so that you can look for it in your hard disk.
4. A new
window will open for
you to indicate the location of the driver. Click Browse…

Figure 7: Screenshot of the browse option menu. Choose the first option which is to look
manually for the folder that contains the Arduino Uno board’s driver

[Microcontroller Laboratory Manual_Preapared by Louis Bonheur Abizeye/RP IPRC TUMBA _2022] Page 6
5. Navigate to your Arduino folder and choose the drivers folder. Click OK upon selection [2].

Figure 8:Screenshot of navigating through the Arduino software folder. Note that the drivers
folder was chosen rather than the FTDI USB Drivers (It was mentioned earlier that only
preceding models use this)

6. A Windows Security window sometimes pops up to confirm if you want to continue the installation. Just click,
Install this driver software anyway.

Figure 9:Screenshot of pop-up window. Windows can’t verify the publisher of the device
software but we know that the software’s publisher is Arduino

6. Wait for Windows to finish installing the device driver. Upon completion, you should see an
installation successful message. Congratulations and click Close. You are ready to start programming using
Arduino!

[Microcontroller Laboratory Manual_Preapared by Louis Bonheur Abizeye/RP IPRC TUMBA _2022] Page 7
Figure 10: Screenshot of successful driver installation of the Uno board. The next step is to
start doing the exercises.

[Microcontroller Laboratory Manual_Preapared by Louis Bonheur Abizeye/RP IPRC TUMBA _2022] Page 8
Arduino Uno Board

B. Component View A pictorial view of the Arduino Uno board’s peripherals can be found in this section.

Figure 11: Photo of the Arduino Uno Board taken from [1]. The components are labeled and a
brief explanation of each one is provided at the next par

Component Explanations (taken from [1])

 Analog input pins – pins (A0-A5) that take-in analog values to be converted to be represented with a number
range 0-1023 through an Analog to Digital Converter (ADC).
 ATmega328 chip – 8-bit microcontroller that processes the sketch you programmed.
 Built-in LED – in order to gain access or control of this pin, you have to change the configuration of pin 13
where it is connected to.
 Crystal Oscillator – clock that has a frequency of 16MHz
 DC Jack – where the power source (AC-to-DC adapter or battery) should be connected. It is limited to input
values between 6-20V but recommended to be around 7-12V.
 Digital I/O pins – input and output pins (0-13) of which 6 of them (3, 5, 6, 9, 10 and 11) also provide PWM
(Pulse Width Modulated) output by using the analogWrite() function. Pins (0 (RX) and 1 (TX)) are also used to
transmit and receive serial data.
 ICSP Header – pins for “In-Circuit Serial Programming” which is another method of programming.
 ON indicator – LED that lights up when the board is connected to a power source.
 Power Pins – pins that can be used to supply a circuit with values VIN (voltage from DC Jack), 3.3V and 5V.
 Reset Button – a button that is pressed whenever you need to restart the sketch programmed in the board.
 USB port – allows the user to connect with a USB cable the board to a PC to upload sketches or provide a
voltage supply to the board. This is also used for serial communication through the serial monitor from the
Arduino software.

[Microcontroller Laboratory Manual_Preapared by Louis Bonheur Abizeye/RP IPRC TUMBA _2022] Page 9
Experiment 1– Basic Digital Output: Blinking LED

A. Objectives
This example aims to familiarize the student with the basic operation of the Arduino Uno board, and Integrated
Development Environment (IDE). By the end of the exercise, the student should be able to know the basic
functionalities of the IDE.
B. Questions
• How do you compile and upload a sketch file into the Arduino Board using the IDE?
• How can the configuration of the pins be specified in the programming?
• What functions are always required in a sketch?
C. Materials Needed
• 1 x Arduino Uno board and USB cable
• 1 x 5mm/3mm Red LED
• 1 x 470Ω resistor
• 1 x breadboard

D. Using the Arduino IDE

1. Proceed to the Arduino Folder and double-click the arduino application to open the Arduino IDE which
is also shown below:

Figure 12:Screenshot of the Arduino IDE. The workspace is very simple and frequently used
buttons are easy to access

1. Type the following lines of code on the front panel. The code can also be found in the examples folder of Arduino.
To copy it, just navigate from arduino-00XX > examples > Basics > Blink. See Appendix under Structure,
Functions and Time or Section F for the functions' definitions

void setup(){
pinMode(13,OUTPUT);
}
void loop(){

[Microcontroller Laboratory Manual_Preapared by Louis Bonheur Abizeye/RP IPRC TUMBA _2022] Page 10
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
}

2. Press Ctrl + S or @ Icon on the IDE to save your sketch {the term for a source code in Arduino).

3. Click the Verify Button (A on fig. 14) to check if the sketch has no errors. At the bottom of the screen you should
see if the sketch was successfully scanned free of problems (B on fig. 14). Moreover, the size of the sketch file (C
on fig. 14) is also indicated on the black screen at the bottom to shows that it fits the flash memory of the Arduino
Uno.

Figure 13: Screenshot of the Blink code after compilation. The verify button looks just like
a play button and is located at the upper left corner. Successful compiling will be indicated
at the bottom. If an error or problem occurred, it will be shown in the black

E. Test your Program


1. Connect an LED to a 470Q resistor. Note: that the longer leg of the LED is the positive (connected to
one end of resistor and the shorter one is the negative (connected to the ground). The other end of
the resistor should then be connected to the specified pin which is 13.

Figure 14:Schematic diagram of blink circuit.

[Microcontroller Laboratory Manual_Preapared by Louis Bonheur Abizeye/RP IPRC TUMBA _2022] Page 11
2. Check if the board selected in the IDE is the one you are using, the Arduino Uno board.

Figure 15: Screenshot showing Arduino Uno as selected board. If you have a
different type of Arduino board, just select it from this list.

3. Next, make sure that the Arduino board’s COM port is properly identified by the IDE. To do this,
connect the board to the computer then from the IDE front panel go to Tools > Serial Port (A on fig.
17). A list of COM’s should be there, choose the COM number associated with the microcontroller
board. Note: the microcontroller board’s COM no. is found near the name of the device’s driver in
Device Manager (B on fig. 17) [2].

[Microcontroller Laboratory Manual_Preapared by Louis Bonheur Abizeye/RP IPRC TUMBA _2022] Page 12
Figure 16: Screenshot of both the Device Manager and Arduino IDE windows.
Note that the COM number selected as Serial Port must be the same one found
beside the device’s name

4. Finally, upload the sketch made earlier into the board by clicking the icon in the IDE. The
status of the upload can be found near the bottom of the program [2].

Figure 17:Screenshot showing a successful upload. Sometimes an error occurs because


the COM port of the Arduino UNO was not properly specified. To solve this just
follow Step 2

5. If the Arduino Uno is connected to the PC or power source the LED should light up for 1
second and turn off for 1 second repeatedly

Figure 18:Photo of the LED blinking. Note that the small LED

on the board is also blinking because it is also connected to pin 13.

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 13
F. Code explanations

Line Explanations
No.
1 Creates a function called setup. The function type is void and therefore does not return
any value after execution. This is only read once in a file execution and is required in a
sketch.
2 The function pinMode() configures the specified pin which is 13 in this sample. It sets
digital pin 13 as output.
3 Creates a function called loop. The function type is also void and therefore also does
not return any value after execution. This is read repeatedly in a file execution and
usually where the main program is. This is also required in a sketch.

4 The digitalWrite() function writes a high(5V) or low(0V) on a specified pin. In this


case, a high state is written on pin 13 to light the LED.

5 The delay function needs a constant or variable of how long in millisecond the delay
will be. This line prolongs for 1000 millisecond the LED at ON state.

6 Similar to line 4, pin 13 was set this time to low to turn off the LED.

7 This line is the same as line 5 and prolongs the LED at OFF state for 1 second.

G. What to expect You have just written a sketch and programmed the Arduino Uno board to cause
both the external LED and built-in LED connected to pin 13 to blink. You should see the

17 LED blink when the board is connected to the PC by a USB cable or the board is connected to a
DC supply.

H. Summary

In this exercise, you have learned how to create .pde files or sketches using the Arduino IDE. You
have also learned how to access and control – specifically, turn ON and OFF –an LED connected to
pin 13 of the Arduino Uno Board.

EXERCICES

1. Create a pde file or sketches using the arduino IDE that will 3 leds in shifting from:
a. right to left
b. right to left then left to right

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 14
Experiment 2 – Basic Digital Output: Dancing LED
operation
A. Objectives
This exercise aims to further expand the student’s knowledge with the basic input/output operation of
the Arduino Uno board, and Integrated Development Environment (IDE) by developing the program
learned for enabling an LED to blink. In the end of this exercise, the student should have learned how
to produce outputs.
B. Questions
• In circuit theory, how is a tact switch represented when it is pressed and not?
• What function enables you to read the input from a pin?
• What function enables you to program efficiently the lighting up of several LED’s?
C. Materials Needed
• 1 x Arduino Uno board and USB cable
• 4 x 5mm/3mm Red LEDs
• 4 x 470Ω resistors and 1 x 10kΩ resistor
• 1 x breadboard
• 1 x tact switch or toggle switch (which ever is available or simply a pull-up wire)

D. Instructions to File Management

1. Open the previous sketch.

To do this, select File > Open… or the button located in the Arduino program. Navigate to where you
saved the .pde file, select it and click Open. 2. Create a copy of this sketch and rename it to
correspond to this new exercise by selecting File > Save As…. By doing this you should now have a
new .pde file that contains the code you made during the previous exercise.

E. Instructions to programming the dancing LED’s.

1. Modify the existing code so that instead of only 1 pin, 4 pins are configured to accommodate the 4
LEDs. Note that you have two main functions so choose wisely where you put the initialization and
working program.

2. Create the part of the program that will produce the desired output. Be sure to understand the Light
Emitting Diode polarities.

3. Make the program that would perform the dancing LED’s from the first to the Last and from the
last to the First

4. Once you are done writing the code, save your file and compile it.

F. Test your program


1. Construct the circuit using the Arduino Uno, Breadboard, LEDs, resistors and jumpers.

2. Connect the board to the PC using the USB cable. Upload the sketch saved earlier and check if the
program works. The LEDs should behave as previously described in Step 3 of the instructions in
programming the dancing LED’s. Note that the board should not perform the blinking LED exercise
anymore since it was overwritten; otherwise, the new program wasn’t uploaded properly. Refer to the
previous the example.

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 15
G. What to expect
You have just written a sketch and programmed the Arduino Uno board to create dancing LEDs. You
should see the LEDs turn ON from the first to the last and from the last to the first.
H. Summary
In this exercise, you have learned how to create more complicated sketches using the Arduino IDE.
You have also learned how to access and control – specifically, create a running light using the
Arduino Uno Board.

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 16
Experiment 3 – Basic Digital Output: One Way Traffic
Light
Objective
Design and implementation of One-way traffic light system using Arduino Uno

 Experiment component:

 Red , Green , Yellow lamp: 4 for each type


 220Ωresistor: 3
 Breadboard & Jumper wires
 Arduino Uno

 Circuit diagram.

 Procedures:
 Connect the circuit as shown by the above circuit
 Connect PC into microcontroller through USB cable
 Type the following program into the personal computer
 Compile and download the program codes into the arduino board

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 17
 Program code:

int redpin1=1;
int greenpin1=2;
int yellowpin1=3;

void setup()
{
pinMode(redpin1, OUTPUT);
pinMode(greenpin1,OUTPUT);
pinMode(yellowpin1,OUTPUT);
}
void loop()
{
digitalWrite(redpin1, HIGH);
delay(5000);
for(int i=0;i<30;i++){
digitalWrite(yellowpin1,HIGH);
delay(50);
digitalWrite(yellowpin1,LOW);
delay(50);
}
digitalWrite(redpin1, LOW);
digitalWrite(greenpin1, HIGH);
delay(5000);
for(int i=0;i<30;i++){
digitalWrite(yellowpin1,HIGH);
delay(50);
digitalWrite(yellowpin1,LOW);
delay(50);
}
digitalWrite(greenpin1, LOW);
}

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 18
Experiment NO4_Basic Digital Input: Reading
Pushbutton
Button

Pushbuttons or switches connect two points in a circuit when you press them. This
example turns on the built-in LED on pin 13 when you press the button.

Hardware

 Arduino or Genuino Board


 Momentary button or Switch
 10K ohm resistor
 hook-up wires
 breadboard

Circuit

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 19
Connect three wires to the board. The first two, red and black, connect to the two long
vertical rows on the side of the breadboard to provide access to the 5 volt supply and
ground. The third wire goes from digital pin 2 to one leg of the pushbutton. That same
leg of the button connects through a pull-down resistor (here 10K ohm) to ground. The
other leg of the button connects to the 5 volt supply.

When the pushbutton is open (unpressed) there is no connection between the two legs
of the pushbutton, so the pin is connected to ground (through the pull-down resistor)
and we read a LOW. When the button is closed (pressed), it makes a connection
between its two legs, connecting the pin to 5 volts, so that we read a HIGH.

You can also wire this circuit the opposite way, with a pullup resistor keeping the input
HIGH, and going LOW when the button is pressed. If so, the behavior of the sketch
will be reversed, with the LED normally on and turning off when you press the button.

If you disconnect the digital I/O pin from everything, the LED may blink erratically.
This is because the input is "floating" - that is, it will randomly return either HIGH or
LOW. That's why you need a pull-up or pull-down resistor in the circuit.

Code
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// variables will change:


int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:


if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 20
Experiment NO5_Basic Digital Input: State
Change Detection (Edge Detection) for
pushbuttons
Once you've got a pushbutton working, you often want to do some action based on how many
times the button is pushed. To do this, you need to know when the button changes state from off
to on, and count how many times this change of state happens. This is called state change
detection or edge detection. In this tutorial we learn how to check the state change, we send a
message to the Serial Monitor with the relevant information and we count four state changes to
turn on and off an LED.

Hardware Required

 Arduino or Genuino Board


 momentary button or switch
 10k ohm resistor
 hook-up wires
 breadboard

Circuit

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 21
Connect three wires to the board. The first goes from one leg of the pushbutton through a pull-
down resistor (here 10k ohm) 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 (here pin 2) which reads the
button's state.

When the pushbutton is open (unpressed) there is no connection between the two legs of the
pushbutton, so the pin is connected to ground (through the pull-down resistor) and we read a
LOW. When the button is closed (pressed), it makes a connection between its two legs,
connecting the pin to voltage, so that we read a HIGH. (The pin is still connected to ground, but
the resistor resists the flow of current, so the path of least resistance is to +5V.)

If you disconnect the digital I/O pin from everything, the LED may blink erratically. This is
because the input is "floating" - that is, not connected to either voltage or ground. It will more or
less randomly return either HIGH or LOW. That's why you need a pull-down resistor in the
circuit.

Code

The sketch below continually reads the button's state. It then compares the button's state to its state
the last time through the main loop. If the current button state is different from the last button state
and the current button state is high, then the button changed from off to on. The sketch then
increments a button push counter.

The sketch also checks the button push counter's value, and if it's an even multiple of four, it turns
the LED on pin 13 ON. Otherwise, it turns it off.

const int buttonPin = 2; // the pin that the pushbutton is attached to


const int ledPin = 13; // the pin that the LED is attached to

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 22
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}

void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);

// compare the buttonState to its previous state


if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the
loop
lastButtonState = buttonState;

// turns on the LED every four button pushes by checking the modulo of
the
// button push counter. the modulo function gives you the remainder of
the
// division of two numbers:
if (buttonPushCounter % 4 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 23
}

https://www.arduino.cc/en/Tutorial/StateChangeDetection

Experiment NO6_Basic Serial Port


Output
Introduction

The Arduino Uno board is capable of serial communication used for communication between the
Arduino board and a computer or other devices. The Uno has a single serial port (also known as a
UART or USART): Serial. It communicates on digital pins 0 (RX) and 1 (TX) as well as with the
computer via USB. Thus, if you use these functions, you cannot also use pins 0 and 1 for digital input
or output.

You can use the Arduino environment's built-in serial monitor to communicate with an Arduino
board. Click the serial monitor button in the toolbar and select the same baud rate used in the call to
begin().

Functions

begin()

Description

Sets the data rate in bits per second (baud) for serial data transmission. For communicating with the
computer, use one of these rates: 300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600,
or 115200.

Syntax

Serial.begin(speed)

println()

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 24
Description

Prints data to the serial port as human-readable ASCII text followed by a carriage return character
(ASCII 13, or '\r') and a newline character (ASCII 10, or '\n'). This command takes the same forms as
Serial.print().

Syntax

Serial.println(val) Serial.println(val, format)

Parameters

val: the value to print - any data type

Format: specifies the number base (for integral data types) or number of decimal places (for floating
point types)

Returns

Byte

println () will return the number of bytes written, though reading that number is optional

Example:
void setup()

// open the serial port at 9600 bps

Serial.begin(9600);

void loop()

println(“Hello World”);

delay(1000);

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 25
Experiment NO7 – Basic Serial Port
Input/Output: “Hello World”

A. Objectives
This exercise aims for the student to learn how to use the serial feature of the Arduino Board and IDE.
As such, the exercise will also serve as an introductory for the students on using the serial to output
symbols and texts based on a program that later will be used to read data values from sensors. By the
end of this exercise, you should be able to output a message onto the serial monitor of the Arduino
IDE.
B. Questions
• How does the Arduino Board to communicate with a PC?
• What are the functions usually used in a serial communication?
• What is the necessary initialization for serial communication to take place?
C. Materials Needed
• 1 x Arduino Uno board and USB cable
D. Introduction to Arduino’s Serial COM
The Arduino Uno board can communicate with a PC, another Arduino or other microcontrollers. The
board’s microcontroller chip, ATmega328, provides the Universal Asynchronous
Receiver/Transmitter (UART) TTL (5V) serial communication which is accessed by digital pins 0
(RX) and 1 (TX) to communicate with other devices.

In addition, the ATmega8U2 chip on the board also channels this serial communication over USB that
appears as a COM port for the board to communicate with the computer. This method is done by
connecting the Arduino Uno Board via USB cable.

The Arduino IDE has a serial monitor which can be used to send to and from the Arduino Uno board
textual data.

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 26
The serial communication through USB is manipulated by functions in a sketch. Note that a
Serial.begin(speed) function is needed in the initialization. The speed is matched with that found in
the serial monitor of the Arduino IDE which is usually or by default 9600.

To access the serial monitor, just click the button right of the upload to I/O device button. By doing
this, the serial communication is activated with a given reading speed.

Functions such as Serial.read(), Serial.available(), Serial.write() and Serial.print() are used for
reading and writing in a serial communication system. Serial functions read inputs and produce
outputs in bytes. For example, the Serial.available() function returns the number of bytes available to
read. The serial.read() outputs the first byte of incoming data.

The serial.write() outputs series of byte data but most of the time, users wants these data translated to
or stay in readable text which is why Serial.print() is used more as it outputs them in ASCII text.
Refer to the Appendix for explanations on these and other serial functions.

E. Instructions to programming “Hello World!”

1. Open Arduino, and create first the part that would read if there are any inputs by the user in the
serial monitor.

2. Continue the code such that upon receiving exactly 0 ready to be read inputs from keyboard, a
“Hello World!” sentence should appear on the serial monitor as output other “you press a long key
please try again”. Note that the program should not output anything if the condition is not met.

3. Once you are done writing the code, save your file and compile it.

F. Test your program

1. Connect the Arduino board to the PC using the USB cable. 2. Upload the sketch saved earlier and
check if the program worked. Open the Serial Monitor by clicking the icon right of the upload icon in
the IDE. Type 3 characters in the new window and see if it would output a “Hello World”. Type more
or less number of characters and the program should remain idle.

G. What to expect

You have just written a sketch that programmed the Arduino Uno board to use the serial monitor to
allow the board and PC to communicate. Typing 0 in the serial Monitor should output a “Hello
World!” otherwise the program “you press a long key please try again”.

H. Summary in this exercise,

You have learned how to communicate the Arduino Board PC. You also understood how to use the
IDE’s serial monitor to read and write data which is an important skill in data analysis or
microcontrollers.

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 27
Experiment 8 – Basic Serial Port
Input/Output: “Switch statement with serial input”
A. Objectives
This exercise aims for the student to learn how to use the serial feature of the Arduino Board and IDE. As
such, the exercise will also serve as an introductory for the students on using the Switch statement with
serial input. The switch statement allows you to choose from among a set of discrete values of a
variable. By the end of this exercise, you should be able to command the switch ON and OFF the devices
onto the serial monitor of the Arduino IDE.
B. Questions
• How does the Arduino Board to communicate with a PC?
• What are the functions usually used in a serial communication?
• What is the necessary initialization for serial communication to take place?
C. Materials Needed
 1 x Arduino Uno board and USB cable
 6xLEDs
 6x1K resistors
 1xBread board
 Jumpers

E. Instructions to programming “Hello World!”

1. Open Arduino, and create first the part that would read if there are any inputs by the user in the
serial monitor.

2. Write an Arduino program to take input from the serial port and perform the following

Input character from serial port Control


Action

‘A’ LED 1 ON

‘B’ LED 2 ON

‘C’ LED3 ON

D’ LED 4 ON

E LED5 ON

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 28
Any other character All LED OFF

3. Once you are done writing the code, save your file and compile it.

F. Test your program

1. Connect the Arduino board to the PC using the USB cable.

2. Upload the sketch saved earlier and check if the program worked. Open the Serial Monitor by
clicking the icon right of the upload icon in the IDE. Type

3 characters in the new window and see if it would output a “Hello World”. Type more or less
number of characters and the program should remain idle.

G. What to expect

You have just written a sketch that programmed the Arduino Uno board to use the serial monitor to
allow the board and PC to communicate. Typing

Input character from serial port Control


Action

‘A’ LED 1 ON

‘B’ LED 2 ON

‘C’ LED3 ON

D’ LED 4 ON

E LED5 ON

Any other character All LED OFF

H. Summary in this exercise,

You have learned how to communicate the Arduino Board PC. You also understood how to use the
IDE’s serial monitor to read and write data which is an important skill in data analysis or
microcontrollers.

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 29
Experiment 9 – Analog Input/Output:
Reading Analog Pins and Converting the Input to a
Voltage
You Will Need

1. Potentiometer (any resistance range will work)


2. Jumper Wires – at least 3
3. Persian Rug

Step-by-Step Instructions

1. Place the potentiometer into your breadboard.


2. Run a jumper wire from the 5-Volt pin of the Arduino to either one of the outside pins of
the potentiometer.
3. Run another jumper wire from one of the ground pins on the Arduino (labeled GND) to the
other outside pin of the potentiometer.
4. Run the final jumper wire from pin A0 on the Arduino to the middle pin of the
potentiometer.
5. Plug the Arduino into your computer.
6. Open up the Arduino IDE.
7. Open the sketch for this section.
8. Click the Verify button on the top left side of the screen. It will turn orange and then back to
blue once it has finished.
9. Click the Upload button (next to the Verify button). It will turn orange and then back to blue
once it has finished.
10. On the menu bar, go to Tools > Serial Monitor – this will open the Serial Monitor window –
you should see numbers rolling down this screen.
11. Now adjust the knob of your potentiometer and watch the serial monitor window, the
numbers should adjust between 0 and 5.

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 30
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

// the loop routine runs over and over again forever:


void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);// Convert the analog reading (which //goes from 0
- 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0); // print out the value you read:
Serial.println(voltage);
}

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 31
Experiment 10 – Analog Input: Light
Level Indicator with Light Dependent Resistor

A. Objectives
This exercise aims for the student to apply the basic I/O methods learned from the previous
experiments with the ADC. As such, the exercise will also serve as an introductory to Analog-to-
Digital Conversion of the Arduino Uno. Here, a Light level indicator will be made with an LDR. By
the end of this exercise, the student should have a good grasp of the I/O features of the board and
understand the ADC function of the Analog Input pins.
B. Questions
• How does the LDR Work? Does the resistance increase as it receives more light?
• What is the necessary function to read analog inputs?
• How does the ADC of the analog input pins process the voltages it receives?
• What estimated range values is needed for the program to determine the light level?
C. Materials Needed
• 1 x Arduino Uno board and USB cable
• 4 x 5mm/3mm LEDs • 4 x 470Ω resistors and 1 x 2.2kΩ (= R) resistor
• 1 x breadboard • 1 x Light Dependent Resistor (LDR)

D. Introduction to Arduino Uno’s Analog-to-Digital Converter


The Arduino Uno board has exactly 6 pins that can read analog values. With the analog pins, you
process values with more significant digits that are needed in applications.

Now, the analog input pins of the board are connected to an Analog-to-Digital Converter that converts
the 0-5V to a range of 0-1023 unit. This means that around 4.88mV (i.e. 5V/1024) is equivalent to 1
unit. The voltage range to be compared can also be changed by using the analogReference(type)
function which is explained in the Appendix. As a visual explanation

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 32
Figure 19: Diagram showing how a 1.25V input to the analog pin is read as 256 units

When a 1.25V source is connected to one analog input pin, the ADC converts it in unit which is by
the conversion ratio mentioned equal to 256 (=1.25V x 1024 / 5V). Now, to activate the ADC of the
analog input pins, it is necessary to use the analogRead(pin) function. Look at the appendix for an
explanation regarding this.

E. Instructions to Programming the LED lamp using an LDR as a switch

1. Open Arduino, and create first the part that would read the analog input coming from one of the
analog input pins.

2. Now, construct a conditional statement for an Arduino program to read the LDR sensor and
display it on the serial monitor. Switch on LED1 when light is high and switch off the LED
when light is low. Note to study the behavior of an LDR and determine the relationship of the
resistance and the light it receives. Expect some calibrating to be done. Also, remember that the LDR is
connected to the built- in 5V source. The less the LDR’s resistance the higher the voltage the analog
pins receive.

3. Once you are done writing the code, save your file and compile it.

F. Test your program

1. Construct the circuit using the Arduino Uno, breadboard, LED, resistors, and LDR.

2. Connect the board to the PC using the USB cable. Upload the sketch saved earlier and check if the
program works. The 4 LEDs should light up in a dark room while none in a well lit room. If it did not
work, some calibration to the ranges specified might be needed.

G. What to expect

You have just written a sketch and programmed the Arduino Uno board to control an LED light level
indicator based on the amount of light the sensor (LDR) receives. After covering the LDR with an
object or your hand, you should see the 4 LEDs turn ON and upon uncovering, the number of lit
LED’s decrease.

H. Summary in this exercise,

you have learned how to use the Arduino Board and IDE to control outputs based on results from
a sensor. You have also learned how the ADC feature of the analog input pins works by
controlling the number of LED’s turned ON based on the amount of light the LDR r

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 33
Experiment 11 – Analog Output:
Pulse width modulation
1. Theory

What’sPWM?

Pulse Width Modulation, or PWM, is a technique for getting analog results with digital
means. Digital control is used to create a square wave, a signal switched between on and
off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by
changing the portion of the time the signal spends on versus the time that the signal spends off.
The duration of "on time" is called the pulse width. To get varying analog values, you
change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an
LED for example, the result is as if the signal is a steady voltage between 0 and 5v controlling
the brightness of the LED.

In the graphic below, the green lines represent a regular time period. This duration or
period is the inverse of the PWM frequency. In other words, with Arduino's PWM frequency at
about 500Hz, the green lines would measure 2 milliseconds each. A call to analogWrite() is on
a scale of 0 - 255, such that analogWrite(255) requests a 100% duty cycle (always on), and
analogWrite(127) is a 50% duty cycle (on half the time) for example.

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 34
For the Arduino, you write a value from 0 to 255 on a PWM pin, and the Arduino library will
cause the pin to output a PWM signal whose on time is in proportion to the value written.

When it comes time for us to actually write an output voltage, the 0-255 value lacks
meaning. What we want is many cases is a voltage. For our purposes, we will assume the
Arduino is running at Vcc = 5 volts. In that case, a value of 255 will also be 5 volts. We can
then easily convert the desired voltage to the digital value needed using simple division. We
first divide the voltage we want by the 5 volts maximum. That gives us the percentage of our
PWM signal. We then multiply this percentage by 255 to give us our pin value. Here is the
formula:

Pin Value (0-255) = 255 * (AnalogWrite / 5);

Arduino use analogWrite()

analogWrite() :Writes an analog value (PWM wave) to a pin. Can be used to light a LED
at varying brightnesses or drive a motor at various speeds. After a call to analogWrite(), the
pin will generate a steady square wave of the specified duty cycle until the next call to
analogWrite() (or a call to digitalRead() or digitalWrite() on the same pin). The frequency of the
PWM signal is approximately 490 Hz.

On most Arduino boards (those with the ATmega168 or ATmega328), this function works on
pins 3, 5, 6, 9, 10, and 11. On the Arduino Mega, it works on pins 2 through 13. Older Arduino
boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.The Arduino Due
supports analogWrite() on pins 2 through 13, plus pins DAC0 and DAC1. Unlike the PWM
pins, DAC0 and DAC1 are Digital to Analog converters, and act as true analog outputs.You do
not need to call pinMode() to set the pin as an output before calling analogWrite().The
analogWrite function has nothing to do with the analog pins or the analogRead function.
Syntax analogWrite(pin, value)

2. Parameters
pin: the pin to write to.
value: the duty cycle: between 0 (always off) and 255 (always on). Notes and Known Issues
The PWM outputs generated on pins 5 and 6 will have higher-than-expected duty cycles. This is
because of interactions with the millis() and delay() functions, which share the same internal
timer used to generate those PWM outputs. This will be noticed mostly on low duty-cycle
settings (e.g 0 - 10) and may result in a value of 0 not fully turning off the output on pins 5 and
6.

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 35
EXPERIMENT REALIZATION

3. Objective

 Converting digital waveform into analog waveform using pulse width modulation techniques
 Lighting light emitting diode at varying brightness

4. Experiment component:

 1 x 220Ω resistor
 1 x LED
 1 x Breadboard
 1x Arduino Uno
 Digital multimeter
 Connecting wires

5. CIRCUIT DIAGRAM.

6. Procedures

 Connect the circuit as shown by the above circuit diagram

 Connect Arduino into computer through USB cable

 Type the following program into the PC

 Compile and download the program into the microcontroller

7. Program code:

int brightness = 0; //define original value of brightness,

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 36
int fadeAmount = 5; //define the value is the amount of brightness //variations’

void setup() {
pinMode(6, OUTPUT);// set pin9 is output
}

void loop() {
for(brightness = 0; brightness<=256; brightness++) {
analogWrite(6, brightness);
brightness = brightness + fadeAmount;
delay(500);
}
for (brightness = 255; brightness>=0; brightness-- )
{
analogWrite(6, brightness);
brightness = brightness - fadeAmount;
delay(1000);
}
}

Experiment 12 – Analog I/O:


Controlling the dc motor speed

1. Theory:

The transistor acts like a switch, controlling the power to the motor, Arduino pin 3 is used to
turn the transistor on and off and is given the name 'motorPin' in the sketch.

The small DC motor, is likely to use more power than an Arduino digital output can handle
directly. If we tried to connect the motor straight to an Arduino pin, there is a good chance that
it could damage the Arduino. A small transistor like the FET can be used as a switch that uses
just a little current from the Arduino digital output to control the much bigger current of the
motor.

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 37
The transistor has three leads. Most of the electricity flows from the Collector to the Emitter, but
this will only happen if a small amount is flowing into the Base connection. This small current
is supplied by the Arduino digital output.

EXPERIMENT REALIZATION

2. Objective
 In this lesson, you will learn how to control a small DC motor using an Arduino and a transistor.

3. Components required
 A small dc motor
 Arduino Uno
 Breadboard
 FET transistor
 Diode
 Variable resistor 10km

4. Circuit diagram

5. Procedures:

 Connect the circuit as shown by the above circuit diagram


 Connect the arduino on the personal computer
 Type the following program into the personal computer

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 38
 Download the circuit into the microcontroller

6. Program code:

void setup()
{
pinMode(6,OUTPUT);
}
V oid loop()
{

for (int speed=50; speed<=255; speed++)


{

analogWrite(6,speed);
delay(1000);
speed=speed+20;
}
}

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 39
Experiment 13 – Analog Input::
Movement detection system
1. Objective
1.The main objective of this system is to design and implement a system for detecting the
movement of any person near by this system.
2. design a home security system

2. Component Requirement

 PIR Sensor:1
 Breadboard:1
 Arduino Uno:1
 LED:1
 Buzer:1
 Connecting wires
 220 resistor:1

3. Circuit diagram
.

4. Procedures

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 40
1.Connect the circuit as shown by the above circuit diagram
2.Connect microcontroller into your PC
3.Type the following Program into computer connected on arduino
4.Compile and download the program into microcontroller

5. Program code:

#define pirpin 2
#define buzzer 3
#define led 4

Void setup()
{
Serial.begin(9600);
pinMode(pirpin,INPUT);
pinMode(buzzer,OUTPUT);
pinMode(led,OUTPUT);

}
Void loop()
{
PIRSensor();
}
Void PIRSensor()
{
If(digitalRead(pirpin)==HIGH)
{
serial.println(“Motion detected”);
digitalWrite(buzzer,HIGH);
digitalWrite(led,HIGH);
delay(50);
}
else if(digitalRead(pirpin)==LOW)
{
serial.println(“Motion ended”);
digitalWrite(buzzer,LOW);
digitalWrite(led,LOW);
delay(50);
}
}
}

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 41
6. Theory:

First of all lets, have a little introduction about the basics and working of the PIR sensor. PIR
sensors are in fact a Passive Infrared Sensor. PIR sensors are actually electronic sensors and
they are used for motion detection. They are also used to detect the Infrared waves emitting
from a particular object.PIR sensors are widely used in motion detection projects now a days.
Since my today experiment is about interfacing of PIR sensor with Arduino micro controller.
Before going to that it is necessary that we should first understand the construction and
working principle of PIR sensor.

Construction of PIR sensor


The construction of PIR sensor is very simple and easy to understand. In the bottom or you
can say in the core of the PIR sensor we have a set of sensors which are made of pyroelectric
materials. The properties of this material is that when this material is exposed to heat then, it
generates energy. Different methods are used to to measure this energy generated by PIR
sensor. On the top of the PIR sensor and above the internal sensors we have a small screen
through which infrared radiations enters into sensor. When these radiations falls on the
pyroelectric material then it generates energy. Generally the size of this sensor is very small
and it is available in the form of thin film in market. Many other materials are also used in
collaboration with pyroelectric material like galliam nitrite and cesium nitrate and all these
materials take the form of an small integrated circuit.

Operating of PIR sensor


The modern studies in the field of quantum physics tells us the fact each and every object
when it is placed at a temperature above absolute zero, emits some energy in the form of heat
and this heat energy is in fact the form of infrared radiations. So an other question comes
into mind that why our eyes can’t see these waves? It is because that these waves have
infrared wavelengths and his wavelength is invisible to human eyes. if you want to detect
these waves then, you have to design a proper electronic circuit.
If you see closely the name of PIR sensor which is Passive Infrared Sensor. Passive elements
are those elements that don’t generate their own voltages or energy. They just only measures
things. So we can say that this sensor is a passive infrared sensor and it doesn’t generate
anything by itself. It is only capable to measure the rediations emitted by other objects around
it. It measures those raditions and do some desired calculations on them.
Interfacing with arduino
PIR sensor have total 3 pins. The configuration of each pin is shown in the image given
below:
1. Pin#1 is of supply pin and it is used to connect +5 DC voltages.
2. Pin#2 is of output pin and this pin is used to collect the output signal which is collected by
PIR sensor.
3. Pin#3 is marked as GND pin. This pin is used to provide ground to internal circuit of PIR
sensor.

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 42
Experiment 14 – Analog Input: LIGHT AND
DARK DETECTION SYSTEM

1. Theory:
What’sphotoresistor?

Photoresistor, also known as light pipes, common production materials is cadmium sulfide,
There are also selenium, aluminum sulfide, lead sulfide and bismuth sulfide material. these
production materials having characteristics in light of a specific wavelength, its resistance
decreases rapidly. This is due to the light generated carriers are involved in the electrical
conductivity, under the applied electric field drift motion, so that the photosensitive resistor
rapid decline.

EXPERIMENT REALIZATION

2. Objective
1. Turning on the light emitting diode when the darkness is detected and off if the light is detected.
2. Lamp control based light and dark detection

3. Component required

 Photoresistor : 1

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 43
 Buzzer : 1
 10K resistor : 1
 220Ωresistor : 1
 Breadboard & Jumper wires

4. Circuit diagram.

5. Procedures:
 Connect the circuit as shown by the above circuit
 Connect PC into microcontroller through USB cable
 Type the following program into the personal computer
 Compile and download the program codes into the arduino board

6. Program code:

Int photocellpin=2;

Int ledpin=12;

Int val=0;

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 44
void setup()
{

pinMode(ledPin, OUTPUT); //set ledPin output


}
void loop()
{

val = analogRead(photocellPin); //get the value from sensor


if(val<=512)
{

//512=2.5V, if want the sensor be more sensitive, increase the number, or lese low //the number.
digitalWrite(ledPin, HIGH);
//when the value of val is less than 512(2.5V), light up led lamp
}
else{

digitalWrite(ledPin, LOW);
}
}

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 45
Experiment 15 – Analog Input::
TEMPERATURE CONTROL SYSTEM

1. Theory:

What’s temperature sensor?

The temperature sensor is that use substances of various physical properties with temperature
variation of the sensor and let the temperature converted to electricity. These regularly change
the physical properties of the main body temperature sensor is a core part of the temperature
measuring instruments, and a wide variety. In accordance with the measurement method is
divided into contact and non-contact two major categories, In accordance with the
characteristics of sensor materials and electronic components into the thermal resistance and
thermocouple.
Used in this experiment is the LM35 temperature sensor.

2. Working principle

LM35 temperature sensor output voltage linear relationship between the Celsius temperature
scale,0 ℃, output is 0V, for every 1℃increases in output voltage of 10mV
LM35 pin diagram is as follows

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 46
temperature sensor side is flat, and the other side is semicircular. Flat face of our own, the
leftmost VCC pin (connected to +5 v), the middle of the GND pin VOUT (voltage value output
pin, then the analog pins on the board), and the rightmost pin (connected board GND). Three
pins, respectively, then you can use.

EXPERIMENT REALIZATION

3. Objective

1. Detecting the temperature from the environment


2. Display temperature on personal computer.

4. Experiment component

 LM35 temperature sensor module*1

 Breadboard & jumper wire few


 Arduino uno

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 47
5. CIRCUIT DIAGRAM

6. Procedures:
 Connect the circuit as shown by the above circuit
 Connect PC into microcontroller through USB cable
 Type the following program into the personal computer
 Compile and download the program codes into the arduino board

Program code:

int potPin = 0 ;//define pin0 connect with LM35

void setup()
{

Serial.begin(9600);

void loop()

int val;
int dat;

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 48
val = analogRead(potPin);
dat = (125*val)>>8 ; // Temperature calculation formula
Serial.print("Tep : ") ; //print “Tep”means temperature
Serial.print(dat) ; // print the value of dat
Serial.println("C"); //print “C”means degree
delay(500);//delay 0.5s

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 49
Experiment 16 – Analog Input::
DISPLAYING MESSAGE ON LIQUID CRYSTAL DISPLAY

 Theory

What’s1602LCD?

Nowadays 1602LCD is application of very wide range. The initial 1602 LCD used HD44780
controller. But now various manufacturers basically adopt compatible IC with their 1602
module. Their characteristics are basically the same.

1602LCD

Display capacity: 16x2 characters;


Chip operating voltage: 4.5V~5.5V; Operating current: 2.0mA(5.0V); Best operating voltage:
5.0V; Character size: 2.95x4.35( WxH ) mm.

Interface pin definition

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 50
number symbol states Number Symbol States

1 VSS GND 9 D2 Date I/O

2 VDD VCC 10 D3 Date I/O

3 VL VO 11 D4 Date I/O

4 RS (V/L) 12 D5 Date I/O

5 R/W Read/write(H/L) 13 D6 Date I/O

6 E enable 14 D7 Date I/O

7 D0 Date I/O 15 BLA Backlight


anode

8 D1 Date I/O 16 BLK Backlight


cathode

1. Two sets of power supply, a set of modules, the other one is the power of the backlight, generally
using the 5V power supply.
2. VL is used to adjust the contrast. It connected in series the potentiometer is not greater than
a 5KΩ.This experimental used one 1KΩ of resistor. There are to high potential set contrast.
connection and low potential connection. It connected in series then 1KΩ resistor connected to
GND.

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 51
Basic Operation

D0~D7=status
Read status Input RS=L, R/W=H, E=H Output word

Write command Input RS=L, R/W=L, Output none


D0~D7=command code, E=
high pulse

Read data Input RS=H, R/W=H, E=H Output D0~D7=data

Write data Input RS=H, R/W=L, D0~D7=data, Output none


E= high pulse

The LCDs have a parallel interface, meaning that the microcontroller has to manipulate several
interface pins at once to control the display. The interface consists of the following pins:

A register select (RS) pin that controls where in the LCD's memory you're writing data to. You
can select either the data register, which holds what goes on the screen, or an instruction
register, which is where the LCD's controller looks for instructions on what to do next.

A Read/Write (R/W) pin that selects reading mode or writing mode . An enable pin that enables
writing to the registers 8 data pins (D0 -D7). The states of these pins (high or low) are the bits
that you're writing to a register when you write, or the values you're reading when you read.

There's also a display constrast pin (Vo), power supply pins (+5V and Gnd) and LED Backlight
(Bklt+ and BKlt-) pins that you can use to power the LCD, control the display contrast, and turn
on and off the LED backlight, respectively.

The process of controlling the display involves putting the data that form the image of what
you want to display into the data registers, then putting instructions in the instruction register.
The LiquidCrystal Library simplifies this for you so you don't need to know the low-level
instructions. The Hitachi-compatible LCDs can be controlled in two modes: 4-bit or 8-bit.

The 4-bit mode requires seven I/O pins from the Arduino, while the 8-bit mode requires 11 pins.
For displaying text on the screen, you can do most everything in 4-bit mode, so example shows
how to control a 2x16 LCD in 4-bit mode.

EXPERIMENT REALIZATION

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 52
 Objective
 Displaying message on liquid crystal display using arduino board.

 Component required
 Breadboard
 Arduino uno
 LCD
 Connecting wires
 10k variable resistor
 Circuit diagram

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 53
 Procedures:

 Circuit

To wire your LCD screen to your Arduino, connect the following pins:

LCD RS pin to digital pin 12

LCD Enable pin to digital pin 11

LCD D4 pin to digital pin 5

LCD D5 pin to digital pin 4

LCD D6 pin to digital pin 3

LCD D7 pin to digital pin 2


Additionally, wire a 10K pot to +5V and GND, with it's wiper (output) to LCD screens VO pin
(pin3).
 Connect the circuit as shown by the above circuit diagram
 Connect microcontroller into your PC
 Type the following Program into computer connected on arduino
 Compile and download the program into microcontroller
 Adjust the potentiometer for adjusting display brightness
 Program code:

#include <LiquidCrystal.h>
LibraryLiquidCrystal lcd(12, 11, 5, 4, 3, 2); //Attach LCDs and Arduino pin comunnication
int time=0; //Entire variable declaration(time)
void setup()//setup section
{
lcd.begin(16, 2); //LCD begins. dimension: 16x2(Coluns x Rows)
lcd.setCursor(0, 0); // Positions the cursor in the first column (0) and the //firt row (1) at LCD
lcd.print("LiquidCrystal.h"); //LCD write comand"LiquidCrystal.h"
lcd.setCursor(0, 1); // Positions the cursor in the first column (0) and the second row (1) at
LCD
lcd.print("Microcontroller Lab "); // LCD write command "GarageLab"
delay(2000);

}
void loop()
{

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 54
lcd.clear();
lcd.setCursor(0, 1); // Positions the cursor on the fourteenth column (13) and the second line
(1) LCD
lcd.print(“The count value is equal to=:”);
lcd.print(); // Write the current value of the count variable in the LCD
delay(1000); // Waits for 1 second
seconds++; // Increment count variable
if(time == 60)
{
time = 0;
}
}
EXERCISE:

2.Develop the hardware circuit diagram and software code program for reading the different
input voltages from potentiometer which is connected on arduino-pin and then after display
those voltages on LCD display.

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 55
Experiment 16: Use of Basic Sensors_distance
measurement

 Theory:

Product features:

Ultrasonic ranging module HC - SR04 provides 2cm


- 400cm non-contact measurement function, the ranging accuracy can reach to 3mm. The
modules includes ultrasonic transmitters, receiver and control circuit. The basic principle of
work:

Using IO trigger for at least 10us high level signal,


The Module automatically sends eight 40 kHz and detect whether there is a pulse signal back.


IF the signal back, through high level , time of high output IO duration is the time from sending
ultrasonic to returning. Test distance = (high level time×velocity of sound (340M/S) /2

0V Ground
If you are sourcing a ultrasonic ranging module , the HC-SR04 is good choose . Its stable
performance and high ranging accuracy make it a popular module in electronic market .
Compared to the Shap IR ranging module , HC-SR04 is more inexpensive than it . But it has the
same ranging accuracy and longer ranging distance.

Specifications:


power supply :5V DC


quiescent current : <2mA

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 56

effectual angle: <15°


ranging distance : 2cm –500 cm


resolution : 0.3 cm

There are 4 pins out of the module : VCC , Trig, Echo, GND . So it's a very easy interface for
controller to use it ranging. The all process is : pull the Trig pin to high level for more than 10us
impulse , the module start ranging ; finish ranging , If you find an object in front , Echo pin will
be high level , and based on the different distance, it will take the different duration of high
level. So we can calculated the distance easily :

Distance = ((Duration of high level)*(Sonic :340m/s))/2

EXPERIMENT REALIZATION:

 Objective:
Measuring distance of any object with reference to the distance sensor and display this
temperature on personal computer.

 Component requirement
 Distance sensor
 Breadboard
 Connecting wires

 circuit diagram.

Procedures:

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 57
1.Connect the circuit as shown by the above circuit diagram
2.Connect microcontroller into your PC
3.Type the following Program into computer connected on arduino
4.Compile and download the program into microcontroller
5.place an object at different position relative to the sensor
6. Record the measured distance
7.By using the meter compare the measured values and recorded values

 Program code:

const int TrigPin = 2;


const int EchoPin = 3;
float cm;

void setup()

Serial.begin(9600);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);

}
void loop()
{
digitalWrite(TrigPin, LOW); //Low-high-low level sent a short time pulse to TrigPin
delayMicroseconds(2);

digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
cm = pulseIn(EchoPin, HIGH) / 58.0; //Echo time converted into cm
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(1000);
}

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 58
Experiment 17: Basic Use of
Actuators_SERVO MOTOR CONTROLL

 Theory:
Controlling a servo motor with an Arduino or other type of microcontroller is probably the
easiest way to get started in robotics, motion art, or any other reason you may have to make
your electronic project interact with the real world. Servos are very simple to interact with .

Servo motors are a specific type of motor, often used in hobby RC cars and planes, that rotate to
a specific angle when a corresponding signal is applied to the pulse pin. Servo motors are very
easy to program and very strong for their size. This makes them useful for a wide array of
applications. The internal components of a servo motor consist of a regular DC motor, which
does the actual work, a system of gears to increase the torque to the output shaft, and a circuit
board and sensors to control the movement of the motor.

The control signal is fairly simple compared to that of a stepper motor. It is just a pulse of
varying lengths. The length of the pulse corresponds to the angle the motor turns to. Typically a
pulse of 1.25 milliseconds causes the motor to rotate to 0 degrees and a pulse of 1.75
milliseconds turns it 180 degrees. Any length of pulse in between will rotate the servo shaft to
its corresponding angle. Some servos will turn more or less than 180 degrees, so you may need
to experiment.

Wiring:

To get started controlling a servo with your Arduino, you only need to connect three pins. There
are two pins for power and ground. For a small servo or just for testing, you can connect these
directly to the Arduino. If you are controlling a large servo motor, you might want to use an
external power source. Just remember to connect the ground from the external source to the
ground of the Arduino.

EXPERIMENT REALIZATION

 Objective
 Turning the shaft of servo motor at different angles by using arduino.
 Component Required
 Arduino

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 59
 Breadboard
 Connecting wires
 Servo motor

 Circuit diagram

 Procedures:
1.Connect the circuit as shown by the above circuit diagram
2.Connect microcontroller into your PC
3.Type the following Program into computer connected on arduino
4.Compile and download the program into microcontroller
5.Adjust the potentiometer for varying the position of the shaft of servo motor

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 60
Program code:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup()
{
myservo.attach(9); // In the setup we need to tell the object where the servo is connected.
}
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and
1023)
val = map(val, 0, 1023, 0, 180); /*We read the analog value to the variable val and by using the map function we map its value in a
range between 0 and 180. */

myservo.write(val);/* 
Now we simply send this value to the servo object and the servo will move to the target angle.*/
delay(15); // waits for the servo to get there
}

Student LAB Manual _ Prepared by Louis Bonheur ABIZEYE_IPRC TUMBA 2021


Page 61

You might also like