DS1307 Arduino based Digital Clock in Proteus

Hello everyone, today I am going to share a complete project which is DS1307 Arduino based digital Clock in Proteus ISIS. In this project, I have designed a digital clock using Arduino UNO and DS1307 RTC Module. So, first of all, if you haven't yet installed then, you should install Arduino Library for Proteus using which you will be able to easily simulate Arduino baords in Proteus. Along with Arduino Library you will also need to install DS1307 Library for Proteus, which I have shared in my previous post as we are gonna use this RTC Module DS1307 for designing our digital clock.

So, now I hope that you have installed both these libraries successfully and are ready to design this DS1307 Arduino based Digital Clock. I have given the Simulation and Code for download below but as I always advise, don't just download the files. Instead design your own simulation and try to write your own code. In this way, you will learn more out of it. So, let's get started with DS1307 Arduino based Digital Clock in Proteus ISIS:

DS1307 Arduino based Digital Clock in Proteus

  • You can download the complete Proteus Simulation along with Arduino Code by clicking the below button.
  • You will also need DS1307 Library for Arduino, which is also available in this package.

Download Project Files

  • Now, let's get started with designing of this DS1307 Arduino based Digital Clock.
  • So, first of all, design a circuit in Proteus as shown in below figure:
  • You can see in the above figure that I have used Arduino UNO along with RTC module, LCD and the four buttons.
  • These four buttons will be used to change the year,date etc as mentioned on each of them.
  • Now here's the code for DS1307 Arduino based Digital Clock.
#include <LiquidCrystal.h>
#include <DS1307.h>
#include <Wire.h>

LiquidCrystal lcd(13,12,11,10,9,8);

int clock[7];

void setup(){
for(int i=3;i<8;i++){
 pinMode(i,INPUT); 
}

lcd.begin(20,2);
DS1307.begin();
DS1307.setDate(16,4,7,0,17,50,04);//ano,mes,dia,semana,horas,minutos,segundos
}

void loop(){
DS1307.getDate(clock);

lcd.setCursor(0,1);
lcd.print("Time: ");
Print(clock[4]);
lcd.print(":");
Print(clock[5]);
lcd.print(":");
Print(clock[6]);
lcd.setCursor(0,0);
lcd.print("Date: ");
Print(clock[1]);
lcd.print("/");
Print(clock[2]);
lcd.print("/");
lcd.print("20");
Print(clock[0]);

if(digitalRead(7)){
 clock[5]++;
 if(clock[5]>59) clock[5]=0;
 DS1307.setDate(clock[0],clock[1],clock[2],0,clock[4],clock[5],clock[6]);
 while(digitalRead(7));
}

if(digitalRead(6)){
 clock[4]++;
 if(clock[4]>23) clock[4]=0;
 DS1307.setDate(clock[0],clock[1],clock[2],0,clock[4],clock[5],clock[6]);
 while(digitalRead(6));
}

if(digitalRead(5)){
 clock[2]++;
  if(clock[2]>31) clock[2]=1;
 DS1307.setDate(clock[0],clock[1],clock[2],0,clock[4],clock[5],clock[6]);
 while(digitalRead(5));
}

if(digitalRead(4)){
 clock[1]++;
 if(clock[1]>12) clock[1]=1;
 DS1307.setDate(clock[0],clock[1],clock[2],0,clock[4],clock[5],clock[6]);
 while(digitalRead(4));
}

if(digitalRead(3)){
 clock[0]++;
 if(clock[0]>99) clock[0]=0;
 DS1307.setDate(clock[0],clock[1],clock[2],0,clock[4],clock[5],clock[6]);
 while(digitalRead(3));
}


delay(100);
}

void Print(int number){
lcd.print(number/10);
lcd.print(number%10);
}
  • Now get your hex file from Arduino software and then upload it in your Proteus software.
  • Now run your simulation and if everything goes fine, then it will look like something as shown in below figure:
  • Now you can see its today's date in the LCD and the same is shown over on the small pop up of DS1307 Clock.
  • Now the time will start on ticking and the buttons will used to change the minutes hours etc.
  • You will get the better demonstration of this project in the below video.
So, that's all for today. I hope this projects DS1307 Arduino based Digital Clock will help you in some way. So see you in next post.

DS1307 Library for Proteus

Hello friends, hope you all are fine and having fun with your lives. In today's post, I am going to share a new DS1307 Library for Proteus. Recently, I have shared the GSM Library for Proteus, which was really appreciated by the readers so I got quite excited and have designed another new Proteus Library. Currently I am working on many Proteus Libraries. In future, I am gonna design almost all the sensors in Proteus. So stay tuned with us.

Anyways coming to today's post, today we are gonna have a look at DS1307 Library for Proteus. Using this library, now you can quite easily simulate DS1307 module in Proteus in a quite stylish way. ;) I have designed it in red color because its available in red color in market. DS1307 module is already available in Proteus but that one is quite basic and it looks quite dull. The one designed by our team looks quite attractive and is also easy to use. I will post its tutorials soon in which I will interface it with Arduino and PIC Microcontroller. You should also have a look at DS1307 Arduino based Digital Clock in Proteus. So, let's get started with DS1307 Library for Proteus:

