Friday, February 1, 2013

Time Switch_6: Prototype miniature switch design


Using all the techniques we have discussed so far I have created a very small design of a switch which can be programmed to operate in four different modes. By modes what I meant was that I can pre-define and hard-code four time values to the design.

  1. Design in the simulation level.



2.  Developed the program that will do the controlling.

3.  Testing it in a breadboard using an LED as the substitute for the AC bulb.



The description of the tagged video,

When I press the first button i change the mode of operation. The second button will activate the mode of operation.
The first LED will indicate the current mode and the second LED can be replaced by an actual relay and an AC bulb or a switch.

4.  Assumed that the relay is working. :D
5.  Designing the PCB and printing it on an A4 in laser.


6.  Etching the design.


7.  Fabricating the design.


  
In this picture you can see the AC wire connector, AC switch, Fuse, Battery connector, Switches to change the mode and finally the indicator bulb.


Here you can see the ICSP port available in the board to change the program of the microcontroller whenever we wish.

That concludes the switch design.
Thank you.

Sunday, November 11, 2012

Time Switch_5: Driving a relay.


In the previous post I mentioned about the designing the alarm. Now it’s the time to drive just one light. Within the microcontroller we don’t need to do much, just one or two entries. But in hardware we have some new things coming in to our design to facilitate the tripping of the light.

Following image will let you know how to connect the relay to the circuit.


We should connect the microcontroller to the Base of the transistor and then the transistor to the relay. That is because the current from the microcontroller is not enough to drive the relay and we need to amplify it using the transistor. The D1 diode is used to provide a current path in the transient stage of the solenoid inside the relay.


Following is the relay connected to the microcontroller.


Finally we should alter the program inside the microcontroller. Following is the alteration we need. There what we do is instead of driving the LCD we program microcontroller so that it will drive the relay by making a pin in microcontroller HIGH.

void interrupt(){

INTCON.GIE = 0;            // global interrupts disabled
if(INTCON.T0IF){             // handling the timer0 interrupt
timer_val++;
if(timer_val == 15000){          // 19531.25 overflows per second
timer_sec++;
timer_val = 0;

 if(timer_sec = 60){
  //minute
time_val--;
 i(time_val == 0){

PORTA.RA0 = 1;

 }
 }
 timer_sec = 0;
}
}
}
INTCON.T0IF = 0;           // clearing the interrupt flag bit
INTCON.GIE = 1;             // enabling global interrupts
}

That's basically it.

Thank you.


Wednesday, November 7, 2012

Time Switch_4: Designing the alarm.


In the last post we have been able to develop the design so that it can now measure seconds. Now it’s just a matter of few lines of coding before we convert it to a alarm.

Basically, we can now measure minutes. If we keep a value in a register, let’s say 10, and keep on decreasing the register by one for each minute then we will come to the value zero in 10 minutes. If we reach zero we can program the processor such that it will print “Alarm” in the LCD so that we can see that our program works.

Almost all the coding will be done inside the interrupt routine,
Following is how the interrupt routine is going to be.

void interrupt(){

INTCON.GIE = 0;            // global interrupts disabled
if(INTCON.T0IF){             // handling the timer0 interrupt
timer_val++;
if(timer_val == 15000){          // 19531.25 overflows per second
timer_sec++;
timer_val = 0;

 if(timer_sec = 60){
  //minute
time_val--;
 i(time_val == 0){
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,”Alarm”);
 }
 }
 timer_sec = 0;
}
}
}
INTCON.T0IF = 0;           // clearing the interrupt flag bit
INTCON.GIE = 1;             // enabling global interrupts
}

 This is what is happening in the above code,

1.       Calculate a second by using timer_val variable.
2.       Calculate number of seconds elapsed using timer_sec variable.
3.       if(timer_sec = 60) will only be true if number of seconds become 60 and that is a minute.
4.       If it’s a minute our known value time_val will be reduced by one.
5.       When time_val is equal to zero LCD will show “Alarm”.

Now we have the alarm. Basically this is what we needed. Now if we tell our processor to drive a relay once the alarm is being triggered it will be enough to switch on a light. J

In the next post I will talk about developing the program to drive a single relay which is even easier than this.

Thank you.

Thursday, August 2, 2012

Time Switch_3: Designing a clock II.

