Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 40

ROBOTICS USING MICROCONTROLLER AND SENSORS

Digital and Analog Signals

ARDUINO MICROCONTROLLER

Arduino Uno Microcontroller Unit

1. Microprocessor (ATMEGA 328P)


Starting with the major part of the Arduino board, the microprocessor that act as the brain of your
microcontroller. This is where you will send the instructions on what your microcontroller will do.

2. USB Port
This is used both to supply power to your system and transmit and upload data from your computer.

3. Barrel Jack
It is a circular port in 2.1 mm diameter. This another way to supply power to your board using an AC to DC
adapter that has 6-20 Volts output.

4. Reset Button
It is used to restarts the system by manually pushing the reset button in your board.

5. Power Header
 Reset
 +3.3V
 +5V
 GND
This is used to supply enough power to external components of your Arduino project.

6. Digital Input/Output Pins


These pins are used to communicate with external devices connected with you board. Digital pins are used to
read or write digital data which is either HIGH or LOW. Digital input pins read digital inputs, whereas digital
output pins write digital output. This will be discussed in details in the succeeding topics.
 Digital Pin I/O 0 to Digital Pin I/O 13
 Pin A0 to A5 (Digital I/O Pin 14 to Digital I/O Pin 19)

7. Analog Input Pins


Compared with digital data which is either HIGH or LOW, analog data it covers all values in a specific range.
Analog pins reads digital input, write digital output but not analog output.
 Analog Input Pins A0 to A5

8. Analog Output Pins/PWM (Pulse Width Modulated) Pins


 Analog Output Pins ~ 3, ~5, ~6, ~9, ~10 and ~11

2|P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

ARDUINO INTERFACE DEVELOPMENT ENVIRONMENT


To open Arduino IDE just click the shortcut or where the file is saved then wait until it launch the IDE.

Arduino Interface Development Environment

1. Title Bar
 Shows the filename of the sketch. Filename refers to the name you used to identify your program.
 Title bar also contains the minimize, maximize and close button of the application

2. Menu Bar
 Contains all the functions and commands of the Arduino IDE

3. Toolbar

 Verify Button
Checks the code for any syntax error before uploading the program to your microcontroller. Syntax
error refers to the error in the source code that causes failure in executing the program. Once error
occurs, the programmer must debug the program. Debugging means finding, fixing or resolving any
errors in your program.
 Upload Button
This is used to transfer the program from your computer into Microcontroller after successfully verifying
the source code from any errors.
 New File Button
3|P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

Allows you to create a new file/new Arduino window


 Open Button
Used to retrieve a previous sketch
 Save Button
This allows you to save the current sketch in your computer.

4. Serial Monitor
 This opens up the serial monitor window

5. Status Bar
 This shows feedback after verification and compiling of the program.

CONNECTING THE ARDUINO MICROCONTROLLER UNIT TO A COMPUTER

Usin
g a Type A- B USB cable you can directly connect your Arduino board to your computer

SETTING UP THE ARDUINO MICROCONTROLLER


4|P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

SELECTING THE MICROCONTROLLER


Before creating your program, you need to select first the type of your Arduino board you are using. To do this, simply
click Tools > Board > Arduino/Genuino Uno board in the Menu Bar.

SELECTING THE PORT


Next, you need to select the port on where the microcontroller board will be connected. To do this, just click Tools >
Port/ COM5(Arduino/Genuino Uno) in the Menu Bar.

Note: The port number may vary depending on the available port provided by your computer.

BASIC ARDUINO CODES AND SYNTAX


5|P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

Arduino codes are case sensitive, meaning, small letters (lowercase) is different from capital letters (uppercase). Typing
a wrong case will result to syntax error. Similar with other languages, Arduino code should conforms the correct syntax,
a set of rules, format, statements, commands or declaration in a programming language. This will be discussed in the
succeeding topics.

It is also very important to remember that almost every line of codes in Arduino must end with a semicolon ( ; ) just like
with other programming languages which sometimes referred to as program terminator, which indicates the end of the
statement in a program. Failure to end with this may cause syntax error.

Upon launching the Arduino IDE, you will be prompted with the default sketch. Aruino sketch is divided into 2 functions
the void setup( ) and void loop( ). Functions are the section of computer codes that allows the computer to perform a
specific task.

1 void setup( ) {
2 // put your setup code here, to run once:
3
4 }
5
6 void loop( ) {
7 // put your main code here, to run repeatedly:
8
9 }

setup( ) FUNCTION

Once the sketch starts, it initially calls the setup( ) function. This only runs once to set up your program by initializing the
variables and values, pin modes, calling libraries, etc. This will be discussed further in the succeeding topics.

1 void setup( ) {
2 // put your setup code here, to run once:
3
4 }

The pair of open and close curly brackets { } in the sketch identifies a line of codes to be executed when a function is
called.

COMMENT
A Comment is generally included anywhere in the program, though not required but recommended to write a
description or notes in the program. It will not affect the execution of the program since it will be ignored by the
compiler. This is used to help and serves as a remarks or reminder for the programmer while editing the codes. There
are two types of comments:

Single line comment starts with // symbol and anything typed within that line will be ignored by the compiler.

1 // this is a single line comment

Multiline comment start with /* and ends with */ then anything in between this symbols will be considered as part of
the comment.

1 /* This
2 is a
3 multiline comment
4 */

loop( ) FUNCTION

Looping is among the powerful basic concepts in computer programming. It is a programming function that repeatedly
executes a sequence of instructions until a certain condition is reached. The loop ( ) function in Arduino code performs

6|P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

the same way by consecutively looping a certain line of codes inside the function. This function is generally used to
control the Arduino board.

6 void loop( ) {
7 // put your main code here, to run repeatedly:
8
9 }

CONTROLLING A DIGITAL OUTPUT DEVICE USING A BUILT-IN LED IN THE MICROCONTROLLER UNIT

The light emitting diode (LED) is the only digital output device that is built-in in the Arduino UNO microcontroller unit
that is directly connected to digital pin 13.

LED ON PIN 13

Open Arduino IDE then write the following codes below and save the sketch. The sketch will use the built-in LED in the
Arduino board and program it to turn-on for 2 seconds and turn-off for 1 second. The codes will continuously run until
the board is disconnected with the power supply or when reset button is pressed.

1 void setup( ) {
2 // put your setup code here, to run once:
3 pinMode(13, OUTPUT);
4 }
5
6 void loop( ) {
7 // put your main code here, to run repeatedly:
8 digitalWrite(13, HIGH);
9 delay(2000);
10 digitalWrite(13, LOW);
11 delay(1000);
12 }

Observe what will happen if the code inside void loop( ) is removed and placed inside the void setup( ) function.

LIGHT EMITTING DIODE


DESCRIPTION
A light-emitting diode (LED) is a device that emits light when an electric current passes through it. LEDs first appeared in
1962 as practical electronic components that only emitted low intensity red light. Modern versions can be very bright
7|P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

and are available in a broad spectrum of colors in the visible and even in the ultraviolet and infrared spectrums. They are
mainly used in various electronic devices like watches, flashlights, cellphones, displays and many more. Because they
have a longer life span and are more energy efficient than regular light bulbs, they have mostly replaced them, especially
in household settings.  

MATERIALS
 Arduino Microcontroller
 USB Connector
 Male to Male Jumper Wires
 220Ω Resistor
 Light Emitting Diode

CIRCUIT DIAGRAM

ARDUINO SKETCH
1 /* Light Emitting Diode
2 This sketch controls the Light Emitting Diode by turning it on and off every second
3 */
4
5 const int ledPin = 2;
6
7 void setup( ) {
8 // put your setup code here, to run once:
9 pinMode(ledPin, OUTPUT);
10 }
11

12 void loop( ) {
13 // put your main code here, to run repeatedly:
14 digitalWrite (ledPin, HIGH);
15 delay(1000);
16 digitalWrite(ledPin, LOW);
17 delay(1000);

8|P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

18 }

BREADBOARD
Solderless Breadboard or simply breadboard is the important components in building your temporary
electronic circuit without soldering them making it possible to reuse it while working with the prototype and
experimenting with the circuit designs. It is composed of bunch of tiny holes that is designed to insert or
connect wires and other electronic components to breadboard to create a circuit.

Solderless Breadboard

BUZZER
DESCRIPTION
Piezo sounders contain a piezo electric vibration plate (also known as a piezo element) within a molded case.   Sound is
emitted when a voltage is applied and the piezo element inside the case vibrates. Piezo buzzers generally use less
current, have a higher sound output and wider operating voltage.  

MATERIALS

 Arduino Microcontroller
 USB Connector
 Male to Male Jumper Wires
 Piezo Buzzer

9|P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

CIRCUIT DIAGRAM

ARDUINO SKETCH
1 /* Piezo Buzzer
2 This sketch generates different tone from the piezo buzzer in a second then tone is stopped for the next
3 second. Tone also increment the pitch every time it generates the sound
4 */
5
6 const int buzzerPin = 2;
7
8 void setup( ) {
9 // put your setup code here, to run once:
10 pinMode(buzzerPin, OUTPUT);
11 }
12
13 void loop( ) {
14 // put your main code here, to run repeatedly:
15 tone (buzzerPin, 1400);
16 delay(1000);
17 noTone (buzzerPin);
18 delay(1000);
19 tone (buzzerPin, 1600);
20 delay(1000);
21 noTone (buzzerPin);
22 delay(1000);
23 tone (buzzerPin, 1800);
24 delay(1000);
25 noTone (buzzerPin);
26 delay(1000);
27 tone (buzzerPin, 2000);
28 delay(1000);
29 noTone (buzzerPin);
30 delay(1000);
31 }

ACTIVITY – LIGHT EMITTING DIODE AND BUZZER

10 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

Using 3 Light Emitting Diodes and Piezo Buzzer, light up each LED one at a time every second from left to right then right
to left. Every time an LED lights up the Piezo Buzzer should generate a sound different from the other LEDs.