DS1307 Library for Proteus

  • First of all, download the DS1307 Library for Proteus, by clicking the below button:
DS1307 Library for Proteus

  • Now, when you click this button you will get a rar file so unrar this file and in it you will find two files named as:
  • RTCModuleTEP.IDX
  • RTCModuleTEP.LIB
  • Place both of these files in the library folder of your Proteus software.
  • Now restart your Proteus software or open it.
  • In the search component bx, search for RTCModuleTEP or DS1307 and place it in your workspace.
  • If everything goes fine then you will get your RTC Module DS1307 as shown in below figure:
  • That's it, now you have the ready to use DS1307 module in Proteus.
  • You need to add a crystal oscillator between X1 and X2 pins while the remaining are used for I2C Protocol, which is a common protocol for RTC Modules.
  • I will also post a tutorial in which I will interface this RTC Module with Arduino or PIC Microcontroller and then you will get a better idea of how to use it.
  • If you design some project using this DS1307 Library for Proteus then do share it with our community so that others could also get benefit out of it.

So, that's all for today. You should also have a look at these New Proteus Libraries for Engineering Students. I hope you are gonna enjoy this DS1307 Library for Proteus. If you have any suggestions or feedback then do let us know in comments. Till next tutorial, take care and have fun!!! :)

Send SMS with SIM900D in Proteus ISIS

Hello friends, hope you all are fine and having fun with your lives. Today, I am going to share a new exciting post on Send SMS with SIM900D in Proteus ISIS. In my previous post, I have shared the new GSM Library for Proteus, which is the first ever designed library for Proteus, and I am quite excited while sharing its features. So, today, we will use this GSM Lirbary for Proteus and we are gonna send sms with Sim900D module available in that library. We are gonna design this whole project in Proteus ISIS.

If you wanna implement it on hardware then you must have a look at Send SMS with SIM900D module with Arduino which is designed in real hardware but today we are gonna just do the simulation part. Iam not gonna use any Microcontroller today. Instead I will just send the AT commands through virtual Terminal and we will make it work.So, let's get started with Send SMSM with Sim900D in Proteus ISIS.

Send SMS with SIM900D in Proteus ISIS

  • First of all, I assume that you have already installed the GSM Library for Proteus. If you haven't then first intall it because without this library you can't use GSM module in Proteus ISIS.
  • Next thing you need to do is to design a circuit as shown in the below figure:
  • Now we need to run the simulation, so run it and in your virtual terminal, send these commands as shown in below figure:
  • The first AT command is for testing our GSM module and as it has given OK in reply so it means its working correctly.
  • The second command AT+CMGF=1 is for converting our GSM module to text messages, which it has accepted correctly and has given us OK in reply.
  • Now we are ready to send our SMS.
  • So, in order to do that we have to send it the command, as shown in below figure:
  • Now you can see, when I have given it the command which is AT+CMGS="+923326062060" , it has accepted it and replied back with this sign ">" , now the GSM module is actually asking for the message body.
  • So, let me write some text and then when I enter the Cntrl+Z command then it will send the message as shown in below figure:
  • So, I have given it the text of my blog link and then when I entered Cntrl+Z then it has sent the SMS and replied me back with +CMGS: 01 means its the first SMS sent from this GSM module.
  • So, that's how you can send any nmber of messages as you want from this module.
  • AS its a simulation, so obviously you are not gonna get the actual message on your mobile number but you can use it for testing your codes.
  • In the next post, I am gonna hopefully implement it using Arduino board or PIC Microcontroller.
So, that's all for today, I hope you have enjoyed the Send SMS with Sim900D in Proteus ISIS software. Till next tutorial, take care and have fun!!! :)

GSM Library for Proteus

Hello friends, hope you all are fine and having fun with your lives. In today's post, I am going to share GSM Library for Proteus. Yeah you have read absolutely fine, today I am gonna share the most awaited and most demanded Proteus Library. :) Till now, I have received hundreds of suggestions and requests about this Library and I have always told them that its under designing process and I will post it real soon. So finally the wait is over and we have our new GSM Library for Proteus, ready to download and simulate in Proteus. Using this GSM library for Proteus, now you can easily simulate your GSM module in Proteus and can test your code. :)

As its the first version of our GSM Library so its not complete or perfect. It is really the basic model of GSM Library and rite now it will only support some commands, which I will post below. I am still working on it and I will soon update these files and will add more commands in it but till then you have to use these commands only. Moreover, this Library contains only one module in it which is SIM900D module. I will add more soon like SIM900A and Sim300 etc. real soon. I will also interface it with different Microcontrollers like Arduino or PIC Microcontroller etc. and will share their tutorials. So. let's get started with GSM Library for Proteus:

Note: Other Proteus Libraries are as follows:

GSM Library for Proteus

  • First of all, download the GSM Library for Proteus by clicking the below button:
GSM Library for Proteus

  • When you download it, you will get three files in it which are:
    • GSMLibraryTEP.IDX
    • GSMLibraryTEP.LIB
    • GSMLibraryTEP.HEX
  • Place all these files in the Libraries folder of your Proteus software.
Note:
  • Now, open your Proteus software or restart it if its already open and in components list search for SIM900D and you will get three results for it.
  • Place all of them in your Proteus workspace and they will look like as shown in below figure:
  • Now, you can see in the above figure that we have three GSM Modules in our Proteus software.
  • These three GSM modules are exactly same in functionality as you can see they all have two pins on them which are TX and RX and they are only differ in color but they all work on Serial Port.
  • One is in light blue color which is kind of our theme color, next one is in green color while the last one is in red color.
  • So, now let's have a look at how you can use it in your Proteus simulations.
  • Double click any of them and in the program file section, browse to the GSMLibraryTEP.HEX file and upload it in SIM900D module as shown in below figure:
  • AS you can see in the above figure that I have uploaded the GSMLibraryTEP.HEX file in the Program file section.
  • Now click OK and interface a Virtual Terminal with SIM900D, as shown in below figure:
  • Now, I am gonna run my simulation and will send it AT commands we will check the response of this GSM module. :)
  • Now these are some basic commands, which are rite now supported by this version of GSM Module.
  • It won't be able to send or receive SMS rite now because these functionalities are not added yet but they are coming soon, as I am still working on it.
Note: Here's the complete list of commands currently supported by this Sim900D module:
  • AT
  • AT+CPIN?
  • AT+CSQ
  • AT+CGMI
  • AT+COPS?
  • AT+CGMM
  • AT+CGSN
  • AT+CNUM
  • ATA
  • ATH
  • AT+COLP
  • AT+CLIP
  • AT+VTS=1
  • AT+CSMP?
  • AT+CSCS?
  • AT+CSCS="HEX"
  • AT+CSMP
  • AT+CNMI=1,2,0,0,0
  • AT+CMGF=1
  • AT+CMGD=1
  • So, these are the commands which are currently supported by this Version 1.0 of our SIM900D GSM Module. I am gonna add more soon. :)
  • Now, here's a quick video in which I have shown its working, which will give you the better idea of this GSM Module.

Upgrade # 1: Send SMS with Sim900D

That's all for today, I hope you are gonna enjoy this GSM Module. Must write your experience in the below comments which will work as a boost for me and I will design it even faster. :) So, till next tutorial, take care and have fun !!! :)

DC Motor Direction Control with Arduino in Proteus

Hello friends, hope you all are fine and having fun with life. Today, I am going to share DC Motor Direction Control with Arduino. I have designed a complete simulation in Proteus, which will help you in understanding the controlling of DC motor. I would recommend you to first read How to Control relay in Proteus ISIS which will help you in understanding the functionality of relays because in today's tutorial, I have used relays to do the DC Motor Direction Control. I have already posted a tutorial on DC Motor Drive Circuit in Proteus ISIS.

So, for DC Motor Direction Control, I have used Arduino UNO baord, so you should also download this Arduino Library for Proteus so that you can use Arduino boards in Proteus software. I have also provide the simulation and the code for DC Motor Direction Control but I would recommend you to design it on your own so that you learn from it. If you have any problem then ask in comments and I will try to resolve them. In this project, I have used Serial Terminal. So, whenever someone, sends character "C" on serial terminal then the motor will move in Clockwise Direction and when someone sends character "A" then it will move in Anti-clockwise Direction and will stop on character "S". Anyways, lets get started with DC Motor Direction Control with Arduino in Proteus ISIS.

DC Motor Direction Control with Arduino in Proteus ISIS

  • You can download the Proteus simulation for DC Motor Direction Control by clicking the below button:
Download Proteus Simulation for DC Motor

  • So, now let's move on with designing it, first of all get the below components from Proteus and place them in your workspace:
  • Now, design a circuit in Proteus software, as shown in below figure:
  • You can see in the above figure that I have used two relays which I have used for DC Motor Direction Control.
  • Moreover, there's a Virtual Terminal through which I am sending the commands.
  • I have used Arduino UNO board for DC Motor Direction Control through Virtual Terminal. You should download the Arduino Library for Proteus so that you can use it in Proteus.
  • Now upload the below code in your Arduino software and get the hex file. You should read how to get the Hex file from Arduino.
int Motor1 = 2;
int Motor2 = 3;

