Automatic electronic bell for school using pic microcontroller

Electronic automatic bell system is designed for school bells. It is used to make school bells automated. We are living in the world of automated system where everything is controlled automatically using intelligent system like microcontrollers and embedded systemsSo electronic bell is designed to operated automatically with pre entered time with the help of keypad.

Block diagram of Automatic electronic bell

Block diagram of electronic bell for school is shown below. Keypad is used to set the timing of electronic bell. After reaching at set time, PIC16F877A microcontroller activates the relay for 2 minutes and electric bell start ringing for 2 minutes. After two minutes, microcontroller gives off signal to relay. Relay coil becomes de energize and switch off the electronic bell. Liquid crystal display is used to display time and status of electronic bell. DS1307 real time clock is used to keep track of time. Power supply for microcontollers is designed using 7812 and 7805 voltage regulators.

Automatic electronic bell block diagram
Automatic electronic bell block diagram

So before starting automatic electronic bell project, you should have know how to interface keypad, LCD , relay and real time clock DS1307 with pic microcontroller. If you don’t know I recommend you to check following articles first, before reading this article further.

After reading and understanding above articles, you will be easily able to build automatic electronic bell for your school.

Automatic electronic bell working

 Automatic electronic bell takes over the task of ringing of the bell in the school. It replaces the Manual Switching of the Bell in the school. It has a built in  Real Time Clock (DS1307) which track over the Real Time. When this time equals to the bell ringing time, then the relay for the bell is switched ON. The Bell Ringing time can be edited at any Time, so that it can be used at Normal Class Timings as well as Exam Times. The Real Time Clock is displayed on four 7-segment display which is able show date and time. The Microcontroller PIC16F877A is used to control all the Functions, it get the time through the keypad and store it in its Memory. And when the Real time and Bell time get equal then the Bell is switched on for a predetermined time. Table below shows the time table of any school, you can adjust the time according to your requirement  with the help of keypad. I will explain it later in circuit diagram of automatic electronic bell.

PERIODSACTIVITIESTIME (min)
1CLASS40
2CLASS40
3CLASS40
4CLASS40
5BREAKFAST30
6CLASS40
7CLASS40
8CLASS40
9LUNCH30
10CLASS40
11CLASS40
TOTAL420

Electric bell ratings are  15W, 230Vac

Circuit diagram of automatic electronic bell

Circuit diagram of automatic electronic bell for school using PIC16F877A microcontroller is given  below. After reading above articles, circuit diagram is self explanatory.  Push button connected to port B allows you to set timing of electronic bell. When push button is pressed, electronic bell will be in time setting mode. User can adjust the time using keypad.

automatic Electronic bell circuit diagram using pic microcontroller
automatic Electronic bell circuit diagram using pic microcontroller

Electronic school bell code

unsigned short kp, cnt, value,oldstate =0;
char txt[6];
char keypadPort at PORTD;
// LCD module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

unsigned short read_ds1307(unsigned short address)
{
 unsigned short r_data;
 I2C1_Start();
 I2C1_Wr(0xD0); //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
 I2C1_Wr(address);
 I2C1_Repeated_Start();
 I2C1_Wr(0xD1); //0x68 followed by 1 --> 0xD1
 r_data=I2C1_Rd(0);
 I2C1_Stop();
 return(r_data);
}


void write_ds1307(unsigned short address,unsigned short w_data)
{
 I2C1_Start(); // issue I2C start signal
 //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
 I2C1_Wr(0xD0); // send byte via I2C (device address + W)
 I2C1_Wr(address); // send byte (address of DS1307 location)
 I2C1_Wr(w_data); // send data (data to be written)
 I2C1_Stop(); // issue I2C stop signal
}


unsigned char BCD2UpperCh(unsigned char bcd)
{
 return ((bcd >> 4) + '0');
}


unsigned char BCD2LowerCh(unsigned char bcd)
{
 return ((bcd & 0x0F) + '0');
}


int Binary2BCD(int a)
{
 int t1, t2;
 t1 = a%10;
 t1 = t1 & 0x0F;
 a = a/10;
 t2 = a%10;
 t2 = 0x0F & t2;
 t2 = t2 << 4;
 t2 = 0xF0 & t2;
 t1 = t1 | t2;
 return t1;
}


