Using DC Gear Motor

Whenever a robotics we talk about making a robot, the first thing comes to our mind is how Robot will move (How its motion part work).  there are always three options in front of the us, one is to  use a DC Gear motor , second is to use a stepper motor and third one is to use servo motors. When we think about  speed, weight, size, cost… DC motors are always preffered over stepper motors and servo motors. There are many things which we can do with  DC motor when we interfacing it with a microcontroller.

DC-Geared-Motor

 

  •  Control the speed of motor.
  •  Control the direction of rotation
  •  Using encoder, keeping track of how many turns are made by  motors.

Here we will work on interfacing a DC motor with a microcontroller. we will use H-bridge in interfacing a DC moto with microcontroller. Some Ic that are used to interface a DC motor with microcontroller are-

H-bridge can also be made with the help of trasistors and MOSFETs etc. rather of being cheap, they only increase the size of the design board, which is somtimes not required so using a small 16 pin IC is preffered for this purpose.

Why we are using Motor Driver for controlling motor. the perfect Ans of thing question is As microcontroller is running with small current and to run a motor need high current in compare to microcontroller  for that purpose we are using Motor Driver.

Most DC motors are normally very easy to reverse. By simply changing the polarity of the DC input, the direction of the drive shaft reverses. This property makes DC motors very popular among enthusiast people involved in robotics. In most cases, DC geared motors are used.

motor driver block

 

As in diagram shown we can run two motors with the help of 1 L293D Driver.

the following Features are available in a L293D

  •  two enable (EN) pins, pin 1 and pin 9. Pin 1 EN enables the motor Channel  M1 whereas pin 9 EN enables motor Channel M2.
  • Connect motor M1 across OUTPUT1 and OUTPUT2 i.e. across pins 3 and 6.
  • Connect motor M2 across OUTPUT3 and OUTPUT4 i.e. across pins 11 and 14.
  • The inputs for motor M1 is given across INPUT1 and INPUT2 i.e. across pins 2 and 7.
  • The inputs for motor M2 is given across INPUT3 and INPUT4 i.e. across pins 10 and 15.
  • Connect GROUND pins 4, 5, 12 and 13 to ground also work for heat sink.
  • Connect pin 16 to Vcc (=5V For Running Driver) and pin 8 to Vs (battery, 4.5V~36V)(Motor Operating voltage).

 

l293d pin diagram

 

  • If Input A1 = 0V and input A2 = 5V the motor1 will run Forward
  • If Input A1 = 5V and input A2 = 0V the motor1 will run Reverse
  • If Input A1 = 0V and input A2 = 0V Or  Input A1 = 5V and input A2 = 5V  the motor will Stop(0V) Or Breaking(5V).

motor clock

Same Situation will be occur for second channel

DC Gear Motor Using With Arduino

The Motor drive’s channel one is connected to pin 6 ,  9 to motor control and pin 8 for enable motor. and a potentiometer is connected to Analog 0 Pin for controlling the speed of motor.diy-servo-wiring

Program

int motor1Pin = 6;    // H-bridge leg 1 (pin 6, 1A)
int motor2Pin = 9;    // H-bridge leg 2 (pin 9, 2A)
int enablePin = 8;    // H-bridge enable pin
int switchPin = A0;
int Motor_speed = 0;

 void setup()                                                                    {   // set the switch as an input:

    pinMode(switchPin, INPUT);
    // set all the other pins you're using as outputs:
    pinMode(motor1Pin, OUTPUT);
    pinMode(motor2Pin, OUTPUT);
    pinMode(enablePin, OUTPUT);
    // set enablePin high so that motor can turn on:
    digitalWrite(enablePin, HIGH);
  }
void loop() {

    Motor_speed = analogRead(sensorPin);
       analogWrite(motor1Pin, Motor_speed );   // For motor run clockwise with speed //adjusted by potentiometer
        digitalWrite(motor2Pin,0);
       delay(10000);
     analogWrite(motor2Pin, Motor_speed );     // For motor run Anticlockwise with speed //adjusted by potentiometer
        digitalWrite(motor1Pin,0);
  }