void setup() {
  pinMode(Motor1, OUTPUT);
  pinMode(Motor2, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if(Serial.available())
  {
    char data = Serial.read();
    Serial.println(data);
    if(data == 'C'){MotorClockwise();}
    if(data == 'A'){MotorAntiClockwise();}
    if(data == 'S'){MotorStop();}
  }
}

void MotorAntiClockwise()
{
  digitalWrite(Motor1, HIGH);
  digitalWrite(Motor2, LOW);
}

void MotorClockwise()
{
  digitalWrite(Motor1, LOW);
  digitalWrite(Motor2, HIGH);
}

void MotorStop()
{
  digitalWrite(Motor1, HIGH);
  digitalWrite(Motor2, HIGH);
}
  • In the above code, I have designed three functions which I am calling on Serial receive.
  • The code is quite self explanatory but if you got problem then ask in comments and I will resolve them.
  • Once everything's done then run your simulation and if you have done fine then it will start working as shown in below figure:
  • Obviously, you can't see a moving DC motor in an image but you can get the idea from Relays position in above figure. :)
  • The below video will give you the better idea of How it works.

So, that's all for today. Hopefully now you have got the idea of How to do DC Motor Direction Control with Arduino in Proteus ISIS. In the next tutorial, I am gonna add speed control of DC Motor. So, till then take care and have fun. :)

Bluetooth Library for Proteus

Hello friends, hope you all are fine. Today, I am going to share a new Bluetooth Library for Proteus. Using this Library, now you can quite easily use Bluetooth modules in Proteus ISIS. I have designed two Bluetooth modules which are HC-05 and HC-06. We all know about these modules. We use these modules for sending data through Bluetooth. Till now, there's no such Bluetooth Library designed for Proteus and we are the first developers of this awesome Bluetooth Library for Proteus. I hope you guys are gonna like it. I have also posted a tutorial in which I have done Arduino Bluetooth Communication using HC05 in hardware. I hope that one will also be interesting to read, if you have planned to start working on Bluetooth Module.

Other bloggers are welcome to share this Bluetooth Library for Proteus on their blogs but do mention our link as a respect to our efforts. These Bluetooth modules are not gonna accept AT Commands rite now as we haven't added much functionality in it but we are gonna add more soon. I will also add more Bluetooth modules in this library and will update it with time. Rite now, it just has two Bluetooth modules in it, which are:

You can do serial communication with these modules quite easily. So, let's get started with Bluetooth Library for Proteus an see How to install it and how to use it in Proteus.

Note: Other Proteus Libraries are as follows:

Bluetooth Library for Proteus

  • So, first of all, download this Bluetooth Library for Proteus by clicking the below button:
Bluetooth Library for Proteus

  • In this rar file, you will find two files which are named as:
    • BluetoothTEP.IDX
    • BluetoothTEP.LIB
  • So, download these two files and place them in the library folder of your Proteus ISIS software.
Note:
  • Now open your Proteus software or restart it if its already open and search for Bluetooth and you will get something as shown in below figure:
  • Now select both of these modules and place them in your workspace and it will look like something as shown in below figure:
  • As, I told earlier, we have just used the basic TX and RX pins of these Bluetooth modules.
  • That's why you can see in the above figure that only TXD and RXD are working while all others are not working.
  • Let's have a look at it working, so let's design a simple circuit and do the communication between these two Bluetooth modules.
  • If you haven't worked on Virtual Terminals then you should read How to use Virtual Terminal in Proteus.
  • So, design a simple circuit as shown in below figure:
  • Now click any HC-05 module and you will get a pop up window.
  • In this window, select COM1 for first HC05 module and COM2 for second HC05 module.
  • Now your COM1 and COM2 should be virtually connected, I have shown how to connect the COM ports virtually in the below video.
  • Now, run your simulation and whatever you send in first terminal will show in second terminal and vice versa.
That's all for today, hope you have liked this post and are gonna enjoy it. Let me know about your remarks for this Bluetooth Library for Proteus. Have fun !!! :)

Arduino Bluetooth Communication using HC05

Hello friends, hope you all are fine and having fun with your lives. Today, I am going to share a new project in which we are gonna do Arduino Bluetooth communication. The Bluetooth module I have used for this project is HC-05, which is a serial Bluetooth module. We can quite easily perform the Bluetooth communication with this module using Arduino board. I have worked on many projects in which I have to send the data from sensors to my computer via Bluetooth. So, in such projects I normally use this Bluetooth module which is connected with the sensors and then Arduino gets the data from these sensors and then send this data to computer via Bluetooth module. In this project, I have used Arduino board but you can use PIC Microcontroller or 8051 Microcontroller as well. Because they both have the Serial pins on them.

Note:

Before reading any further, I think you must have a look at the below post from where you can download the Bluetooth Library for Proteus, using this library you can easily simulate HC-05 or , HC-06 in Proteus software:

I have also done Bluetooth communication with Android mobiles. In these projects I have sent the data from this Bluetooth module to Android mobiles but in such projects I have also designed Bluetooth app on which this data is received. Anyways, that's a topic of another tutorial. Today, I am gonna connect this Bluetooth module with Arduino board and then will send some data to my computer using Bluetooth. So, let's get started with Arduino Bluetooth communication using HC-05 module.

Arduino Bluetooth Communication using HC-05

  • First of all, what you need to do is to buy the Arduino board. I have designed this project using Arduino UNO board but you can buy any of the Arduino Microcontroller board.
  • Next thing you are gonna need is Bluetooth module which is HC-05. But this tutorial will also work for HC-06 or HC-07.
  • Now if you have seen HC-05 then the pins are written on it so connect them with your Arduino board as shown below:
  • This pin configuration is also shown in the below figure:
  • Now that you have connected your Arduino board with the Bluetooth module HC-05 so you are ready to do the Arduino Bluetooth communication.
  • Now upload the below code in your Arduino board:
#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3);

void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop() 
{
  if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());
}
  • Its a simple software serial code in which we are sending data from our Serial terminal to Bluetooth means whatever you write in your serial terminal will be sent over to Bluetooth and whatever you receive on your Bluetooth will be shown in serial terminal.
  • Now, download this Serial monitor software, I have designed this software and its quite simple one. You can use any other serial monitor like Virtual Terminal in Proteus or Hyper Terminal in Windows XP.
  • We are gonna use this software to get the data on our computer via Bluetooth and you computer must have the Bluetooth in your computer. :P
  • So, download this software by clicking the below button and you can read more about it Microsoft Visual Basic 2010 - Com Port Tutorial.

