ATL Arduino Homework Activity Workbook - Prof. Dattaraj Vidyasagar Vidyasagar Academy

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

ATL Arduino Activity Workbook

32

ATL Arduino Homework Activity Workbook – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in
ATL Arduino Activity Workbook

ATL Arduino Homework Activity Workbook – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in
ATL Arduino Activity Workbook

Introduction to Arduino
1. Arduino is an open source platform for robotics
2. Uses ATMega328p microcontroller
3. Arduino UNO R3 is the clone of original Arduino UNO
4. Special Arduino programming language
5. Simplified form of C/C++ language
6. Simple programming language
7. Easy connections to make any circuit
8. Easy interface of sensors with Arduino
9. Compatible with AVR Programming…!

Internal Structure of Arduino UNO

Connection Details

1  7-12V DC supply, 2  USB-B socket (connect data cable), 3  ATMega328p microcontroller,


4  16MHz crystal, 5  5V regulator, 6  digital pins (13 pins), 7  analog pins (5 pins),
8  RESET switch (to reset the Arduino UNO and start the code from the beginning)

Features of Arduino

1. Operating voltage = 5V 8. Digital pins = 14 pins


2. Input voltage = 7V-12V 9. PWM output = 6 pins
3. Consumption = 200mA 10. Flash memory = 32kB
4. Output pin current = 40mA 11. SRAM (Static RAM) = 2kB
5. AVR compatible design 12. EPROM = 1kB
6. Internal/External RESET 13. Clock frequency = 16MHz
7. Compatible output level for other 14. Analog input = 6 pins
microcontrollers 15. In-built LED @ pin-13

ATL Arduino Homework Activity Workbook – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in
Vidyasagar Academy’s STEM Education Worksheet

Coding Details of Arduino

These details are universally applicable to any type of Arduino like UNO, nano, Mega, etc. We can write
Arduino code in AVR style also.

Human Comments

/*
* Project of Servo Motor Control
* Date: 27 August, 2019;
* Vidyasagar Academy, Akola
*/
#include <Servo.h> // library file linked

Linking a file in the program

All header files must be linked at the beginning of program, below the top comment. Examples are given below:

#include <LiquidCrystal.h>
#include <Servo.h>

Definition Section

Define all the connections and other details below Link Section, as given in following examples:

#define LED 13
#define PotPin A0
#define SS 1

Global Declaration

int i=3; // 2-4 bytes


float a=7.35; // 4 bytes
short j; // 2 bytes

Keywords

Just remember these keyboards given below. They are required in different Arduino programming:

void, int, float, if, else, while, for, Servo, continue, long, short,
signed, unsigned, return, break, register, case, switch, double, char,
static, default, volatile, const,

Useful Functions

Functions in Arduino execute a particular action. There are lot of functions used in Arduino programming.
Some regularly required functions are given below.

delay(1000); // 1 second delay


digitalWrite(variable,action);
digitalRead(assigned variable);
analogRead(assigned variable);
void setup()
void loop()

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet
Body of Arduino Program

void setup()
{
Write all commands here which you want to initialize at the beginning
of program. This function runs “once” only.
}

Example of void setup() function

void setup()
{
pinMode(LED,OUTPUT);
pinMode(SS,INPUT);
pinMode(variable,MODE);
}

Example of void setup() function

You can use “for” loop in void setup() function to rotate the assignments of pins, as follows:

int LEDPins[]={0,1,2,3,4,5,6,7};
void setup() {
for(int i=0;i<8;i++)
{
pinMode(LEDPins[i],OUTPUT);
}
}

Example of void loop() function

This function is also known as infinite loop. Whatever actions you write in this loop, will be executed again and
again.

Void loop(){
Write all commands here to perform repeatedly
}

Character Set in C Language

There are capital alphabets (A to Z) and small alphabets (a to z), 0 to 9 – in all ten digits and some special
characters. The list of special characters are given below –

+ Plus Sign - Minus * Asterisk


/ Slash % Percentage ^ Caret or cap
\ Back Slash $ Dollar ? Question Mark
! Exclamation # Hash ~ Tilde sign
_ Underscore | Vertical pipe & Ampersand
, Comma . Period Sign ; Semicolon
: Colon ‘ Apostrophe “ Quotation
< Less Than > Greater Than ( Left Parenthesis
) Right Parenthesis { Left brace } Right brace
[ Left Bracket ] Right bracket = Equal

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet
Rules of using variables

A variable in ‘C’ is an identifier, whose value may change during program execution is called as memory
variable. There are rules about variable names, as follows:

1. A variable name must start with an alphabet.


2. A variable name can be up to 10 characters long.
3. A to Z, a to z alphabets and 0 to 9 digits and a special character ‘underscore’ ( _ ) can be used in a
variable name.
4. Blank spaces are NOT ALLOWED in a variable name.

Note: All the variables must be declared in the definition section of every program.

What is DataType?

Data: The collection of facts and figures is called data.