DC Gear Motor Using With AVR Microcontroller
Generating Signals for controlling the direction of motor using Port A
Motot is connected to Port A at Pin PA0, PA1, PA2 and PA3.
                               Port A Avr

#include <avr/io.h>
#include <util/delay.h>          // for _delay_ms()

int main(void)
{
DDRA = 0x0F; // initialize port A
// motors connected across PA0…PA3
while(1)
{
// clockwise rotation
PORTA = 0b00000101; // PA0 = High = Vcc
// PA1 = Low = 0
// PA2 = High = Vcc
// PA3 = Low = 0

_delay_ms(500); // wait 0.5s

// counter-clockwise rotation
PORTA = 0b00001010; // PA0 = Low = 0
// PA1 = High = Vcc
// PA2 = Low = 0
// PA3 = High = Vcc

_delay_ms(500); // wait 0.5s
}
}

Here a Video of a Robot that is developed by using DC Gear Motor

[https://www.youtube.com/watch?v=lhk7XxzQQtY]

Avr Tutorial

AVR-Studio-6-Splash

Avr Studio is IDE is used for programming Avr and Arm two different architectures. Atmel has been manufacturing a lot of ARM Cortex-M and wanted to give its users seamless and easy-to-use environment to write, build and debug their applications written in C/C++ or assembly code. the introduction and creating a new project is describe in this Video.

[https://www.youtube.com/watch?v=iTTZUpzAjKc]

Before going to programming part we should learn about Pin configuration of microcontrollers. First thing is that we need to know that which microcontroller we used. in this tutorial we will use Atmega 8, Atmega 16, Atmega 32, Atmega 168, Atmega 328.  below there is pin map of   Atmega 16 and Atmega 32 microcontroller. both microntroller are 8 bit microcontroller with 40 pin configuration.

ATMEGA32-pinout

As we can see from PinMap image there are 4 Ports named asPortA, PortB, PortC and PortD. The Datasheet of Atmega16_32

atmega

Note:-  ATMEGA16 and ATMEGA32 MCUs are completely similar, except the fact that ATMEGA16 has 16KB Flash, 1KB RAM and 512B EEPROM whereas ATMEGA32 has 32KB Flash, 2KB RAM and 1KB EEPROM.

Each port consist of 8 Pin 0-7. these are 8 digital Input and output pins.with this port A can also be used as analog to digital convertors pinsnamed as ADC0 – ADC7.

There are Three registers DDRx , PINx and PORTx that are important for digital input / Output operation of AVR microcontrllers.

Selecting the direction of pin:-  DDRx bit  selects the direction of this pin.

  • DDRx =1, Portx nth pin is configured as an output pin (since there are eight pins in all in a particular port so ‘n’ can be any number between 0-7).
  • DDRx=0, Portx nth is configured as an input pin.

DDRA

The ‘x’ in DDRx is just for representation. It is replaced by the corresponding port i.e. x = A, B, C, D. Say for the example shown in the diagram above.

DDRA = (1<<DDA2) | (1<<DDA4) | (1<<DDA5);

Same as  DDRA = 0b00110100;

Now we had done with declaration of Port A pin 2, 4 and 5 as output.

PORTx Register -: This port determine the status of output pins of Port x. if the pin x of a port is defined as output pin than this register tell the value of that pin.PIN

this register declare same as DDR

PORTA = (1 << PA1) | (1 << PA3) | (1 << PA6) ;

PORTA = (1 << 1) | (1 << 3) | (1 << 6) ;

PORTA = 0b01001010;

PINx Register: this register is used for reading data from microcontroller pins.

PINx

 

Note : By default all pins of microcontroller declare as input pins.

DDRC = 0b01001000;

PINC = 0b10110111;

Sample code :

#include <avr/io.h>                                      // Includes all the definition of register port etc  #ifndef F_CPU                                                                                                                  #define F_CPU 16000000UL      //Need to include it before <util/delay.h>                            #endif                              //Change 16000000 with your crystal freq. In my case its16 MHz  #include <util/delay.h>    //includes delay functions delay_ms and delay_us                      int main(void)                                                                                                                           {                                                                                                                                   DDRD|= 0xFF; //Configured Port D as Output                                                                    while (1)                                                                                                                                    {                                                                                                                                                 PORTD |= 0xFF; //Output logic 1 on all the pins of Port D

_delay_ms(1000);//1 sec delay

PORTD &= 0×00; //Output logic 0 on all the pins of Port D

_delay_ms(1000); //1 sec delay

}

}

 