In the previous entry I discussed about all the requirements that we have to meet and what are the options that are available for us to use in order to generate the clock. Now it's the time to actually code it in MicroC and see how it works. If you don't know how to write a program in MicroC first you will need to learn basics of C language and then to further understand the platform it's better to read the manual that comes with the software. You can easily download it from microelectronica website for free.

Here I will directly connect the LCD to the PIC and I will configure it, so that we can easily get a feedback on how our program is working through proteous simulations. Going through the LCD datasheet will give you an insight of how it is working.

First of all I drew the schematic diagram of the system in proteous. It should be mentioned that this is not the final system but just an arrangement with the LCD module.


Figure 1: Schematic diagram of the circuit.


Now we have the design, so let's start coding.

First step would be to create a project for our product in MicroC. Use the PIC16F628A as the micro-controller and use 20MHz as the clock frequency. Now we are set up to go.

This is the code that I implemented. Following the code would be a step by step description of the code. Here also I should state that this is just a demo code, and this can be and will be changed during the course of the product development.

void configure();

unsigned int timer_val = 0;
unsigned int timer_sec = 0;
char a[] = "0";
int a1 = 0;
//LCD configuration

sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;

//Setting all the necessary configurations.
void configure(){
TRISA = 0b00000000; // port-A 0~7 output / no input
PORTA = 0x00; // port-A clear
CMCON = 0x07; // turn OFF comparators
VRCON = 0x00; // turn OFF voltage reference
TRISB = 0b00000000; // port-B 0~7 output / no input
Lcd_Init();
INTCON = 0xA0; // timer0 interrupt enable and flag cleared
OPTION_REG.PSA = 1; // clock prescalar for timer0 disabled
OPTION_REG.T0CS = 0; // timer0 connected to instruction cycle clock
}

void interrupt(){
INTCON.GIE = 0; // global interrupts disabled
if(INTCON.T0IF){ // handling the timer0 interrupt
timer_val++;
if(timer_val == 15000){ // 19531.25 overflows per second
timer_sec++;
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
a1 = a[0];
a1++;
a[0] = a1;
Lcd_Out(1,1,a);
timer_val = 0;
}
}

INTCON.T0IF = 0; // clearing the interrupt flag bit
INTCON.GIE = 1; // enabling global interrupts
}


void main() {
configure();
while(1);
}


Okay, to those who know C language the above code will be kinda familiar, but others should do their homework. ;)

Above code has two main parts,
  1. The configuration method.
  2. The interrupt method.

1. The configuration method.

This is the method I use to put all the configuration to the micro-controller. Most of the steps above are commented so that you can understand what is going on. That I believe is a very good practice for a programmer. Basically, the ports are configured as outputs(TRIS), comparators have been switched off so that all the ports can be be used for digital I/O, LCD configuration was done, timer0 interrupt has been enabled and the timer0 module parameters have been changed accordingly, referring the datasheet.

2. The interrupt method.

Within this method, I have counted the number of timer0 register overflows. Since it will overflow around 19600 times per second I have put another counter to count the number of seconds. I used the LCD to display something every second so that I can get a feedback of my program easily.

You might be wandering why I used 15000 instead of 19600. This is because, processor will spend some time to execute the instructions available inside the interrupt method. That time should be mitigated accordingly. The number will be changed later(after the practical implementation) to tune the device to measure time accurately.

Now we have a mechanism to count seconds. Let's move-on and design the alarm. :)

Saturday, June 30, 2012

Time Switch_3: Designing a clock I


Now that we have the equipment list on mind we will start programming the PIC. The softwares that I will be using for this project include 'Proteous' and 'MicroC pro for PIC'. To understand the coding a prior knowledge in C will be needed. I will try to elaborate the best I can.

Time switch is a product which deals with time. So we must have a mechanism to measure or count time. If we have that, then the rest of the project is just some coding. How can we count a second in PIC16F628A? Or what are the methods available?

  1. We can use the command 'delay_ms(1000);' in MicroC to generate a delay of 1000 milliseconds.
  2. We can use Timer modules in the PIC and use its overflow interrupts to count time.

Method 1:

This is straight forward and easy. What actually happens inside the PIC when we execute this command will give us a better insight of its capabilities.

You should be able to understand the PIC architecture in order to interpret any command.



Figure 1: How a delay is generated in a PIC.

Above flowchart explains a method to generate a delay in a micro-controller. By using appropriate values for A, B and C variables we can generate a delay. But in this method the processor will always be busy updating registers so that the user will not be able to change any configuration without interrupting the delay process. That means the processor will not be able to multi-task without interrupting the delay. Therefore I prefer not using this method.

