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

Arduino Programming Lab

The document is a lab observation guide for Arduino-based programming at Aditya College of Engineering & Technology, detailing various experiments and their objectives. It includes an introduction to Arduino, descriptions of different Arduino boards, and instructions for setting up the Arduino IDE. The guide serves as a comprehensive resource for students in the Electronics & Communication Engineering department to learn about Arduino programming and applications.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Arduino Programming Lab

The document is a lab observation guide for Arduino-based programming at Aditya College of Engineering & Technology, detailing various experiments and their objectives. It includes an introduction to Arduino, descriptions of different Arduino boards, and instructions for setting up the Arduino IDE. The guide serves as a comprehensive resource for students in the Electronics & Communication Engineering department to learn about Arduino programming and applications.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

ADITYA COLLEGE OF ENGINEERING & TECHNOLOGY

DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING

ARDUINO BASED PROGRAMMING

LAB OBSERVATION

Name:________________________________________
Pin:__________________________________________
Class:_________________ Section:_________________

(R-20)
III-B.Tech : II-Sem
CONTENTS

Exp. Page.
No Date EXPERIMENT NAME
No
INTRODUCTION TO ARDUINO
1. 1

2. MEASURE ANALOG SIGNAL FROM TEMPERATURE


21
SENSOR

GENERATE PWM OUTPUT


3. 27

4. DRIVE SINGLE CHARACTER GENERATION ON HYPER 31


TERMINAL

5. DRIVE A GIVEN STRING ON HYPER TERMINAL 35

6. GENERATE ASCII TABLE FOR DIFFERENT CHARACTERS 39

FULL DUPLEX LINK ESTABLISHMENT USING HYPER


7. TERMINAL 45

8. DRIVE A GIVEN VALUE ON A 8 BIT DAC 51

DRIVE STEPPER MOTOR USING ANALOG GPIOS


57
9.

DRIVE ACCELEROMETER AND DISPLAY THE READINGS


10. 63
ON HYPER TERMINAL
ARDUINO BASED PROGRAMMING

1. INTRODUCTION TO ARDUINO

AIM: Study of Arduino,

DESCRIPTION:

ARDUINO UNO

Arduino is a prototype platform (open-source) based on an easy-to-use hardware and


software. It consists of a circuit board, which can be programed (referred to as a microcontroller)
and a ready-made software called Arduino IDE (Integrated Development Environment), which is
used to write and upload the computer code to the physical board.

Arduino provides a standard form factor that breaks the functions of the micro-controller into a
more accessible package.

The key features are −

 Arduino boards are able to read analog or digital input signals from different sensors and turn it
into an output such as activating a motor, turning LED on/off, connect to the cloud and many
other actions.
 You can control your board functions by sending a set of instructions to the microcontroller on
the board via Arduino IDE (referred to as uploading software).
 Unlike most previous programmable circuit boards, Arduino does not need an extra piece of
hardware (called a programmer) in order to load a new code onto the board. You can simply use
a USB cable.
 Additionally, the Arduino IDE uses a simplified version of C++, making it easier to learn to
program.
 Finally, Arduino provides a standard form factor that breaks the functions of the micro-controller
into a more accessible package.

Dept of ECE Aditya College of Engg & Technology Page 1


ARDUINO BASED PROGRAMMING

Board Types

Various kinds of Arduino boards are available depending on different microcontrollers


used. However, all Arduino boards have one thing in common: they are programmed through the
Arduino IDE.

The differences are based on the number of inputs and outputs (the number of sensors, LEDs,
and buttons you can use on a single board), speed, operating voltage, form factor etc. Some
boards are designed to be embedded and have no programming interface (hardware), which you
would need to buy separately. Some can run directly from a 3.7V battery, others need at least
5V.

Here is a list of different Arduino boards available.

Arduino boards based on ATMEGA328 microcontroller

Operating Digital Analog Programming


Board Name Clock Speed PWM UART
Volt i/o Inputs Interface

USB via
Arduino Uno R3 5V 16MHz 14 6 6 1
ATMega16U2

USB via
Arduino Uno R3 SMD 5V 16MHz 14 6 6 1
ATMega16U2

Red Board 5V 16MHz 14 6 6 1 USB via FTDI

Arduino Pro 3.3v/8 FTDI-Compatible


3.3V 8MHz 14 6 6 1
MHz Header

Arduino Pro FTDI-Compatible


5V 16MHz 14 6 6 1
5V/16MHz Header

Arduino mini 05 5V 16MHz 14 8 6 1 FTDI-Compatible

Dept of ECE Aditya College of Engg & Technology Page 2


ARDUINO BASED PROGRAMMING

Arduino Pro mini FTDI-Compatible


3.3V 8MHz 14 8 6 1
3.3v/8mhz Header

Arduino Pro mini FTDI-Compatible


5V 16MHz 14 8 6 1
5v/16mhz Header

FTDI-Compatible
Arduino Ethernet 5V 16MHz 14 6 6 1
Header

FTDI-Compatible
Arduino Fio 3.3V 8MHz 14 8 6 1
Header

LilyPad Arduino 328 FTDI-Compatible


3.3V 8MHz 14 6 6 1
main board Header

LilyPad Arduino FTDI-Compatible


3.3V 8MHz 9 4 5 0
simple board Header

Arduino boards based on ATMEGA32u4 microcontroller

Operating Clock Digital Analog Programming


Board Name PWM UART
Volt Speed i/o Inputs Interface

Arduino Leonardo 5V 16MHz 20 12 7 1 Native USB