Download Serial Terminal

  • Now turn on your Arduino and search for the Bluetooth device in your Bluetooth settings and paired with it as shown in below figure.
  • The default pin code for HC-05 is 1234.
  • Now you can see I have paired the HC-05 device.
  • Now, open this software and connect with the COM port of your Bluetooth device.
  • The Bluetooth device generates two COM ports in my case it generated COM11 and COM12 but COM11 worked.
  • So, I connected with COM11 and then whatever I entered in my software is shown on the serial monitor of my Arduino and whatever I entered in the Serial monitor of Arduino is shown in the serial terminal software.
  • Its quite simple and you can do it quite easily.

So, that's all for today and I hope you are gonna make it work in the single attempt. If still having problems then ask in comments and I will resolve them. So, today we have done Arduino Bluetooth communication using HC-05 module.

555 Timer Projects

Hello friends, hope you all are fine and having fun with your lives. Today I am gonna post 555 Timer projects list which are already posted on our blog. Actually, I have posted many 555 Timer Projects on my blog but we don't have a list of these tutorials and they are quite scattered. So, today I thought to arrange them in a proper list so that you can find all of them in one place. All these 555 timer projects are simulated in my favorite simulating software Proteus. I have also given their simulations for download in almost all tutorials. If you feel problem in any of them then ask in comments and I will resolve them.

All these 555 Timer Projects and tutorials are written and designed completely by our team so we hold the complete ownership for these projects. Other bloggers are welcome to share them on their blogs to spread knowledge but do mention our post link as we have done a lot of work and effort in designing these tutorials and projects. :)

I will keep on updating this list in future as I am gonna add more projects on 555 Timer, I will add their links below. So, enough with the talking, let's get started with 555 Timer projects.

555 Timer Projects

I have divided these projects and tutorials in different sections depending on their complexity. Follow all these tutorials step by step and you are gonna be expert in 555 Timer real soon. I will keep on updating this list in future, whenever I am gonna add new project on 555 Timer, I will post the link here.

Basics of 555 Timer

Below tutorials will give you the basics of 555 Timer IC. So these tutorials are kind of must because if you don't know the basics of any integrated chip then how can you use it in your ciruits. So must read them once and then move to next section:

555 Timer Projects - Basics

I hope you have read the basics of 555 Timer, so now here's time to get started with 555 Timer Projects. These projects are designed in Proteus simulating software and are working perfectly. Simulations are given for download in most of these tutorials. So, lets get started:

555 Timer Projects - Advanced

Now I think you are quite expert in 555 Timer and have done the basic projects so now its time to move to the next level and design advance level projects with 555 Timer. In these projects we are gonna interface difference electronic modules with 555 Timer.

How to Generate PWM in 8051 Microcontroller

Hello everyone, hope you all are fine and having fun with your lives. In today's post, I am going to share How to generate PWM in 8051 Microcontroller. PWM is an abbreviation of Pulse Width Modulation and is used in many engineering projects. It is used in those engineering projects where you want an analog output. For example, you want to control the speed of your DC motor then you need a PWM pulse. Using PWM signal you can move your motor at any speed from 0 to its max speed. Similarly suppose you wanna dim your LED light, again you are gonna use PWM pulse. So, in short, it has numerous uses. If you are working on Arduino then you should read How to use Arduino PWM Pins.

PWM, as the name suggests, is simply a pulse width modulation. We take a pulse and then we modulate its width and make it small or big. Another term important while studying PWM is named duty cycle. The duty cycle shows the duration for which the PWM pulse remains HIGH. Now if the pulse remains high for 50% and LOW for 50% then we say that PWM pulse has a duty cycle of 50%. Similarly, if the pulse is HIGH for 70% and Low for 30% then it has a duty cycle of 70%.

Most of the microcontrollers have special pins assigned for PWM as in Arduino UNO it has 6 PWM pins on it. Similarly, PIC Microcontrollers also have PWM pins but unfortunately, the 8051 Microcontroller doesn't have this luxury means there are no special PWM pins available in 8051 Microcontroller. But PWM is necessary so we are going to manually generate the PWM pulse using Timer0 interrupt. So, before reading this tutorial you must first read How to use Timer Interrupt in 8051 Microcontroller so that you understand the functioning of Timer Interrupt. Anyways, let's get started with the generation of PWM in the 8051 Microcontroller.