Data type: The type of value stored in a variable is called as data type. In ‘C’, there are different types of
datatypes, as given below. In any program, the datatype declares the type of data that the variable can hold.

1) int – it is integer datatype. It requires memory of 2 bytes.


2) float – it is used to store decimal numbers with floating point. It requires memory of 4 bytes. It has
single point precision.
3) double – it is used to store decimal numbers with floating point. It requires memory of 8 bytes.

In this way, there are number of datatypes with different memory allocation.

Basic Commands in Arduino

Arduino UNO Dev. Board contains one built-in LED. It is connected to pin-13 internally. We shall write a
simple program to blink this LED. We shall also adjust the value in delay() function to blink this LED with
different time periods. Get ready with paper and pencil or your PC.

In-built LED
connected at pin-13

int LED=13; // assigning pin-13 to variable “LED”


void setup()
{
pinMode(LED, OUTPUT); // defining pin-13 as output pin
}
void loop() // infinite loop to repeat the action continuously
{
digitalWrite(LED, HIGH); // sending “HIGH” signal to LED
delay(1000); // delay of 1000ms = 1 sec.
digitalWrite(LED, LOW); // sending “LOW” signal to LED
delay(1000); // delay of 1000ms = 1 sec.
}

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet
Blinking 8 LEDs simultaneously

 To write program for blinking 8 LEDs for above given circuit, you have to define 8 variables first.
 Then you have to write 8 pinMode statements.
 Then you have to write 8+8=16 digitalWrite commands to make the LEDs on/off.
 This is sooooooo boring…!

Actual program for blinking 8 LEDs simultaneously

int LED0=0;
int LED1=1;
int LED2=2;
int LED3=3;
int LED4=4;
int LED5=5;
int LED6=6;
int LED7=7;
void setup() {
pinMode(LED0, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(LED7, OUTPUT);
}
void loop() {
digitalWrite(LED0, HIGH);
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
digitalWrite(LED7, HIGH);
delay(1000);

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet

digitalWrite(LED0, LOW);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);
delay(1000);
}

Trick of using userDefined_Function() function

Such types of functions are called as User Defined Functions (UDFs). They are NOT the default functions of
Arduino. The userDefined_Function() function can be used in the program as follows:

int LED0=0;
int LED1=1;
int LED2=2;
int LED3=3;
int LED4=4;
int LED5=5;
int LED6=6;
int LED7=7;

void allLEDs_ON()
{
digitalWrite(LED0, HIGH);
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
digitalWrite(LED7, HIGH);
}

void allLEDs_OFF()
{
digitalWrite(LED0, LOW);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);
}