MATERIALS
 Arduino Microcontroller
 USB Connector
 Male to Male Jumper Wires
 220Ω Resistor
 Piezo Buzzer
 Light Emitting Diodes

CIRCUIT DIAGRAM

MAGNETIC SWITCH
DESCRIPTION
Magnetic switch is an electrical switch that switches when a magnetic field affects it. This sensor is essentially a reed
switch, encased in an ABS plastic shell. Normally the reed is 'open' (no connection between the two wires). The other
half is a magnet. When the magnet is less than 13mm (0.5") away, the reed switch closes. They're often used to detect
when a door or drawer is open, which is why they have mounting tabs and screws. You can also pick up some double-
sided foam tape from a hardware store to mount these, that works well without needing screws.

MATERIALS

 Arduino Microcontroller
 USB Connector
 Male to Male Jumper Wires
 Magnetic Switch

11 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

CIRCUIT DIAGRAM

ARDUINO SKETCH
1 /* Magnetic Switch
2 This sketch reads from the magnetic switch then sends it out to the Serial Monitor
3 */
4
5 const int magSwitchPin = 2;
6 int magSwitchValue;
7
8 void setup( ) {
9 // put your setup code here, to run once:
10 pinMode(magSwitchPin, INPUT);
11 Serial.begin(9600);
12 }
13
14 void loop( ) {
15 // put your main code here, to run repeatedly:
16 magSwitchValue = digitalRead(magSwitchPin);
17
18 Serial.println (magSwitchValue);
19 delay(100);
20 }

CONDITIONAL CONTROL STRUCTURE - if … else CONDITIONAL STATEMENT


12 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

DESCRIPTION
Create a Security System Prototype using a magnetic switch attached into a window. The green light emitting diode is a
signal that there’s no security breach but when the magnetic switch is separated the red light emitting diode turns-on
and a sound should be generated as it is an indicator that a security breach just happened. Take note that only one light
emitting diode should light up at a time.

MATERIALS
 Arduino Microcontroller
 USB Connector
 Male to Male Jumper Wires
 220Ω Resistor
 Magnetic Switch
 Light Emitting Diodes
 Piezo Buzzer

CIRCUIT DIAGRAM

ARDUINO SKETCH
1 /* Magnetic Switch
2 This sketch uses the magnetic switch to cause an alarm through LED and Buzzer
3 Serial Monitor to check if the input device is really transmitting the correct data to the microcontroller
4 */
5
6 const int magneticPin = 2, buzzer = 3, greenLED = 4, redLED = 5;
7 int magneticValue;
8
9 void setup( ) {
10 // put your setup code here, to run once:
11 pinMode(magneticPin, INPUT);
12 pinMode(greenLED, OUTPUT);
12 pinMode(redLED, OUTPUT);
13 Serial.begin(9600);
14 }
15
16 void loop( ) {
17 // put your main code here, to run repeatedly:
18 magneticValue = digitalRead(magneticPin);
19
13 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

20 if (magneticValue == 1)
21 {
22 digitaWrite(redPin, HIGH);
23 digitaWrite(greenPin, LOW);
24 }
25 else
26 {
27 digitaWrite(greenPin, HIGH);
28 digitaWrite(redPin, LOW);
29 }
30
31 Serial.println (magneticValue);
32 delay(100);
33 }

SOUND ACTIVATED SWITCH


DESCRIPTION
The sound sensor module provides an easy way to detect sound and is generally used for detecting sound intensity. This
module can be used for security, switch, and monitoring applications. Its accuracy can be easily adjusted for the
convenience of usage. It uses a microphone which supplies the input to an amplifier, peak detector and buffer. When
the sensor detects a sound, it processes an output signal voltage which is sent to a microcontroller then performs
necessary processing.  

MATERIALS
 Arduino Microcontroller
 USB Connector
 Male to Male Jumper Wires
 Sound Activated Switch

CIRCUIT DIAGRAM

ARDUINO SKETCH

14 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

1 /* Sound Activated Switch


2 This sketch uses the sound activated switch to turn-on or turn-off a Light Emitting Diode. It also uses the
3 Serial Monitor to check if the input device is really transmitting the correct data to the microcontroller
4 */
5
6 const int soundPin = 2;
7 int soundValue;
8
9 void setup( ) {
10 // put your setup code here, to run once:
11 pinMode(buzzerPin, OUTPUT);
12 Serial.begin(9600);
13 }
14
15 void loop( ) {
16 // put your main code here, to run repeatedly:
17 soundValue = digitalRead(soundPin);
18
19 Serial.println (soundValue);
20 delay(100);
21 }

CONDITIONAL CONTROL STRUCTURE - if … else CONDITIONAL STATEMENT


DESCRIPTION
Using an if…else conditional statement and nested conditional statement, light up a Light Emitting Diode when the
Sound Activated Switch hears a clap and light it off when it hears another clap. This will make the Sound Activated
Switch function as a toggle switch for the Light Emitting Diode.

MATERIALS
 Arduino Microcontroller
 USB Connector
 Male to Male Jumper Wires
 220Ω Resistor
 Sound Activated Switch
 Light Emitting Diode

15 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

CIRCUIT DIAGRAM

ARDUINO SKETCH
1 /* Sound Activated Switch
2 This sketch uses the sound activated switch to turn-on or turn-off a Light Emitting Diode. It also uses the
3 Serial Monitor to check if the input device is really transmitting the correct data to the microcontroller
4 */
5
6 const int soundPin = 5, ledPin = 4;
7 int soundValue;
8
9 void setup( ) {
10 // put your setup code here, to run once:
11 pinMode(soundPin, INPUT);
12 pinMode(ledPin, OUTPUT);
13 Serial.begin(9600);
14 }
15

16 void loop( ) {
17 // put your main code here, to run repeatedly:
18 soundValue = digitalRead(soundPin);
19
20 if (soundValue == 1)
21 {
22 if (digitalRead(ledPin) == 0)
23 {
24 digitaWrite(ledPin, HIGH);
25 }
26 else
27 {
28 digitaWrite(ledPin, LOW);
29 }
30 }
31
32 Serial.println (soundValue);

16 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

33 delay(100);
34 }

MULTILEVEL if … else if CONDITIONAL STATEMENT


DESCRIPTION
Using a Sound Activated Switch, 3 Light Emitting Diodes and Piezo Buzzer. Light up each LED one at a time every time the
sound activated switch hears a sound from left to right then repeat.

MATERIALS
 Arduino Microcontroller
 USB Connector
 Male to Male Jumper Wires
 220Ω Resistor
 Sound Activated Switch
 Light Emitting Diodes

CIRCUIT DIAGRAM

ARDUINO SKETCH
1 /* Sound Activated Switch
2 This sketch use a sound activated switch to turn-on and off the light of a Light Emitting Diode one at a time.
3 Serial Monitor to check if the input device is really transmitting the correct data to the microcontroller
4 */
5
6 const int soundPin = 5, redLED = 4, blueLED = 3, greenLED = 2;
7 int soundValue;
7 int ledStatus = 1;
8
9 void setup( ) {
10 // put your setup code here, to run once:
11 pinMode(soundPin, INPUT);
12 pinMode(redLED, OUTPUT);
17 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

13 pinMode(blueLED, OUTPUT);
14 pinMode(greenLED, OUTPUT);
15 Serial.begin(9600);
16 }
17

18 void loop( ) {
19 // put your main code here, to run repeatedly:
20 soundValue = digitalRead(soundPin);
21
22 if (soundValue == 1)
23 {
24 if (ledStatus == 1)
25 {
26 digitaWrite(redLED, HIGH);
27 digitaWrite(blueLED, LOW);
28 digitaWrite(greenLED, LOW);
29 digitaWrite(greenLED, LOW);
30 ledStatus = 2;
31 }
32 else if (ledStatus == 2)
33 {
34 digitaWrite(redLED, LOW);
35 digitaWrite(blueLED, HIGH);
36 digitaWrite(greenLED, LOW);
37 ledStatus = 3;
38 }
39 else if (ledStatus == 3)
40
41 {
42 digitaWrite(redLED, LOW);
43 digitaWrite(blueLED, LOW);
44 digitaWrite(greenLED, HIGH);
45 ledStatus = 1;
46 }
47 }
48
49 Serial.println (soundValue);
50 delay(100);
51 }

TAC SWITCH/TACTILE SWITCH


DESCRIPTION
A switch is a component which controls the open-ness or closed-ness of an electric circuit. They allow control over
current flow in a circuit (without having to actually get in there and manually cut or splice the wires). Switches are
critical components in any circuit which requires user interaction or control.

18 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

A switch can only exist in one of two states: open or closed. In the off state, a switch looks like an open gap in the circuit.
This, in effect, looks like an open circuit, preventing current from flowing.
In the on state, a switch acts just like a piece of perfectly-conducting wire. A short. This closes the circuit, turning the
system “on” and allowing current to flow unimpeded through the rest of the system.
MATERIALS
 Arduino Microcontroller
 USB Connector
 Male to Male Jumper Wires
 1KΩ Resistor
 Tactile Switch

CIRCUIT DIAGRAM

ARDUINO SKETCH
1 /* Tactile Switch
2 This sketch uses the Tactile Switch to send digital data to the microcontroller as inputs
3 Serial Monitor to check if the input device is really transmitting the correct data to the microcontroller
4 */
5
6 const int tacPin = 2;
7 int tacValue;
8
9 void setup( ) {
10 // put your setup code here, to run once:
11 pinMode(tacPin, INPUT);
12 Serial.begin(9600);
13 }
14
15 void loop( ) {
16 // put your main code here, to run repeatedly:
17 tacValue = digitalRead(tacPin);
18
19 Serial.println (tacValue);
20 delay(100);
21 }

19 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

ACTIVITY – TACTILE SWITCH


Following the circuit diagram below, create an Arduino Sketch that will toggle on and off the two Light Emitting Diodes.
The buzzer should generate a sound for 100 milliseconds every time the tactile switch is pressed.

MATERIALS
 Arduino Microcontroller
 USB Connector
 Male to Male Jumper Wires
 Tactile Switch
 220Ω Resistor
 1KΩ Resistor
 Light Emitting Diodes
 Piezo Buzzer

CIRCUIT DIAGRAM

20 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

ULTRASONIC SENSOR
DESCRIPTION
An ultrasonic sensor is an instrument that measures the distance to an object using ultrasonic sound waves. An
ultrasonic sensor uses a transducer to send and receive ultrasonic pulses that relay back information about an object’s
proximity. High-frequency sound waves reflect from boundaries to produce distinct echo patterns.  

Ultrasonic sound vibrates at a frequency above the range of human hearing. Transducers are the microphones used to
receive and send the ultrasonic sound. Like many others, use a single transducer to send a pulse and to receive the echo.
The sensor determines the distance to a target by measuring time lapses between the sending and receiving of the
ultrasonic pulse.
MATERIALS

 Arduino Microcontroller
 USB Connector
 Male to Male Jumper Wires
 Ultrasonic Sensor

CIRCUIT DIAGRAM

SKETCH
1 /* Ultrasonic Sensor
2 This sketch reads uses the Ultrasonic Sensor to measure and display the distance between the sensor and
3 any object infront of it then display the value in the Serial Monitor
4 */
5
6 const int echoPin = 2, triggerPin = 3;
21 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

7 float pulseValue, metersDistance;


8
9 void setup( ) {
10 // put your setup code here, to run once:
11 pinMode(echoPin, INPUT);
12 pinMode(triggerPin, OUTPUT);
13 Serial.begin(9600);
14 }
15

16 void loop( ) {
17 // put your main code here, to run repeatedly:
18 digitalWrite(triggerPin, LOW);
19 delayMicroseconds(5);
20 digitalWrite(triggerPin, HIGH);
21 delayMicroseconds(5);
22
23 pulseValue = pulseIn(echo, HIGH);
24 metersDistance = pulseValue * 0.0001657;
25
26 Serial.println (meterDistance);
27 delay(50);
28 }

ACTIVITY – ULTRASONIC SENSOR


Convert and display to inches the measurement from the Ultrasonic Sensor and display it over the Serial Monitor. Use 3
Light Emitting Diodes and create a warning system based on the given conditions below:

CONDITIONS ACTIONS

Distance <= 5 inches Red LED blinks and generate sounds from the buzzer
Distance <= 10 inches Blue LED lights-up
Distance > 10 inches Green LED lights-up

MATERIALS
 Arduino Microcontroller
 USB Connector
 Male to Male Jumper Wires
 Ultrasonic Sensor
 220Ω Resistor
 Light Emitting Diodes
 Piezo Buzzer

22 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

CIRCUIT DIAGRAM

1800 SERVO MOTOR


DESCRIPTION
A servomotor is a rotary actuator or linear actuator that allows for precise control of angular or linear position, velocity
and acceleration. It consists of a suitable motor coupled to a sensor for position feedback. It also requires a relatively
sophisticated controller, often a dedicated module designed specifically for use with servomotors.
Servomotors are not a specific class of motor although the term servomotor is often used to refer to a motor suitable for
use in a control system. Servomotors are used in applications such as robotics, CNC machinery or automated
manufacturing.
MATERIALS
 Arduino Microcontroller
 USB Connector
 Male to Male Jumper Wires
 1800 Servo Motor

23 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

CIRCUIT DIAGRAM

SKETCH
1 /* 180 Degree Servo Motor
2 This sketch reads uses the Ultrasonic Sensor to measure and display the distance between the sensor and
3 */
3 #include<Servo.h>
4 Servo servo01;
8
9 void setup( ) {
10 // put your setup code here, to run once:
11 servo01.attach(2);
14 }
15

16 void loop( ) {
17 // put your main code here, to run repeatedly:
18 servo01.write(0);
19 delay(1000);
20 servo01.write(30);
21 delay(1000);
22 servo01.write(60);
23 delay(1000);
24 servo01.write(90);
25 delay(1000);
26 servo01.write(120);
27 delay(1000);
28 servo01.write(150);
29 delay(1000);
30 servo01.write(180);
31 delay(1000);
32 }
24 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

ACTIVITY – 1800 SERVO MOTOR


Create an Arduino Sketch that will move the arm of the servo from 0 0 to 1800 and vice versa with 20 milliseconds delay
per degree of rotation.

LIGHT EMITTING DIODE


DESCRIPTION
A light-emitting diode (LED) is can be both digital and analog output device. Controlling an LED as digital output means
turning it on or off but with LED being controlled as an analog output device means controlling it brightness.

Take note that in comparison with digital input and output which shares the same pin, analog input pins is separated
with analog output pins. 

MATERIALS
 Arduino Microcontroller
 USB Connector
 Male to Male Jumper Wires
 220Ω Resistor
 Light Emitting Diode

25 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

CIRCUIT DIAGRAM

ARDUINO SKETCH
1 /* Light Emitting Diode
2 This sketch controls the Light Emitting Diode by turning it on and off every second
3 */
4
5 const int ledPin = 3;
6
7 void setup( ) {
8 // put your setup code here, to run once:
9 pinMode(ledPin, OUTPUT);
10 }
11

12 void loop( ) {
13 // put your main code here, to run repeatedly:
14 analogWrite (ledPin, 0);
15 delay(500);
16 analogWrite(ledPin, 30);
17 delay(500);
18 analogWrite (ledPin, 60);
19 delay(500);
20 analogWrite(ledPin, 90);
21 delay(500);
22 analogWrite (ledPin, 120);
23 delay(500);
24 analogWrite(ledPin, 150);
25 delay(500);
26 analogWrite (ledPin, 180);
27 delay(500);
26 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

28 analogWrite(ledPin, 210);
29 delay(500);
30 analogWrite (ledPin, 230);
31 delay(500);
32 analogWrite(ledPin, 255);
33 delay(500);
34 }

ACTIVITY – LIGHT EMITTING DIODE


Create an Arduino Sketch that will adjust the brightness of the Light Emitting Diode from zero (minimum brightness) to
two hundred fifty-five (maximum brightness) with an interval of 20 milliseconds on each additional value.

POTENTIOMETER
DESCRIPTION
Potentiometer is a small sized electronic component whose resistance can be adjusted manually. Increasing or
decreasing the value of resistance controls the amount of current flowing in a circuit. The potentiometer is used in
various electronics, for example: is used as volume knob in music systems, as fan regulators etc. Potentiometer has two
strips made on it resistive and conductive. Resistive strip is made of carbon and is responsible for potentiometer’s
resistance variance feature. Conductive strip helps the potentiometer to carry the current into the circuit in accordance
with the resistance.

MATERIALS

 Arduino Microcontroller
 USB Connector
 Male to Male Jumper Wires
 Potentiometer

27 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

CIRCUIT DIAGRAM

ARDUINO SKETCH
1 /* Potentiometer
2 This sketch uses a potentiometer to send an analog value to the microcontroller unit from 0 to 1023
3 The value of the potentiometer will be displayed through the serial monitor
4 */
5
6 const int potPin = A0;
7 int potValue;
8
9 void setup( ) {
10 // put your setup code here, to run once:
11 pinMode(potPin, INPUT);
12 Serial.begin(9600);
13 }
14

15 void loop( ) {
16 // put your main code here, to run repeatedly:
17 potValue = analogRead(potPin);
18
19 Serial.println (potValue);
20 delay(100);
21 }

map( ) FUNCTION
DESCRIPTION
Re-maps a number from one range to another. That is, a value of fromLow would get mapped to toLow, a value
of fromHigh to toHigh, values in-between to values in-between, etc. Does not constrain values to within the range,
because out-of-range values are sometimes intended and useful. The constrain() function may be used either before or
after this function, if limits to the ranges are desired.

28 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

Example:
y = map(x, 1, 50, 50, 1);

The function also handles negative numbers well, so that this example
y = map(x, 1, 50, 50, -100);

The map () function uses integer math so will not generate fractions, when the math might indicate that it should do so.
Fractional remainders are truncated, and are not rounded or averaged.

MATERIALS
 Arduino Microcontroller
 USB Connector
 Male to Male Jumper Wires
 Potentiometer
 Light Emitting Diode
 220Ω Resistor

CIRCUIT DIAGRAM

ARDUINO SKETCH
1 /* Potentiometer
2 This sketch uses a Potentiometer and a Light Emitting Diode. This example shows on how 2 different values
3 will be consolidated to be equal using the map( ) function
4 */
5
6 const int potPin = A0, ledPin = 2;
7 int potValue;
8
9 void setup( ) {
10 // put your setup code here, to run once:
11 pinMode(potPin, INPUT);
12 pinMode(ledPin, OUTPUT);
13 Serial.begin(9600);
14 }
15
16 void loop( ) {
17 // put your main code here, to run repeatedly:
18 potValue = analogRead(potPin);
19

29 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

20 potValue = map(potValue, 0, 1023, 0, 255);


21
22 Serial.println (potValue);
23 delay(100);
24 }

ACTIVITY – POTENTIOMETER
Create an Arduino Sketch and a circuit that will do the same as the example above but with this activity instead of light
emitting diode use the servo motor. Take note that the servo motor should be reactive or responsive as you turn the
potentiometer clockwise or counter-clockwise.

LIGHT DEPENDENT RESISTOR


DESCRIPTION
An LDR is a component that has a (variable) resistance that changes with the light intensity that falls upon it. This allows
them to be used in light sensing circuits. The most obvious application for an LDR is to automatically turn on a light at a
certain light level. An example of this could be a street light or a garden light.

MATERIALS
 Arduino Microcontroller
 USB Connector
 Male to Male Jumper Wires
 Light Dependent Resistor
 1KΩ Resistor

CIRCUIT DIAGRAM

ARDUINO SKETCH
30 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

1 /* Light Dependent Resistor


2 This sketch reads from Analog Pin A0 where the Light Depende Resistor is connected
3 */
4
5 const int ldrPin = A0;
6 float ldrValue;
7
8 void setup( ) {
9 // put your setup code here, to run once:
10 pinMode(ldrPin, INPUT);
11 Serial.begin(9600);
12 }
13
14 void loop( ) {
15 // put your main code here, to run repeatedly:
16 ldrValue = analogRead(ldrPin);
17
18 Serial.println (ldrValue);
19 }

ACTIVITY – LIGHT DEPENDENT RESISTOR


Create an Arduino Sketch and a circuit that will use the Light Dependent Resistor as an automatic toggle switch that will
turn-on and off the 3 Light Emitting Diodes based on a certain brightness value similar with what is being used in
automating the light along the streets based on the suns brightness.

MATERIALS

 Arduino Microcontroller
 USB Connector
 Male to Male Jumper Wires
 Light Dependent Resistor
 1KΩ Resistor
 220Ω Resistors
 Light Emitting Diodes

CIRCUIT DIAGRAM

LM35 TEMPERATURE SENSOR


31 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

DESCRIPTION
Usually, a temperature sensor is a thermocouple or a resistance temperature detector (RTD) that gathers the
temperature from a specific source and alters the collected information into understandable type for an apparatus or an
observer. Temperature sensors are used in several applications namely HV system and AC system environmental
controls, medical devices, food processing units, chemical handling, controlling systems, automotive under the hood
monitoring and etc.

MATERIALS

 Arduino Microcontroller
 USB Connector
 Male to Male Jumper Wires
 LM35 Temperature Sensor

CIRCUIT DIAGRAM

ARDUINO SKETCH
1 /* LM35 Temperature Sensor
2 This sketch reads from Analog Pin A0 where the LM35 Temperature Sensor is connected
3 */
4
5 const int tempPin = A0;
6 float tempValue, TempCelisius;
7
8 void setup( ) {
9 // put your setup code here, to run once:
10 pinMode(tempPin, INPUT);
11 Serial.begin(9600);
12 }
13
14 void loop( ) {
15 // put your main code here, to run repeatedly:
16 tempValue = analogRead(tempPin);
17 tempCelsius = (tempValue * 1024.0) * 5000 / 10;
18
19 Serial.println (tempCelsius);
20 delay (100);
21 }
32 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

ACTIVITY – LM35 TEMPERATURE SENSOR


Using an LM35 Temperature sensor, create a prototype where the LED will light up depending on the temperature being
transmitted to the Microcontroller Unit. Follow the given conditions below:

CONDITIONS ACTIONS

Temperature <=85 Fahrenheit Green LED lights-up


Temperature <=95 Fahrenheit Blue LED lights-up
Temperature > 95 Fahrenheit Red LED blinks very 200 milliseconds and generate sounds from the buzzer

MATERIALS

 Arduino Microcontroller
 USB Connector
 Male to Male Jumper Wires
 LM35 Temperature Sensor
 220Ω Resistor
 Magnetic Switch
 Light Emitting Diodes
 Piezo Buzzer

33 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

CIRCUIT DIAGRAM

34 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

JOYSTICK CONTROLLER
DESCRIPTION
The joystick in the picture is nothing but two potentiometers that allow us to measure the movement of the stick in 2-D.
Potentiometers are variable resistors and, in a way, they act as sensors providing us with a variable voltage depending
on the rotation of the device around its shaft.

The kind of program that we need to monitor the joystick has to make a polling to two of the analog pins. We can send
these values back to the computer, but then we face the classic problem that the transmission over the communication
port has to be made with 8bit values, while our DAC (Digital to Analog Converter - that is measuring the values from the
potentiometers in the joystick) has a resolution of 10bits. In other words, this means that our sensors are characterized
with a value between 0 and 1024.

MATERIALS
 Arduino Microcontroller
 USB Connector
 Male to Male Jumper Wires
 Joystick Controller

CIRCUIT DIAGRAM

35 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

ARDUINO SKETCH
1 /* Joystick Controller
2 This sketch reads from Analog Pin A0 for the x-axis potentiometer and A1 for the y-axis potentiometer
3 */
4
5 const int xPin = A0, yPin = A1;
6 float xValue, yValue;
7
8 void setup( ) {
9 // put your setup code here, to run once:
10 pinMode(xPin, INPUT);
11 pinMode(yPin, INPUT);
12 Serial.begin(9600);
13 }
14
15 void loop( ) {
16 // put your main code here, to run repeatedly:
17 xValue = analogRead(xPin);
18 yValue = analogRead(yPin);
19
20 Serial.print (“Value of x – axis : “);
21 Serial.println (xValue);
22 Serial.print (“ Value of y – axis : “);
23 Serial.println (yValue);
24
25 delay (100);
26 }

ACTIVITY – JOYSTICK CONTROLLER


Use the Joystick Controller and LED to identify in the Cartesian Plane the Quadrant of the x and y values from it and
display over the Serial Monitor the given text and actions below:

CONDITIONS ACTIONS

x is positive and y is positive Print QUADRANT I then light up the RED LED
x is negative and y is positive Print QUADRANT II then light up the GREEN LED
x is negative and y is negative Print QUADRANT III then light up the BLUE LED
x is positive and y is negative Print QUADRANT IV then light up the YELLOW LED
x is zero and y is zero Print POINT OF ORIGIN then OFF ALL LED

SAMPLE OUTPUT ON THE SERIAL MONITOR

Value of x is 50 and Value of y is 90 – QUADRANT I


Value of x is 50 and Value of y is 90 – QUADRANT I
Value of x is 50 and Value of y is 90 – QUADRANT I
Value of x is 50 and Value of y is 90 – QUADRANT I
Value of x is 50 and Value of y is 90 – QUADRANT I
Value of x is -25 and Value of y is -800 – QUADRANT III
Value of x is -25 and Value of y is -800 – QUADRANT III
Value of x is -25 and Value of y is -800 – QUADRANT III
Value of x is -25 and Value of y is -800 – QUADRANT III
Value of x is -25 and Value of y is -800 – QUADRANT III
Value of x is -25 and Value of y is -800 – QUADRANT III

SAMPLE CONDITIONAL STATEMENT

36 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

if (xValue == 0) && (yValue == 0)


{

MATERIALS
 Arduino Microcontroller
 USB Connector
 Male to Male Jumper Wires
 LM35 Temperature Sensor
 220Ω Resistor
 Magnetic Switch
 Light Emitting Diodes
 Piezo Buzzer

CIRCUIT DIAGRAM

37 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

Light Emitting Diode – RGB


DESCRIPTION
The joystick in the picture is nothing but two potentiometers that allow us to measure the movement of the stick in 2-D.
Potentiometers are variable resistors and, in a way, they act as sensors providing us with a variable voltage depending
on the rotation of the device around its shaft.

The kind of program that we need to monitor the joystick has to make a polling to two of the analog pins. We can send
these values back to the computer, but then we face the classic problem that the transmission over the communication
port has to be made with 8bit values, while our DAC (Digital to Analog Converter - that is measuring the values from the
potentiometers in the joystick) has a resolution of 10bits. In other words, this means that our sensors are characterized
with a value between 0 and 1024.

MATERIALS

 Arduino Microcontroller
 USB Connector
 Male to Male Jumper Wires
 RGB Light Emitting Diode
 220Ω Resistors

CIRCUIT DIAGRAM

38 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

ARDUINO SKETCH
1 /* Light Emitting Diode - RGB
2 This sketch changes the color of the LED from red then green then blue every 1 second interval
3 */
4
5 const int redPin = 9, greenPin = 10, bluePin = 11;
7
8 void setup( ) {
9 // put your setup code here, to run once:
10 pinMode(redPin, OUTPUT);
10 pinMode(greenPin, OUTPUT);
10 pinMode(bluePin, OUTPUT);
13 }
14
15 void loop( ) {
16 // put your main code here, to run repeatedly:
17
18 //changing color to red
19 digitalWrite(redPin, HIGH);
20 digitalWrite(greenPin, LOW);
21 digitalWrite(redPin, LOW);
22 delay (1000);
23
24 // changing color to green
25 digitalWrite(redPin, LOW);
26 digitalWrite(greenPin, HIGH);
27 digitalWrite(redPin, LOW);
28 delay (1000);
29
30 // changing color to blue
31 digitalWrite(redPin, LOW);
32 digitalWrite(greenPin, LOW);
33 digitalWrite(redPin, HIGH);
34 delay (1000);
35
36 // changing color to green
37 digitalWrite(redPin, LOW);
38 digitalWrite(greenPin, HIGH);
39 digitalWrite(redPin, LOW);
40 delay (1000);
41
42 }

SOURCES
 ADAFRUIT
https://www.adafruit.com/product/375
 ABC – ALLAN BUTCHER COMPONENTS LTD
https://www.abcomponents.co.uk/piezo-buzzer-definition/
39 | P a g e
ROBOTICS USING MICROCONTROLLER AND SENSORS
Digital and Analog Signals

 TECHOPEDIA
https://www.techopedia.com/definition/2231/light-emitting-diode-led
 TINKBOX
http://tinkbox.ph/sites/mytinkbox.com/files/downloads/SOUND_SENSOR_MODULE.pdf
 SPARKFUN
https://learn.sparkfun.com/tutorials/switch-basics/all
 MAXBOTIX
https://www.maxbotix.com/articles/how-ultrasonic-sensors-work.htm
 ARDUINO
https://www.arduino.cc/reference/en/language/functions/math/map/
 KITRONIK
https://www.kitronik.co.uk/blog/how-an-ldr-light-dependent-resistor-works/
 EFXKITS
https://www.efxkits.us/lm35-temperature-sensor-circuit-working/
 ARDUINO
https://www.arduino.cc/en/Tutorial/JoyStick
 HOMETOMECHATRONICS
 https://howtomechatronics.com/tutorials/arduino/how-to-use-a-rgb-led-with-arduino/
 INSTRUCTABLES
https://www.instructables.com/id/How-to-use-an-RGB-LED-Arduino-Tutorial/

40 | P a g e

You might also like