Monday, 30 June 2014

MOTOR DRIVER Pt-2

Note: see motor driver H bridge "blog" for basic concept

Use of motor driver-



In the above block diagram, we can see that there is a micro controller (MCU). Now, this MCU may/may not take in inputs (inputs as in from sensors, other digital inputs, etc). Next, as per our programming, the MCU will generate control signals. Please note that the MCU will generate signals in form of HIGH (Vcc = 5v) or LOW (zero). But this voltage is insufficient to drive a motor. That’s why we need to use a Motor Driver.

A motor driver always has a battery input Vs (which depends upon the rating of the motor). In simple terms, what a motor driver does is that it directs the Vs voltage to the motors connected (or in fact, the output pins) to it. Thus, the motors behave as per the control signals generated using the MCU with the excitation from the external battery voltage.
The most commonly used motor driver is the L293Dwhich is a driver IC. Apart from this we have other motor driver such as L298 ,which has some advance features over L293D. Discussed separately both driver ICs.


Motor Driver H-BRIDGE Concept Pt-1


H-BRIDGE:


The H-Bridge is designed to drive a motor clockwise and anticlockwise. To reverse a motor, the supply must be reversed and this is what the H-Bridge does. An H-Bridge can be made with SWITCHES, RELAYS, TRANSISTORS or MOSFETS.

H-Bridge with switches:




Do not make circuit "A." It can easily create a SHORT-CIRCUIT. It is only a demonstration circuit.
Switch A and D will make the motor rotate clockwise. 
Switch B and C will make the motor rotate anti-clockwise. 
Switch A and B will create a BRAKE. 
Do not close switch A and C at the same time. 
Do not close switch B and D at the same time. 



An improved design is shown in Circuit C. It does not create any short-circuit:

H-bridge Motor Control




So how does it work?. The four transistors are connected together in a “H-bridge” configuration with the motor connected in the middle. To make the motor rotate in the forward direction, a high (logic “1″ or +5V) signal is applied to the forward input, while no signal is applied to the reverse input (applying a voltage to both inputs at the same time is not allowed). The speed of the motor is controlled by using the pulse width modulating signal as before. Transistors TR1 and TR4 conduct. Current flows from terminal A through to terminal B (left-to-right direction) of the motor. To reverse the motor’s direction a high signal is applied to the reverse input and transistors TR3 and TR2 conduct, allowing current to pass through the motor in the opposite direction from terminal B through to terminal A (right-to-left direction). The flywheel diodes, D1 to D4 across the transistors of the H-bridge motor control circuit help to protect the transistors from any induced back emf generated by the motor during braking. Any suitable PNP or NPN transistor can be used other than the ones above, but all the transistors should have high power ratings, if not use heatsinks.While it is possible to construct the H-bridge motor control circuit from scratch using individual components, there are lots of IC’s and “black boxes” available off-the-shelf to make motor speed control and design far easier and usually cheaper. Two H-bridge motor control IC’s that are popular and easy to connect are the National Semiconductors LMD18200 and the Texas Instruments L293D motor-driver chips. Both are easy to use with in-built diodes, shorted circuit protection and are TTL and CMOS compatible. 

WORKING OF IR SENSORS


  IR SENSORS

An IR (Infrared) sensor is used to detect obstacles in front of the robot. Whenever an obstacle is detected it gives out a signal.
                                        Applications of the IR sensor:
IR sensors are mostly used in robotic applications for the following:
1.       Proximity sensing: Detecting whether an obstacle is present within a pre-specified distance or not.
2.       Color sensing: The main application is the line follower. It is used to differentiate between black and white surfaces.
3.       Mapping and localization : Making a map of the surrounding and finding the position of the robot with respect to the surroundings.


                     WORKING OF THE IR SENSORS:
The IR sensors basically work using infrared rays. Infrared rays are a part of light spectrum that human can’t see but can be detected using special electronic gadgets. All objects in this universe emit infrared radiation including humans. The radiations emitted by different objects differ significantly. The IR sensor detects changes in the IR radiation when different objects fall on it.

The IR sensor is essentially composed of two parts:
1. Emitter
2. The detector

 The emitter generates infrared rays of a particular type. The detector continuously monitors the environment for any traces of IR light of the same type as the emitter. Whenever an obstacle comes in front of the IR sensor, the IR light emitted by the emitter gets reflected to the detector. The detector detects this light and gives out a signal.


The picture shown is very simple black box model of the IR sensor. The sensor emits IR light and gives a signal when it detects the reflected light.
Now suppose we want to use our IR sensor outdoors in sunlight. Sunlight also has an IR component. This causes our sensors to wrongly assume that there is an obstacle in front of it so how do we get around this? We use an essential property of sunlight – it is continuous. So instead of sending out the ir signal continuously from the emitter, we send it pulses. This type of IR sensor is called a TSOP- thin small outline package.