int BCD2Binary(int a)
{
 int r,t;
 t = a & 0x0F;
 r = t;
 a = 0xF0 & a;
 t = a >> 4;
 t = 0x0F & t;
 r = t*10 + r;
 return r;
}

int second;
int minute;
int hour;
int hr;
int day;
int dday;
int month;
int year;
int ap;


unsigned short set_count = 0;
short set;

char time[] = "00:00:00 PM";
char date[] = "00-00-00";

void main()
{
 I2C1_Init(100000); //DS1307 I2C is running at 100KHz

 CMCON = 0x07; // To turn off comparators
 ADCON1 = 0x06; // To turn off analog to digital converters
 TRISC.F0 = 0;
 TRISA = 0x07;
 PORTA = 0x00;
 PORTC.F0 = 0;
 TRISB.f1 = 1;
 PORTB.F1 = 0;
 Lcd_Init(); // Initialize LCD
 Keypad_Init(); // Initialize Keypad
 Lcd_Cmd(_LCD_CLEAR); // Clear display
 Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
 Lcd_out(1,1,"Time:");
 Lcd_out(2,1,"Date:");

 do
 {
 set = 0;
 if(PORTA.F0 == 0)
 {
 Delay_ms(1000);
 if(PORTA.F0 == 0)
 {
 set_count++;
 if(set_count >= 7)
 {
 set_count = 0;
 }
 }
 }
 if(set_count)
 {
 if(PORTA.F1 == 0)
 {
 Delay_ms(100);
 if(PORTA.F1 == 0)
 set = 1;
 }

 if(PORTA.F2 == 0)
 {
 Delay_ms(100);
 if(PORTA.F2 == 0)
 set = -1;
 }
 if(set_count && set)
 {
 switch(set_count)
 {
 case 1:
 hour = BCD2Binary(hour);
 hour = hour + set;
 hour = Binary2BCD(hour);
 if((hour & 0x1F) >= 0x13)
 {
 hour = hour & 0b11100001;
 hour = hour ^ 0x20;
 }
 else if((hour & 0x1F) <= 0x00)
 {
 hour = hour | 0b00010010;
 hour = hour ^ 0x20;
 }
 write_ds1307(2, hour); //write hour
 break;
 case 2:
 minute = BCD2Binary(minute);
 minute = minute + set;
 if(minute >= 60)
 minute = 0;
 if(minute < 0)
 minute = 59;
 minute = Binary2BCD(minute);
 write_ds1307(1, minute); //write min
 break;
 case 3:
 if(abs(set))
 write_ds1307(0,0x00); //Reset second to 0 sec. and start Oscillator
 break;
 case 4:
 day = BCD2Binary(day);
 day = day + set;
 day = Binary2BCD(day);
 if(day >= 0x32)
 day = 1;
 if(day <= 0)
 day = 0x31;
 write_ds1307(4, day); // write date 17
 break;
 case 5:
 month = BCD2Binary(month);
 month = month + set;
 month = Binary2BCD(month);
 if(month > 0x12)
 month = 1;
 if(month <= 0)
 month = 0x12;
 write_ds1307(5,month); // write month 6 June
 break;
 case 6:
 year = BCD2Binary(year);
 year = year + set;
 year = Binary2BCD(year);
 if(year <= -1)
 year = 0x99;
 if(year >= 0x50)
 year = 0;
 write_ds1307(6, year); // write year
 break;
 }
 }
 }

 second = read_ds1307(0);
 minute = read_ds1307(1);
 hour = read_ds1307(2);
 hr = hour & 0b00011111;
 ap = hour & 0b00100000;
 dday = read_ds1307(3);
 day = read_ds1307(4);
 month = read_ds1307(5);
 year = read_ds1307(6);


 time[0] = BCD2UpperCh(hr);
 time[1] = BCD2LowerCh(hr);
 time[3] = BCD2UpperCh(minute);
 time[4] = BCD2LowerCh(minute);
 time[6] = BCD2UpperCh(second);
 time[7] = BCD2LowerCh(second);

 date[0] = BCD2UpperCh(day);
 date[1] = BCD2LowerCh(day);
 date[3] = BCD2UpperCh(month);
 date[4] = BCD2LowerCh(month);
 date[6] = BCD2UpperCh(year);
 date[7] = BCD2LowerCh(year);

 if(ap)
 {
 time[9] = 'P';
 time[10] = 'M';
 }
 else
 {
 time[9] = 'A';
 time[10] = 'M';
 }
 if(PORTB.F1)
 {
 Lcd_Cmd(_LCD_CLEAR); // Clear display
 Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
 Lcd_Out(1, 1, "1");
 Lcd_Out(1, 1, "Enter time :"); // Write message text on Lcd
 Lcd_Out(2, 1, "Times:");
 do
 {
 if(!PORTB.F1)
 { Lcd_Cmd(_LCD_CLEAR); // Clear display
 Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
 Lcd_out(1,1,"Time:");
 Lcd_out(2,1,"Date:");
 goto jump;
 }
 kp = 0; // Reset key code variable
 // Wait for key to be pressed and released
 do
 //kp = Keypad_Key_Press(); // Store key code in kp variable
 kp = Keypad_Key_Click(); // Store key code in kp variable
 while (!kp);
 // Prepare value for output, transform key to it's ASCII value
 switch (kp)
 {
 case 1: kp = 55; break; // 7 - Cmp kpi with equivalent ASCII code of 7, break if equal
 case 2: kp = 52; break; // 4
 case 3: kp = 49; break; // 1
 case 4: kp = 32; break; // Space
 case 5: kp = 56; break; // 8
 case 6: kp = 53; break; // 5
 case 7: kp = 50; break; // 2
 case 8: kp = 48; break; // 0
 case 9: kp = 57; break; // 9
 case 10: kp = 54; break; // 6
 case 11: kp= 51; break; // 3
 case 12: kp = 61; break; // =
 case 13: kp = 47; break; // /
 case 14: kp = 42; break; // x
 case 15: kp = 45; break; // -
 case 16: kp = 43; break; // +
 }
 if (kp != oldstate)
 { // Pressed key differs from previous
 cnt = 1;
 oldstate = kp;
 }
 else
 { // Pressed key is same as previous
 cnt++;
 }
 Lcd_Chr(1, 14, kp); // Print key ASCII value on Lcd
 value = kp;
 if (cnt == 255)
 { // If counter varialble overflow
 cnt = 0;
 Lcd_Out(2, 10, " ");
 }

 WordToStr(cnt, txt); // Transform counter value to string
 Lcd_Out(2, 10, txt); // Display counter value on Lcd
 } while (1);
 
 }
 jump:
 Lcd_out(1, 6, time);
 Lcd_out(2, 6, date);
 if(hr==value)
 {
 PORTC.F0=1;
 }
 else
 PORTC.F0=0;
 Delay_ms(100);


 }while(1);
}

28 thoughts on “Automatic electronic bell for school using pic microcontroller”

  1. The DS1307 has accuracy problems. It can drift between a few seconds up to a few minutes per day. It’s perfectly matched to a 32.768khz xtal with a load capacitance of 12.5pf. Even with the perfectly matched crystal and perfectly placed ground plane to eliminate change in capacitance you still see several seconds of drift per day on these bad boys. Go with the DS3231. It’s more expensive, but it has a built in in crystal driven PLL and it has automatic capacitance sensing to adjust automatically. They are very precise. They can also use the same libraries as the 1307 since the electrical interface is identical (aside from the need for an external xtal). So they might be more expensive, but they also reduce the BOM (no xtal needed).

    Reply
  2. I did everything as your tutorial but I dont know why the LCD cannot show any messages. Can you help me??? Thank you!

    Reply
  3. Sorry sir, can you help me the some technic ideas to successfull this project? Its jacob from Tanzania and second year KIITEC.

    Reply
  4. sir i am interested in this project . please provide me the code and detailed description of components used with their schematics .

    Reply
  5. Please sir send me the files including the hex code and the proteus similation, thanks. Email
    bamigbolajoy7@gmail.c

    Reply

Leave a Comment