0% found this document useful (0 votes)
20 views

Arduino Hands-On

Uploaded by

liktimbulnugroho
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Arduino Hands-On

Uploaded by

liktimbulnugroho
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Part 1: Arduino Basic

OBJECTIVES
• To build basic Tinkercard circuits.
• To create elementary Arduino circuits.
• To manage electronic elements as resistors, leds, breadboards,…
• To follow the sequence of instructions of a basic program and preview
the output results.
• To read and understand initial C++ programs.
• To write short and basic C++ programs using the most elementary
structures.
ARDUINO BASIC – LED Programming

1. Arduino – Blink – 1 LED

Arduino Code:

Note:

Arduino output when high is 5V. The led, when lighting has a Vled near 2V, so Vr=5V-Vled=3V. If we
want the led shines properly we will use a current of 15mA, so R=3V/15mA = 200ohm.

Pengabdian Masyarakat DTEDI SV UGM – 2024 Arduino Basic – Input / Output 1


2. Arduino – Blink – 4 LED

Arduino Code:

Exercises Based on number 1 & 2,


1. Draw a circuit with 4 LEDs and program it so that 2 LEDs turn on and 2 LEDs turn off alternately,
2. Draw a circuit with 4 LEDs and program it so that odd LEDs are on, even LEDs are off alternately,
3. Draw a circuit with 4 LEDs and program it so the LEDs run left to right (running led).

Pengabdian Masyarakat DTEDI SV UGM – 2024 Arduino Basic – Input / Output 2


3. Arduino – Traffic Light

Arduino Code:

Pengabdian Masyarakat DTEDI SV UGM – 2024 Arduino Basic – Input / Output 3


Part 2: Arduino Input/Output

OBJECTIVES

• To use a LCD in Arduino to display messages to the user.


• To use different types of sensors and actuators (temperature sensors,
distance sensors, servomotors).
• To differenciate digital and analog use of inputs and outputs in Arduino.
• To write basic Arduino programs using libraries with C++ classes and
objects.
ARDUINO BASIC – Input / Output
4. Arduino I/O – Serial Monitor – Hello World!
The serial monitor is the 'tether' between the computer and the Arduino - it lets to send and
receive text messages, handy for debugging and also controlling the Arduino from a keyboard!

Serial.begin(rate)
Opens serial port and set baud rate for serial data trasmission. Typically baud rate for
communication with computer is 96000 although other speeds are supported.
Note: when using serial communication digital pins 0(RX) and 1(TX) cannot be used at the same
time

Serial.println(data)
Print data to the serial port followed by an automatic carriage return and line feed .This
command takes the same form as Serial.print(), but is easier for reading data on the Serial
monitor.

Arduino Code:

Pengabdian Masyarakat DTEDI SV UGM – 2024 Arduino Basic – Input / Output 4


Serial Monitor:

Pengabdian Masyarakat DTEDI SV UGM – 2024 Arduino Basic – Input / Output 5


5. Arduino I/O – Analog Read
A potentiometer is a simple knob that provides a variable resistance, which can be read into the
Arduino board as an analog value. For example, read the value of Potentiometer and display it
in serial monitor.

Connect three wires to the Arduino board. The first goes to ground from one of the outer pins of
the potentiometer. The second goes from 5 volts to the other outer pin of the potentiometer.
The third goes from analog input 2 to the middle pin of the potentiometer.

By turning the shaft of the potentiometer, it change the amount of resistence on either side of
the wiper which is connected to the center pin of the potentiometer. This changes the relative
"closeness" of that pin to 5 volts and ground, giving us a different analog input. When the shaft
is turned all the way in one direction, there are 0 volts going to the pin, and we read 0. When the
shaft is turned all the way in the other direction, there are 5 volts going to the pin and we read
1023. In between, analogRead() returns a number between 0 and 1023 that is proportional to
the amount of voltage being applied to the pin.

Arduino Code:

Pengabdian Masyarakat DTEDI SV UGM – 2024 Arduino Basic – Input / Output 6


Pengabdian Masyarakat DTEDI SV UGM – 2024 Arduino Basic – Input / Output 7
6. Arduino – LED with Push Button
Pushbuttons or switches connect two points in a circuit when it pressed. This example turns on
the LED on pin 0 when the button pressed.

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 read as LOW.
When the button is closed (pressed), it makes a connection between its two legs, connecting the
pin to 5 volts, so that read as HIGH.

If it 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 it need a
pull-up or pull-down resistor in the circuit.

Arduino Code:

Pengabdian Masyarakat DTEDI SV UGM – 2024 Arduino Basic – Input / Output 8


7. Arduino I/O – Piezo (Buzzer)
A buzzer or beeper is an audio signaling device, which may be mechanical, electromechanical, or
piezoelectric. Typical uses of buzzers and beepers include alarm devices, timers, and
confirmation of user input such as a mouse click or keystroke.
In the circuit, the positive lead of the buzzer is connected to pin 0 of the Arduino, the negative
lead of the buzzer is connected to GND.

Arduino Code:

tone()
Generates a square wave of the specified frequency (and 50% duty cycle) on a pin. A duration can
be specified, otherwise the wave continues until a call to noTone(). The pin can be connected to a
piezo buzzer or other speaker to play tones. Only one tone can be generated at a time. If a tone is
already playing on a different pin, the call to tone() will have no effect. If the tone is playing on the
same pin, the call will set its frequency.

Pengabdian Masyarakat DTEDI SV UGM – 2024 Arduino Basic – Input / Output 9


8. Arduino I/O – LCD – Hello World!
The LiquidCrystal library allows you to control LCD displays usually the 16-pin interface. 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 contrast 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.

LCD Pin configuration:

Pengabdian Masyarakat DTEDI SV UGM – 2024 Arduino Basic – Input / Output 10


Arduino Code:

Pengabdian Masyarakat DTEDI SV UGM – 2024 Arduino Basic – Input / Output 11


9. Arduino I/O – Temperature Sensor
The TMP36 is a low voltage, precision centigrade temperature sensor manufactured by Analog
Devices. It is a chip that provides a voltage output that is linearly proportional to the
temperature in °C and is, therefore, very easy to use with an Arduino.
The TMP36 temperature sensor is fairly precise, never wears out, works under many
environmental conditions and requires no external components to work. In addition, the TMP36
sensor does not require calibration and provides a typical accuracy of ±1°C at +25°C and
±2°C over the −40°C to +125°C temperature range.
The sensor can be powered with a 2.7V to 5.5V power supply and consumes only 50µA during
active temperature conversions, providing very low self-heating (less than 0.1°C in still air). In
addition, a shutdown function is provided to reduce the supply current to less than 0.5µA

Arduino Code:

Pengabdian Masyarakat DTEDI SV UGM – 2024 Arduino Basic – Input / Output 12


Pengabdian Masyarakat DTEDI SV UGM – 2024 Arduino Basic – Input / Output 13
10. Arduino I/O – LCD & Temperature Sensor

Arduino Code:

Pengabdian Masyarakat DTEDI SV UGM – 2024 Arduino Basic – Input / Output 14


11. Arduino I/O – Measure Distances (PING Sensor)
An Ultrasonic sensor is a device that can measure the distance to an object by using sound
waves. It measures distance by sending out a sound wave at a specific frequency and listening
for that sound wave to bounce back. By recording the elapsed time between the sound wave
being generated and the sound wave bouncing back, it is possible to calculate the distance
between the sonar sensor and the object.
The PING is an ultrasonic range finder from Paralax. It detects the distance of the closest object
in front of the sensor (from 3 cm up to 400 cm). It works by sending out a burst of ultrasound
and listening for the echo when it bounces off of an object. It pings the obstacles with
ultrasound.

The Arduino board sends a short pulse to trigger the detection, then listens for a pulse on the
same pin using the pulseIn() function. The duration of this second pulse is equal to the time taken
by the ultrasound to travel to the object and back to the sensor. Using the speed of sound, this
time can be converted to distance.

Pengabdian Masyarakat DTEDI SV UGM – 2024 Arduino Basic – Input / Output 15


Arduino Code:

Exercises Based on number 8, 10 & 11,


4. Draw a circuit with the Ping sensor and display the distance measurement results on the LCD

Pengabdian Masyarakat DTEDI SV UGM – 2024 Arduino Basic – Input / Output 16


12. Arduino I/O – Measure Distances (Ultrasonic Sensor/HC-SR04)
The HC-SR04 Ultrasonic Module has 4 pins, Ground, VCC, Trig and Echo. The Ground and the VCC
pins of the module needs to be connected to the Ground and the 5 volts pins on the Arduino
Board respectively and the trig and echo pins to any Digital I/O pin on the Arduino Board.
In order to generate the ultrasound, you need to set the Trig on a High State for 10 µs. That will
send out an 8 cycle sonic burst which will travel at the speed sound and it will be received in the
Echo pin. The Echo pin will output the time of travel of the sound wave in microseconds.

For example, if the object is 10 cm away from the sensor, and the speed of the sound is 340 m/s
or 0.034 cm/µs the sound wave will need to travel about 294 u seconds. But what it will get from
the Echo pin will be double that number because the sound waves need to travel forward and
bounce backward. In order to get the distance in cm it need to multiply the received travel time
value from the echo pin by 0.034 and divide it by 2.

Pengabdian Masyarakat DTEDI SV UGM – 2024 Arduino Basic – Input / Output 17


Arduino Code:

Pengabdian Masyarakat DTEDI SV UGM – 2024 Arduino Basic – Input / Output 18


13. Arduino I/O – PIR (Passive Infrared) Sensor
PIR sensor which is also known as Pyroelectric sensor or Passive Infrared Sensor is basically an
electronic sensor employed in motion detecting applications. A PIR sensor detects or measures
IR (Infra-Red) radiations emitted by any object inside its field of view. A PIR sensor is generally
known to the world as motion sensor or motion detector.

The PIR sensor itself has two slots in it, each slot is made of a special material that is sensitive
to IR. The lens used here is not really doing much and so we see that the two slots can 'see' out
past some distance (basically the sensitivity of the sensor). When the sensor is idle, both slots
detect the same amount of IR, the ambient amount radiated from the room or walls or outdoors.
When a warm body like a human or animal passes by, it first intercepts one half of the PIR sensor,
which causes a positive differential change between the two halves. When the warm body leaves
the sensing area, the reverse happens, whereby the sensor generates a negative differential
change. These change pulses are what is detected.

Pengabdian Masyarakat DTEDI SV UGM – 2024 Arduino Basic – Input / Output 19


Arduino Code:

Pengabdian Masyarakat DTEDI SV UGM – 2024 Arduino Basic – Input / Output 20


14. Arduino I/O – LDR (Light Dependent Resistor) Sensor
LDR that allow to detect light. They are small, inexpensive, low-power, easy to use and don't
wear out. For that reason they often appear in toys, gadgets and appliances. LDR is basically a
resistor that changes its resistive value (in ohms Ω) depending on how much light is shining onto
the squiggly face. They are very low cost, easy to get in many sizes and specifications.
LDR is basically a resistor that change resistance depending upon the intensity of light falling on
it. When the light intensity increases the semiconductor layer gets excited leading to the
generation of charge carries hence better conduction that means lower resistance.

Arduino Code:

Exercises Based on number 13 & 14,


5. Draw the circuit with the PIR sensor and the LED that lights up (blinks) when motion is detected!
6. Draw a circuit with an LDR sensor and an LED that lights up when it's dark and turns off when it's
bright!

Pengabdian Masyarakat DTEDI SV UGM – 2024 Arduino Basic – Input / Output 21


15. Arduino I/O – Servo Motor with push button
The Servo Library is a great library for controlling servo motors. The example controls the
position of a servo motor with your Arduino and a push button, you can modify the angle to 180
degree.
Servo motors have three wires: power, ground, and signal. The power wire is typically red, and
should be connected to the 5V pin on the Arduino board. The ground wire is typically black or
brown and should be connected to a ground pin on the board. The signal pin is typically yellow
or orange and should be connected to PWM pin on the board. In these examples, in this example
it is pin number 3.

(next) Arduino Code: ...

Pengabdian Masyarakat DTEDI SV UGM – 2024 Arduino Basic – Input / Output 22


Arduino Code:

Pengabdian Masyarakat DTEDI SV UGM – 2024 Arduino Basic – Input / Output 23

You might also like