Where To Buy?
No.ComponentsDistributorLink To Buy
18051 MicrocontrollerAmazonBuy Now

How to Generate PWM in 8051 Microcontroller ???

  • You can download both the simulation and the programming code for PWM in 8051 Microcontroller by clicking the below button:
Download PWM Code & Simulation
  • First of all, design a simple circuit as shown in the below figure:
  • Now what we are gonna do is we are gonna generate a PWM pulse using timer0 interrupt and then we are gonna send it to P2.0.
  • I have attached an oscilloscope on which we can easily monitor this PWM pulse and can check whether it's correct or not.

Code in Keil uvision 3

  • Now, copy the below code and paste it into your Keil uvision software. I have used Keil uvision 3 for this code compiling.
#include<reg51.h>

// PWM_Pin
sbit PWM_Pin = P2^0;		   // Pin P2.0 is named as PWM_Pin

// Function declarations
void cct_init(void);
void InitTimer0(void);
void InitPWM(void);

// Global variables
unsigned char PWM = 0;	  // It can have a value from 0 (0% duty cycle) to 255 (100% duty cycle)
unsigned int temp = 0;    // Used inside Timer0 ISR

// PWM frequency selector
/* PWM_Freq_Num can have values in between 1 to 257	only
 * When PWM_Freq_Num is equal to 1, then it means highest PWM frequency
 * which is approximately 1000000/(1*255) = 3.9kHz
 * When PWM_Freq_Num is equal to 257, then it means lowest PWM frequency
 * which is approximately 1000000/(257*255) = 15Hz
 *
 * So, in general you can calculate PWM frequency by using the formula
 *     PWM Frequency = 1000000/(PWM_Freq_Num*255)
 */
#define PWM_Freq_Num   1	 // Highest possible PWM Frequency


// Main Function
int main(void)
{
   cct_init();   	       // Make all ports zero
   InitPWM();              // Start PWM
 
   PWM = 127;              // Make 50% duty cycle of PWM

   while(1)                // Rest is done in Timer0 interrupt
   {}
}

// Init CCT function
void cct_init(void)
{
	P0 = 0x00;   
	P1 = 0x00;   
	P2 = 0x00;   
	P3 = 0x00;  
}

// Timer0 initialize
void InitTimer0(void)
{
	TMOD &= 0xF0;    // Clear 4bit field for timer0
	TMOD |= 0x01;    // Set timer0 in mode 1 = 16bit mode
	
	TH0 = 0x00;      // First time value
	TL0 = 0x00;      // Set arbitrarily zero
	
	ET0 = 1;         // Enable Timer0 interrupts
	EA  = 1;         // Global interrupt enable
	
	TR0 = 1;         // Start Timer 0
}

// PWM initialize
void InitPWM(void)
{
	PWM = 0;         // Initialize with 0% duty cycle
	InitTimer0();    // Initialize timer0 to start generating interrupts
					 // PWM generation code is written inside the Timer0 ISR
}

// Timer0 ISR
void Timer0_ISR (void) interrupt 1   
{
	TR0 = 0;    // Stop Timer 0

	if(PWM_Pin)	// if PWM_Pin is high
	{
		PWM_Pin = 0;
		temp = (255-PWM)*PWM_Freq_Num;
		TH0  = 0xFF - (temp>>8)&0xFF;
		TL0  = 0xFF - temp&0xFF;	
	}
	else	     // if PWM_Pin is low
	{
		PWM_Pin = 1;
		temp = PWM*PWM_Freq_Num;
		TH0  = 0xFF - (temp>>8)&0xFF;
		TL0  = 0xFF - temp&0xFF;
	}

	TF0 = 0;     // Clear the interrupt flag
	TR0 = 1;     // Start Timer 0
}

  • I have added the comments in the above codes so it won't be much difficult to understand. If you have a problem then ask in the comments and I will resolve them.
  • Now in this code, I have used a PWM variable and I have given 127 to it as a starting value.
  • PWM pulse varies from 0 to 255 as it's an 8-bit value so 127 is the mid-value which means the duty cycle will be 50%.
  • You can change its value as you want it to be.

Proteus Simulation Result

  • So, now when you upload the hex file and run your simulation then you will get below results:
  • Now you can check in the above figure that the duration of HIGH and LOW is the same means the pulse is HIGH for 50% and LOW for the remaining 50% cycle.
  • Now let's change the PWM duty cycle to 85 which is 1/3 and it will generate a PWM pulse of 33% duty cycle. Here's the result:
  • Now you can easily compare the above two figures and can get the difference. In the above figure now the duty cycle has decreased as the HIGH timing of the pulse is now reduced to 1/3 and pulse is LOW for 2/3 of the total time.
That's all, for today. That's how we can generate PWM in 8051 Microcontroller. Will meet you guys in the next tutorial. Till then take care !!! :)

Interrupt Based Digital Clock with 8051 Microcontroller

