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

Micro-Controllers & Robotics: Robotics Club It-Bhu

The document discusses microcontrollers and robotics. It provides an agenda for a robotics club session that includes topics like introduction to microcontrollers, programming ports, motors, motor drivers, LCD, timers and building projects like a line follower robot. It explains what a microcontroller is and compares it to a computer. It describes the Atmega16 microcontroller and its components. It discusses how to program the ports of a microcontroller to output values and take inputs. Examples are given to glow LEDs and make blinking patterns. Motors and motor drivers are explained along with how to interface motors with a microcontroller using a motor driver.

Uploaded by

Elisée Ndjabu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Micro-Controllers & Robotics: Robotics Club It-Bhu

The document discusses microcontrollers and robotics. It provides an agenda for a robotics club session that includes topics like introduction to microcontrollers, programming ports, motors, motor drivers, LCD, timers and building projects like a line follower robot. It explains what a microcontroller is and compares it to a computer. It describes the Atmega16 microcontroller and its components. It discusses how to program the ports of a microcontroller to output values and take inputs. Examples are given to glow LEDs and make blinking patterns. Motors and motor drivers are explained along with how to interface motors with a microcontroller using a motor driver.

Uploaded by

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

Micro-Controllers

&
Robotics

Robotics Club
IT-BHU
Agenda
Session
SessionI I Session III
•• Introduction
IntroductiontotoMicrocontrollers(uCs)
Microcontrollers(uCs) • Introduction to LCD
•• Introduction
IntroductiontotoCompiler
Compiler&&Downloader
Downloader • Programming of LCD
•• Atmega16 Components
Atmega16 Components • Intro & Prog of Timers
•• Programming
ProgrammingofofPORTs
PORTs • Making Digital Clock
•• Glowing
Glowing LEDs n makingdancing
LEDs n making dancingpatterns
patterns • Lab Session
•• Introduction
IntroductiontotoMotors
Motors&&Motor
MotorDrivers
Drivers
•• Controlling
ControllingMotors
Motorsusing
usingAtmega16
Atmega16
•• Lab Session
Lab Session

Session
SessionIIII Session IV
•• Introduction
IntroductiontotoIRIRLED
LED&&RxRx • Doubt Clearing
•• Making IR Sensor
Making IR Sensor • Completing programs / projects
•• Using
UsingIRIRsensor
sensorwith
withATmega16
ATmega16 • Algorithms for various Robots
•• Introduction
IntroductiontotoADC
ADC • Advanced topics – Keypad &
•• Programming
ProgrammingofofInbuilt
InbuiltADC
ADC Communication
•• Making Line Follower Robot
Making Line Follower Robot
•• Lab
LabSession
Session
Introduction

What is a uC ?

In simple words -- a single chip Computer


Introduction

Computer uC
• uP
• uP
• RAM
• Flash Memory
• HDD
• EEPROM
• Clock
• Clock Unit
• Ports (Serial/USB)
• SPI / UART Controllers
• Ports (Parallel)
• Digital I/O Ports
• Mic/Headphone Jack (ADC/
• ADC
DAC)
• Power Supply Unit
• Power Supply Unit
• Reset
• Reset
• IC
• Mother-board
From uP to uC

1. uP
2. Memory
3. Oscillator
4. Buffers
5. ADC
6. Comparator
7. UART
8. Interrupt controllers
9. SPI
10.Two Wire connector
11. JTEG Connectors
Atmega16L : An Overview
8-bit Micro-cotroller
40-pin DIP
32 Programmable I/O Lines
Operating Voltages 2.7 - 5.5V
Speed Grades 0 - 8 MHz
16K Bytes of In-System Self-programmable Flash program memory
512 Bytes EEPROM
Two 8-bit Timer/Counters with Separate Prescalers and Compare Modes
One 16-bit Timer/Counter with Separate Prescaler, Compare Mode, and
Capture Mode
8-channel, 10-bit ADC
Programmable Serial USART
Master/Slave SPI Serial Interface
Programmable Watchdog Timer with Separate On-chip Oscillator
On-chip Analog Comparator
Pin diagram
Block Daigram
Simplified Diagram

4 8bit Parallel Input-Output ports through which you can i/p or o/p digital data.
Port Architecture

P D P
O D I
uC External
R R N
world
T A A
A
Programming the Ports

Normal C program Program for Ports

int x; DDRA = 0b11111111 or 0xFF // o/p


float y; DDRC = 0b00000000 or 0x00 // i/p

x= 10; PORTA = 27; // decimal


y= 8.97; PORTC= 0b11010110; // binary

DDRx defines whether the port will act as input port or o/p port. Just as ‘int’
declares a variable as integer type. It does not assign any value to it.

PORTx assigns the value to be output. If DDRx=0 ie port is defined as


input port, PORTx will store the value but will not o/p it.
More Examples

PORTA=0x5A ;
PORTA=0b01011010;
PORTA=5;

DDRA=0xFF;
DDRA=0b11110000; // pins A7-A4 as o/p & A3-A0 as i/p

You can even address individual pins of any Port


DDRA.3= 1; // Only Pin 3 of Port A (A4) is configured as o/p pin , rest untouched
PORTA.0=1; // pin 0 of Port A outputs 1, rest retain there previous values
Taking Inputs
To take Digital i/p from external world first configure the port as i/p port using
DDRx=0

Use PINx to read the digital value.

x = PINA // read 8 bit integer value from Port A. 0 < x < 255
y= PINB

x,y must be defined as unsigned integers in ur C program.

Individual pins can be read in the same way.

x= PINA.2 // as the individual pins are read n the value is digital, therefore x can either be 0 or 1
y= PINC.6
Complete Program
A program to o/p 33 (hex) on PortD and configure and read pins 2 and
7 of Port A

#include <mega16.h>

void main( )
{
unsigned int x,y;

DDRD=0xFF; // all pins o/p


DDRA=0b01111011; // pin 7 & 2 i/p rest immaterial

PORTD=0x33;
x=PINA.2;
y=PINA.7;
}
LED

- GND
+ +V
Glowing LED

P 0
o 1 -
r +
t
A

To glow LED set


PORTA.0=0;
PORTA.1=1;
or PORTA=0b00000010;
or PORTA=0x02;
Glowing LED contd..

P 0
o 1 -
+
r
t
A
LED Panel on PCB
+V

G
N
D
Blinking Pattern

While (1)
While
{ (1)
{ PORTA=0xFF;
PORTA=0xFF;
delay_ms(500);
PORTA=0x00;
PORTA=0x00;
} delay_ms(500);
}
Complete Program
A program to make a blinking pattern

#include <mega16.h>
#include <delay.h>

void main( )
{

DDRA=0xFF; // all pins o/p

While (1) {
PORTA=0xFF; // all LEDs ON
delay_ms(500);
PORTA=0x00; // all LEDs OFF
delay_ms(500);
}

}
Motors
&
Motor Drivers
Motors and Motor Drivers
Types Motors
 AC Motor
 DC Motor
 Stepper Motor
 Servo Motor
DC motor
Stepper Motor
• Similar principle to DC motors
• More accurate control than DC motors
• Rotate in fixed angles
• Speed control is in our hands
• Multiple coils – so excitation done in
pattern for rotation
Stepper Motor
Servo Motors
• DC motors with built in gearing and
feedback Control loop
• Generally rotate through 90 and 180 deg.
(360 deg also available)
• Used for extreme precision
Motor Driver
Why do we need the motor driver?

The total output current that Atmega16 can provide is 200mA. But motors need much
higher current to run. Therefore a driver circuit is required which provides the
necessary current.

Moreover the o/p from uC can either be 0V or +5V, but motors come in various ratings
2V-36V (typically small ones). Atmega cannot provide this voltage hence we need
external supply to drive motor depending upon i/p from uC

Basically a motor driver connects or disconnects the motor from some external supply
depending upon the i/p from uC.

If the uC sends ‘1’ , driver connects the motor to some external supply ( battery in our
case) thus motor actually runs by drawing current from the battery.
If the uC sends a ‘0’ , o/p is connected to ground and hence the motor does not get
the supply.
Working of Motor Driver
+VPP

O/p 1 10 I/p 1 ( O/p from uC)

O/p 2 I/p 2

O/p 3 I/p 3

O/p 4 I/p 4

If uC send ‘1’ to I/p 1, O/p is conn to + VPP


If uC send ‘0’ to I/p 1, O/p is conn to GND GND
Working of Motor Driver
+VPP

O/p 1 I/p 1 ( O/p from uC)

O/p 2
10I/p 2

O/p 3 I/p 3

O/p 4 I/p 4

If uC send ‘1’ to I/p 2, O/p is conn to + VPP


GND
If uC send ‘0’ to I/p 2, O/p is conn to GND
Similarly other 2 o/p are connected /disconnected by I/p 3 & I/P 4

All the O/p’s operate independently, ie if all I/p 1-I/p 4 are 1, all O/p1 – O/p4
will be connected to +VPP

L298 can provide 1A current / Output channel , therefore total of 4A. But heat
sinks should be installed for such high currents .

VPP can be anything in the range 2V- 46V , therefore any motor can be driven
even though Atmega provides only 0/5 V.
L298N Motor Driver
A
T 4 Inputs 4 Outputs
M MOTOR MOTOR
E DRIVER
G
A
Driving a Motor

To drive a motor

A motor has two wires : +ve & -ve. To drive it u need to apply a voltage between
them
Lets connect +ve wire to O/p1 & -ve to O/p2 of L298

If we give I/p1 =1 & I/p2 = 0, O/p1 will be +VPP & O/p2 GND
IfIfwe
wegive
giveI/p1
I/p1=0
=1=0&&I/p2
I/p2==1
1,0,,O/p1
O/p1
O/p1will
will
willbe
be
be+VPP
GND
GND&& O/p2
O/p2 GND
+VPP
The voltage diff between two wires of motor = +VPP , therefore it will run in
The
The voltage diff
diffbetween
betweentwotwowires
wiresof
ofmotor
motor==-VPP
0,0,therefore
, therefore
ititwill
it NOT
will
NOTrun
run
runin
onevoltage
direction therefore will
reverse direction
Lets Connect I/p 1 to PortB.0 & I/p 2 to PortB.1

As these ports will o/p data from uC, therefore their DDR should be 1

DDRB= 0b00000011;
Or
DDRB=0x03;

For running in reverse dir, PORTB.0 =0 & PORTB.1=1


For running in forward dir, PORTB.0 =1 & PORTB.1=0
PORTB=0b00000010;
Or
PORTB=0b00000001;
PORTB=0x02;
Or
PORTB=0x01;
Complete Program
Sample program for running motor forward for 1s , reverse for 1s & stop for 1s in sequence
#include<mega16.h>
#include<delay.h>

void main()
{
DDRB=0x03;
// other part included by CVAVR comes here

while (1)
{
PORTB=0x01; // forward
delay_ms(1000);

PORTB=0x02; // reverse
delay_ms(1000);

PORTB=0x00; // stop
delay_ms(1000);
}
}
Session II
Sensors
• A sensor is a device which measures a physical
quantity and converts it into a signal which can
be read by an observer or by an instrument.
• They are used to provide feedback from the
external environment
IR Sensor Pair
Object /
IR sensor Pair
Line

Transmitter (Tx)

Receiver (Rx)

Transmitter = LED( Light Emitting Diode)

Receiver = Photodiode
Principle of Operation

WHITE surface
Maximum reflection
T1 is turned ON
Vout is LOW

BLACK surface
Minimum or No Reflection
T1 is turned OFF
Vout is HIGH

R1< R2
Interfacing IR sensor with ATmega16

Out: 1
0
1
2 Out: 0
A
PORTA
T
M
E
G
A

In
Programming IR sensor with ATmega16

#include <mega16.h>

