ATtiny based bidirectional motor control using L293D
Hello friends,In this ATtiny based Project we will learn to interface L293D with ATMEL ATtiny microcontroller and control them bidirectionally.The microcontroller used here is ATtiny2313A which is interfaced to two L293D motor control chips for controlling upto 4 DC brushed motors.You can control upto 8 motors if bidirectional control is not needed.
The ability to control upto 4 motors is essential when we are building 4 wheel drive robotic projects like Rovers or under water ROV's.
If you are interested in a fully assembled ATtiny2313 Development board with twin L293D chips and MAX485 based RS485 communication,You can check out our ATtiny Motor Control Board from here (pictured below).
Circuit Diagram of the ATtiny2313 Quad Motor Control Board can be downloaded from here.
All the source codes related to this product can be downloaded from our github repository.
Use the " Motor Control " and " Motor Control Advanced " directories in the repo.
If you are new to github and want to download the code in a zip file. Please check this tutorial.
PortA,PortB and PortD of ATtiny2313A are interfaced with two L293D chips as shown in the below figure for controlling 4 DC brushed motors.Here only relevant sections of the circuit are shown ,you can check the full circuit from here.
Each L293D contains 4 half H bridge drivers that can be combined together for bidirectional control of two Motors.L293D operates on a 5V supply(VCC1,Pin16) and has a seperate input for the Motor supply (VCC2,Pin8).
In the case of ATtiny Motor Control board the voltage at VCC2 pin of both the L293D chips are controlled by the jumper JP3 (M-VCC).By selecting the jumper you can run the connected motors at either 5V or at 12V DC (VCC2 at 5V or 12V).This makes it quite easy to interface both 5V and 12V DC motors with the ATtiny Motor Control board .
In case you wants to connect motors of smaller voltage like 3V ,you can use PWM to reduce the voltage by switching the EN pin of L293D chip .All the EN pins of L293D's on the board are connected to the Timer compare channels of ATtiny2313 which makes implementing pulse width modulation quite easy.
Rotational direction of the motor connected to L293D ic controlled by changing the direction of the current flowing through the motor pins (1Y,2Y or 3Y,4Y).
If you make 1A High and 2A Low ,current will from 1Y to 2Y and motor will rotate in one direction.If you make 1A LOW and 2A HIGH,current will from 2Y to 1Y and the motor will rotate in the opposite direction.
When both 1A and 2A are at the same logic levels ie both 1A and 2A HIGH or LOW ,no current flows and the motor stops.The enable pin of the corresponding half H bridges should be high(in this case 1&2_EN =1) for the actions said above to have any effect.
The following table shows the logic levels of 1A and 2A and the actions associated with it.Here H means logic High,L means logic Low and X means Dont care.The directions Forward and Reverse are relative and is intented to show that motor rotates in opposite directions.
Here is a small code snippet to illustrate the operation of what i said above.We are going to start the motor connected to P6 in the ATtiny Motor Control board .
//Motor Connector P6
Please note that direction bits for the corresponding port bits should be set first.You can find the complete code in the github repo under "Motor Control".
All the codes in the github repo are written in embedded C and compiled using AVR-GCC (WinAVR actually).You can also use AVR Studio for compiling the source codes.The MakeFiles required for compiling the code are availabe in their corresponding directories.
You can download the hexcode into the ATtiny Motor Control board using any AVR ISP compatible programmer like USBTinyISP,USBasp or Arduino.Please note that the makefile is configured to use Arduino Uno as the programmer by default.Please change it your preferred programmer.
All the motor control functions are in the motor.c file (in the Motor Control Advanced folder ) .You can use those functions in your programs by including the motor.h file in your main c file.
You can easily control the motors by using the function "Motor_Control_P5(char Operation);" .
Here P5 stands for connector P5 on the ATtiny Motor Control board . if you want to use another connector just replace P5 with P6 or P7.
You can start the motor connected to P5 by passing the ON parameter,
Stop it by passing the OFF parameter
and reverse the motor by passing the REVERSE parameter.All are defined in the header file motor.h
Here is a small example .
You can find a more complete program in the github repo under "Motor Control Advanced" folder.
You can see the "Motor Control Advanced" program running on the ATtiny Motor Control board .The LED's on the development board makes it easy to visualize the Logic levels on the L293D motor driver chip.