Archive for the ‘8051 Tutorials’ Category

Brushed Direct Current Motors or commonly referred to as DC motor is the most common among the motors we use in every day life. A brushed DC motor can be small like those that are used in toy cars, others are more larger and powerful like those that are used in cordless electric drills. These motors can be directly connected to a battery then they spin, if we reverse the connection of the battery, the direction of rotation changes.

Different DC Motors

These motors usually operates at a higher voltage and requires much larger current. To interface it to the microcontroller, we use a driver to handle the larger voltage and current required by the motor. For simple operations like if you only want to turn the motor on and off, or need to control its speed, a simple transistor is used. However, if you would like to reverse the direction of rotation, we will be using a circuit called H-Bridge.

Shown above is one of the common way of connecting the motor, using a transistor. The transistor must be chosen that it can handle the current of the motor even if it is in stalled condition. Small DC motors found in toys can go as high as 1.5A. The motors produce reverse EMF that can destroy our transistor as well as the microcontroller, the addition of the diode protects us from these large amount of reverse voltage produced. The transistor is operated in cut-off and saturation regions, thus it acts as a switch, the resistor R1 limits the current going to the base of the transistor.

The above code will turn on the motor if a switch connected at P0 is pressed, then it will turn off the motor if not pressed. Basically, just like turning an LED on and off.

Now, if we want to reverse the direction, we need to connect our motor to an h-bridge circuit.

A h-bridge is just like 4 switches lined up and the motor at the center forming an H shape. If S1 and S4 are closed the current will flow at a certain direction, if S2 and S3 are closed the direction of current flow reverses, resulting to a change in the direction of rotation. H-bridge can be build by BJT Transistor or MOSFET. It only needs 2 pins from the microcontroller to control the direction and to stop the motors.

Nowadays, integrated h-bridge drivers such as L298 and L293D can be easily obtained. The L298 can handle upto 1.5A while the L293D can handle at most 600mA

If working with larger motors, it is wise to use a beefier H-bridge such as the Beefy H-Bridge Motor driver which can handle upto 6A motor load or a BTS7960 based if you need current up to 43A.

Considering a L298 H-Bridge, we need 3 pins to control each motor, in this example we connect the EN A of the L298 to P2.0 and IN1 and IN2 to P2.1 and P2.2.

By taking a look at the datasheet, we know what logic levels is needed by the H-Bridge to control the direction of rotation, in addition for the case of the L298, the speed of the motor can be controlled thru the enable pin. It is always wise to check the datasheet of the motor driver you are working with, like for others such as the A3941 or LB11847 , it only requires two pins to control the direction of the motor as well as its speed.

To demonstrate, we use the code above and load it to our microcontroller. Switches connected to P0.0 and P0.1 controls the speed of the motor, switch at P0.2 changes the direction. We user Timer 0 interrupt to generate Pulse Width Modulation (PWM) to control the speed of the motor. By varying the pulse width we change the average value of the voltage going to the motor thus the speed is controlled. To change direction, we follow what was stated in the datasheet.

With the advent of internet of things and more powerful microcontrollers, the 8051 and its derivative and a GSM modem is a cheaper alternative if you want to remotely control lights or actuators, monitor sensors in places where there is no internet or mobile data connection.

Various GSM modules are available in the market like SIM900 and SIM800 from Simcom. In this tutorial we will be using a SIM900A module. This modules allows users to send and receive SMS, make and receive calls as well as to send data not too large through GPRS network.

SIM900A GSM Module

GSM/GPRS Module

This modems communicate with the computer or a microcontroller through the serial port with AT commands. With this said, we will be using the UART of the 8051 to communicate with the GSM Modem. But, with the limited amount of memory, we could use the 8051 to send and receive SMS, but if you want to use HTTP, a microcontroller with a larger ROM and RAM is suggested. In addition, this modules requires a large amount of current and your power supply must be at least 1A – 2A. The above module requires at 5V 1A power supply.

Insert sim card, connect the tx and rx pins of the modem to the UART of the 8051, apply power and we are now ready to issue commands. AT commands are sent to the modem and the modem replies with a response in the following format:

<CR><LF><Response><CR><LF>

Shown in the table below is the commonly used AT commands to send and receive SMS.

AT Commands

We connect our hardware as follows:

Connection

Hardware Connections

Connect the GSM Modem to the Hardware UART of the 8051 and connect a LED to P2.7 and a switch to P0.0 (can be changed accordingly). In the later part a RGB LED was added to indicate the status of the system.

Going to the firmware. We designed the program to send an SMS if a button is pressed, and toggle the LED if the “toggle” message is received.

Loop

The program is divided into several parts such as serial port initialization, receiving sms, decoding response, sending sms, decoding received sms and processing received SMS. The whole Keil Project file can be downloaded from here.

Let’s see how it works!

8051 Tutorials

Posted: March 28, 2020 in 8051 Tutorials
I’m starting a youtube channel to put all my lecture for my students amidst the current worldwide situation

https://www.youtube.com/playlist?list=PLtOFe9m-L0iQQ0iJmj7aoyzxwla7L8u1r

Like and Subscribe!
Ciao!