Method 2: [Please go through PIC16F628A datasheet]

To use this method you must know about the 'Timer' modules available in the PIC. There are 3 modules 'Timer0, Timer1 and Timer2'.

'Timer0' is a 8bit register and it can be connected to the device clock with or without a pre scalar. When ever there is an overflow in the Timer0, it will generate an interrupt. By handling interrupts we can easily do the job. The user will also able to interact with the system through multi-tasking.

That is Timer0 will overflow every 51us.
(system clock = 20MHz, instruction clock = 5MHz, 255/5MHz = 51us)

So it will overflow approximately 19600 times per second.
(1 / 51us = 19600)

Let us assume that the interrupt handling will take 10us. So all together 61us ;(51+10), will be consumed per overflow which gives us around 16400 overflows per second. This will give user a window of 0.84seconds to do interactions with the device and it will not interrupt with the time counting processes.
(51us * 16400 = 0.84seconds)

If this sounds Greek. I am truly sorry, but going through the datasheet and understanding the micro-controller will solve the problem. In the TS device I will use the Method 2 to count seconds.

Next step will be coding. :)

Time Switch_2: Selecting equipment.

In any product design this step is of utmost importance. This is due to several factors. First one is that the performance of the system you design will be depending on the equipment/components you use to create it. Next is that you should purchase the equipment keeping at least one from each critical components redundant. So if you have mistakenly broken one critical component you can use the spare. Another reason is that some equipment needed to be purchased directly from the manufacture so it will take some time to reach you due to shipping, so you will be able to order components keeping a time cushion for safety and you will be able to meet the deadlines.
What are the most critical equipments in the TS?
First is the processor. Here I used a PIC16F628A. This is one of the very basic micro-controllers that you can purchase for around 1$. Due to the simplicity of the project, we don’t need to buy any sophisticated micro-controller. This is just a choice. This can be done by using any other processors (Atmel AVR, etc.).
The tasks that I want to accomplish using this micro-controller are quite simple. I want to of course drive around five switches (the number of switches will be decided later, for now we’ll plan for five). I want a LCD display to control these switches. I want a keyboard/keypad to insert commands.
We’ll forget about power supply for now and just think about the main board. I need relays to drive AC switches. I need transistors to drive the relays since they need high current (output current of a PIC is limited, so we need a current driver to drive the relays). Those are the most critical parts. Some capacitors, resistors and diodes will also be needed.
All the equipment I stated above are available everywhere.
Relays should be able to be driven by a mere 5V. That should be kept in mind. And the LM016L LCD display will be used here. I chose it because I have it with me :) and also it is available in the market for around 5$.
With those equipments in mind we’ll proceed to the next step.
:)


Friday, June 29, 2012

Time Switch_1



This is a project which will use simple methods to design a time switch. Time switch is the name I have given to the product. What this time switch(TS) does is, it will switch off or switch on a device/ or many devices which is/are connected to AC power after a given interval. It is a very simple task, but I want to design one myself and use it for my needs. I should provide that this equipment is already available in the market.

To start with I needed a logical design to track my progress as well as to make the designing process efficient. I prefer modularizing the design and implementing one by one in order to reduce the complexity of the design. This logical design may be and will be changed according to the practical scenarios that I will be facing later in my designing stages (which is the case in almost all the designs which are done by individuals).

Following is my initial logical design,



Figure 1 : Initial logical design of the TS.


The figure itself is self explanatory so I will not go in detail about it. To understand the process correctly, some prior knowledge is needed on PIC based micro-controllers, relays, rectifiers, etc.. If you don't have any knowledge on these things, don't panic because problem based learning is the best way to do. Just google what you don't understand.

Figure 1 is the design that we are going to make, next task will be to generate a design flow. The flow for this particular design can be different from one person to the other, due to his/her personal knowledge. Some of the steps can be skipped, or else some additional steps maybe required. But the intention should be to generate milestones along the way so that you can track where you are. Moreover, the flow should be in such a manner that you will be able to modularize the process. Now the design and the flow both are modularized, so you can easily design one by one and finally integrate them together (which becomes the hardest task in almost all the cases).

Figure 2: Design flow of the TS

Figure 2 is what I think the best way to develop the product, and there can be many subprocesses for each step. I will keep these as milestones and develop the project.

:)