void setup()
{
pinMode(LED0, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(LED7, OUTPUT);
}

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet

void loop() // infinite loop


{
allLEDs_ON();
delay(1000);

allLEDs_OFF()
delay(1000);
}

Note: Though the use of user defined functions is good, but still its boring. If we have to use the UDF many
times in a program, then UDFs are recommended. But if you want to use the UDF only once, in the program,
then this trick is still boring…!

Simultaneous Simulation Method

This method is also called as Direct Port Register Addressing Technique. In this technique we use the
programming methodology of AVR microcontroller programming. So to understand this method, we shall see
the basics of AVR programming first. Remember that C & C++ commands are used in AVR.

The pinout of ATMega328p is given below.


AVcc
Aref
PC5

PC4

PC3

PC2

PC1

PC0

Vcc

PB5

PB4

PB3

PB2

PB1
28

27

26

25

24

23

22

21

20

19

18

17

16

15
A T M e g a 3 2 8 p
10

12

13

14
11
1

9
Gnd
PD0

PD2

PB7

PD7

PB0
RST

PD1

PD3

PD4

Vcc

PB6

PD5

PD6

Unlike Arduino, in AVR programming technique, there are I/O PORTs instead of pins. The Arduino UNO uses
ATMega328p microcontroller. It has 3 I/O PORTs i.e. INPUT-OUTPUT Ports: PORTB, PORTC & PORTD

1. PORTB (8-pins): PB7 to PB0. First pin is PB7 and last pin is PB0 i.e. PB7 is at MSB and PB0 at LSB.
2. PORTC (6-pins): PC5 to PC0. First pin is PC5 and last pin is PC0 i.e. PC5 is at MSB and PC0 at LSB.
3. PORTD (8-pins): PD7 to PD0. First pin is PD7 and last pin is PD0 i.e. PD7 is at MSB and PD0 at LSB.

Registers in ATMega 328p

Register Name Type Remark

DDR Read/Write 1=output, 0=input

PORT Read/Write ---


PIN Read/Write ---

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet
Direct I/O PORT Register Addressing (DPRA) Technique

The Arduino can be coded using Direct PORT Register Addressing. For that we have to understand the
architecture of Arduino in terms of PORTs, as shown below:

Direct PORT Register Addressing Code

MSB Position LSB Position

PORTD: PD7 PD6 PD5 PD4 PD3 PD2 PD1 PD0


PORTD=0b11111111;

void setup() {
DDRD=0b11111111; // all pins of PORTD defined as output pins
}
void loop() {
PORTD=0b00000000; // LOW signal is sent on all pins of PORTD
delay(1000);
PORTD=0b11111111; // HIGH signal is sent on all pins of PORTD
delay(1000);
}

Exercise

1. Write different codes to produce decorative effects in 8 LEDs array using DPRA.
2. At least 5 different codes must be written by the student.

Servo Motor Control in Arduino

A servo motor is a device that contains an encoder which converts the mechanical motion into digital pulses.
Servo motor works on the PWM (Pulse Width Modulation) principle, which means its angle of rotation, is
controlled by the duration of pulse applied to its control PIN. Basically servo motor is made up of
DC motor which is controlled by a variable resistor (potentiometer) and some gears.

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet
Pinout of Servo Motor

Miniature Servo Motor SG-90 Pinout of its connection socket

How to connect Servo to Arduino?

It is recommended to connect servo motor to PWM pin of Arduino. But it is not mandatory. You can connect it
to other digital pins also.

Following circuit shows a simple connection of servo motor to pin-0 of Arduino and its positive (+) negative (-)
terminals are connected to 5V and Gnd terminals of Arduino. Use the give code to control the servo motor.

The Code

#include <Servo.h> Rotate Servo motor in reverse direction


Servo i;
void setup() { void loop(){
i.attach(0); i.write(0);
} delay(1000);
void loop() { i.write(90);
i.write(0); delay(1000);
delay(1000); i.write(45);
i.write(90); delay(1000);
delay(1000); }
}
Note: Do not use negative sign to rotate servo in reverse direction. Count the angle from 0 to 180

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet
Code to control multiple servo motors

#include <Servo.h>
Servo motor1,motor2,motor3; // creating servo objects
void setup(){
motor1.attach(3);
motor2.attach(5);
motor3.attach(6);}
void loop(){
motor1.write(0);
delay(1000);
motor2.write(90);
delay(1000);
motor3.write(180);}

Mapping Technique in Arduino

Mapping in Arduino converts one range into another (ADC). It uses following syntax for the same.

Map(value, fromLow, fromHigh, toLow, toHigh)

POT Controlled Servo Motor

We can control the servo motor using simple potentiometer. When we rotate the pot the servo motor rotates
proportionally. Such type of arrangement is typically used in manual robotic arm controlling systems like in
surgical robots, industrial robots.

The Code

#include <Servo.h>
Servo i; // create servo object
int potpin=0;
int pot_voltage;
void setup() {
i.attach(9);
}
void loop() {
pot_voltage=analogRead(potpin);
pot_voltage=map(pot_voltage,0,1023,0,255); // i.e. 210 = 1024  28 = 256
i.write(pot_voltage);
delay(15);
}

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet
Automatic CCTV Camera Controlling Servo Code

int position=0;
void loop()
{
for(position=0; position<=180; position++) {
i.write(position);
delay(50);
}
delay(1000);

for(position=180; position>=0; position--) {


i.write(position);
delay(50);
}
delay(1000);
}

Note: with this code, CCTV camera will rotate from 0 – 180 and then 180  – 0 to watch entire area.

How to use with Arduino?

Many students find it difficult to use LCD display with Arduino due to large number of wire connections. We
shall see simple & effective method of using LCD display with Arduino.

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet
LCD Display Simple Code

#include <LiquidCrystal.h> Alternately you can use following simple


const int rs=2,en=3,d4=4,d5=5,d6=6,d7=7; code for LCD display.
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
#include "LiquidCrystal.h"
int i;
LiquidCrystal lcd(1,2,3,4,5,6);
void setup() {
lcd.begin(16,2); // the LCD display is initiated
}

void loop() {
lcd.clear(); // clears previous message on the display
lcd.setCursor(0,0); // first row selected
lcd.print(“*VSAGAR ACADEMY*”);
delay(3000);
lcd.setCursor(0,1); // second row
lcd.print(“ www.vsagar.org “);
delay(3000);

// display decimal counter (0-9) in second row


for(i=0;i<10;i++)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“*Decimal Digits*”);
lcd.setCursor(0,1);
lcd.print(“Decimal Count: “);
lcd.print(i);
delay(1000);
}
}

How to scroll the message on LCD Display?