Hello friends, hope you all are fine and having fun with your lives. In today's post, I am going to share Interrupt based Digital clock with 8051 Microcontroller. In the previous post, I have explained in detail How to use Timer Interrupt in 8051 Microcontroller. We have seen in that post that we can use two timers in 8051 Microcontroller which are Timer0 and Timer1. Using these timers we can easily generate interrupts. So, before going into details of this post, you must read that timer post as I am gonna use these timer interrupts in today's post.

After reading this post, you will also get the skilled hand on timer interrupt and can understand them more easily. In today's post, I am gonna design a digital clock which will increment after every one second and we will calculate this one second increment using timer interrupt. This clock will be displayed on LCD so if you are not familiar with LCD then must read Interfacing of LCD with 8051 Microcontroller. You can also implement this digital clock with any other microcontroller like Arduino or PIC Microcontroller but today we are gonna implement it on 8051 Microcontroller. The complete simulation along with code is given at the end of this post but my suggestion is to design it on your own so that you get most of it. Use our code and simulation as a guide. So, let's get started with Interrupt based Digital clock with 8051 Microcontroller. :)

Interrupt Based Digital Clock with 8051 Microcontroller

  • First of all, design a circuit as shown in below figure:

  • Now use the below code and get your hex file. I have designed this code in Keil uvision 3 compiler for 8051 Microcontroller.
#include<reg51.h>

//Function declarations
void cct_init(void);
void delay(int);
void lcdinit(void);
void WriteCommandToLCD(int);
void WriteDataToLCD(char);
void ClearLCDScreen(void);
void InitTimer0(void);
void UpdateTimeCounters(void);
void DisplayTimeToLCD(unsigned int,unsigned int,unsigned int);
void WebsiteLogo();
void writecmd(int);
void writedata(char);

//*******************
//Pin description
/*
P2.4 to P2.7 is data bus
P1.0 is RS
P1.1 is E
*/
//********************

// Defines Pins
sbit RS = P1^0;
sbit E  = P1^1;

// Define Clock variables
unsigned int usecCounter = 0;
unsigned int msCounter   = 0;
unsigned int secCounter  = 0;
unsigned int minCounter  = 0;
unsigned int hrCounter   = 0;



// ***********************************************************
// Main program
//
void main(void)
{
   cct_init();             // Make all ports zero
   lcdinit();              // Initilize LCD
   InitTimer0();           // Start Timer0
  // WebsiteLogo();			
	while(1)
	{
		if( msCounter == 0 )                                       // msCounter becomes zero after exact one sec
		{
			DisplayTimeToLCD(hrCounter, minCounter, secCounter);   // Displays time in HH:MM:SS format
		}

		UpdateTimeCounters();                                      // Update sec, min, hours counters
  	}
}
void writecmd(int z)
{
   RS = 0;             // This is command
   P2 = z;             //Data transfer
   E  = 1;             // => E = 1
   delay(150);
   E  = 0;             // => E = 0
   delay(150);
}

void writedata(char t)
{
   RS = 1;             // This is data
   P2 = t;             //Data transfer
   E  = 1;             // => E = 1
   delay(150);
   E  = 0;             // => E = 0
   delay(150);
}

void cct_init(void)
{
	P0 = 0x00;   //not used 
	P1 = 0x00;   //not used 
	P2 = 0x00;   //used as data port
	P3 = 0x00;   //used for generating E and RS
}


void InitTimer0(void)
{
	TMOD &= 0xF0;    // Clear 4bit field for timer0
	TMOD |= 0x02;    // Set timer0 in mode 2
	
	TH0 = 0x05;      // 250 usec reloading time
	TL0 = 0x05;      // First time value
	
	ET0 = 1;         // Enable Timer0 interrupts
	EA  = 1;         // Global interrupt enable
	
	TR0 = 1;         // Start Timer 0
}


void Timer0_ISR (void) interrupt 1     // It is called after every 250usec
{
	usecCounter = usecCounter + 250;   // Count 250 usec
	
	if(usecCounter==1000)              // 1000 usec means 1msec 
	{
		msCounter++;
		usecCounter = 0;
	}

	TF0 = 0;     // Clear the interrupt flag
}

void WebsiteLogo()
{
   writecmd(0x95);
   writedata('w');                                 //write
   writedata('w');                                 //write
   writedata('w');                                 //write
   writedata('.');                                 //write
   writedata('T');                                 //write
   writedata('h');                                 //write
   writedata('e');                                 //write
   writedata('E');                                 //write
   writedata('n');                                 //write
   writedata('g');                                 //write
   writedata('i');                                 //write
   writedata('n');                                 //write
   writedata('e');                                 //write
   writedata('e');                                 //write
   writedata('r');                                 //write
   writedata('i');                                 //write
   writedata('n');                                 //write
   writedata('g');                                 //write
 
   writecmd(0xd8);
 
   writedata('P');                                 //write
   writedata('r');                                 //write
   writedata('o');                                 //write
   writedata('j');                                 //write
   writedata('e');                                 //write
   writedata('c');                                 //write
   writedata('t');                                 //write
   writedata('s');                                 //write
   writedata('.');                                 //write
   writedata('c');                                 //write
   writedata('o');                                 //write
   writedata('m');                                 //write
   writecmd(0x80);
}

