Professional Documents
Culture Documents
STUDENT LAB - Manual ETTMM601 PDF
STUDENT LAB - Manual ETTMM601 PDF
STUDENT LAB - Manual ETTMM601 PDF
PREPARED BY:
[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.
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].
[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.
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].
[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 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
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
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
[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].
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
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.
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
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.
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.
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.
Experiment component:
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
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);
}
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
Circuit
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
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);
Hardware Required
Circuit
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.
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);
// 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);
}
https://www.arduino.cc/en/Tutorial/StateChangeDetection
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()
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
Parameters
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()
Serial.begin(9600);
void loop()
println(“Hello World”);
delay(1000);
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.
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.
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.
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”.
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.
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
‘A’ LED 1 ON
‘B’ LED 2 ON
‘C’ LED3 ON
D’ LED 4 ON
E LED5 ON
3. Once you are done writing the code, save your file and compile it.
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
‘A’ LED 1 ON
‘B’ LED 2 ON
‘C’ LED3 ON
D’ LED 4 ON
E LED5 ON
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.
Step-by-Step Instructions
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)
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
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.
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.
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.
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
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.
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:
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.
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
7. Program code:
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);
}
}
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.
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:
6. Program code:
void setup()
{
pinMode(6,OUTPUT);
}
V oid loop()
{
analogWrite(6,speed);
delay(1000);
speed=speed+20;
}
}
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
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);
}
}
}
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.
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
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;
//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);
}
}
1. Theory:
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
EXPERIMENT REALIZATION
3. Objective
4. Experiment component
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:
void setup()
{
Serial.begin(9600);
void loop()
int val;
int dat;
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
3 VL VO 11 D4 Date I/O
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.
D0~D7=status
Read status Input RS=L, R/W=H, E=H Output word
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
Component required
Breadboard
Arduino uno
LCD
Connecting wires
10k variable resistor
Circuit diagram
Circuit
To wire your LCD screen to your Arduino, connect the following pins:
#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()
{
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.
Theory:
Product features:
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
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 :
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:
Program code:
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);
}
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
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
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
}