void loop() {
for (int positionCounter = 0; positionCounter < 13; positionCounter++)
{
lcd.scrollDisplayLeft(); // or use lcd.scrollDisplayRight();
delay(400);
}

How to read servo position?

When we control servo motor through a pot, it is necessary to know the position of the servo for accurate
control. To obtain the correct position, we can use this code. To read the correct position of servo, we use the
default function: servo.read(); as given below.

#include <Servo.h>
#include <LiquidCrystal.h>
const int rs=2, en=3, d4=4, d5=5, d6=6, d7=7;

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
Servo I; // create servo object

int potpin=0; // A0 pin is assigned to the center terminal of pot


int value; // variable to record the changing value of pot voltage

void setup() {
i.attach(9); // servo motor attached to pin-9
lcd.begin(16,2);
}

void loop() {
value=analogRead(potpin); // read pot voltage
// and store in “value” variable
value=map(value, 0, 1023, 0, 255); // ADC process
i.write(value); // controlling servo as per the value of potpin
i.read();
lcd.clear();
lcd.print(i.read());
delay(15); // perceptible pause
}

Note: this function is useful in CCTV cameras to read the correct positioning or direction of camera.

Procedure to create New Project in Arduino Software

1) First open Arduino 1.8.5 Software.


2) Click on File > New to create new file for your project.
3) Write the above given code into it and then save the project as “single blinking LED”.
4) Then click on Verify button to check for any errors in your code.
5) If there are no errors, you will see the message “Done compiling”.
6) Now your code is ready to upload into Arduino UNO dev. Board.
7) Connect your Arduino UNO dev. Board to your PC/Laptop through data cable.
8) Now click on Tools > Port to select the COM port. Click on the available COM port.
9) Now finally click on Upload button to burn the compiled code into Arduino UNO.
10) When program is uploaded, you will see message “Done uploading”.
11) Now observe the built-in LED on your Arduino dev. Board. It will start blinking.
12) You have successfully completed the project. CONGRATS...!
13) You can download complete code in .txt format from this link: https://goo.gl/2rG8TA

____________________________________

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet

Precise Temperature Measurement

Problem Statement

Write an embedded C Program to measure the real time temperature using NTC Thermister.

Objectives

1) To understand the structure of servo motor and its connections.


2) To learn the use of <math.h> header file and its library commands.
3) To learn the use of Serial.print function to print output on serial monitor and serial plotter.

Description

In this project, the thermister of 10kΩ/25C and a resistor of 10kΩ form a potential divider. The centre point of
this circuit connected to analog input pin A0, as shown below.

10kΩ

The voltage across 10kΩ resistor, which is connected to pin A0 is given by:

10𝑘Ω 10𝑘Ω×5V
𝑉=( ) × 5V ∴ 10𝑘Ω + 𝑅𝑇ℎ =
10𝑘Ω+𝑅𝑇ℎ V

10𝑘Ω × 5V
∴ 𝑅𝑇ℎ = − 10𝑘Ω
V

This voltage is analog voltage. It is converted into digital by using the ADC process. Its formula is given by:

𝐴𝐷𝐶 = 𝑎𝑛𝑎𝑙𝑜𝑔𝑅𝑒𝑎𝑑(𝑉)

The analogRead() function reads the value from A0. It’s a 10-bit analog to digital converter in Arduino
UNO. This means that it will map input voltages between 0 and the operating voltage of 5V into integer values
between 0 and 1023. For example, 5V/ 1024 units = 4.9 mV per unit.

5V × ADC
Vo =
1023

Finally the standard Steinhart-Hart Formula for thermister is given by:

1
𝑇𝑒𝑚𝑝𝑒𝑟𝑎𝑡𝑢𝑟𝑒 𝑖𝑛 𝐾𝑒𝑙𝑣𝑖𝑛 =
(𝐴 + (𝐵. log 𝑅𝑇ℎ ) + (𝐶. (log 𝑅𝑇ℎ )3))

𝐴 = 0.001129148 𝐵 = 0.000234125 𝐶 = 8.76741 × 10−8

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet
Required Material

Arduino UNO Dev. Board – 1, Breadboard – 1, Jumper wires, 10kΩ/25C thermister, 10kΩ resistor, LCD
display, data cable, PC/Laptop, etc.

The Code

#include <math.h>
#include <LiquidCrystal.h>
const int rs=2, en=3, d4=4, d5=5, d6=6, d7=7;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);

void setup() {
Serial.begin(9600);
lcd.begin(16,2);
}

void loop() {
int ADCvalue;
double Vo, RTh, Log_RTh, Temp;

ADCvalue=analogRead(A0); // getting ADC voltage value across 10k


resistor at pin A0
Vo=((ADCvalue*5.0)/1023.0); // it is the voltage measured by ADC
RTh=((5*(10000.0/Vo))-10000.0); // 10k resistance value converted into
ohms i.e. 10000 ohms

Log_RTh=log(RTh); // taking logarithm of RTh value

// Temperature in Kalvin is calculated as follows:

Temp=(1/(0.001129148+(0.000234125*Log_RTh)+(0.0000000876741*Log_RTh*Log_RTh
*Log_RTh)));

// Converting Temperature value into Degree Celsius as follows:


Temp=(Temp-273.15);

// Printing the calculated output on LCD Display


lcd.setCursor(0,0);
lcd.print("Temp= ");
lcd.print(Temp);
lcd.print((char) 223);
lcd.print(" ");
lcd.print(223);
lcd.setCursor(0,1);
// print the number of seconds since reset:
lcd.print("RTh= ");
lcd.print(RTh);

delay(2000);
}

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet

Ultrasonic Distance Measurement

Problem Statement

Write an embedded C Program to measure the accurate distance between ultrasonic sensor and the object.

Objectives

1) To learn the triggering and echo reading techniques of US sensor


2) To learn the use of LCD display with US sensor

Description

In this project we shall learn how to trigger and receive the echo signal of Ultrasonic sensor.

Required Material

Arduino UNO Dev. Board – 1, Breadboard – 1, LCD Display, Ultrasonic sensor, Jumper wires, servo motors,
data cable, PC/Laptop, measuring scale, etc.

Connection Diagram

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet
The Code

#include <LiquidCrystal.h>
#define trigger 9 // trigger pin connected to pin-9
#define echo 10 // echo pin connected to pin-10

const int rs=2, en=3, d4=4, d5=5, d6=6, d7=7;


LiquidCrystal lcd(rs,en,d4,d5,d6,d7);

float time=0, distance=0; // time and distance variables

void setup() {
lcd.begin(16,2);
pinMode(trigger,OUTPUT);
pinMode(echo,INPUT);
}

void loop() {
lcd.clear(); // clear the LCD display first

// US Sensor is triggered for 1 microseconds


digitalWrite(trigger,LOW);
delayMicroseconds(1);
digitalWrite(trigger,HIGH);
delayMicroseconds(1);
digitalWrite(trigger,LOW); // triggering stopped
delayMicroseconds(1);

// echo received
time=pulseIn(echo,HIGH);

// formula to calculate the distance


distance=((time/2)/29.412);

// printing the result on LCD display


lcd.clear();
lcd.print("Distance:");
lcd.print(distance); // distance in centimeter
lcd.print("cm");

lcd.setCursor(0,1);
lcd.print("Distance:");
lcd.print(distance/100); // distance in meter
lcd.print("m");
delay(1000);
}

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet

Arduino UNO: DTMF Controlling System

Problem Statement

Write an embedded C Program to control relay switch and a high power bulb as per the DTMF signal received
in Arudino UNO dev. board. When a ‘1’ is pressed on mobile, the relay must be on and when ‘0’ or any other
switch is pressed, the relay must be off.

Objectives

1) To understand and use DTMF signal coding.


2) To study the use of if conditional statements in program.
3) To understand how to extend this task for number of relays and electrical appliances for controlling.

Description

Here we use the DTMF sensor which has 4 outputs D3, D2, D1 and D0. These outputs are connected to Arduino
pins 8,9,10,11 respectively. The DTMF signal input is applied to this sensor through a good quality mobile
phone through its earphone socket. The relay controlling switch is connected to pin-0. The complete connection
diagram is given below.

Required Material

Arduino UNO Dev. Board – 1, Breadboard – 1, Jumper wires, Relay switch – 1, 60W/100W 230V bulb, data
cable, PC/Laptop, connecting wires for bulb, etc.

Connection Diagram

Make the necessary connections and with the respective pins of Arduino UNO Dev. Board as shown below:

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet
Relay Driver Circuit

The Code
Button D3 D2 D1 D0 Decimal
#define Relay_Sw 0
#define D3 8 0 1 0 1 0 10
#define D2 9 1 0 0 0 1 1
#define D1 10 2 0 0 1 0 2
#define D0 11
3 0 0 1 1 3
void setup() 4 0 1 0 0 4
{ 5 0 1 0 1 5
pinMode(D3,INPUT); 6 0 1 1 0 6
pinMode(D2,INPUT);
pinMode(D1,INPUT); 7 0 1 1 1 7
pinMode(D0,INPUT); 8 1 0 0 0 8
pinMode(Relay_Sw,OUTPUT); 9 1 0 0 1 9
}
* 1 0 1 1 11
void loop() # 1 1 0 0 12
{
// when '1' is pressed on mobile
if(!(digitalRead(D3)) && !(digitalRead(D2)) && !(digitalRead(D1)) &&
(digitalRead(D0)))
{
digitalWrite(Relay_Sw,HIGH); // relay switch is ON
}

// when '0' is pressed on mobile


if(!(digitalRead(D3)) && !(digitalRead(D2)) && (digitalRead(D1)) &&
!(digitalRead(D0)))
{
digitalWrite(Relay_Sw,LOW);
delay(1000);
}

// when any other number is pressed, relay switch becomes OFF


else
digitalWrite(Relay_Sw,LOW);
}

More Problem Statements

1) Write an embedded C Program to control 4 different electrical appliances using Arduino UNO with
individual on/off system with the help of DTMF signals.
2) Write an embedded C Program to control 4 different electrical appliances using Arduino UNO with
simultaneous on/off system, with the help of DTMF signals.

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet

Sound Sensor with Arduino UNO

Problem Statement

Write an embedded C Program to control an LED by the signal of sound sensor. The sound sensor receives
sharp sound. When it receives sharp sound, its output either becomes 1/0 and then within a short time 0/1.

Objectives

1) To understand the use of sound sensor.


2) To study the use of if conditional statements in program.
3) To understand how to write code for more complex controlling using sound sensor.

Description

In this project, the output of sound sensor is connected to pin-0 of Arduino. When the sensor receives sharp
sound its output becomes 0 (in some type of sound sensors its output becomes 1). This output remains for a very
short time. When the sound stops, the output of the sensor becomes 1. Now we have to use this property of the
sound sensor to control the in-built LED at pin-13 of the Arduino Dev. Board.

Required Material

Arduino UNO Dev. Board – 1, Breadboard – 1, Jumper wires, Sound Sensor – 1, LED – 1, resistor 330Ω – 1,
data cable, PC/Laptop, connecting wires, etc.

Connection Diagram

Make the necessary connections and with the respective pins of Arduino UNO Dev. Board as shown below:

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet
The Code

#define SS 0 // sound sensor connected to pin-0


#define LED 13 // LED connected to pin-13

void setup()
{
pinMode(SS,INPUT);
pinMode(LED,OUTPUT);
}

void loop()
{
if(digitalRead(SS)) // if sensor output becomes ‘0’ with sharp sound
{
digitalWrite(LED,HIGH); // LED becomes ON
delay(5000);
}

else
digitalWrite(LED,LOW); // otherwise the LED is OFF
}

More Problem Statements

1) Write an embedded C Program to control two LEDs such that when one clap is produced, the first LED
glows. When two claps are produced one after another quickly, the second LED glows. When three
claps are produced one after another quickly, both LEDs become OFF.
2) Connect LCD display in this project count the number claps produced during a time of say 1 minute.
Display the count on the LCD display.

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet

Study of Serial Monitor & Plotter

Problem Statement

With the help of serial monitor and plotter in Arduino Software, we can actually visualise the real time changes
in our program. Write a program to study the serial monitor and plotter given in Arduino Software.

Objectives

1) To learn the commands of serial monitor and plotter.


2) To understand the use of serial monitor and plotter.
3) To understand the use of mapping in Arduino coding.

Description

In this project, we shall connect a pot to analog pin-0 of Arduino UNO. When we shall rotate this pot, its
voltage will change proportionally. With the help of mapping (in the form of a formula:
writeValue=(255.0/1023.0)*readValue;) we shall convert this analog value into proportional
digital signal. Then we shall write this calculated value on the LED to control its brightness and also observe
this activity on Serial Monitor/Plotter.

Required Material

Arduino UNO Dev. Board – 1, Breadboard – 1, Jumper wires, Sound Sensor – 1, LED – 1, Pot 10kΩ – 1, data
cable, PC/Laptop, connecting wires, etc. (any value of pot from 1kΩ to 1MΩ can be used).

Connection Diagram

Make the necessary connections and with the respective pins of Arduino UNO Dev. Board as shown below:

www.vsagar.org

www.vsagar.org

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet
The Code

int potPin=A0; // connect middle pin of pot to A0


int LED=9; // connect LED to pin-9. Ground its cathode.
int readValue; // variable to read pot voltage
int writeValue; // variable to write this value to control LED brightness

void setup()
{
pinMode(potPin,INPUT);
pinMode(LED,OUTPUT);
Serial.begin(9600); // turn on Serial Port
}

void loop()
{
readValue=analogRead(potPin); // read pot voltage
writeValue=(255.0/1023.0)*readValue; // calculate write value for LED
analogWrite(LED,writeValue); // write to the LED
Serial.print("Writing value: "); // print values on serial monitor
Serial.println(writeValue);
}

The Mathematics behind the code

In this code, simple linear relationship is used and it allows us to use the skills we have learned in math class.
The following graph explains the relationship:

We shall find the equation of line, as follows: 255


(1023,255)
𝑦2 − 𝑦1 255 − 0 255
𝑠𝑙𝑜𝑝𝑒 = 𝑚 = = =
𝑥2 − 𝑥1 1023 − 0 1023
analogWrite values

Point slope form of line is given by:

𝑦2 − 𝑦1 = 𝑚. (𝑥2 − 𝑥1 )

255
∴ 𝑦2 − 0 = . (𝑥2 − 0)
1023
(0,0)
255 1023
i.e. 𝑦2 = ( ). 𝑥2 analogRead values
1023

So in our above given code, the values are calculated as follows:

255
𝑤𝑟𝑖𝑡𝑒𝑣𝑎𝑙𝑢𝑒 = ( ) . 𝑅𝑒𝑎𝑑𝑉𝑎𝑙𝑢𝑒
1023

More Problem Statements

1) Can you control the rotational angle of servo using this code? Why?
2) Write a program to control the brightness of two LEDs such that when brightness of one LED increases
the brightness of the other will decrease proportionately.

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet

Toggle Switch with LED Project

Problem Statement

