Introduction: Bidirectional Control of 4 DC Motors Using ATtiny Microcontroller and L293D

Hello friends,

This is a simple ATtiny based Project on controlling DC motors using ATMEL ATtiny2313 micro controller and L293D motor controller chip. In this Instructable we will learn to control the rotational direction of up to 4 DC motors using ATtiny2313 chip.

ATtiny2313 is a cheap ATMEL AVR micro controller with 2KB ISP flash memory, 128 Bytes ISP EEPROM, 128 Bytes internal SRAM. The chip is available in an easy to use 20 pin DIP package making it ideal for hobbyists.

Please note that we will be using ATtiny Quad motor Control Board sold by us in this Tutorial to control the DC motors.

In case you want to build the Motor control board from scratch you can get the complete Circuit diagram from our Github repository.

You can use the board to build your own

  • 4 wheel drive Robot /Rover
  • Slave Motor controller for Arduino
  • Learn about RS485 Programming and many more .........

All source codes for this Inscrutable are written in embedded C and compiled using AVR GCC .

You can find

Step 1: Things Required

For building this Project you will require the following things.

  1. ATtiny2313 ( 20 pin Microcontroller)
  2. Two L293D Motor Controller Chip
  3. Any 12V to 5V linear Regulator (Here LM1117 - 5V) or LM7805**
  4. Couple of LED's to monitor Logic states of L293D Pins

or a Single ATtiny2313 Quad Motor Development Board instead of 1,2,3,4

  • 4 DC brushed Motors
  • Soldering Iron
  • AVR ISP Programmer like USBtinyISP
  • Wires
  • 12V DC Power Supply

** Please note that the original circuit diagram uses LM1117-5V Linear Regulator.If you are planning on using LM7805,Please make sure to change the circuit diagram .AS LM7805 and LM1117 are not pin compatible.

Step 2: About ATtiny Motor Control Development Board

ATtiny RS485 Quad Motor Control board is a AVR development board with 20 pin ATtiny2313 microcontroller along with Motor control and RS485 communication Ports.

The motor control section contains two L293D chips and the RS485 communication section is build around the popular MAX485 chip from Maxim.

The Board is designed for controlling up to 4 DC brushed motors bidirectionally(Clockwise,Anticlockwise) or 8 motors unidirectionally through a serial interface like UART or RS485 port.

You can use the board as a slave unit for controlling the motors of your robot through a serial interface.

The user can also select the voltage levels (5V and 12V) at which the motors are driven using a jumper. This provides a flexible way for connecting a variety of motors to your board.

The 12 LED's present on the board shows the Logic Levels of the Two L293D chips on the board.

Screw terminals on the board provide an easy way to connect your motors to the board.

Step 3: Interfacing ATtiny2313 With Two L293D Motor Control Chips

Here the 2 L293D's are interfaced to ATtiny2313A using

  • Port A
  • Port B
  • and Port D.

On the ATtiny motor Control board the pins controlling the ENABLE pins of L293D (PB3,PB4,PB2,PD5) are connected to RED LED's

while the rest of the pins are connected to Green LED's making it easier for the user to visualize the logic states of ATtiny pins.

Step 4: Circuit Diagram for Interfacing ATtiny With DC Motor

Here is the circuit diagram for interfacing 4 motors with ATtiny2313.

For clarity, i have divided the ATtiny2313 chip into two parts inside the Red Rectangle.

Full Circuit diagram is available on our GitHub page

Step 5: L293D Motor Control Chip Operation

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.

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<br>//PB5 -> 1A  [U5]
//PB6 -> 2A  [U5]
//PB3 -> EN1 [U5]

PORTB  |=    (1<<PB5);  // PB5 ->  1A = High

PORTB  &= ~(1<<PB6);  // PB6 ->  2A = Low

PORTB  |=    (1<<PB3);  // PB3 -> EN1 = High or 1&2_EN =1

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".

Step 6: Debugging LED's

The ATtiny Motor Control Board has a set of LED's connected to control pins of both the L293D's which helps us to visualize the logic states of L293D chips.

All the ENABLE pins of the L293D are represented by RED LED's while the other control pins are represented by using GREEN LED's.

All the ENABLE pins of L293D are also connected to PWM output channels of ATtiny which would help you in providing speed control to the connected motors.

Step 7: Connecting the Motors to ATtiny Motor Control Board

The 1Y,2Y,3Y and 4Y terminals of both the L293D's are brought outside to Screw terminals (Terminal Block Connectors) so that you can easily connect your motors to the ATtiny Motor Control Board.

Here the terminals driven by the U5 L293D chip is shown inside the blue rectangle

and terminals driven by the U4 L293D chip is shown inside the Yellow rectangle.

Step 8: Configuring the Voltage at Which Motor Should Be Driven

The ATtiny Board comes with a handy feature to select the voltage at which the motors will be driven.

There are two voltage options (5V and 12V) which can be selected by using the jumper JP3.

The jumper helps to connect the MOTOR_VCC (refer circuit diagram) line to either a 12V power supply rail or 5V power supply rail.

MOTOR_VCC is connected to pin 8 of both L293D's and helps to set the voltage at which the motors will be driven.

If you are using 5V rated motors move the jumper to RIGHT (towards +5V) or if you are using 12V rated motors move the jumper to the LEFT (towards +12V).

**Please note that +12V line is driven directly by the DC adapter,So if you are using a 12V DC adapter the line will be at +12V potential and incase of a 9V DC adapter the line will be driven at +9V potential and so on...

Step 9: Downloading Code to ATtiny Motor Control Board

The ATTiny2313A on the board is provided with a 6 pin ATMEL ISP connector (P1) for downloading the hex file.

You can use any ATMEL ISP programming tool like USBtinyISP to program the controller on board.

Step 10: Running and Controlling the Motors

Connect the 4 DC brushed motors to P6,P7,P4,P5 screw terminals on the ATtiny Motor control Board.

Select the appropriate voltage setting (12V or 5V) for the DC motors.

Connect a 12V DC adapter to the ATtiny Motor control Board.You can see the RED Power LED lighting up.

Go to our Github repo and download the code file named "Motor_Control.c" in the Directory "Motor Control".

Compile the file "Motor_Control.c" using AVR GCC or ATMEL studio and generate the HEX file.

Download the HEX file using AVR DUDE into the ATtiny board.

Press RESET button on the Board.

The Motors connected to the board will start running.

Step 11: Direction Control of DC Motor Using ATtiny

In the previous step we learned to turn on the motors connected to our ATtiny Board.In this step we will learn to control the direction of the connected motors.

Go to our Github repo and download the code in directory "Motor Control Advanced".

you will find 3 files in that repo along with a make file .

  • main.c - Main Program
  • motor.c - Routines for controlling the Direction of Motors
  • motor.h - Header 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 .

#include <avr/io.h>
#include "motor.h" main() { // P5 Operation Motor_Control_P5(ON); // Motor on P5 ON Motor_Control_P5(OFF); // Motor on P5 OFF Motor_Control_P5(REVERSE); // Motor on P5 Reverses

}

You can find a more complete program in the github repo under "Motor Control Advanced" folder.

You can compile and download the program by using the makefile provided along with the code.

The development environment used here is AVR GCC along with AVRdude to download the code into micro controller.

Please remember to change the port settings in the makefile to suit your needs.

After you have downloaded the code the motors connected to the board will run in clockwise direction then stop and then rotate in anticlockwise direction.

This will then repeat in a cycle.

If you are interested

Please let me know about your opinions using the comment box.

Regards

Rahul.S

www.xanthium.in