Basic Before Embedded Programing

Bitwise or bitlevel operations form the basis of embedded programming. A knowledge of Hexadecimal and Binary Numbering system is required along with conversion from binary to hex and vice-verse. the link http://www.electronics-tutorials.ws/binary/bin_3.html provides information regarding Hexadecimal Numbers.

Bit-wise Operators : Now getting armed with the knowledge of interconversion between Hexadecimal and Binary we can start with Bitwise(or bit level) operations in C. There are bascially 6 types of Bitwise operators. These are :

  • 1. Bitwise OR operator denoted by ‘|
  • 2. Bitwise AND operator denoted by ‘&
  • 3. Bitwise Complement or Negation Operator denoted by ‘~
  • 4. Bitwise Right Shift & Left Shift denoted by ‘>>‘ and ‘<<‘ respectively
  • 5. Bitwise XOR operator denoted by ‘^

Bittable

Bitwise OR ( | ): This is same as OR Operator in digital logic. if we operate OR 0 with any No n then result will be the No ie. n. itself and if we operate OR 1 with any No n then result will be 1.

Ex. if the no is N then N | 1 = 1 and N | 0 = N

with value – 1 | 8 = 9 and 0 | 8 = 8

If you are using PORTA and want to Set pin 17 without changing rest of bits than by using OR Operator: PORTA |= (1<<17) you can change that bit.

Bitwise AND ( & ) : Bitwise AND operator in C is denoted by ‘&’. When 2 numbers are operated as AND then each pair of ‘corresponding’ bits in both numbers are operated as AND. Consider two 4-bit binary numbers ANDed : 1010 & 1101 , here nth bit of both numbers are operated as AND to get the result. if A and B are two no AND with each other than if any bit value of A or B is 0 than resultant bit value is zero.

Ex. 1 & 8 = 0 and 1 & 9 = 1

(0b10001000) & (0b01111000) = 0b00001000

If you are using PORTA and want to Clear pin 17 without changing rest of bits than by using AND Operator: PORTA &= ~ (1<<17) you can change that bit.

Bitwise Negation ( ~) :

If we need to convert all 1 in a bit to 0 and all 0 to 1 than that will be done by using operator ~ . if 1001 is operated with ~ than the result will be 0110.

in Hex No if ~(0x0F0F00) than result is 0xF0F0FF

If you are using PORTA and want to toggle all bits of a port than

PORTA = ~ PORTA will be use.

XOR Operator( ^ ): XOR is short for eXclusive-OR. By definition of XOR , the result will be a ’1′ if both the input bits are different and result will be ’0′ if both are same (as seen in the table few paragraphs above). XOR can be used to check the bit difference between 2 numbers. if XOR is operated between two no A and B and if bit value of A and B are same than result will be 0 and if they are different than result will be 1.

(0b01101001) ^ ( 0b00001111) = 0b10010110

 

Setting a bit
Use the bitwise OR operator (|) to set a bit.

number |= 1 << x;

That will set bit x.

Clearing a bit
Use the bitwise AND operator (&) to clear a bit.

number &= ~(1 << x);

That will clear bit x. You must invert the bit string with the bitwise NOT operator (~), then AND it.

Toggling a bit
The XOR operator (^) can be used to toggle a bit.

number ^= 1 << x;

That will toggle bit x.

Checking a bit
To check a bit, AND it with the bit you want to check:

bit = number & (1 << x);

Arduino Tutorial