Write an embedded C Program to control single LED with the help of a switch. The LED must remain ON when
the switch is pressed once & released. When the switch is pressed again the LED must be OFF and remain OFF
till again the switch is pressed.

Objectives

1) To use digitalRead()statement in program.


2) To study the use of if conditional statements in program.
3) To extend this task for number of LEDs and switches.

Description

We connect one RED LED to the Arduino Dev. Board with a simple push-to-on type switch. The switch is
connected to a pull-up resistor of 2.2kΩ to avoid accidental triggering of the LED. The other resistor of 330Ω is
used to limit the current flowing through the LED.

Required Material

Arduino UNO Dev. Board – 1, Breadboard – 1, Jumper wires – 6, LEDs – 1, Push-to-on Switch – 1, Resistors
330Ω – 1, 2.2kΩ – 1, data cable, PC/Laptop

Connection Diagram

Make the necessary connections on breadboard and with the respective pins of Arduino UNO Dev. Board as
shown below:

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet
The Code

/*
* Program of Toggle Switch with LED
*/

int inPin = 2; // pin 2 is connected to one point of switch


// and other point of switch to ground

int outPin = 7; // connect anode of LED to pin-7 through resistor (330Ω)


// and connect cathode of LED to grounds

int state = HIGH; // the current state of the output pin


int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin

long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase this if LED flickers

void setup()
{
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
}

void loop()
{
reading = digitalRead(inPin);

if (reading == HIGH && previous == LOW && millis() - time > debounce)
{
if (state == HIGH)
{
state = LOW;
}
else
state = HIGH;

time = millis();
}

digitalWrite(outPin, state);

previous = reading;
}

More Problem Statements

1) Write an embedded C Program to control single LED such that when the switch is pressed & released
once, the LED should be ON and start blinking at 1 sec rate for 5 times. Then the LED should remain
ON until the switch is pressed again. Now the LED should be OFF and remain OFF until the switch is
pressed again to repeat the action.
2) Write an embedded C Program to control two LEDs with two different switches. The action for each
switch should be toggling action.

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet

Bluetooth Controlled Motor

Problem Statement

Write the program of Bluetooth controlled DC motor and study the working of Bluetooth sensor.

Objectives

1) To learn the use of Bluetooth sensor.


2) To understand the use of Bluetooth application.
3) To learn the coding of Bluetooth sensor with Arduino.

Precautions

1) You need to remove the RX and TX cables when you’re uploading the sketch to your Arduino.
2) Sometimes people connect the TX from the Bluetooth module to the TX of the Arduino – that is wrong
and it won’t work.
3) Make sure you connect it properly, the TX into RX and the RX into the TX.

Note: If the HC-05 Bluetooth Module asks for a password, enter the password as: 1234

Required Material

Arduino UNO Dev. Board – 1, Breadboard – 1, Jumper wires, DC Motor – 1, Bluetooth Module (HC-05),
Smartphone (any Android phone), data cable, PC/Laptop, connecting wires, etc.

Connection Diagram

Make the necessary connections and with the respective pins of Arduino UNO Dev. Board as shown below:

www.vsagar.org

www.vsagar.org

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet
The Code

int motorPin1 = 3; // pin 2 on L293D IC


int motorPin2 = 4; // pin 7 on L293D IC
int enablePin = 5; // pin 1 on L293D IC
int state;
int flag=0; // make sure that the serial only prints once the state

void setup() {
// sets the pins as outputs:
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
// sets enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

void loop() {
//if some date is sent, reads it and saves in state
if(Serial.available() > 0){
state = Serial.read();
flag=0;
}
// if the state is '0' the DC motor will turn off
if (state == '0') {
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
if(flag == 0){
Serial.println("Motor: off");
flag=1;
}
}
// if the state is '1' the motor will turn right
else if (state == '1') {
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high
if(flag == 0){
Serial.println("Motor: right");
flag=1;
}
}
// if the state is '2' the motor will turn left
else if (state == '2') {
digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
if(flag == 0){
Serial.println("Motor: left");
flag=1;
}
}
}

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet
Bluetooth Application for Android Phone

For the android communication with our Bluetooth module we have to use the BlueTerm app, It’s completely
free, so you just need to go to “Play store” and download it. Then you just need to connect your smarthphone
with the Bluetooth module. Remember to remove the TX and RX cables.

The 3 Commands

1) When we press ‘0’ on the Smartphone app, the DC motor is OFF.


2) When we press ‘1’, the motor rotates in clockwise direction.
3) When we press ‘2’, the motor rotates in anti-clockwise direction.
4) You can change the commands values in the code.

Motor Driver IC L293D Pinout

Following diagram is given to control 2 DC motors simultaneously. LM means left motor, RM means right
motor. You can connect 2 motors to control a 2-wheeled robot using Bluetooth sensor.

More Problem Statements

1) Learn the pinout of L293D motor driver IC and try to control 2 motors by doing modifications in the
code. Connect 2 motors to the IC after understanding its pinout.
2) Can we control a Servo motor using Bluetooth? Search for the solution.

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet

7-Segment LED Display with Arduino

Problem Statement

Write the program to display decimal digits from 0 – 9 on common cathode LED display.

Objectives

1) Learn the pinout of 7-segment LED display.


2) To understand the use of switch()and case() functions.

Required Material

Arduino UNO Dev. Board – 1, Breadboard – 1, Jumper wires, Resistor 100Ω – 1, Common cathode LED
display – 1, data cable, PC/Laptop, connecting wires, etc.

Connection Diagram

Make the necessary connections and with the respective pins of Arduino UNO Dev. Board as shown below:

Binary signals to LED Display

Digit PD7 - X PD6 – g PD5 – f PD4 – e PD3 – d PD2 – c PD1 – b PD0 – a


0 0 0 1 1 1 1 1 1
1 0 0 0 0 0 1 1 0
2 0 1 0 1 1 0 1 1
3 0 1 0 0 1 1 1 1
4 0 1 1 0 0 1 1 0
5 0 1 1 0 1 1 0 1
6 0 1 1 1 1 1 0 1
7 0 0 1 0 0 1 1 1
8 0 1 1 1 1 1 1 1
9 0 1 1 0 1 1 1 1

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet
The Code

int i; // variable for counting 10 cases

void setup() {
DDRD=0b01111111; // PD6-PD0 output pins, PD7 not used
}

MSB Posi on LSB Posi on


void loop() {
for(i=0;i<10;i++) PORTD: PD7 PD6 PD5 PD4 PD3 PD2 PD1 PD0
{
PORTD=0b11111111;
delay(1000);

switch(i)
{
case 0:
PORTD=0b00111111; // '0' is displayed
break;

case 1:
PORTD=0b00000110; // '1' is displayed
break;

case 2:
PORTD=0b01011011; // '2' is displayed
break;

case 3:
PORTD=0b01001111; // '3' is displayed
break;

case 4:
PORTD=0b01100110; // '4' is displayed
break;

case 5:
PORTD=0b01101101; // '5' is displayed
break;

case 6:
PORTD=0b01111101; // '6' is displayed
break;

case 7:
PORTD=0b00100111; // '7' is displayed
break;

case 8:
PORTD=0b01111111; // '8' is displayed
break;

case 9:
PORTD=0b01101111; // '9' is displayed
break;
} // switch closed
} // “for” loop closed
} // void loop closed

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet

DC motor Speed Control: PWM Technique

Problem Statement

Write the program to control the speed of DC motor using PWM technique of servo motor.

Objectives

1) Learn the pinout of 7-segment LED display.


2) To understand the use of switch()and case() functions.

Required Material

Arduino UNO Dev. Board – 1, Breadboard – 1, Jumper wires, Resistor 100Ω – 1, Common cathode LED
display – 1, data cable, PC/Laptop, connecting wires, etc.

Connection Diagram

Make the necessary connections and with the respective pins of Arduino UNO Dev. Board as shown below:

Binary signals to LED Display

Digit PD7 - X PD6 – g PD5 – f PD4 – e PD3 – d PD2 – c PD1 – b PD0 – a


0 0 0 1 1 1 1 1 1
1 0 0 0 0 0 1 1 0
2 0 1 0 1 1 0 1 1
3 0 1 0 0 1 1 1 1
4 0 1 1 0 0 1 1 0
5 0 1 1 0 1 1 0 1
6 0 1 1 1 1 1 0 1
7 0 0 1 0 0 1 1 1
8 0 1 1 1 1 1 1 1
9 0 1 1 0 1 1 1 1

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in


Vidyasagar Academy’s STEM Education Worksheet
The Code

int i; // variable for counting 10 cases

void setup() {
DDRD=0b01111111; // PD6-PD0 output pins, PD7 not used
}

MSB Posi on LSB Posi on


void loop() {
for(i=0;i<10;i++) PORTD: PD7 PD6 PD5 PD4 PD3 PD2 PD1 PD0
{
PORTD=0b11111111;
delay(1000);

switch(i)
{
case 0:
PORTD=0b00111111; // '0' is displayed
break;

case 1:
PORTD=0b00000110; // '1' is displayed
break;

case 2:
PORTD=0b01011011; // '2' is displayed
break;

case 3:
PORTD=0b01001111; // '3' is displayed
break;

case 4:
PORTD=0b01100110; // '4' is displayed
break;

case 5:
PORTD=0b01101101; // '5' is displayed
break;

case 6:
PORTD=0b01111101; // '6' is displayed
break;

case 7:
PORTD=0b00100111; // '7' is displayed
break;

case 8:
PORTD=0b01111111; // '8' is displayed
break;

case 9:
PORTD=0b01101111; // '9' is displayed
break;
} // switch closed
} // “for” loop closed
} // void loop closed

Simple Notes on Arduino – Prof. Dattaraj Vidyasagar; Vidyasagar Academy, www.vsa.edu.in

You might also like