The IR light is emitted in pulses. The frequency is the inverse of Time period.

                               CHOOSING THE CORRECT IR SENSOR:

Choosing an IR SENSOR with TSOP for indoor purposes is redundant. Hence it is important to use the right sensor, keeping in mind the following:
Usage conditions: whether the sensor will be used indoors or outdoors.
Cost: TSOPs are immune to sunlight but it is not the right choice for indoor applications simply because it is costly.

                              Advanced reading – IR sensor:

An IR sensor consists of an emitter, detector and associated circuitry. The emitter is simply an IR led and the detector is simply an IR photodiode which is sensitive to IR light of the same wavelength as that emitted by the IR led .whenever light falls on the photodiode, its resistance decreases and this allows more current to flow through it. Now if a resistance is connected in series with the photo diode, the increase in current through it causes the voltage across the resistance to increase. This is because
                                                                                 V= IR
  Where,
V= voltage across the resistor
I= current flowing through the resistor
R=resistance of the resistor
  As it apparent from the above relation, the voltage increases with increase in current. Now if we compare this voltage with a preset voltage, we can know when IR light is falling on the detector, i.e., when there is an obstacle in front of the robot. The current that does this is called an operation amplifier.
It has three terminals: a +ve terminal, a –ve terminal and an output terminal. If the voltage at the +ve terminal is more than the voltage at the –ve terminal the output is a HIGH (+5V) and if the voltage at the –ve terminal is more than the voltage at the +ve terminal the output is low (0) volt. Now if we connect the –ve terminal to a preset voltage and the +ve terminal to the photo diode resistor junction, we can get  an output indicating whether an obstacle is present or not.

HERE HOW IT HAPPENS:
1. When the IR emitter is powered it begins to emit IR light .this cannot be seen with the naked eye. However it can be seen with naked eyes .However it can be seen through the lens of a camera.
2. The resistance and hence the output voltage of the IR receiver keeps changing as the intensity of the ir light incident on it is changed. When less IR light is incident (when there is no obstacle), the output voltage and vice versa.
3. This output voltage is connected to the non inverting pin of the op amp.
4. The output of the potentiometer is connected to the inverting voltage pin of the op amp.
5. Recall that the non inverting voltage of the op amp is greater than the inverting voltage, the output of the op amp is HIGH (5V) and when the non inverting voltage of the op amp is greater than the inverting voltage, the output of the op amp is HIGH (5V).
6. An LED is usually connected to the output of the op amp.
7 . When there is no obstacle in front of the sensor, the output voltage of the receiver is higher than the output of the potentiometer.
8. Hence, non inverting voltage of the op amp is greater than its inverting voltage and correspondingly the output of the op amp id HIGH and the LED glows.
9. When there is an obstacle in front of the sensor, the output voltage of the receiver decreases and goes below that of the potentiometer.
10. Hence the non inverting voltage of the op amp is lesser than its inverting voltage and its output becomes LOW (0V). Hence the LED stops glowing

                                                        CALIBRATING AN IR SENSOR

By changing the resistance of the potentiometer, the reference or the inverting voltage of the op amp is changed. This is very useful in order to make the IR sensor work in different lightning conditions. Doing this changes the range of the IR sensor.
Sunlight and some artificial lights have a significant components of IR light .This IR light adds to and in some cases replaces the light of the IR emitter. Hence the receiver continuously receives IR light irrespective of the presence and absence of an obstacle. Therefore a conventional IR sensor does not work in presence of sunlight or alight with a significant component of IR light. 

Sunday, 29 June 2014

how to install the software required for programming and how to program an AtMega16 micro-controller

Note: 1st see main components of programming a micro controller "blog" for better understanding

Installation of Software and Programming
This tutorial will show you how to install the software required for programming and how to program an At Mega 16 micro-controller.

Installing the Required Software and Drivers:
The contents of the programmer support CD include the following:

1) USBasp Driver (Programmer device driver S/W)
2) At Mega 16 Datasheet
3) Installation and Programming Tutorials
4) Make File
5) WINAVR Installation File (Programming S/W)


 Contents of the support CD

Once we have written our C code, programming a micro-controller involves two discreet steps.
1) The code in high level C language must be converted into low-level (hex code) which can be understood by the micro-controller.
2) This hex code must be ported or burnt into the micro-controller.

WINAVR is the software required to convert the code written in our high-level language (C Language) into hex code and burn it into the micro-controller.
Let us see how to install WINAVR. If you are not using the support CD, then you will first need to download the setup file. Double click on the installation file of WINAVR and install it in a preferred location.
Once WINAVR is installed, we will proceed to install the driver of the USBdude programmer. In order to do this, first open Device Manager on your computer.
Plug in the programmer into the USB port of your computer. You will find the Programmer added to the list of devices attached to the computer. However, it will tell you that the driver for USBtiny was not installed correctly.
(Download and) Right click on the USBdude in the device manager and select ‘update device driver’. You will have to manually browse the folder location to identify the folder containing the device driver. Browse through to the folder named USBdude Driver. The folder contains drivers for all the configurations of windows computers. You need not select the driver specifically. Your computer will choose the right driver from the folder. Once you have directed to the folder, click ‘Update’.