void main()
{

int x;

DDRA=0b00000011; // last two pins as o/p for Vcc & GND


PORTA.0 =1; // Vcc
PORTA.1= 0; // Gnd

x= PINA.2;

// now you can use x for any calculation u want


}
• Vout is ANALOG

But uC being digital can only read it as 0 or 1.


0 if Vout < 1.2 V
1 if Vout > 1.2 V

However most applications require much higher


resolution n multiple levels.
Analog to Digital Convertor (ADC)

• The ATmega16 has an inbuilt 10 bit ADC


(1024 Levels)
• 8 Multiplexed Single Ended Input
Channels
• 7 Differential Input Channels
Registers used in the ADC
 ADMUX – ADC Multiplexer Selection
Register
 ADCSRA – ADC Control and Status
Register A

These registers are used to configure ADC


However when using CVAVR this will be done by code
wizard
Configuring ADC using Code Wizard

ADC
1. Open a new project
2. Select ‘yes’ to use code wizard
3. Goto ADC tab on Code Wizard
Configuring ADC

Set the parameters as shown or


as per your requirement.

Finally Generate Save & Exit


Programming ADC

When you generate the program after setting the code


wizard you will a find a function
unsigned int read_adc(unsigned char adc_input)
declared & defined just after #include

It accepts an Unsigned character as argument which is basically the


hex address of the pin on which Analog i/p is applied. However we
don’t need to give the address but just the name of pin and the
conversion is done by compiler.

Return value is the digital value ( 0-1023 or 0-255) of the analog i/p.
Programming ADC

To use ADC just call the function read_adc( ) as shown

unsigned int s;
Argument specifies which pin the analog
i/p (ie o/p of sensor) is connected
s = read_adc(PINA.2) ;

S can now be used for any calculations. Its value ranges from
0-1023 (if Use 8 bits was NOT checked in configuration)
0-255 (if Use 8 bits was checked in configuration)
Interfacing IR sensor with ATmega16

Out: 1
0
1
2 Out: 0
A
PORTA
T
M
E 6
G 7
A

GND
In
Sample program

This program glows Red Led if the digital value is less than 200 and green
otherwise.
#include<mega16.h>
#include<delay.h>

read_adc as generated by Code Wizard

Void main ( ) {
unsigned int x;

DDRA=0b11000011; // bits 0,1,6,7 as o/p coz dey will o/p data to glow LEDs

while(1) {
PORTA=0b00000001; // glow only IR LED , rest switched off
x=read_adc( PINA.2); // read analog i/p & conv to digital value

if ( x<200)
PORTA.7=1; // glow RED LED if val <200
else
PORTA.6=1; // glow GREEN LED if val > 200

delay_ms(500);
}
Points to note

ADC is multiplexed only on Port A. Therefore analog inputs can only be


applied to Port A.

The LEDs used in prev example can be connected to any port A/B/C/D
as they require a digital o/p.

Even the Vcc & GND of IR sensor can be connected to any other port as
they simply require 1 & 0 to be outputted. However it is advised to
connect the Vcc & GND pins of sensor directly to the Vcc/GND points on
PCB or battery so that uC is not loaded. (although it consumes more
power as LED is always ON)

As there are 8 pins on Port A, therefore max of 8 sensors can be


connected,(with their Vcc & Gnd connected to directly battery).

For more than 8 sensors, external MUX is to used.


Line Follower

Line Follower is a robot that can follow a black line drawn on a


white/bright surface or a white line on black/dark surface without any
human intervention.

It is the simplest autonomous robot.


Line Follower : Design

The bot has two IR sensors


facing downwards (towards the
floor).
Sleft
Sright The separation between the
sensors is such that in normal
(correct) position of the bot , both
the sensors are on white
surface. ie separation > strip
width

The bot is driven by two wheel


differential drive ie the two back
wheels are driven by
independent motors.
Line Follower : Algo

When the bot is in correct pos,


both sensors are on white & read
LOW. The robot should move
forward in this case.
Sleft
Sright

Sleft Sright Movement


LOW LOW Forward
Line Follower : Algo

When the bot is over line from


Sleft
Sright right side, left sensor is on white
& reads LOW whereas right
sensor is on black & reads HIGH
The robot should take a right turn
in this case to come back in
correct pos.

Sleft Sright Movement


LOW HIGH Right Turn
Line Follower : Algo

When the bot is over line from


S left

left side, right sensor is on white


& reads LOW whereas left
sensor is on black & reads HIGH
S rig

The robot should take a left turn


ht

in this case to come back in


correct pos.

Sleft Sright Movement


HIGH LOW Left Turn
Line Follower

Sleft Sright Movement Left Motor Right Motor


LOW LOW Forward Forward Forward
LOW HIGH Right Turn Forward Back
HIGH LOW Left Turn Back Forward
ADC Advanced topic
Differential Mode
Single Ended Vs Differential Mode

In single ended mode the absolute value of the voltage


(wrt to ground) applied to the ADC pin is converted to
digital.

But if we want to measure differential voltage ie the


voltage difference between two points and convert this
difference to digital value.. Like in digital voltmeter …

What are the options ?


Single Ended Vs Differential Mode
Option 1:

X = read_adc(PINA.0)
y = read_adc(PINA.1)

D= x-y;

But each conv takes 13


cycles. So total time = 26
cycles or 26us

Moreover the quantization


error of two i/p gets added,
hence ‘d’ has double error in it.
Differential Mode

Atmega provides an efficient way of doing this

In Differential Mode, the difference in the analog voltage


between two pins is quantized, instead of the absolute
value of potentials on each pin.

To use ADC in Differential mode, only the value of ADMUX


is to be changed.
Session III
LCD

It is basically an array of LEDs.

Already has a inbuilt diver/controller, so you just need to specify


the location and character to be printed.

Two broad divisions:-


• Character LCD’s : ex calculators
• Graphic LCD’s : ex Mobile phones, Xerox Machines.

Commonly available config for character LCD’s


16 X 2 , 20 X 2 , 40 X 4
Character LCD

The LCD that has been given to you comes under character LCD and has 2 rows
with 16 columns each. The coordinates mapping is shown below.

0,0 15,0

0,1 15,1

It has 16 pins whose description in given on the next slide.


Pin Configuration of character LCD’s
All the connections for LCD are already printed on PCB, except you have to connect
them to any port. As shown below

LCD A
T
M
01234567 E
G
A

Connect this to any of the ports but the pin alignment must be there, ie
PORTx.0 should be connected to ‘0’ shown above and so on
Programming of LCD
LCD Commands

lcd_clear( ); clears the LCD


lcd _gotoxy(col,row); places cursor at cordinates (col,row) , see slide2 for cord
lcd_putsf(“ Hello”); prints the string on LCD. If the string is more than 16 chars it overflows to next line
lcd_puts(d); prints the string in array d on LCD.(simlar to puts function in C) If the string is more
than 16 chars it overflows to next line
lcd_putchar(s); prints the character in variable ‘s’ on LCD.

Related functions

itoa(x,s); converts interger ‘x’ to string ‘s’. This is required when u want to print an integer. You must
first convert it to string as puts accepts only strings

ftoa(x,s); converts float ‘x’ to string ‘s’. This is required when u want to print a float no. You must first
convert it to string as puts accepts only strings

Note: you must include “stdlib.h” for using these functions


Initializing LCD
Goto LCD tab in CodeWizard . Select the port on which you have connected LCD .
Keep the Chars/Line to 16 as the given LCD is 16X2 . Generate the code
Sample Program
Blinking LCD, it prints Hello for 1s and then is cleared for 1s

// whatever Code wizard generates


#include<delay.h>

void main() {

// whatever code wizard generates

lcd_clear();
While (1)
{
lcd_gotoxy(0,0);
lcd_putsf(“Hello”);
delay_ms(1000);

lcd_clear( );
delay_ms(1000);
}
}
Timer & Counter

A timer/Counter is a circuit that repeatedly counts from BOTTOM to TOP

A simple way to do this is run a loop…

Is there any harm???


Timer & Counter

Atmega16 has 3 counters, namely

TIMER0 ( 8 bit) : 0- 255


TIMER1 (16 bit) : 0- 65535
TIMER2 (8 bit) : 0- 255

Each timer has 3 registers associated with them that hold the
count values and used for configuration.
TCNT0- Timer/Counter Register

7 6 5 4 3 2 1 0
TCNT0[7:0]

It holds the counter value at any point of time.

Similarly we have TCNT1[15:0] & TCNT2[7:0] .

TCNT1 [15:0] is also written as TCNT1H[7:0] & TCNT1L[7:0]


OCR0 – Output Compare Register

7 6 5 4 3 2 1 0
OCR0[7:0]

OCR holds the value to which the count is to be compared.

Similarly we have OCR1[15:0] & OCR2[7:0].

Timer 1 has two compare match registers – OCR1A & OCR1B


TCCR0 – Timer/Counter Control
Register
7 6 5 4 3 2 1 0
FOC0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00

FOC0 Force Output Compare


WGM00:01 Waveform Generation Mode
COM01:00 Compare Match Output Mode
CS02 : 01 Clock Select

TCCR is used for configuring the timer, deciding modes of operation


etc.
Timers : Modes of Operation

Normal Mode

Clear Timer on Compare Match (CTC)

Fast PWM

Phase Correct

Commonly used are : Normal & CTC


Timers : Modes of Operation

Normal Mode
The timer counts from Initial value to 0xFF ( 0xFFFF for timer 1) and
then loops back to initial value. If the interrupt on compare match is
enabled, an interrupt is given when the value of TCNT = OCR.

CTC Mode
The timer counts from Initial value to OCR and then loops back to
initial value.ie whenever TCNT = OCR the timer is cleared to zero. If
the interrupt on compare match is enabled, an interrupt is given
when the value of TCNT = OCR.
Timers : Interrupts

Two types of interrupts are available in timers.

Overflow Interrupt
This interrupt is issued at the next clock after the timer value
reaches 0xFF (0xFFFF for time 1). ie when TCNT=0xFF

Compare match interrupt


This interrupt is issued when the timer value equals OCR. ie when
TCNT=OCR
Timers : Configuration

1. Goto Timers tab.

2. Select the appropriate timer

3. Set the parameters as required.


Programming Timers
When you generate the program after setting the code
wizard you will a find a function
interrupt [TIM0_COMP] void timer0_comp_isr(void) *
& / or interrupt [TIM0_OVF] void timer0_ovf_isr(void) **
declared & defined just after #include
* If compare match interrupt is enabled
** If overflow interrupt is enabled

These functions are automatically called whenever the interuppt is


generated by the timer, irrespective of where the execution of main
program was. Just place the code for corresponding actions in these
functions.
Sample Program
This program glows LEDs attached on PORTD for 5us after every 250us

Lets use timer 0

Clock : 1000 KHz // timer increments every 1us


Counter Value : 0 // initial value
Compare Value: 250 = FAh // as timer is incrementing every 1us, n we
need to measure 250us so Compare value of
250 will do that
CTC Mode: // otherwise counter will counter upto 255 before
going to zero, so it will measure 255us when it comes to 250 next time
Sample Program
#include <mega16.h>
#include<delay.>

interrupt [TIM0_COMP] void timer0_comp_isr(void)


{
PORTD=0xFF; // glow LEDs
delay_us(5); // wait 5us
PORTD=0x00; // Put off LEDs
}

Void main () {
DDRD = 0xFF; // as uC o/ps data on all pins to glow LED
// gen by codewizard
TCNT0=0;
OCR0=0xFA;
TCCR0= something

While (1) { }
}
Contact

Robotics Club
IT-BHU

For any queries:-

Contact: Ritesh Harjani


E-mail : [email protected]
Phone no. 9453313165

Contact: Sourabh Malwe


E-mail : [email protected]
Phone no. 9795356073

You might also like