Pro micro
5V 16MHz 14 6 6 1 Native USB
5V/16MHz

Pro micro
5V 16MHz 14 6 6 1 Native USB
3.3V/8MHz

LilyPad Arduino
3.3V 8MHz 14 6 6 1 Native USB
USB

Dept of ECE Aditya College of Engg & Technology Page 3


ARDUINO BASED PROGRAMMING

Arduino boards based on ATMEGA2560 microcontroller

Operating Clock Digital Analog Programming


Board Name PWM UART
Volt Speed i/o Inputs Interface

Arduino Mega USB via


5V 16MHz 54 16 14 4
2560 R3 ATMega16U2B

FTDI-Compatible
Mega Pro 3.3V 3.3V 8MHz 54 16 14 4
Header

FTDI-Compatible
Mega Pro 5V 5V 16MHz 54 16 14 4
Header

Mega Pro Mini FTDI-Compatible


3.3V 8MHz 54 16 14 4
3.3V Header

Arduino boards based on AT91SAM3X8E microcontroller

Operating Clock Digital Analog Programming


Board Name PWM UART
Volt Speed i/o Inputs Interface

Arduino Mega
3.3V 84MHz 54 12 12 4 USB native
2560 R3

Dept of ECE Aditya College of Engg & Technology Page 4


ARDUINO BASED PROGRAMMING

Arduino UNO

We will study the Arduino UNO board because it is the most popular board in the Arduino board
family. In addition, it is the best board to get started with electronics and coding. Some boards
look a bit different from the one given below, but most Arduinos have majority of these
components in common.

Dept of ECE Aditya College of Engg & Technology Page 5


ARDUINO BASED PROGRAMMING

Power USB
Arduino board can be powered by using the USB cable from your computer. All you need to do
is connect the USB cable to the USB connection (1).

Power (Barrel Jack)


Arduino boards can be powered directly from the AC mains power supply by connecting it to the
Barrel Jack (2).

Voltage Regulator
The function of the voltage regulator is to control the voltage given to the Arduino board and
stabilize the DC voltages used by the processor and other elements.

Crystal Oscillator
The crystal oscillator helps Arduino in dealing with time issues. How does Arduino calculate
time? The answer is, by using the crystal oscillator. The number printed on top of the Arduino
crystal is 16.000H9H. It tells us that the frequency is 16,000,000 Hertz or 16 MHz.

Arduino Reset
You can reset your Arduino board, i.e., start your program from the beginning. You can reset the
UNO board in two ways. First, by using the reset button (17) on the board. Second, you can
connect an external reset button to the Arduino pin labelled RESET (5).

Pins (3.3, 5, GND, Vin)


 3.3V (6) − Supply 3.3 output volt
 5V (7) − Supply 5 output volt
 Most of the components used with Arduino board works fine with 3.3 volt and 5 volt.
 GND (8)(Ground) − There are several GND pins on the Arduino, any of which can be
used to ground your circuit.
 Vin (9) − This pin also can be used to power the Arduino board from an external power
source, like AC mains power supply.

Dept of ECE Aditya College of Engg & Technology Page 6


ARDUINO BASED PROGRAMMING

Analog pins
The Arduino UNO board has six analog input pins A0 through A5. These pins can read the
signal from an analog sensor like the humidity sensor or temperature sensor and convert it into a
digital value that can be read by the microprocessor.

Main microcontroller
Each Arduino board has its own microcontroller (11). You can assume it as the brain of your
board. The main IC (integrated circuit) on the Arduino is slightly different from board to board.
The microcontrollers are usually of the ATMEL Company. You must know what IC your board
has before loading up a new program from the Arduino IDE. This information is available on the
top of the IC. For more details about the IC construction and functions, you can refer to the data
sheet.

ICSP pin
Mostly, ICSP (12) is an AVR, a tiny programming header for the Arduino consisting of MOSI,
MISO, SCK, RESET, VCC, and GND. It is often referred to as an SPI (Serial Peripheral
Interface), which could be considered as an "expansion" of the output. Actually, you are slaving
the output device to the master of the SPI bus.

Power LED indicator


This LED should light up when you plug your Arduino into a power source to indicate that your
board is powered up correctly. If this light does not turn on, then there is something wrong with
the connection.

TX and RX LEDs
On your board, you will find two labels: TX (transmit) and RX (receive). They appear in two
places on the Arduino UNO board. First, at the digital pins 0 and 1, to indicate the pins
responsible for serial communication. Second, the TX and RX led (13). The TX led flashes with

Dept of ECE Aditya College of Engg & Technology Page 7


ARDUINO BASED PROGRAMMING

different speed while sending the serial data. The speed of flashing depends on the baud rate
used by the board. RX flashes during the receiving process.

