0% found this document useful (0 votes)
31 views19 pages

EC303G Practical

Uploaded by

great69overthere
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views19 pages

EC303G Practical

Uploaded by

great69overthere
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Practical No.

6
EC303G

Aim: Develop and execute C language program to generate square wave on


port pin of Atmega8.

Theory:
A square wave is a signal that changes between high and low state
where the primary requirement is that the on-time (high state) and
off-time (low state) are equal. Then the square wave signal has a
50% duty cycle. Simply put, a digital signal with exact same on and
off time can be considered as a square wave.
Generating a square wave using an Arduino is as simple as turning
on an I/O, wait for a certain amount time, turn off the I/O, wait for a
certain amount of time and continue the cycle indefinitely.
Arduino’s Atmel CPU (ATmega 328) runs at 16MHz,
but that doesn’t mean that each instruction takes one clock cycle
(62.5ns). Each instruction might take more than one clock cycle. In
practice, there are some instructions that can often take one cycle
or two cycles. So, in order to see what is going on, you need to go
through the Atmel datasheet to find out how long each individual
instruction takes.
Arduino Uno has pulse width modulation (PWM) outputs on digital
I/O pins 3, 5, 6, 9, 10 and 11. The frequency of PWM signal on pins 5
and 6 will be about 980Hz and on other pins will be 490Hz. On
these pins the “analogWrite” function can be used to set the duty
cycle of the pulse train that operates at a fixed (predefined)
frequency

Procedure:
1. Turn on computer.
2. Open Arduino IDE.
3. Create a new sketch(if not already).
4. Write the program.
5. Connect the arduino to the computer.
6. Verify and Upload code to the arduino.
7. Connect Arduino to oscilloscope to analyse the waveform produced.
Program:

unsigned int freq = 0;


int sqwPin = 13;
void setup() {
Serial.begin(9600); // Serial Port Baud = 9600 8,N,1
}
void loop() {
while (Serial.available() > 0)
{
char tmpChar = Serial.read();
if (isDigit(tmpChar))
{
freq = (freq * 10 ) + tmpChar - '0';
}
if (tmpChar == '\n' || tmpChar == '\r')
{
if (freq > 0)
{
Serial.print(freq);
Serial.println(" Hz SQW OUT OK");
tone(sqwPin, freq);
}
freq = 0;
}
}
}

Observation:

Conclusion:
An C language program was developed and executed to generate square wave
on port pin of Atmega8.
Experiment No. 7
EC303G

Aim: Develop and execute C language program to read the status of key from
port pin and turn ON/OFF a LED connected to another port of Atmega8.

Apparatus Required: Push Button, LED, Arduino UNO, Connecting Wires,


Resistor(1k to 10k), Computer.

Theory:
A light-emitting diode (LED) is a semiconductor device that emits light when
an electric current flows through it. When current passes through an LED, the
electrons recombine with holes emitting light in the process. LEDs allow the
current to flow in the forward direction and blocks the current in the reverse
direction.

Push Buttons are normally-open tactile switches. Push buttons allow us to


power the circuit or make any particular connection only when we press the
button. Simply, it makes the circuit connected when pressed and breaks when
released.

Procedure:
1. Turn on computer.
2. Open Arduino IDE.
3. Create a new sketch(if not already).
4. Write the program.
5. Connect the led to pin13 of arduino and ground.
6. Connect the push button to 5v and ground through a pull down resistor
and to pin 7.
7. Connect the arduino to the computer.
8. Verify and Upload code to the arduino.

Program:
#define ledPin 13 // choose the pin for the LED
#define switchPin 7 // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
void setup()
{
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(switchPin, INPUT); // declare pushbutton as input
}

void loop()
{
val = digitalRead(switchPin); // read input value
if (val == HIGH)
{ // check if the input is HIGH (button released)
digitalWrite(ledPin, LOW); // turn LED OFF
}
else
{
digitalWrite(ledPin, HIGH); // turn LED ON } }
}
}

Conclusion:
An C language program was developed and executed to read the status of key
from port pin and turn ON/OFF a LED connected to another port of Atmega8.
Experiment No. 8
EC303G

Aim: Interface 7 segment display to Atmega8. Develop and execute C


language program to Display digits 0 to F on it.

Apparatus Required: Arduino Uno R3, Seven Segment Display, Resistors,


Connecting Wires.

Theory:
The seven-segment displays are designed for displaying numeric values. You
can find them anywhere from instruments to space shuttles. They are the
most practical way to display numeric values. They are cheap and easy to use.
Not only that, they are highly readable in any light condition, unlike LCDs. You
can find them in many everyday usages like counters or token systems, etc.

As the name suggests, the seven-segment display consists of seven LEDs,


which are arranged in a particular order to form the digit eight. And most
common modules will come with an additional LED to indicate the decimal
point. This will be useful when using multiple display modules to display a
decimal number. Each one of these LEDs are arranged in a specific order
as a segment and one of its pins is being brought out of the plastic package.
The other LED pins are connected together and wired to form a common pin.

Types of Seven Segment Displays


Seven segment displays are mainly categorised into two types, Common
Cathode(CC) and Common Anode (CA). This classification is done based on
which pin of the LEDs is connected to the common pin. If the Anodes of these
LEDs are connected together, then it’s called Common Anode displays. And if
the Cathode is connected together, then they are called Common Cathode
displays.

1. Common Cathode Displays

In a Common Cathode (CC) display, all the cathodes of the LEDs are
connected together to the common pin. This common pin is then connected
to the ground rail of the circuit. The individual LEDs can be activated by
applying a high pulse or a logic 1 signal to the corresponding pin.
2. Common Anode Displays

In a Common Anode (CA) display, all the anodes of the LEDs are connected
together to the common pin. This common pin is then connected to the
positive rail of the circuit. The individual LEDs can be activated by applying a
low pulse or a logic 0 signal to the corresponding pin.

Procedure:
1. Turn on computer.
2. Open Arduino IDE.
3. Create a new sketch(if not already).
4. Write the program.
5. Connect 7 segment led with the Arduino
6. Connect the arduino to the computer.
7. Verify and Upload code to the arduino.

Program:
#include "SevSeg.h"
SevSeg sevseg;
void setup()
{
//Set to 1 for single-digit display
byte numDigits = 1;
//defines common pins while using multi-digit display. Left for single
digit display
byte digitPins[ ] = {};
//Defines Arduino pin connections in order: A, B, C, D, E, F, G, DP
byte segmentPins[] = {9,8, 7, 6, 5, 4, 3, 2};
byte displayType = COMMON_CATHODE; //Use COMMON_ANODE for
Common Anode display
bool resistorsOnSegments = true; //‘false’ if resistors are connected to
common pin
//Initialize sevseg object. Use COMMON_ANODE instead of
COMMON_CATHODE for CA display
sevseg.begin(displayType, numDigits, digitPins, segmentPins,
resistorsOnSegments);
sevseg.setBrightness(90);
}
void loop()
{
//Display numbers 0-9 with 1 seconds delay
for(int i = 0; i <= 10; i++)
{
if (i == 10)
{
i = 0;
}
sevseg.setNumber(i);
sevseg.refreshDisplay();
delay(1000);
}
}

Conclusion:
7 segment display was interfaced with arduino and an C program was written
to run the display.
Experiment No. 9
EC303G

Aim: Interface 16 x 2 LCD to Atmega8. Develop and execute C language


program to display string “G P Nagpur” on it.

Apparatus Required: 220 ohm Resistor, 10k Pot, 16×2 character LCD, Arduino
Uno R3.

Theory: LCD, or Liquid Crystal Display, is a type of display that uses liquid
crystals to show characters. When activated by an electric current, these liquid
crystals become opaque, blocking the backlight that is located behind the
screen. As a result, that area will be darker than the rest. By activating the
liquid crystal layer in specific pixels, characters can be generated.

16×2 Character LCD Pinout:

Before we get into the hookup and example code, let’s check out the pinout. A
standard character LCD has 16 pins (except for an RGB LCD, which has 18
pins).

GND is the ground pin.

VCC is the LCD’s power supply and is typically connected to 5 volts.

Vo (LCD Contrast) pin controls the contrast of the LCD. Using a simple voltage
divider network and a potentiometer, we can make precise contrast
adjustments.

RS (Register Select) pin is used to separate the commands (such as setting


the cursor to a specific location, clearing the screen, etc.) from the data. The
RS pin is set to LOW when sending commands to the LCD and HIGH when
sending data.

R/W (Read/Write) pin allows you to read data from or write data to the LCD.
Since the LCD is only used as an output device, this pin is typically held low.
This forces the LCD into WRITE mode.
E (Enable) pin is used to enable the display. When this pin is set to LOW, the
LCD ignores activity on the R/W, RS, and data bus lines; when it is set to HIGH,
the LCD processes the incoming data.

D0-D7 (Data Bus) pins carry the 8 bit data we send to the display. To see an
uppercase ‘A’ character on the display, for example, we set these pins to 0100
0001 (as per the ASCII table).

A-K (Anode & Cathode) pins are used to control the backlight of the LCD.

Procedure:
1. Turn on computer.
2. Open Arduino IDE.
3. Create a new sketch(if not already).
4. Write the program.
5. Connect lcd with the Arduino
6. Connect the arduino to the computer.
7. Verify and Upload code to the arduino.

Program:
#include <LiquidCrystal.h>
// Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Clears the LCD screen
lcd.clear();
}
void loop()
{
// Print a message to the LCD.
lcd.print("G P Nagpur");
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// Print a message to the LCD.
lcd.print(" LCD Tutorial");
}

Conclusion:
The LCD was interfaced and a C program was written for printing GP NAGPUR.
Experiment No. 10
EC303G

Aim: Interface a 4 x 4 matrix keyboard and 16 x 2 LCD toAtmega8. Develop


and execute C language program to read and display key code on LCD.

Apparatus Required: Arduino Uno,1k Pot, Matrix Keypad, LCD screen.

Theory:
In Embedded system design, matrix keypad (4x4, 4x3, 3x3 or 5x5)
is used for key in the user inputs. Similarly character LCD display
[16x2, 16x4, 20x2 or 20x4 LCDs] is used for indicating the system
status / parameters. This intractable is about interfacing 16×2 LCD
and 4x4 matrix keypad with Arduino microcontroller.

The 16x2 is very common type LCD, with two rows, and each
row displays 16 characters of either 5x7 or 5x8 dot matrix
characters.
• The LCD is available in a 16 pin package. It consists of back
light and contrast adjustment function and each dot matrix has
5×8 dot resolution.
• The 16x2 LCD display is connected to the Arduino
(A0,A1,A2,A3,A4,A5) analog IO pins, where those pins are
configured as digital in / out, where LCD operates at 4 bit data
mode.
• If the display is not visible, adjust the Contrast pot (1K), to
make it visible.

Procedure:
1. Turn on computer.
2. Open Arduino IDE.
3. Create a new sketch(if not already).
4. Write the program.
5. Interface.
6. Verify and Upload code to the arduino.

Program:
#include
#include
LiquidCrystal lcd(A0,A1,A2,A3,A4,A5);
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] =
{
{'7','8','9','/'},
{'4','5','6','X'},
{'1','2','3','-'},
{'#','0','=','+'}
};
byte rowPins[ROWS] = { 5, 4, 3, 2 };
byte colPins[COLS] = { 6, 7, 8, 9 };
Keypad kpd = Keypad( makeKeymap(keys), rowPins,
colPins, ROWS, COLS );
void setup()
{
lcd.begin(16, 2);
lcd.print("KEYPAD LCD TEST");
}
void loop()
{
char key = kpd.getKey();
lcd.setCursor(0, 1);
lcd.print("SEC= ");
lcd.print(millis() / 1000);
if(key)
{
lcd.setCursor(9, 1);
lcd.print("KEY= ");
lcd.print(key);
}
}
Result: we Interfaced a 4 x 4 matrix keyboard and 16 x 2 LCD toAtmega8. Also
Develop and execute C language program to read and display key code on
LCD.
Experiment No. 11
EC303G

Aim: Interface POT and 16 x 2 LCD to Atmega8. Develop and execute C


language program to read and display data of ADC on LCD.

Apparatus Required:
● Arduino Uno
● 16*2 LCD display
● Potentiometer (1 kilo Ohm)
● Resistors
● Breadboard
● Jumper wires

Theory:
We find it difficult to read color codes on resistors to find its resistance. In
order to overcome the difficulty of finding the resistance value, we are going to
build a simple Ohm Meter using Arduino. The basic principle behind this
project is a Voltage Divider Network. The value of the unknown resistance is
displayed on 16*2 LCD display. This project also serves as 16*2 LCD display
interfacing with Arduino.

Procedure:
1. Turn on computer.
2. Open Arduino IDE.
3. Create a new sketch(if not already).
4. Write the program.
5. Interface.
6. Verify and Upload code to the arduino.

Program:
#include<LiquidCrystal.h>
LiquidCrystal lcd(2,3,4,5,6,7); //rs,e,d4,d5,d6,d7
int Vin=5; //voltage at 5V pin of arduino
float Vout=0; //voltage at A0 pin of arduino
float R1=3300; //value of known resistance
float R2=0; //value of unknown resistance
int a2d_data=0;
float buffer=0;
void setup()
{
lcd.begin(16,2);
}
void loop()
{
a2d_data=analogRead(A0);
if(a2d_data)
{
buffer=a2d_data*Vin;
Vout=(buffer)/1024.0;
buffer=Vout/(Vin-Vout);
R2=R1*buffer;
lcd.setCursor(4,0);
lcd.print("ohm meter");
lcd.setCursor(0,1);
lcd.print("R (ohm) = ");
lcd.print(R2);
delay(1000);
}
}

Result:
We interfaced POT and 16 x 2 LCD to Atmega8. And Developed and executed
C language program to read and display data of ADC on LCD.
Experiment No. 12
EC303G

Aim: Interface 8 bit DAC to Atmega8. Develop and execute C language


program to generate triangular waveforms.

Apparatus Required:
● Arduino uno
● Computer
● Resistor
● Capacitor

Theory:

Procedure:
1. Turn on computer.
2. Open Arduino IDE.
3. Create a new sketch(if not already).
4. Write the program.
5. Interface.
6. Verify and Upload code to the arduino.
Result:
We Interfaced 8 bit DAC to Atmega8. And Developed and executed C language
program to generate triangular waveforms.
Experiment No. 13
EC303G

Aim: Interface stepper motor to Atmega8. Develop and execute C language


program to rotate stepper motor with different speed in clockwise and counter
clockwise direction.

Apparatus Required: Arduino Uno, Stepper motor, ULN 2003 ic, Computer

Theory: Stepper motor is a brushless DC motor that divides the full rotation
angle of 360° into a number of equal steps. The motor is rotated by applying a
certain sequence of control signals. The speed of rotation can be changed by
changing the rate at which the control signals are applied.

You might also like