Micro-Controllers & Robotics: Robotics Club It-Bhu
Micro-Controllers & Robotics: Robotics Club It-Bhu
&
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 ?
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
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.
PORTA=0x5A ;
PORTA=0b01011010;
PORTA=5;
DDRA=0xFF;
DDRA=0b11110000; // pins A7-A4 as o/p & A3-A0 as i/p
x = PINA // read 8 bit integer value from Port A. 0 < x < 255
y= PINB
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;
PORTD=0x33;
x=PINA.2;
y=PINA.7;
}
LED
- GND
+ +V
Glowing LED
P 0
o 1 -
r +
t
A
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( )
{
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 2 I/p 2
O/p 3 I/p 3
O/p 4 I/p 4
O/p 2
10I/p 2
O/p 3 I/p 3
O/p 4 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;
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)
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;
x= PINA.2;
ADC
1. Open a new project
2. Select ‘yes’ to use code wizard
3. Goto ADC tab on Code Wizard
Configuring ADC
Return value is the digital value ( 0-1023 or 0-255) of the analog i/p.
Programming ADC
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>
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
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)
X = read_adc(PINA.0)
y = read_adc(PINA.1)
D= x-y;
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
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
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
void main() {
lcd_clear();
While (1)
{
lcd_gotoxy(0,0);
lcd_putsf(“Hello”);
delay_ms(1000);
lcd_clear( );
delay_ms(1000);
}
}
Timer & Counter
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]
7 6 5 4 3 2 1 0
OCR0[7:0]
Normal Mode
Fast PWM
Phase Correct
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
Overflow Interrupt
This interrupt is issued at the next clock after the timer value
reaches 0xFF (0xFFFF for time 1). ie when TCNT=0xFF
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