VIRTUAL HANDS ON TRAINING ON ARDUINO PROGRAMMING
VIRTUAL HANDS ON TRAINING ON ARDUINO PROGRAMMING
ON ARDUINO PROGRAMMING
User Manual
Prepared by
Mr. S Arun, Assistant Professor,
ECE Department.
Contents
Basic Components of Arduino UNO board and their functions. ............................................................. 2
Setup Procedure ..................................................................................................................................... 3
Understanding basics of Arduino programming ..................................................................................... 6
Procedure to Login and use Tinkercad to Simulate Arduino Projects .................................................... 8
LED Blink................................................................................................................................................ 11
Reading Analog input using POT ........................................................................................................... 13
LED ON OFF USING PUSH BUTTON ....................................................................................................... 15
Interfacing TMP36 Temperature sensor ............................................................................................... 17
Interfacing 4x4 keyboard ...................................................................................................................... 19
LCD Display Interfacing ......................................................................................................................... 22
Interfacing Ultrasonic Sensor with Arduino .......................................................................................... 24
Interfacing Servo Motor with Arduino .................................................................................................. 26
Interfacing IR Sensor with Arduino. (Home Automation) ..................................................................... 27
1|Page
Basic Components of Arduino UNO board
and their functions.
Here are the basic components of an Arduino board and their functions.
1. Reset Button – This will restart any code that is loaded to the Arduino board
2. AREF – Stands for “Analog Reference” and is used to set an external reference voltage
3. Ground Pin – There are a few ground pins on the Arduino and they all work the same.
4. Digital Input/output – Pins 0-13 can be used for digital input or output
5. PWM – The pins marked with the (~) symbol can simulate analog output
6. USB Connection – Used for powering up your Arduino and uploading sketches
7. TX/RX – Transmit and receive data indication LEDs
8. ATmega Microcontroller – This is the brains and is where the programs are stored
9. Power LED Indicator – This LED lights up anytime the board is plugged in a power
source
10. Voltage Regulator – This controls the amount of voltage going into the Arduino board
11. DC Power Barrel Jack – This is used for powering your Arduino with a power supply
12. 3.3V Pin – This pin supplies 3.3 volts of power to your projects
13. 5V Pin – This pin supplies 5 volts of power to your projects
14. Ground Pins – There are a few ground pins on the Arduino and they all work the same
15. Analog Pins – These pins can read the signal from an analog sensor and convert it to
digital
2|Page
Setup Procedure
Step 1: Install the Arduino Software (IDE)
(Ignore step1 if software is already installed)
For downloading Arduino software go to https://www.arduino.cc/en/Main/Software.
3|Page
Step 4: Select your board
You'll need to select the entry in the Tools > Board menu that corresponds to your
Arduino board as shown in Figure 4.
4|Page
Select the corresponding com port in the
window as shown in Figure 6
5|Page
Arduino interface introduction
A ->Compile
B ->Upload
C ->New
D ->Open
E ->Save
F ->Serial monitor
The setup() method will run once after the Arduino is powered up and the loop() method will
run continuously afterwards. The setup() is where you want to do any initialisation steps, and
in loop() you write the code which you want to run over and over again.
6|Page
Program Structure
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
Void setup()
{
pinMode(13,OUTPUT);
}
In this case pin number 13 is selected and configured pin number 13 as output pin.
In Void loop, the main code which has to run repeatedly has to be written. In our case, we
need to program Arduino to switch ON the inbuilt LED(Connected to 13th pin).
Syntax
digitalWrite(pin,value);
pin: the pin number
Value:HIGH OR LOW
For example you want to keep the LED conned to pin 13 in ON state, we should write
digitalWrite(13,HIGH);
Suppose, we need to write the code which makes the digital pin 13 an OUTPUT and toggles
it by alternating between HIGH and LOW at one second pace.
void setup()
{
pinMode(13,OUTPUT);
}
void loop()
7|Page
{
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
}
8|Page
Step 3: Select Circuits Create new Circuits
9|Page
Step 6: Click on blocks and select text
10 | P a g e
Hands on Experiment No.1:
Experiment Name:
LED Blink
Connection Diagram
12 | P a g e
Hands on Experiment No.2:
Experiment Name:
Figure 11 Potentiometer
Program
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
Reads the value from the specified analog pin.Arduino has 10-bit analog to digital converter. This
means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023.
This yields a resolution between readings of: 5 volts / 1024 units or, .0049 volts (4.9 mV) per unit.
Syntax
analogRead(pin)
Pin: the number of the analog input pin to read from (0 to 5 on most boards)
14 | P a g e
Hands on Experiment No.3:
Experiment Name:
Program:
// 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);
digitalWrite(ledPin,HIGH);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
15 | P a g e
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
Function used
digitalRead()
Description
Reads the value from a specified digital pin, either HIGH or LOW.
Syntax
digitalRead(pin)
Parameters
pin: the number of the digital pin you want to read
Returns
HIGH or LOW
if...else
Description
The if statement checks for a condition and executes the proceeding statement or set of
statements if the condition is 'true'. Otherwise statement present in else part will be executed.
Syntax
if (condition)
{
//statement(s)
}else
{
//Statement(s)
}
Parameters
condition: a boolean expression i.e., can be true or false.
16 | P a g e
Hands on Experiment No.4:
Experiment Name:
Program
//TMP36 Pin Variables
int sensorPin = 0;
//the analog pin the TMP36's Vout (sense) pin is connected //to
//the resolution is 10 mV / degree centigrade with a
//500 mV offset to allow for negative temperatures
/*
* setup() - this function runs once when you turn your Arduino on
* We initialize the serial connection with the computer
*/
void setup()
{
Serial.begin(9600); //Start the serial connection with the computer
//to view the result open the serial monitor
}
Result can be viewed in serial monitor. To open serial monitor, select Tools Serial monitor as
shown in Figure 15.
18 | P a g e
Hands on Experiment No.5
Experiment Name:
Program
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
void setup(){
Serial.begin(9600);
}
void loop(){
char customKey = customKeypad.getKey();
19 | P a g e
if (customKey){
Serial.println(customKey);
}
}
Internal structure of KeyPad Module
Consider we have connected the KEYPAD MODULE to a microcontroller.STEP1: First set all
ROWS to OUTPUT and set them at +5V. Next set all COLUMNS as INPUT to sense the HIGH
logic. Now consider a button is pressed on keypad. And that key is at 2nd COLUMN and 3rd ROW.
With the button being pressed the current flows as shown in Figure 17. With that a voltage of +5V
appears at terminal C2. Since the COLUMN pins are set as INPUTS, the controller can sense C2
going high. The controller can be programmed to remember that C2 going high and the button pressed
is in C2 COLUMN.
STEP2: Next set all COLUMNS to OUTPUT and set them at +5V. Next set all ROWS as INPUT to
sense the HIGH logic. Since the key pressed is at 2nd COLUMN and 3rd ROW. The current flows as
shown in Figure 18.
20 | P a g e
With that current flow a positive voltage of +5V appears at R3 pin. Since all ROWS are set as INPUTS,
the controller can sense +5V at R3 pin. The controller can be programmed to remember the key being
pressed at third ROW of KEYPAD MATRIX.
From previous step, we have known the COLUMN number of key pressed and now we know ROW
number. With that we can match the key being pressed. We can take the key INPUT provided by this
way for 4X4 KEYPAD MODULE.
21 | P a g e
Hands on Experiment No.6
Experiment Name:
1. Connect Arduino pin 0,1,2,3 with D7, D6, D5, D4 pins of LCD display.
2. Connect 4th pin of Arduino with E pin of LCD display.
3. Connect 5th pin of Arduino with RS pin of LCD display.
4. Connect RW, Vss pin of LCD display with ground pin of Arduino.
5. Connect Vcc Pin of LCD display with 5V supply.
6. Connect middle wire of potentiometer with V0. Other two ends of potentiometer are
connected to Vcc and Ground.
7. Anode of LCD display is connected to +5V through 330 Ω resistor and cathode is connected
to Ground
Program:
#include<LiquidCrystal.h>
LiquidCrystal lcd (5, 4, 3, 2, 1, 0); // sets the interfacing pins
void setup ()
{
lcd.begin(16, 2); // initializes the 16x2 LCD
}
void loop ()
{
lcd.setCursor (0, 0); //sets the cursor at row 0 column 0
lcd.print ("Amrita Vishwa"); // prints 16x2 LCD MODULE
lcd.setCursor (0,1); //sets the cursor at row 0 column 1
lcd.print ("Vidyapeetham,NGL");
}
22 | P a g e
The name and functions of each pin of the 16×2 LCD module is given below.
Pin1(Vss):Ground pin of the LCD module.
Pin2(Vcc): Power to LCD module (+5V supply is given to this pin)
Pin3(V0):Contrast adjustment pin. This is done by connecting the ends of a 10K potentimeter
to +5V and ground and then connecting the slider pin to the VEE pin. The voltage at the VEE
pin defines the contrast. The normal setting is between 0.4 and 0.9V.
Pin4(RS):Register select pin. The JHD162A has two registers namely command
register and data register. Logic HIGH at RS pin selects data register and logic LOW at RS
pin selects command register. If we make the RS pin HIGH and feed an input to the data lines
(DB0 to DB7), this input will be treated as data to display on LCD screen. If we make the RS
pin LOW and feed an input to the data lines, then this will be treated as a command ( a
command to be written to LCD controller – like positioning cursor or clear screen or scroll).
Pin5(R/W): Read/Write modes. This pin is used for selecting between read and write modes.
Logic HIGH at this pin activates read mode and logic LOW at this pin activates write mode.
Pin6(E): This pin is meant for enabling the LCD module. A HIGH to LOW signal at this pin
will enable the module.
Pin7(DB0) to Pin14(DB7): These are data pins. The commands and data are fed to the LCD
module though these pins.
Pin15(LED+): Anode of the back light LED. When operated on 5V, a 330 ohm resistor
should be connected in series to this pin.
Pin16(LED-): Cathode of the back light LED.
23 | P a g e
Hands on Experiment No.7
Experiment Name:
Description: The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object. It
comes with ultrasonic transmitter and receiver modules. Its range of detection is between 2cm to
400cm or 1’’ to 13ft. Its trigger input pulse width is 10µs.
Program
// Ultrasonic Sensor HC-SR04 interfacing with Arduino.
// defining the pins
const int trigPin = 11;
const int echoPin = 12; // defining variables long duration; int distance; void setup()
{
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an
Input Serial.begin(9600); // Starts the serial communication
}
void loop()
{ // Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
24 | P a g e
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.0343/2; // Prints the distance on the Serial Monitor
Serial.print("Distance in cm: ");
Serial.println(distance);
delay(500);
}
25 | P a g e
Hands on Experiment No.8
Experiment Name:
Program
// Include the Servo library
#include <Servo.h>
// Declare the Servo pin
int servoPin = 3;
// Create a servo object
Servo Servo1;
void setup() {
// We need to attach the servo to the used pin number
Servo1.attach(servoPin);
}
void loop(){
// Make servo go to 0 degrees
Servo1.write(0);
delay(3000);
// Make servo go to 90 degrees
Servo1.write(90);
delay(3000);
// Make servo go to 180 degrees
Servo1.write(180);
delay(1000);
}
26 | P a g e
Hands on Experiment No.9
Experiment Name:
Program
#include <IRremote.hpp>
const int rcvPin=3;
IRrecv irrecv(rcvPin);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
pinMode(5, OUTPUT);
pinMode(12,OUTPUT);
pinMode(11,OUTPUT);
27 | P a g e
void loop() {
if(IrReceiver.decode()) {
auto value= IrReceiver.decodedIRData.decodedRawData;
//switch(results.value)
switch(value)
{
case 4010852096:
Serial.println("1"); // Button 1
digitalWrite(5,HIGH);
break;
case 3944005376:
Serial.println("4"); // Button 1
digitalWrite(11,HIGH);
break;
case 3877158656:
Serial.println("7"); // Button 1
digitalWrite(12,HIGH);
break;
28 | P a g e
Following Program can be used to identify the code for IR remote buttons
#include <IRremote.hpp>
const int rcvPin=3;
IRrecv irrecv(rcvPin);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
void loop() {
if(IrReceiver.decode()) {
auto value= IrReceiver.decodedIRData.decodedRawData;
Serial.println(value);
IrReceiver.resume(); // Receive the next value
}
}
29 | P a g e