Categories
8051-8052 Assembly Language

Controlling a Motor with 8051 Microcontroller and L298N Motor Driver

Today we are going to use the L298N motor driver with 8051 microcontrollers to control a DC Motor. This motor control system is specially for robotic 2-wheel differential drive car. Or could be used with any other use case where we need to drive one or two motors with the push buttons.

The program which we are demonstrating in this post is 4 push button interfaced with 8051 microcontroller. We read the button states continuously in our main loop and wait for any button to be pressed. Once any button is pressed, we jump to the respective action. We take 4 actions based on the push button. One action is for driving motor in forward direction. Which is achieved by moving both motors in similar forward direction. Second is reverse direction which is achieved by moving both motors in reverse direction. For turning right and left we move one respective motor in forward direction.

Understanding the L298N Motor Driver

he L298N is a popular H-bridge motor driver IC capable of driving two DC motors. It provides a simple and efficient way to control motor direction and speed. We will not touch the speed control part in this post as we will only be focusing on directional control with the help of L298N motor driver.

L298N PinFunction
IN1 Input for Motor A, direction control
IN2 Input for Motor A, direction control
IN3Input for Motor B, direction control
IN4Input for Motor B, direction control
ENAEnable input for Motor A
ENBEnable input for Motor B
OUT1Output to Motor A, high-side
OUT2Output to Motor A, low-side
OUT3Output to Motor B, high-side
OUT4Output to Motor B, low-side
VSSGround
VDDPower supply
L298N pin descriptions

Hardware Setup

The system comprises several essential components. The 8051 microcontroller acts as the system’s brain, processing instructions and controlling the overall operation. An L298N motor driver is employed to regulate the motor’s behavior based on signals received from the microcontroller. The DC motor, the system’s actuator, converts electrical energy into mechanical motion. A power supply provides the necessary electrical energy for both the microcontroller and the motor driver.

Software Implementation

Here we will discuss the Assembly language code for controlling the motor direction with the help of 8051 microcontroller. First of all, we have to define the pins which we are using for our switches and L298N motor driver direction control. Remember we pulled the ENA and ENB pins to 5 volt giving full speed to the motors.

OUT1		BIT		P2.0
OUT2		BIT		P2.1
OUT3		BIT		P2.2
OUT4		BIT		P2.3
SW1		BIT		P0.0
SW2		BIT		P0.1
SW3		BIT		P0.2
SW4		BIT		P0.3Code language: AVR Assembler (avrasm)

These lines define the bit addresses for motor control outputs and switch inputs.

Now coming to our main loop it looks like this.

ORG		00H


MAIN:		
		SETB		OUT1
		SETB		OUT2
		SETB		OUT3
		SETB		OUT4



AGAIN:

	JNB		SW1,ACTION1
	JNB		SW2,ACTION2
	JNB		SW3,ACTION3
	JNB		SW4,ACTION4
	ACALL	        DELAY1	
	SJMP		AGAIN

The main section initializes all motor outputs to high, effectively stopping the motor. The Again loop continuously checks the state of the switches and jumps to the corresponding action if a switch is pressed.

The delay function is pretty much similar to all our previous posts.



DELAY1:
	MOV		R7,#255
	DJNZ		R7,$
	RET


DELAY3:
	MOV		R7,#3
D3L2:	MOV		R6,#255
D3L1:	MOV		R5,#255   
	DJNZ		R5,$
	DJNZ		R6,D3L1
	DJNZ		R7,D3L2
	RET

ENDCode language: PHP (php)

our subroutines for driving motors forward, reverse, right, left and stopping the motors looks like following. .

FWD:
	CLR		OUT1
	SETB		OUT2
	ACALL	DELAY1
	RET

REV:
	SETB		OUT1
	CLR		OUT2
	ACALL	DELAY1
	RET

RIGHT:
	CLR		OUT3
	SETB		OUT4
	ACALL	DELAY1
	RET

LEFT:
	SETB		OUT3
	CLR		OUT4
	ACALL	DELAY1
	RET

STOP1:
	SETB		OUT1
	SETB		OUT2
	ACALL	DELAY1
	RET

STOP2:
	SETB		OUT3
	SETB		OUT4
	ACALL	DELAY1
	RET
Code language: ARM Assembly (armasm)

Finally our 4 actions are as follows.



ACTION1:
	ACALL	FWD
	ACALL	DELAY3
	ACALL	STOP1
	SJMP		AGAIN

ACTION2:
	ACALL	REV	
	ACALL	DELAY3
	ACALL	STOP1
	SJMP		AGAIN

ACTION3:
	ACALL	RIGHT
	ACALL	DELAY3
	ACALL	STOP2
	SJMP		AGAIN

ACTION4:
	ACALL	LEFT
	ACALL	DELAY3
	ACALL	STOP2
	SJMP		AGAINCode language: HTTP (http)

Additional Considerations

Ensure the motor power supply is sufficient to meet the motor’s demands. Given its capability to generate heat, especially when driving high-current motors, consider using a heat sink for the L298N motor driver. Optimizing the code for efficiency and performance is crucial. Implementing error handling mechanisms can detect issues like motor stalls or overcurrent, enhancing system robustness. Finally, incorporating sensors such as encoders or current sensors can provide valuable feedback for advanced motor control applications.

By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.