Blink LED As Digital output: digitalWrite(PinNo, Pin Value);
In most programming languages, the first program you write prints “hello world” to the screen. Since an Arduino board doesn’t have a screen, we blink an LED instead. The boards are designed to make it easy to blink an LED using digital pin 13. Some (like the Diecimila and LilyPad) have the LED built-in to the board. On most others (like the Mini and BT), there is a 1 KB resistor on the pin, allowing you to connect an LED directly. (To connect an LED to another digital pin, you should use an external resistor.) .LEDs have polarity, which means they will only light up if you orient the legs properly. The long leg is typically positive, and should connect to pin 13. The short leg connects to GND; the bulb of the LED will also typically have a flat edge on this side. If the LED doesn’t light up, trying reversing the legs (you won’t hurt the LED if you plug it in backwards for a short period of
time) .

arduin1

 

Program :

int ledPin = 13; // LED connected to digital pin 13
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
void loop()
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // waits for a second
}

 

Button For Digital Input : digitalRead(inPin);
The pushbutton is a component that connects two points in a circuit when you press it.The example turns on an LED when you press the button. We connect three wires to the Arduino board. The first goes from one leg of the pushbutton through a pull-up resistor (here 2.2 KOhms) to the 5 volt supply. The second goes from the corresponding leg of the pushbutton to ground. The third connects to a digital i/o pin (here pin 7) which reads the button’s state. When the pushbutton is open (unpressed) there is no connection between the two legs of the pushbutton, so the pin is connected to 5 volts (through the pull-up resistor) and we read a HIGH. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to ground, so that we read a LOW. (The pin is still connected to 5 volts, but the resistor in-between them means that the pin is “closer” to ground.) You can also wire this circuit the opposite way, with a pull-down resistor keeping the input LOW, and going HIGH when the button is pressed. If so, the behavior of the sketch will be reversed, with the LED normally on and turning off when you press the button. If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is because the input is “floating” – that is, it will more-or-less randomly return either HIGH or LOW. That’s why you need a pull-up or pull-down resister in the
circuit.

 

arduin2

 

 

Program :

int ledPin = 13; // choose the pin for the LED
int inPin = 2; // 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(inPin, INPUT); // declare pushbutton as input
}
void loop(){
val = digitalRead(inPin); // 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
}
}

 

 

Potentiometer Analog Input :analogRead(Analog pin no)
A potentiometer is a simple knob that provides a variable resistance, which we can read into the Arduino board as an analog value. In this example, that value controls the rate at which an LED blinks. We connect three wires to the Arduino board. The first goes to ground from one of the outer pins of the potentiometer. The second goes from 5 volts to the other outer pin of the potentiometer. The third goes from analog input 2 to the middle pin of the potentiometer. By turning the shaft of the potentiometer, we change the amount of resistence on either side of the wiper which is connected to the center pin of the potentiometer. This changes the relative “closeness” of that pin to 5 volts and ground, giving us a different analog input. When the shaft is turned all the way in one direction, there are 0 volts going to the pin, and we read 0. When the shaft is turned all the way in the other direction, there are 5 volts going to the pin and we read 1023. In between, analogRead() returns a number between 0 and 1023 that is proportional to the amount of voltage being applied to the pin.

arduino3

 

Program :

int potPin = 2; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
}
void loop() {
val = analogRead(potPin); // read the value from the sensor
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(val); // stop the program for some time
digitalWrite(ledPin, LOW); // turn the ledPin off
delay(val); // stop the program for some time
}

 

LPC21xx

When getting started in embedded programming, GPIO (viz. General Purpose Input Output) pins are one of the first things played with. Its also quite evident that the most popular embedded systems Program is Blinking LED i.e a LED connected to pin on the Microcontroller that keeps blinking. The use of GPIO is not limited to driving LEDS but can be also used for reading digital signal , generating triggers for external components , controlling external devices and what not. In this tutorial we see how to use and program GPIO Pins for lpc214x ARM 7 microcontrollers from NXP/Philips.

In this tutorial we also learn to make a project in keil MDK with a blinking LED.

First of all make download Keil MDK from keil official Site ,install it and open it .

Your page look like this

lpc1

go to

Project Menu from main menu and click on new uVision Project

Save your project with name you want to save