Device Manager before installation of driver



Device manager after installation of driver

You have now installed the driver of the USBasp programmer.
Once WINAVR and USBdude are both installed on your computer, you will not require the setup and driver files again.
Now, whenever you want to write a new code, you may follow the steps given below. The aforementioned steps are not required to be repeated.
Now, let us see how to connect the programmer to the AtMega16 micro-controller.


Connecting the programmer to the micro-controller:


  Fig :- AVR DUDE Programmer


When we program a micro-controller, we must always make sure that each specific programming pin of the programmer is connected to each programming pin of the micro-controller we want to program.

This USBdude programmer is designed in such a way that it’s six programming pins and the six programming pins of the AtMega16 mirco-controller are in order. Hence, we can directly program an AtMega16 micro-controller on a breadboard using this programmer without using any wires.

If you have to program a micro-controller with different configuration of programming pins, you will have to connect the programming pins of the programmer to those of the micro-controller using wires.

The six programming pins of any micro-controller (MOSI, MISO, SCK, RESET, Vcc and GND) can be found in its datasheet. The datasheet of the AtMega16 micro-controller has been provided in this support CD.
Ensure that the Ground (GND) pin of the programmer is connected to the GND pin of the microcontroller, the Vcc pin of the programmer is connected to the Vcc pin of the micro-controller and so on.

Note: If can be seen that the micro-controller is being powered by the programmer while it is being programmed (as the power pins of the programmer and the micro-controller are connected). Similarly, after the micro-controller is programmed, we can use the programmer itself to power the micro-controller. The programmer in turn is being powered by your computer. However, the amount of current that the programmer can provide is limited. It is, therefore, not recommended to power the circuit using the programmer.
The GND pin of the AtMega16 micro-controller is its 11th pin and the MOSI pin is the 6th pin. This is how the micro-controller and the programmer sit on the breadboard when you want to program the micro-controller.

_________________________________________________________________________________

Programming the Micro-controller:

Now, we may proceed to write the C code. We will write our code in a software called Programmers Notepad (pn.exe) which automatically installs when you install WINAVR. Create a separate folder with an appropriate name. Go to the folder where WINAVR was installed. In the WINAVR folder, select the pn folder. In this folder, open the pn.exe file. You can also find this file in the WINAVR folder in the All Programs menu when you open START.





This opens the ‘Programmer’s Notepad’, a commonly used editing software for programming.
Write your C code in the programmer’s notepad and save the file in the new folder with a “.c”
extension. For example, if your program is called main, you will have to save it as main.c
In order to program the micro-controller successfully, we have to pass on certain information about the hardware we are using to the compiler. We do this with the help of a MakeFile. You will find this MakeFile in the Support CD or in the ‘Resources’ tab.
Open this MakeFile with Programmer’s notepad. It will open as a separate tab in addition to the tab containing your C code.
Save this MakeFile in the same folder as your C code. This step is very important and is often
forgotten by beginners. Next, we need to modify certain parameters of the MakeFile.
Generally, we will modify only three parameters of the Makefile;

1) The microcontroller used: Change it to atmege16 (all small and no gaps)
                          #MCU name
                          MCU = atmega16

2) The TARGET (referring to the code to be burnt): Change it to the <name of your C code> without the .c extension
                         #Target File name (without extension)
                         TARGET=my program

3) The programmer used: Change it to usbtiny (all small and no gaps).

AVRDUDE_PROGRAMMER=usbasp

Save the Makefile and the code. Remember, the MakeFile and the C code should be in the same folder.
Now we are ready to compile and burn the code. Go to Tools in the menu and select ‘Make All’.
This command converts the C code into the hex code. On completion of this process, the output
window says ‘Process Exit Code 0’. This indicates that the hex file has been generated without any errors. You will now find the generated hex file in the folder which had your C code and the MakeFile.





Next, go to Tools in the menu and select ‘Program’. This command burns the hex code from your computer into the micro-controller. You can see the progress of this process in the output window. The code is first written onto the micro-controller and is then read, by the programmer. This is done to check for any errors that might have occurred during writing the code.


On completion of this write process, the output window says ‘Process Exit Code 0’. This indicates that the write operation has been completed without any errors.












Components that are use in Programming of a Micro Controller

Main components that are use in Programming of a Micro Controller

>>Micro controller(here At-mega 16)
>>Micro controller Programmer kit
>>Cable for connecting PC USB(usb2.0/usb3.0) to micro controller kit

fig: different components reqd for programming