void UpdateTimeCounters(void)
{
	if (msCounter==1000)
	{
		secCounter++;
		msCounter=0;
	}

	if(secCounter==60)
	{
		minCounter++;
		secCounter=0;
	}

	if(minCounter==60)
	{
		hrCounter++;
		minCounter=0;
	}

	if(hrCounter==24)
	{
		hrCounter = 0;
	}
}


void DisplayTimeToLCD( unsigned int h, unsigned int m, unsigned int s )   // Displays time in HH:MM:SS format
{
	ClearLCDScreen();      // Move cursor to zero location and clear screen

	// Display Hour
	WriteDataToLCD( (h/10)+0x30 );
	WriteDataToLCD( (h%10)+0x30 );

	//Display ':'
	WriteDataToLCD(':');

	//Display Minutes
	WriteDataToLCD( (m/10)+0x30 );
	WriteDataToLCD( (m%10)+0x30 );

	//Display ':'
	WriteDataToLCD(':');

	//Display Seconds
	WriteDataToLCD( (s/10)+0x30 );
	WriteDataToLCD( (s%10)+0x30 );
}


void delay(int a)
{
   int i;
   for(i=0;i<a;i++);   //null statement
}

void WriteDataToLCD(char t)
{
   RS = 1;             // This is data

   P2 &= 0x0F;		   // Make P2.4 to P2.7 zero
   P2 |= (t&0xF0);     // Write Upper nibble of data

   E  = 1;             // => E = 1
   delay(150);
   E  = 0;             // => E = 0
   delay(150);

   P2 &= 0x0F;		   // Make P2.4 to P2.7 zero
   P2 |= ((t<<4)&0xF0);// Write Lower nibble of data

   E  = 1;             // => E = 1
   delay(150);
   E  = 0;             // => E = 0
   delay(150);
}


void WriteCommandToLCD(int z)
{
   RS = 0;             // This is command

   P2 &= 0x0F;		   // Make P2.4 to P2.7 zero
   P2 |= (z&0xF0);     // Write Upper nibble of data

   E  = 1;             // => E = 1
   delay(150);
   E  = 0;             // => E = 0
   delay(150);

   P2 &= 0x0F;		   // Make P2.4 to P2.7 zero
   P2 |= ((z<<4)&0xF0);// Write Lower nibble of data

   E  = 1;             // => E = 1
   delay(150);
   E  = 0;             // => E = 0
   delay(150);
}

void lcdinit(void)
{
  ///////////// Reset process from datasheet /////////
     delay(15000);

	 P2 &= 0x0F;		   // Make P2.4 to P2.7 zero
	 P2 |= (0x30&0xF0);    // Write 0x3
	
	 E  = 1;               // => E = 1
	 delay(150);
	 E  = 0;               // => E = 0
	 delay(150);

     delay(4500);

	 P2 &= 0x0F;		   // Make P2.4 to P2.7 zero
	 P2 |= (0x30&0xF0);    // Write 0x3
	
	 E  = 1;               // => E = 1
	 delay(150);
	 E  = 0;               // => E = 0
	 delay(150);

     delay(300);

	 P2 &= 0x0F;		   // Make P2.4 to P2.7 zero
	 P2 |= (0x30&0xF0);    // Write 0x3
	
	 E  = 1;               // => E = 1
	 delay(150);
	 E  = 0;               // => E = 0
	 delay(150);

     delay(650);

	 P2 &= 0x0F;		   // Make P2.4 to P2.7 zero
	 P2 |= (0x20&0xF0);    // Write 0x2
	
	 E  = 1;               // => E = 1
	 delay(150);
	 E  = 0;               // => E = 0
	 delay(150);

	 delay(650);

  /////////////////////////////////////////////////////
   WriteCommandToLCD(0x28);    //function set
   WriteCommandToLCD(0x0c);    //display on,cursor off,blink off
   WriteCommandToLCD(0x01);    //clear display
   WriteCommandToLCD(0x06);    //entry mode, set increment
}

void ClearLCDScreen(void)
{
	WriteCommandToLCD(0x01);   // Clear screen command
	delay(1000);
}
  • Now run your simulation and if everything goes fine then you will get results as shown in below figure:
  • The above figure is taken after 10 seconds of start of simulation in Proteus ISIS.
  • As the simulation keeps on running the clock will also keep on ticking.
  • The code is self explanatory but let me explain the interrupt function.
  • I have used Timer0 interrupt in this digital Clock.
  • The timer interrupt function is incrementing the userCounter variable by 250 which is in micro seconds. So we need 1000us as it will become 1 second. That's why I have placed the check that when userCounter == 1000 then increment the second.
  • I have added comments in the code so read it in detail and still if you stuck somewhere then ask in comments and I will resolve them.
  • You can download the complete code along with Proteus Simulation by clicking the below button:

Download Proteus Simulation and Code for Digital Clock

That's all for today. Hope you have enjoyed today's project. Will meet you guys soon in the next post. Till then take care !!! :)
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir