Workshop on Microcontroller · PDF fileInterfacing Servo motor and controlling angular...
Transcript of Workshop on Microcontroller · PDF fileInterfacing Servo motor and controlling angular...
Workshop on
Designing a wireless Controlled Battlebot and it’s
transmitter via bluetooth using joystick.
Research & Training Circle
Presented By
Mohammed Abdul Kader
B.Sc. in EEE, CUET (2006-10)
Assistant Professor, Dept. of EEE, IIUC
Email:[email protected] Website: kader05cuet.wordpress.com
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 2
Objective
1) Designing a battlebot which has following features-
* Remote Controlled
* Can move at any direction with regulated speed
* has an arm to hit other bot.
2) Designing a transmitter using joystick and bluetooth module which has
following features-
* Can control the speed and movement of Battlebot
* Can control the movement of arm of battlebot
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 3
Session Topics
Session-1
(10 am – 1 pm)
Microcontroller, Familiarization with ARDUINO UNO and programming,
Controlling speed and direction of DC motor, Familiarization with motor driver
(L298) and Joystick, A/D conversion, Controlling DC motor by joystick,
Interfacing Servo motor and controlling angular position of servo motor by
microcontroller.
Session-2
(2 pm-5 pm)
Familiarization with HC-05 Bluetooth module, Configuring BT module by AT
command, Paring two BT modules, Design of battlebot transmitter using joystick
and Bluetooth module as master, Design of battlebot using DC motor-servo
motor and Bluetooth module as slave.
Workshop Lecture plan
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 4
Session-1
Microcontroller, Familiarization with ARDUINO UNO and programming, Controlling
speed and direction of DC motor, Familiarization with motor driver (L298) and Joystick,
A/D Conversion, Controlling DC motor by joystick, Interfacing Servo motor and
controlling angular position of servo motor by microcontroller.
Timer/ Counter
I/O Port
What is Microcontroller?
Micro-computer in a single chip.
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 5
Name of Some Microcontroller Manufacturer
ATMEL (AVR microcontroller)
Microchip (PIC microcontroller)
Texas Instruments (TI)
Freescale
Philips
Motorola ARDUINO
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 6
Crystal Oscillator Program Loader Microcontroller Linear Voltage Regulator
Advantages of Arduino: Reduces hardware complexity
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 7
Advantages of Arduino: Programming Easy
• Programming in C
• Lot of functions in the library.
• No need to learn internal
architecture of microcontroller.
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 8
Arduino UNO Digital I/O Pins
Serial
Tx-Rx Pins
Vcc and GND Pins
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 9
Digital I/O Pins
Most of the important pins of microcontroller are digital input-output pins.
These pins are used to connect INPUT Device (i.e. Push Button, keypad, digital sensors
etc) and OUTPUT Device (i.e. LED, Display, Relay, Motor etc.) with microcontroller.
These pins can act as INPUT or
OUTPUT.
Digital Output pin means microcontroller
can make this pin HIGH or LOW state
(Write Operation).
Digital Input pin means microcontroller
can read HIGH or LOW state from other
devices (Read Operation).
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 10
Configuring Digital I/O Pins
digitalWrite(pin number, HIGH/LOW)
digitalWrite(13,HIGH)
digitalWrite(13,LOW)
pinMode(pin Number, OUTPUT)
An LED is connected with pin 13.
The pin should be an OUTPUT pin.
We can configure a pin as OUTPUT by
“pinMode” function.
pinMode(13, OUTPUT)
Configuring as OUTPUT
Making a Pin HIGH or LOW
Microcontroller can make a digital pin HIGH or LOW by
digitalWrite function.
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 11
Program Structure in ARDUINO
Verify Program
Load Program
Processor run the instructions
written here only once after
loading program or reset .
Serial monitor
Processor run the instructions written here
repeatedly after loading program or reset .
i.e. void loop() function creates an infinite
loop.
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 12
void setup()
{
pinMode(13,OUTPUT);
}
void loop()
{
digitalWrite(13,HIGH);
delay(300);
digitalWrite(13,LOW);
delay(300);
}
LED Blinking
Note: To insert a time delay in the program use following function-
delay(time_in_ms);
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 13
Moving a Robocar
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 14
Direction control of DC Motor
+
-
+
-
HIGH
LOW
2.7 to 5V
0 to 0.5V
40 mA
P=VI = 5 X 40 mA = 200 mW= 0.2 W
Not enough power to drive a DC motor.
The DC motor used in our project
needs voltage of 6 to 12 V and 300-
1000 mA current. Minimum of
(6X300) =1800 mW or, 1.8 W power
So, OUTPUT pins of microcontroller can not
drive the DC motor required in our project.
How can we control the direction of DC motor
by microcontroller? Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC
15
H-Bridge Motor driver
HIGH
LOW
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 16
HIGH
LOW
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 17
H-Bridge Motor driver
LOW
HIGH
ON
ON
OFF
OFF
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 18
H-Bridge Motor driver
HIGH
LOW
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 19
H-Bridge Motor driver
Motor Driver IC (L298N)
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 20
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 21
Speed Control of DC Motor
In ARDUINO,
duty cycle of PWM is set by the function
analogWrite(pin_no, duty_cycle)
Where, duty_cycle = 0 to 255 (0 to 100%)
L298 Motor Driver Module
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 22
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 23
The joystick in the picture is nothing but two potentiometers that allow us to measure the movement
of the stick in 2-D. Potentiometers are variable resistors and, in a way, they act as sensors providing us
with a variable voltage depending on the rotation of the device around its shaft.
Joystick
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 24
A/D
Converter
D0 D1 D2 D3 D4 D5 D6 D7 D8 D9
FSR 1
FSR 2 Mult
iple
xer
S0 and S1 used for
Analog Channel
Selection S0 S1
A0
A1 Analo
g
input
Digital output
Positive ref. is 5V and
Negative ref. is 0V
i.e.Conversion range (0-5
V)
Sensor Physical
parameter A/D Converter
Converts physical parameter to
corresponding analog voltage
Converts analog voltage to
digital voltage
Microprocessor
Analyze or process the
physical parameter
A/D Converter
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 25
A/D Converter in ATmega328P (ARDUINO UNO)
• 10-bit resolution (Divide 0 to Ref Volt into 1024 levels)
• 6 channel
• Internal or, External reference selection
Function to read analog value
analogRead(pin number)
Reads the analog voltage from the specified
analog pin and returns the digital value (0 to
1023) of the analog voltage.
Syntax: analogRead(pin)
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 26
Controlling the speed and direction of DC motor by Joy-stick
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 27
Controlling the speed and direction of DC motor by Joy-stick
const int MT1=4;
const int MT2=5;
const int x_axis=A0;
const int y_axis=A1;
const int enable=9;
int x_value, y_value;
void setup()
{
pinMode (MT1, OUTPUT);
pinMode(MT2, OUTPUT);
Serial.begin(9600);
}
void loop()
{
byte duty_cycle;
x_value=analogRead(x_axis);
y_value=analogRead(y_axis);
Serial.println(x_value);
Serial.println(y_value);
delay(10);
if(x_value>600)
{
duty_cycle=map(x_value,600,1023,0,255);
analogWrite(enable,duty_cycle);
digitalWrite(MT1,HIGH);
digitalWrite(MT2,LOW);
}
else if(x_value<400)
{
duty_cycle=map(x_value,400,0,0,255);
analogWrite(enable,duty_cycle);
digitalWrite(MT1,LOW);
digitalWrite(MT2,HIGH);
}
else
{
analogWrite(enable,0);
digitalWrite(MT1,LOW);
digitalWrite(MT2,LOW);
}}
Servo motor Interface
A servo motor (or servo) is a little box that contains a DC motor, an output shaft (servo arm) which is
connected to the motor through a series of gears, and an electronic circuit to control the position of the
shaft. The objective of using a servo is to achieve precise angular positioning of an object.
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 28
How servo motor works
In order to accomplish a servo function, an instantaneous positioning information of the output shaft is
fed back to the control circuit using a transducer. A simplest way of doing this is by attaching a
potentiometer to the output shaft or somewhere in the gear train. The control electronics compares the
feedback signal (which contains the current position of the shaft) from the potentiometer to the control
input signal (which contains information of the desired position of the shaft), and any difference
between the actual and desired values (known as an error signal) is amplified and used to drive the DC
motor in a direction necessary to reduce or eliminate the error. The error is zero when the output shaft
gets to the desired position. The functioning block diagram of a typical servomotor is shown below.
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 29
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 30
#include <Servo.h>
Servo arm;
int potpin = 0;
int val;
void setup()
{
arm.attach(9);
}
void loop()
{
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 180);
arm.write(val);
delay(15);
}
Controlling angular position of servo motor by potentiometer.
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 31
Session-2
Familiarization with HC-05 Bluetooth module, Configuring BT module by AT command,
Paring two BT modules, Design of battlebot transmitter using joystick and Bluetooth
module as master, Controlling DC motor by joystick, Design of battlebot using DC
motor-servo motor and Bluetooth module as slave.
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 32
• Bluetooth is a wireless technology standard for exchanging data over short distances (range of
approximately 10 meters or 32 feet) using short-wavelength UHF radio waves in the ISM band from
2.4 to 2.485 GHz from fixed and mobile devices, and building personal area networks (PANs).
Invented by telecom vendor Ericsson in 1994.
• It’s a secure protocol, and it’s perfect for short-range, low-power, low-cost, wireless transmissions
between electronic devices.
• The industrial, scientific, and medical radio band (ISM band) refers to a group of radio bands or
parts of the radio spectrum that are internationally reserved for the use of radio frequency (RF)
energy intended for scientific, medical and industrial requirements rather than for communications.
Bluetooth Communication
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 33
Serial Communication Protocol (UART)
Baud rate: The term baud rate is used to denote the number of bits transferred per second
[bps].
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 34
Pairing two Bluetooth Modules
Steps
Step-1: Loading program and setting serial Monitor.
Step-2: Connection of BT module with ARDUINO.
Step-3: Setting at AT mode.
Step-4: Configuring Bluetooth module by AT command mode.
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 35
a) Load a blank sketch to ARDUINO board.
b) Open the serial monitor. The bluetooth module is communicating at a baud rate of 38400 in
AT command mode. So change the baud rate to 38400 at bottom right corner of the serial
monitor. Also change "no line ending " to "both NL & CR" found just beside the baud rate.
Pairing two Bluetooth Modules (Cont.)
Step-1: Loading program and setting serial Monitor.
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 36
Pairing two Bluetooth Modules (Cont.)
Step-2: Connection of BT module with ARDUINO.
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 37
Step-4: Configuring Bluetooth Module by AT command
For HC05: Type "AT" (without the quotes) on the serial monitor and press enter. if "OK" appears
then everything is all right and the module is ready to take command. Now you can change the name
of the module, retrieve address or version or even reset to factory settings. To see the default name,
type AT+NAME. The name will be prompted, by default it is HC-05 or JY_MCU or something like
that. To change the name just type AT+NAME=your desired name.
Pairing two Bluetooth Modules (Cont.)
Step-3: Setting at AT mode.
Press the reset button of BT Module and then connect the module with power. The module will
enter into AT command mode, A blink at every 2 sec represent that the module is in AT command
mode.
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 38
Most useful AT commands are
AT : Ceck the connection.
AT+NAME : See default name
AT+ADDR : see default address
AT+VERSION : See version
AT+UART : See baudrate
AT+ROLE: See role of bt module(1=master/0=slave)
AT+RESET : Reset and exit AT mode
AT+ORGL : Restore factory settings
AT+PSWD: see default password
To check status:
AT+COMMAND?
To set value:
AT+COMMAND= value
Here is an important note, if the key pin is not high, i.e. not connected to Vcc while receiving AT
commands(if you released it after the module entered AT mode), it will not show the default name
even after giving right command. But you can still change the name by the command mentioned
above. To verify if the name has really changed, search the device from your pc/mobile. The changed
name will appear. To change baud rate, type AT+UART=desired baud rate. Exit by sending
AT+RESET command.
Pairing two Bluetooth Modules (Cont.)
Step-4: Configuring Bluetooth Module by AT command (Cont.)
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 39
AT+ROLE=0 (Slave mode)
AT+ADDR? (Find address of slave)-Copy address, change all colons (: ) by comma (,)
Change Role to Master: AT+ROLE=1 (Master Mode)
Change Connection Mode: AT+CMODE=0 (Connect to one device only) [N.B. AT+CMODE=1,
indicates the device can be connected to any device]
Bind to slave module: AT+BIND= slave address (address of the slave copied during configuring slave)
Pairing two Bluetooth Modules (Cont.)
Step-4: Configuring Bluetooth Module by AT command (Cont.)
Configuring Slave
Configuring Master
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 40
Circuit Diagram of Transmitter
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 41
int x_value=0, y_value=0;
int arm_state=0;
const int buttonPin=8;
int servo=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
servo=analogRead(A2);
servo=map(servo,0,1023,0,250);
if(servo>115 && servo<130) servo=120;
x_value=analogRead(A0);
y_value=analogRead(A1);
x_value=map(x_value,0,1023,0,250);
y_value=map(y_value,0,1023,0,250);
//Serial.println(x_value);
//Serial.println(y_value);
Serial.write(255);
delay(10);
Serial.write(x_value);
Serial.write(y_value);
Serial.write(servo);
}
Transmitter Program
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 42
Circuit Diagram of Receiver
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 43
Battleboat Program (Receiver Program)
#include <Servo.h>
Servo armServo;
const int rmp= A0;
const int rmn=A1;
const int lmp=A2;
const int lmn=A3;
const int ren=3;
const int len=5;
int value, x_value,y_value, rmspeed=0,lmspeed=0,
arm_state=0;
int jstick[3];
boolean a=0;
void setup(){
pinMode(rmp, OUTPUT);
pinMode(rmn, OUTPUT);
pinMode(lmp, OUTPUT);
pinMode(lmn, OUTPUT);
pinMode(ren,OUTPUT);
pinMode(len, OUTPUT);
armServo.attach(9);
Serial.begin(9600); }
void loop()
{
if(Serial.available()>0)
{
int i=0;
value=Serial.read();
if(value=255) a=1;
while(a)
{
if(Serial.available())
{
jstick[i]=Serial.read();
i++;
if(i>2) a=0;
}
}
x_value=jstick[0];
y_value=jstick[1];
arm_state=jstick[2];
}
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 44
Serial.println(arm_state);
armServo.write(arm_state);
if(y_value < 110)
{
digitalWrite(rmp,HIGH);
digitalWrite(rmn,LOW);
digitalWrite(lmp,HIGH);
digitalWrite(lmn,LOW);
rmspeed=110-y_value;
rmspeed=map(rmspeed, 0, 110, 0, 255);
lmspeed=110-y_value;
lmspeed=map(lmspeed, 0, 110, 0, 255);
//Serial.println("Reverse");
}
else if(y_value>140)
{
digitalWrite(rmp,LOW);
digitalWrite(rmn,HIGH);
digitalWrite(lmp,LOW);
digitalWrite(lmn,HIGH);
rmspeed=map(y_value, 140, 255, 0, 255);
lmspeed=map(y_value, 140,255,0,255);
}
else
{
//Serial.println("Stop");
lmspeed=0;
rmspeed=0;
}
if(x_value < 110)
{
x_value=120-x_value;
x_value=map(x_value, 0, 110, 0, 255);
rmspeed=rmspeed-x_value;
lmspeed=lmspeed+x_value;
if(rmspeed<0) rmspeed=0;
if(lmspeed>255) lmspeed=255;
}
Workshop on "Designing Battlebot" By-Mohammaed Abdul Kader, Assistant Professor, EEE, IIUC 45
else if(x_value>140)
{
x_value=map(x_value, 140, 255, 0, 255);
rmspeed=rmspeed+x_value;
lmspeed=lmspeed-x_value;
if(lmspeed<0) lmspeed=0;
if(rmspeed>255) rmspeed=255;
}
// to prevent buzzing
if(lmspeed<10) lmspeed=0;
if(rmspeed<10) rmspeed=0;
//Serial.println(rmspeed);
//Serial.println(lmspeed);
analogWrite(ren,rmspeed);
analogWrite(len,lmspeed);
delay(10);
}