Digital I/O
The Arduino UNO board has 14 digital I/O pins (15) (of which 6 provide PWM (Pulse Width
Modulation) output. These pins can be configured to work as input digital pins to read logic
values (0 or 1) or as digital output pins to drive different modules like LEDs, relays, etc. The
pins labeled “~” can be used to generate PWM.

AREF
AREF stands for Analog Reference. It is sometimes, used to set an external reference voltage
(between 0 and 5 Volts) as the upper limit for the analog input pins.

After learning about the main parts of the Arduino UNO board, we are ready to learn how to set
up the Arduino IDE. Once we learn this, we will be ready to upload our program on the Arduino
board.

Setup the Arduino IDE on our computer

In this section, we will learn in easy steps, how to setup the Arduino IDE on our computer and
prepare the board to receive the program via USB cable.

Step 1 − First you must have your Arduino board (you can choose your favorite board) and a
USB cable. In case you use Arduino UNO, Arduino Duemilanove, Nano, Arduino Mega 2560,
or Diecimila, you will need a standard USB cable (A plug to B plug), the kind you would
connect to a USB printer as shown in the following image.

Dept of ECE Aditya College of Engg & Technology Page 8


ARDUINO BASED PROGRAMMING

In case you use Arduino Nano, you will need an A to Mini-B cable instead as shown in the
following image.

Step 2 − Download Arduino IDE Software.

You can get different versions of Arduino IDE from the Download page on the Arduino Official
website. You must select your software, which is compatible with your operating system
(Windows, IOS, or Linux). After your file download is complete, unzip the file.

Dept of ECE Aditya College of Engg & Technology Page 9


ARDUINO BASED PROGRAMMING

Step 3 − Power up your board.

The Arduino Uno, Mega, Duemilanove and Arduino Nano automatically draw power from
either, the USB connection to the computer or an external power supply. If you are using an
Arduino Diecimila, you have to make sure that the board is configured to draw power from the
USB connection. The power source is selected with a jumper, a small piece of plastic that fits
onto two of the three pins between the USB and power jacks. Check that it is on the two pins
closest to the USB port.

Connect the Arduino board to your computer using the USB cable. The green power LED
(labeled PWR) should glow.

Step 4 − Launch Arduino IDE.

After your Arduino IDE software is downloaded, you need to unzip the folder. Inside the folder,
you can find the application icon with an infinity label (application.exe). Double-click the icon to
start the IDE.

Dept of ECE Aditya College of Engg & Technology Page 10


ARDUINO BASED PROGRAMMING

Step 5 − Open your first project.

Once the software starts, you have two options −

 Create a new project.


 Open an existing project example.

To create a new project, select File → New.

Dept of ECE Aditya College of Engg & Technology Page 11


ARDUINO BASED PROGRAMMING

To open an existing project example, select File → Example → Basics → Blink.

Dept of ECE Aditya College of Engg & Technology Page 12


ARDUINO BASED PROGRAMMING

Here, we are selecting just one of the examples with the name Blink. It turns the LED on and off
with some time delay. You can select any other example from the list.

Step 6 − Select your Arduino board.

To avoid any error while uploading your program to the board, you must select the correct
Arduino board name, which matches with the board connected to your computer.

Go to Tools → Board and select your board.

Dept of ECE Aditya College of Engg & Technology Page 13


ARDUINO BASED PROGRAMMING

Here, we have selected Arduino Uno board according to our tutorial, but you must select the
name matching the board that you are using.

Step 7 − Select your serial port.

Select the serial device of the Arduino board. Go to Tools → Serial Port menu. This is likely to
be COM3 or higher (COM1 and COM2 are usually reserved for hardware serial ports). To find

Dept of ECE Aditya College of Engg & Technology Page 14


ARDUINO BASED PROGRAMMING

out, you can disconnect your Arduino board and re-open the menu, the entry that disappears
should be of the Arduino board. Reconnect the board and select that serial port.

Step 8 − Upload the program to your board.

Before explaining how we can upload our program to the board, we must demonstrate the
function of each symbol appearing in the Arduino IDE toolbar.

Dept of ECE Aditya College of Engg & Technology Page 15


ARDUINO BASED PROGRAMMING

A − Used to check if there is any compilation error.

B − Used to upload a program to the Arduino board.

C − Shortcut used to create a new sketch.

D − Used to directly open one of the example sketch.

E − Used to save your sketch.

F − Serial monitor used to receive serial data from the board and send the serial data to the
board.

Now, simply click the "Upload" button in the environment. Wait a few seconds; you will see the
RX and TX LEDs on the board, flashing. If the upload is successful, the message "Done
uploading" will appear in the status bar.

Note − If you have an Arduino Mini, NG, or other board, you need to press the reset button
physically on the board, immediately before clicking the upload button on the Arduino Software.

Sketch − The first new terminology is the Arduino program called “sketch”.

Dept of ECE Aditya College of Engg & Technology Page 16


ARDUINO BASED PROGRAMMING

Structure

Arduino programs can be divided in three main parts: Structure, Values (variables and
constants), and Functions. In this tutorial, we will learn about the Arduino software program,
step by step, and how we can write the program without any syntax or compilation error.

Let us start with the Structure. Software structure consist of two main functions −

 Setup( ) function
 Loop( ) function

Void setup ( ) {

Dept of ECE Aditya College of Engg & Technology Page 17


ARDUINO BASED PROGRAMMING

 PURPOSE − The setup() function is called when a sketch starts. Use it to initialize the
variables, pin modes, start using libraries, etc. The setup function will only run once, after each
power up or reset of the Arduino board.
 INPUT − -
 OUTPUT − -
 RETURN − -
Void Loop ( ) {

}
 PURPOSE − After creating a setup() function, which initializes and sets the initial values,
the loop() function does precisely what its name suggests, and loops consecutively, allowing
your program to change and respond. Use it to actively control the Arduino board.
 INPUT − -
 OUTPUT − -
 RETURN − -
Print Page

Dept of ECE Aditya College of Engg & Technology Page 18


ARDUINO BASED PROGRAMMING

Dept of ECE Aditya College of Engg & Technology Page 19


ARDUINO BASED PROGRAMMING

CIRCUIT DIAGRAM:

Dept of ECE Aditya College of Engg & Technology Page 20


ARDUINO BASED PROGRAMMING

2 .MEASURE ANALOG SIGNAL FROM TEMPERATURE SENSOR

AIM: To measure the analog signal from temperature sensor

APPARATUS:
1. Arduino uno board
2. Temperature sensor LM35
3. 5V Adaptor
4. Bread board
5. USB Cable
6. Jumper Wires

THEORY:

The LM35 Analog Temperature Sensor is a popular option for temperature sensing over the
range of -55 to 150°C.

 Provides analog output directly readable in degrees Celsius


 Output voltage corresponds to 10.0mV/°C
 -55 to 150°C measurement range with ±0.5°C accuracy°
 5V compatible. 3.3V uC compatible if the sensor is run off separate 4 to 20V supply.

If you have a microcontroller like an Arduino and you want to measure temperature, the
LM35 is one of the more popular temperature measurement ICs to use. The LM35 is a 3-pin
analog thermometer packaged in a TO-92 package. The output is a linear voltage that
corresponds to 10.0mV/°C, so the output is directly readable in degrees Celsius using an
analog port on a microcontroller. Accuracy is typically within 0.5°C. Since the voltage output
range of the sensor is limited to 1.5V, if a greater resolution is desired, the analog reference
voltage on the uC can be changed from the default 5V to something closer to 1.5V which will
decrease the voltage step size on the analog readings.

Dept of ECE Aditya College of Engg & Technology Page 21


ARDUINO BASED PROGRAMMING

Dept of ECE Aditya College of Engg & Technology Page 22


ARDUINO BASED PROGRAMMING

PROGRAM:
// Define the analog pin, the LM35's Vout pin is connected to
#define sensorPin A0

void setup() {
// Begin serial communication at 9600 baud rate
Serial.begin(9600);
}
void loop() {
// Get the voltage reading from the LM35
int reading = analogRead(sensorPin);
// Convert that reading into voltage
float voltage = reading * (5.0 / 1024.0);
// Convert the voltage into the temperature in Celsius
float temperatureC = voltage * 100;
// Print the temperature in Celsius
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.print("\xC2\xB0"); // shows degree symbol
Serial.print("C | ");
// Print the temperature in Fahrenheit
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
Serial.print(temperatureF);
Serial.print("\xC2\xB0"); // shows degree symbol
Serial.println("F");

delay(1000); // wait a second between readings


}

Dept of ECE Aditya College of Engg & Technology Page 23


ARDUINO BASED PROGRAMMING

OUTPUT:

Dept of ECE Aditya College of Engg & Technology Page 24


ARDUINO BASED PROGRAMMING

PROCEDURE:

1.Click on Arduino IDE software.

2. Click on file

3. Click on New

4. Write a Program as per circuit Pin connections

5. Click on Save

6. Click on Verify

7. Click on Upload the code into Arduino Uno by using USB cable.

8. Connect the Circuit as per circuit diagram .

9. Observe output value using serial monitor / serial plotter.

RESULT:

Dept of ECE Aditya College of Engg & Technology Page 25


ARDUINO BASED PROGRAMMING

CIRCUIT DIAGRAM

Dept of ECE Aditya College of Engg & Technology Page 26


ARDUINO BASED PROGRAMMING

3. GENERATE PWM OUTPUT

AIM: To generate PWM wave form using arduino uno

APPARATUS:

1. Arduino uno board-1


2. 5V Adaptor
3. Bread board
4. USB Cable
5. Jumper Wires

THEORY:
Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means.
Digital control is used to create a square wave, a signal switched between on and off. This on-
off pattern can simulate voltages in between the full Vcc of the board (e.g., 5 V on UNO, 3.3 V
on a MKR board) and off (0 Volts) by changing the portion of the time the signal spends on
versus the time that the signal spends off. The duration of "on time" is called the pulse width.
To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-
off pattern fast enough with an LED for example, the result is as if the signal is a steady
voltage between 0 and Vcc controlling the brightness of the LED.

Dept of ECE Aditya College of Engg & Technology Page 27


ARDUINO BASED PROGRAMMING

OUTPUT:

Dept of ECE Aditya College of Engg & Technology Page 28


ARDUINO BASED PROGRAMMING

PROGRAM:

void setup() {
// initialize digital pin 13 as an output.
pinMode(9, OUTPUT);
}

// the loop function runs over and over again forever


void loop() {
analogWrite(9,64);
delay(10);
analogWrite(9,127);
delay(10);
analogWrite(9,191);
delay(10);
analogWrite(9,255);
delay(10);
analogWrite(9,0);
delay(10);

PROCEDURE:

1.Click on Arduino IDE software.

2. Click on file

3.Click on New

4.Write a Program as per circuit Pin connections

5. Click on Save

6.Click on Verify

7.Click on Upload the code into Arduino Uno by using USB cable.

8. Connect the Circuit as per circuit diagram .

9. observe output value using CRO.

RESULT:

Dept of ECE Aditya College of Engg & Technology Page 29


ARDUINO BASED PROGRAMMING

Dept of ECE Aditya College of Engg & Technology Page 30


ARDUINO BASED PROGRAMMING

4. DRIVE A SINGLE CHARACTER ON HYPER TERMINAL


AIM: To drive a given string on hyper terminal

APPARATUS:
1. Arduino uno board-1
2. 5V Adaptor
3. Bread board
4. USB Cable
5. Jumper Wires
6. RS232 to TTL connector
7. RS232 to USB cable

PROGRAM:

char my_str[1]; // an array big enough for a 5 character string


void setup() {

Serial.begin(9600);

my_str[0] = 'H'; // the string consists of 5 characters

}
void loop() {

Serial.println(my_str);

Dept of ECE Aditya College of Engg & Technology Page 31


ARDUINO BASED PROGRAMMING

OUTPUT:

Dept of ECE Aditya College of Engg & Technology Page 32


ARDUINO BASED PROGRAMMING

PROCEDURE:
1. Click on Arduino IDE software.

2. Click on file

3. Click on New

4. Write a Program as per circuit Pin connections

5. Click on Save

6. Click on Verify

7. Click on Upload the code into Arduino Uno by using USB cable.

8. Connect the Circuit as per circuit diagram .

9. Connect arduino board to USB through RS232 cable and select comport on hyper terminal

10. Observe output value On hyperterminal

RESULT:

Dept of ECE Aditya College of Engg & Technology Page 33


ARDUINO BASED PROGRAMMING

Dept of ECE Aditya College of Engg & Technology Page 34


ARDUINO BASED PROGRAMMING

5. DRIVE A GIVEN STRING ON HYPER TERMINAL


AIM: To drive a given string on hyper terminal

APPARATUS:
1. Arduino uno board-1
2. RS232 to TTL connector
3. RS232 to USB cable
4. 5V Adaptor
5. Bread board
6. USB Cable
7. Jumper Wires

THEORY:
Strings are used to store text. They can be used to display text on an LCD or in the Arduino IDE
Serial Monitor window. A string is a special array that has one extra element at the end of the
string which always has the value of 0 (zero). This is known as a "null terminated string".The
string can be printed out to the Arduino IDE Serial Monitor window by
using Serial.println() and passing it the name of the string.

PROGRAM-1
char my_str[6]; // an array big enough for a 5 character string
void setup() {
Serial.begin(9600);
my_str[0] = 'H'; // the string consists of 5 characters
my_str[1] = 'e';
my_str[2] = 'l';
my_str[3] = 'l';
my_str[4] = 'o';
my_str[5] = 0; // 6th array element is a null terminator
}
void loop() {
Serial.println(my_str);}

Dept of ECE Aditya College of Engg & Technology Page 35


ARDUINO BASED PROGRAMMING

Dept of ECE Aditya College of Engg & Technology Page 36


ARDUINO BASED PROGRAMMING

PROGRAM-2

char my_str[] = "Hello";

void setup() {
Serial.begin(9600);

void loop() {
Serial.println(my_str);
}

PROCEDURE:

1. Click on Arduino IDE software.

2. Click on file

3. Click on New

4. Write a Program as per circuit Pin connections

5. Click on Save

6. Click on Verify

7. Click on Upload the code into Arduino Uno by using USB cable.

8. Connect the Circuit as per circuit diagram .

9. Connect arduino board to USB through RS232 cable and select comport on hyper terminal

10. Observe output value On hyperterminal

RESULT:

Dept of ECE Aditya College of Engg & Technology Page 37


ARDUINO BASED PROGRAMMING

Dept of ECE Aditya College of Engg & Technology Page 38


ARDUINO BASED PROGRAMMING

6. GENERATE ASCII TABLE USING ARDUINO


AIM: To generate ASCII table using arduino

APPARATUS:
1. Arduino uno board-1
2. 5V Adaptor
3. Bread board
4. USB Cable
5. Jumper Wires

THEORY:
It contains the character, followed by its ASCII code in decimal, hexadecimal, and
sometimes, even octal and binary representations. In this program, we will print out all these
representations for printable ASCII characters. Remember that the first printable ASCII
character starts from number 33, and the printable characters go on up to number 126. Since we
will print out the ASCII table on the Serial Monitor, we will concern ourselves with only
printable characters. Let us begin the walkthrough of this code. As you can see, we begin with
the initialization of Serial in the setup and then wait for the Serial port to connect. After that, we
just print the title of the sketch. Within the loop, we print the value of thisByte as a character
(using Serial.write()), then we print its decimal value (using Serial.print()), and then we print its
hexadecimal, octal and binary representations, using format specifiers in Serial.print().

We later increment the value of this Byte, until we reach the value of 126, after which we enter
an infinite loop, and essentially do nothing.

Dept of ECE Aditya College of Engg & Technology Page 39


ARDUINO BASED PROGRAMMING

Dept of ECE Aditya College of Engg & Technology Page 40


ARDUINO BASED PROGRAMMING

PROGRAM:

void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("ASCII Table ~ Character Map");
}

// first visible ASCIIcharacter '!' is number 33:


int thisByte = 33;
void loop() {
// prints value unaltered, i.e. the raw binary version of the byte.
// The Serial Monitor interprets all bytes as ASCII, so 33, the first number,
Serial.write(thisByte);

Serial.print(", dec: ");


// prints value as string as an ASCII-encoded decimal (base 10).
// Decimal is the default format for Serial.print() and Serial.println(),
// so no modifier is needed:
Serial.print(thisByte);
// But you can declare the modifier for decimal if you want to.
// this also works if you uncomment it:

// Serial.print(thisByte, DEC);

Serial.print(", hex: ");


// prints value as string in hexadecimal (base 16):
Serial.print(thisByte, HEX);

Serial.print(", oct: ");


// prints value as string in octal (base 8);
Serial.print(thisByte, OCT);

Serial.print(", bin: ");


// prints value as string in binary (base 2) also prints ending line break:
Serial.println(thisByte, BIN);

// if printed last visible character '~' or 126, stop:


if (thisByte == 126) { // you could also use if (thisByte == '~') {
// This loop loops forever and does nothing

Dept of ECE Aditya College of Engg & Technology Page 41


ARDUINO BASED PROGRAMMING

OUTPUT:

Dept of ECE Aditya College of Engg & Technology Page 42


ARDUINO BASED PROGRAMMING

while (true) {
continue;
}
}
// go on to the next character
thisByte++;
delay(500);
}

PROCEDURE:

1.Click on Arduino IDE software.

2. Click on file

3. Click on New

4. Write a Program as per circuit Pin connections

5. Click on Save

6. Click on Verify

7. Click on Upload the code into Arduino Uno by using USB cable.

8. Connect the Circuit as per circuit diagram.

9. Observe output value on serial monitor.

RESULT:

Dept of ECE Aditya College of Engg & Technology Page 43


ARDUINO BASED PROGRAMMING

Dept of ECE Aditya College of Engg & Technology Page 44


ARDUINO BASED PROGRAMMING

7. FULL DUPLEX LINK ESTABLISHMENT USING HYPER


TERMINAL
AIM: To establish fullduplex communication between serial monitor and arduino

APPARATUS:
1. Arduino uno board-1
2. RS232 to TTL connector
3. RS232 to USB cable
4. 5V Adaptor
5. Bread board
6. USB Cable
7. Jumper Wires

THEORY:
Communication is of two types, simplex, where the role of the sender and receiver are
fixed (one way) and duplex, where either side is capable of sending and receiving (both ways).
Duplex communication is further divided into half-duplex, where only one side can send data at a
given moment (the other side recieves and waits for their turn) and full-duplex, where both sides
can simultaneously transmit and receive data. The communication between your PC and Arduino
is full-duplex.

A PC has a USB port, which the operating system (like Windows) presents as a virtual
COM (communication) port for the Arduino IDE to use. The Arduino board, on the other end,
has a built-in Serial port along with a USB-UART converter. The converter on the Arduino,
allows outgoing data to be converted to USB, and incoming data to be converted to UART.

The hardware is then abstracted upon via software on both ends. On the Arduino’s side,
the running program uses the Serial library (a collection of functions) to read/write data. The
program you write will use the Serial library’s functions and methods to be able to use the port.

On the PC, the user can see the data received from Arduino or send back data using
the Serial monitor built in to the Arduino IDE. The Serial monitor is a program that ships with
the Arduino IDE and allows you to view the data (and also send some back) which is being sent
on the virtual Serial port which your Arduino connects to. It can be opened by going to
Tools>Serial monitor

Dept of ECE Aditya College of Engg & Technology Page 45


ARDUINO BASED PROGRAMMING

OUTPUT

Dept of ECE Aditya College of Engg & Technology Page 46


ARDUINO BASED PROGRAMMING

PROGRAM:

#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);

char rx_byte = 0;
String rx_str = "";

void setup() {
Serial.begin(9600);
lcd.begin(16,2);
//Serial.println("Enter your Data");
lcd.setCursor(0,0);
lcd.print("Enter Your Data");
}

void loop() {
if (Serial.available() > 0) {
rx_byte = Serial.read();

if (rx_byte != '\n') {
rx_str += rx_byte;
}
else {
// end of string
Serial.println(rx_str);
lcd.setCursor(0,1);
lcd.print(rx_str);
rx_str = "";
Serial.println("");
Serial.println("Enter Your Data");
}
}

Dept of ECE Aditya College of Engg & Technology Page 47


ARDUINO BASED PROGRAMMING

Dept of ECE Aditya College of Engg & Technology Page 48


ARDUINO BASED PROGRAMMING

PROCEDURE:

1. Click on Arduino IDE software.

2. Click on file

3. Click on New

4. Write a Program as per circuit Pin connections

5. Click on Save

6. Click on Verify

7. Click on Upload the code into Arduino Uno by using USB cable.

Connect arduino board to USB through RS232 cable and select comport on HyperTerminal

8. Observe the data which prints on HyperTerminal.

9. Enter your text and observe the same on HyperTerminal

RESULT:

Dept of ECE Aditya College of Engg & Technology Page 49


ARDUINO BASED PROGRAMMING

CIRCUIT DIAGRAM:

USING R-2R LADDER:

Dept of ECE Aditya College of Engg & Technology Page 50


ARDUINO BASED PROGRAMMING

8. DRIVE A GIVEN VALUE ON A 8 BIT DAC


AIM: To convert given digital data in to analog data using R-2R ladder circuit

APPARATUS:
1. Arduino uno board-1
2. 5V Adaptor
3. Bread board
4. Resistors 10k-8 NO
5. Resistors 22k-8 NO
6. USB Cable
7. Jumper Wires

THEORY:
Digital to Analog Converter (DAC) is a system that converts digital signal to its corresponding
analog signal. The R-2R Ladder is one of the most popular DAC converters because of its
simplicity. It utilizes only two values of resistors and it can be extended to any number of
bits. The number of levels are equal to two power the number of bits. In this project, 8 bits means
there will be 256 levels. We can calculate the maximum voltage output by this equation.

and the voltage output equation for R-2R DAC itself is this :

As you can see in the circuit, there are 8 digital input from b0 to b7 that represent each bit. It’s in
the format of LSB. I used R = 1k ohm resistors because It’s the one that I already have so many
and I can get 2R by series 1k ohm resistor to be 2k ohm easily.
From the simulation, we know that the highest logic 11111111 (or 255 in decimal) is giving
4.96V at the output voltage and the 00000000 (or 0 in decimal) logic gives 0V. And 10000000
(128 in decimal) gives exactly 2.5V. This result proves that the Voltage output of the R-2R
Ladder DAC Equation is right.

Dept of ECE Aditya College of Engg & Technology Page 51


ARDUINO BASED PROGRAMMING

OUTPUT:

SINE WAVE:

RAMP SIGNAL:

Dept of ECE Aditya College of Engg & Technology Page 52


ARDUINO BASED PROGRAMMING

SINE WAVE:

PROGRAM:

int valSin;
void setup() {
Serial.begin(9600);
DDRD = B11111111; // Port D at Arduino Uno (pin 0-7)
}
void loop() {
for (int i = 0; i < 360; i++) { //looping the value of i from 0 to 360 degree
valSin = ((sin(i * DEG_TO_RAD) + 1) * 255) / 2; // creating the sinwave from i, 255 is the highest
value in 8 bit (0-255)
PORTD = valSin; // assign the sinwave to portD
delay(10); // set the delay of the analog value
Serial.println(analogRead(A0));
}
}

RAMP SIGNAL

PROGRAM:

void setup()
{
Serial.begin(9600);
DDRD = B11111111; // Port D at Arduino Uno (pin 0-7)
}
void loop()
{
for ( int i = 0; i < 256; i++) { //looping the value of i from 0 to 255, creating a ramp wave from 0 to 255,
it's more like stairs wave
PORTD = i; // the integer value of i is automatically translated to binary and assign to port D, ex 3 is
automatically transformed into 00000011 which means only pin 0 and pin 1 is high (1) and the other pin
is low (0)
delay(10); // set the delay of the analog value
Serial.println(analogRead(A0));
}

Dept of ECE Aditya College of Engg & Technology Page 53


ARDUINO BASED PROGRAMMING

Dept of ECE Aditya College of Engg & Technology Page 54


ARDUINO BASED PROGRAMMING

PROCEDURE:

1. Click on Arduino IDE software.

2. Click on file

3. Click on New

4. Write a Program as per circuit Pin connections

5. Click on Save

6. Click on Verify

7. Click on Upload the code into Arduino Uno by using USB cable.

8. Connect the Circuit as per circuit diagram .

9. Observe output value On serial plotter

RESULT:

Dept of ECE Aditya College of Engg & Technology Page 55


ARDUINO BASED PROGRAMMING

CIRCUIT DIAGRAM:

Dept of ECE Aditya College of Engg & Technology Page 56


ARDUINO BASED PROGRAMMING

9. DRIVE STEPPER MOTOR USING ANALOG GPIOS


AIM:To drive the stepper motor using Analog GPIO pins of Arduino

APPARATUS:
1. Arduino uno board-1
2. Stepper motor(28BYJ-48)-1
3. Motor driver(DRV8825)-1
4. 5V Adaptor
5. Bread board
6. USB Cable
7. Jumper Wires

THEORY:

DRV8825
The DRV8825 stepper motor driver carrier is a breakout board for TI’s DRV8825 micro-stepping
bipolar stepper motor driver. The DRV8825 features adjustable current limiting, over-current and over
temperature protection, and six microstep resolutions (down to 1/32-step). It operates from 8.2 – 45 V and
can deliver up to approximately 1.5 A per phase without a heat sink or forced air flow (rated for up to 2.2
A per coil with sufficient additional cooling).

The DRV8825 driver has three step size (resolution) selector inputs: M0, M1 and M2 . By setting
the appropriate logic levels for these pins, we can set the motor to one of six step resolutions
M0 M1 M2 Microstep Resolution

Low Low Low Full step

High Low Low Half step

Low High Low 1/4 step

High High Low 1/8 step

Low Low High 1/16 step

High Low High 1/32 step

Low High High 1/32 step

High High High 1/32 step

Dept of ECE Aditya College of Engg & Technology Page 57


ARDUINO BASED PROGRAMMING

Dept of ECE Aditya College of Engg & Technology Page 58


ARDUINO BASED PROGRAMMING

STEP input controls the microsteps of the motor. Each HIGH pulse sent to this pin drives the
motor according to the number of microsteps determined by the microstep selection pins. The
higher the pulse frequency, the faster the motor will spin.

DIR input controls the spinning direction of the motor. Pulling it HIGH turns the motor
clockwise, while pulling it LOW turns it counterclockwise.
If you want the motor to only turn in one direction, you can connect the DIR directly to VCC or
GND.

EN is an active low input pin. When this pin is pulled LOW, the DRV8825 driver is enabled. By
default, this pin is pulled low, so unless you pull it high, the driver is always enabled. This pin is
particularly useful when implementing an emergency stop or shutdown system.

SLP is an active low input pin. Pulling this pin LOW puts the driver into sleep mode, reducing
power consumption to a minimum. You can use this to save power, especially when the motor is
not in use.

RST is an active low input as well. When this pin is pulled LOW, all STEP inputs are ignored. It
also resets the driver by setting the internal translator to a predefined “home” state. Home state is
basically the initial position from which the motor starts, and it varies based on microstep
resolution.

28BYJ-48 stepper
The 28BYJ-48 stepper motor is a commonly used stepper motor. which converts electrical pulses
into discrete mechanical rotation. When electrical signals are applied, the stepper motor rotates
in precise and fixed angle increments known as steps. The motor consists of 4 coils that make a
ring around the rotor. These coils are known as the stator, as they are stationary and static. Each
coil is rated at +5V, making it easy to control with any microcontroller, such as an Arduino.

PROGRAM:

int M0Pin = A0; // Stepstick M0 to Arduino Analog pin A0


int M1Pin = A1; // Stepstock M1 to Arduino Analog pin A1
int M2Pin = A2; // Stepstick M2 to Arduino Analog pin A2
int StepPin = A3; // Stepstick STEP pin to Arduino Analog pin A3
int DirPin = A4; // Stepstick DIR pin to Arduino Analog pin A4
int NumofSteps = 708; //720/5.625 degree = 128/2 = 64
int rotations = 3; // Number of rotations of the rotor for each
int delay1 = 2; // Delay between coil activations (ms)
int delay2 = 1000; // Delay between subsequent rotations
int i; // int to use in for loop

//------------------------------

Dept of ECE Aditya College of Engg & Technology Page 59


ARDUINO BASED PROGRAMMING

Dept of ECE Aditya College of Engg & Technology Page 60


ARDUINO BASED PROGRAMMING

void setup() {

pinMode(M0Pin, OUTPUT); // M0/MS1 set to receive Arduino signals


pinMode(M1Pin, OUTPUT); // M1/MS2 set to receive Arduino signals
pinMode(M2Pin, OUTPUT); // M2/MS3 set to receive Arduino signals
pinMode(StepPin, OUTPUT); // stepPin set to receive Arduino signals
pinMode(DirPin, OUTPUT); // DIR set to receive Arduino signals
}

//------------------------------

void loop() {

delay(delay2);
digitalWrite(DirPin, LOW); // set direction
// Rotate stepper rotatations revolutions, using full step method
digitalWrite(M0Pin, LOW); // define step size
digitalWrite(M1Pin, LOW);
digitalWrite(M2Pin, LOW);

for (i = 1; i <= (numofSteps * 1 * rotations); ++i) {


digitalWrite(StepPin, LOW); // Prepare to take a step
digitalWrite(StepPin, HIGH) ; // Take a step
delay(delay1); // Allow some delay between energizing
// the coils to allow stepper rotor time to respond.
}
}

PROCEDURE:

1. Click on Arduino IDE software.

2. Click on file

3. Click on New

4. Write a Program as per circuit Pin connections

5. Click on Save

6. Click on Verify

7. Click on Upload the code into Arduino Uno by using USB cable.

8. Connect the Circuit as per circuit diagram .

9. Observe output

Dept of ECE Aditya College of Engg & Technology Page 61


ARDUINO BASED PROGRAMMING

CIRCUIT DIAGRAM:

Dept of ECE Aditya College of Engg & Technology Page 62


ARDUINO BASED PROGRAMMING

10. DRIVE ACCELEROMETER AND DISPLAY THE READINGS


ON HYPER TERMINAL

AIM: To drive accelerometer and display the readings on hyper terminal

APPARATUS:

1. Arduino uno board-1


2. Accelerometer(ADXL335)-1
3. RS232 to TTL connector
4. RS232 to USB cable
5. 5V Adaptor
6. Bread board
7. USB Cable
8. Jumper Wires

THEORY:

Accelerometers are widely used in low-power, low-cost motion and tilt sensing
applications such as mobile devices, gaming systems, disk drive protection, image stabilization,
and sports and health devices.

At the core of the module is a small, low-power, low-noise triple axis MEMS
accelerometer from Analog Devices – ADXL335. It can measure not only static acceleration
caused by gravity, but also dynamic acceleration caused by motion, shock, or vibration. This
breadboard-friendly module breaks out every pin of the ADXL335 to a 6-pin, 0.1′′ pitch header,
including 3 analog outputs for X, Y, and Z axis measurements, 2 supply pins, and a self-test pin.

The ADXL335 operates on 1.8V to 3.6VDC (typically 3.3V). However, the on-board
3.3V regulator makes it ideal for interfacing with 5V microcontrollers like the Arduino.

The ADXL335 has a full sensing range of ±3g. Meaning the maximum amount of
acceleration that the ADXL335 can accurately measure and represent as an output is ±3g. If it is
accelerated at 4g, for example, the accelerometer will not break, but the output may rail.

Dept of ECE Aditya College of Engg & Technology Page 63


ARDUINO BASED PROGRAMMING

OUTPUT:

Dept of ECE Aditya College of Engg & Technology Page 64


ARDUINO BASED PROGRAMMING

PROGRAM:

const int xpin = A2; // x-axis of the accelerometer


const int ypin = A1; // y-axis
const int zpin = A0; // z-axis (only on 3-axis models)

void setup() {
Serial.begin(9600);
}

void loop() {
Serial.print("X = : ");
Serial.print(analogRead(xpin));

Serial.print("\t");
Serial.print("\t");
Serial.print("Y = : ");
Serial.print(analogRead(ypin));

Serial.print("\t");
Serial.print("\t");
Serial.print("Z = : ");
Serial.print(analogRead(zpin));

Serial.println();
delay(500);
}

Dept of ECE Aditya College of Engg & Technology Page 65


ARDUINO BASED PROGRAMMING

Dept of ECE Aditya College of Engg & Technology Page 66


ARDUINO BASED PROGRAMMING

PROCEDURE:

1.Click on Arduino IDE software.

2. Click on file

3.Click on New

4.Write a Program as per circuit Pin connections

5. Click on Save

6. Click on Verify

7. Click on Upload the code into Arduino Uno by using USB cable.

8. Connect the Circuit as per circuit diagram .

9. Connect arduino board to USB through RS232 cable and select comport on hyper terminal

10. And move the accelerometer in different directions and observe values on hyper terminal.

RESULT:

Dept of ECE Aditya College of Engg & Technology Page 67

You might also like