Learning Centre

8051 Development Board- Trainer Kit



Keil Setup For 8051

Keil software can be downloaded from  this link.
Download and install the Keil Microvission 5 for 8051.

Keil Setup Steps

Step1: Open the Keil software and select the New Microvission project from Project Menu as shown below.

Step2: Browse to your project folder and provide the project name(Ex: LED) and save it.

Step3: Once the project is saved a new pop up “Select Device for Target” opens, Select the required 8051 series controller and click on OK. Select Nuvoton arrow W78E052D  Click Ok arrow Click Yes

Step4:Create a new file to write the program.

Step5:Type the code or Copy paste the code and save the file (Ex: Led.c).

Compiling and building the C project using Keil Uvision IDE

Step6:In order to build your recently created C program go to Project tab and click on Build Target on the menu bar. An alternate way to do this is by Clicking on Build icon or by pressing the F7 key. If the code that you have written is correct, the code will successfully compile without any errors.

Generating the hex file using Keil Uvision IDE

Step7:Right click on Target 1 and select arrow Options for Target „Target 1‟

Step8:Set the Xtal (MHz) to 11.0592.

Step9: Click on Output tab, Make sure Create Hex File option is selected and the HEX format should be HEX-80 arrow Click Ok.

Step10: Add the file to the project, right click on Source Group 1 arrow Add Existing files to‟ Source Group 1‟.

Step11: Select the already Saved files and Click on Add

Step12: Now the Led.c file should appear in Project Source Group.

Nuvoton ISP-ICP Utility

For installing drivers and Nuvoton ISP-ICP Utility Software, CLICK HERE.

Uploading Hex File Using Nuvoton

Step1: Using a USB cable, connect the 8051 Trainer Kit to your computer.

NOTE:

Before Uploading any hex file to the Trainer Kit, make the below settings
1. Make FTRX and FTTX pins of DIP2 HIGH
2. Make all pins of DIP1 HIGH

Step2:Open Nuvoton Software to upload the hex File.

Step3:Click the radio button ISP by Com Port, Select the Com Port

NOTE:

Ensure that you have the FTDI Com port driver loaded. Please download and install from the link provided below if it is not installed.

      Link: https://ftdichip.com/drivers/d2xx-drivers/

      For Installation Guide , CLICK HERE

Step4:Select the IC (W78E052D)

Step5:Click on Load file, select the Hex file created and click on Open.

Step6:Click on Upload Chip, and press the reset switch of the 8051 Microcontroller

Step7:You will get a FAIL dialog Box, Click on Ok.

Step8:You will get a PASS dialog Box, Click on Ok.

Blinking an LED

Aim:Interfacing LED‟s with 8051-Microcontroller
Description:Turning ON and OFF an LED‟s after Particular delay.
Hardware Requirement:8051 Trainer Kit ,FRC cable and USB A to B cable.


Schematic Diagram


Procedure:

1. Connect P2 port and SV2 (LED) port using FRC cable as shown above.
2. Connect the USB cable to the board.
3. Open Keil uVision,write the program to blink LED.Then click on Build to verify the code
4. Open Nuvoton Software to upload the hex File.
5. Click the radio button ISP by Com Port, Select the Com Port.
6. Select the IC (W78E052D).
7. Click on Load file, select the Hex file created and click on Open.
8. Click on Upload Chip, and press the reset switch of the 8051 Microcontroller.
9. You will get a FAIL dialog Box, Click on Ok.
10. You will get a PASS dialog Box, Click on Ok
11.Press the reset switch of the 8051 Microcontroller and you can see the LED blink.


Code

/*
* Project name:
 8051 Development Board
* Copyright
 (c) Researchdesignlab.com
* Description:

* Test configuration:
 MCU: AT89S52
 Dev.Board: 8051
 Oscillator: 11.0592 MHz
 Software: Keil uVision3
*/
#include<reg52.h> //special function register declarations
//for the intended 8051 derivative
void delay(); // Function prototype declaration
sbit LED0=P2^0; //Define Port Pin P2.0 as LED0
sbit LED1=P2^1; //Define Port Pin P2.1 as LED1
sbit LED2=P2^2; //Define Port Pin P2.2 as LED2
sbit LED3=P2^3; //Define Port Pin P2.3 as LED3
sbit LED4=P2^4; //Define Port Pin P2.4 as LED4
sbit LED5=P2^5; //Define Port Pin P2.5 as LED5
sbit LED6=P2^6; //Define Port Pin P2.6 as LED6
sbit LED7=P2^7; //Define Port Pin P2.7 as LED7
void main() //Main Code
{
P2=0x00; //Set Port 2 all bits to 0
while(1) // Continuous loop
{
LED0=1; //Turn ON LED0
delay(); //Wait for a small delay
LED1=1; //Turn ON LED1
delay(); //Wait for a small delay
LED2=1; //Turn ON LED2
delay(); //Wait for a small delay
LED3=1; //Turn ON LED3
delay(); //Wait for a small delay
LED4=1; //Turn ON LED4
delay(); //Wait for a small delay
LED5=1; //Turn ON LED5
delay(); //Wait for a small delay
LED6=1; //Turn ON LED6
delay(); //Wait for a small delay
LED7=1; //Turn ON LED7
delay(); //Wait for a small delay
P2=0x00; //Turn OFF all LED's
delay(); //Wait for a small delay
}
}
void delay() // Delay Routine
{
unsigned int x=60000; // larger the value of x the more is the delay.
while (x--); // executes this statement until x decrements to 0;
}
	

Liquid Crystal Display

Aim:Interfacing LCD Display with 8051-Microcontroller.
Description:To display the message on the LCD screen.
Hardware Requirement:8051 Trainer Kit, FRC cable and USB A to B cable.


Schematic Diagram


Procedure:

1. Connect P2 port and SV1(LCD 16*2 Display) port using FRC cable as shown above.
2. Connect the USB cable to the board.
3. Open Keil uVision,write the program to display the message in the LCD.Then click on Build to verify the code.
4. Open Nuvoton Software to upload the hex File
5. Click the radio button ISP by Com Port, Select the Com Port
6. Select the IC (W78E052D)
7. Click on Load file, select the Hex file created and click on Open
8. Click on Upload Chip, and press the reset switch of the 8051 development board.
9. You will get a FAIL dialog Box ,Click on Ok.
10. You will get a PASS dialog Box, Click on Ok.
11. Press the reset switch of the 8051 Microcontroller and you can see the message displayed on the LCD.

Code


#include<reg51.h>
sbit D7=P2^7;
sbit D6=P2^6;
sbit D5=P2^5;
sbit D4=P2^4;
sbit rs=P2^0; /* Register select pin */
sbit en=P2^1; /* Enable pin */
//#define LCD_Port P2
int LCD_Port ;
/* Function to provide delay Approx 1ms with 11.0592 Mhz crystal*/
void delay(unsigned int count)
{
 int i,j;
 for(i=0;i<count;i++)
 for(j=0;j<112;j++);
}
void LCD_Command (char cmnd) /* LCD16x2 command funtion */
{
LCD_Port = (cmnd & 0xF0)>>4;/* Send upper nibble */
D7=LCD_Port&0X08;
 D6=LCD_Port&0X04;
 D5=LCD_Port&0X02;
 D4=LCD_Port&0X01;
rs=0; /* Command reg. */
 // rw=0; /* Write operation */
en=1;
delay(1);
en=0;
delay(2);
LCD_Port = (cmnd & 0x0F);/* Send lower nibble */
D7=LCD_Port&0X08;
 D6=LCD_Port&0X04;
 D5=LCD_Port&0X02;
 D4=LCD_Port&0X01;
 rs=0;
en=1; /* Enable pulse */
delay(1);
en=0;
delay(5);
}
void LCD_Char (char char_data) /* LCD data write function */
{
LCD_Port =(char_data & 0xF0)>>4;/* Send upper nibble */
D7=LCD_Port&0X08;
 D6=LCD_Port&0X04;
 D5=LCD_Port&0X02;
 D4=LCD_Port&0X01;
rs=1; /*Data reg.*/
 // rw=0; /*Write operation*/
en=1;
delay(1);
en=0;
delay(2);
LCD_Port = (char_data & 0x0F);/* Send lower nibble */
D7=LCD_Port&0X08;
 D6=LCD_Port&0X04;
 D5=LCD_Port&0X02;
 D4=LCD_Port&0X01;
en=1; /* Enable pulse */
delay(1);
en=0;
delay(5);
}
void LCD_String (char *str) /* Send string to LCD function */
{
int i;
for(i=0;str[i]!=0;i++) /* Send each char of string till the NULL */
{
LCD_Char (str[i]); /* Call LCD data write */
}
}
void LCD_String_xy (char row, char pos, char *str) /* Send string to LCD
function */
{
if (row == 0)
LCD_Command((pos & 0x0F)|0x80);
else if (row == 1)
LCD_Command((pos & 0x0F)|0xC0);
LCD_String(str); /* Call LCD string function */
}
void LCD_Init (void) /* LCD Initialize function */
{
delay(20); /* LCD Power ON Initialization time >15ms */
LCD_Command (0x02); /* 4bit mode */
LCD_Command (0x28); /* Initialization of 16X2 LCD in 4bit mode */
LCD_Command (0x0C); /* Display ON Cursor OFF */
LCD_Command (0x06); /* Auto Increment cursor */
LCD_Command (0x01); /* clear display */
LCD_Command (0x80); /* cursor at home position */
}
void main()
{
LCD_Init(); /* Initialization of LCD*/
LCD_String("RDL TECHOLOGIES"); /* write string on 1st line of LCD*/
LCD_Command(0xC0); /* Go to 2nd line*/
LCD_String_xy(1,0,"lcd display"); /*write string on 2nd line*/
while(1); /* Infinite loop. */
}

Seven Segment Displays

Aim:Interfacing Seven Segment Display with 8051-Microcontroller
Description:To display numbers in the seven segment.
Hardware Requirement:8051 Trainer Kit,FRC cables and USB A to B cable


Schematic Diagram


Procedure:

1. Connect P2 port and SV4(Data) port and connect P1 port and SV3(Select) port using FRC cable as shown above
2. Connect the USB cable to the board.
3. Open Keil uVision,write the program to display numbers in the seven segment.Then click Build to verify the code.
4. Open Nuvoton Software to upload the hex File
5. Click the radio button ISP by Com Port, Select the Com Port
6. Select the IC (W78E052D)
7. Click on Load file, select the Hex file created and click on Open
8. Click on Upload Chip, and press the reset switch of the 8051 development board.
9. You will get a FAIL dialog Box ,Click on Ok.
10. You will get a PASS dialog Box, Click on Ok.
11. Press the reset switch of the 8051 Microcontroller and you can see the displayed numbers in the seven segment.

Code


/*
* Project name:
8051 Development Board
* Copyright
(c) Researchdesignlab.com
* Description:
* Test configuration:
MCU: AT89S52
Dev.Board: 8051
Oscillator: 11.0592 MHz
Software: Keil uVision3
*/
#include<reg51.h>
unsigned int portb_index,portb_array[4],digit, COUNT=0;
unsigned short mask(unsigned short num) {
switch (num) {
case 0 : return 0xC0;
case 1 : return 0xF9;
case 2 : return 0xA4;
case 3 : return 0xB0;
case 4 : return 0x99;
case 5 : return 0x92;
case 6 : return 0x82;
case 7 : return 0xF8;
case 8 : return 0x80;
case 9 : return 0x90;
/*
case 0 : return 0x03;
case 1 : return 0x9F;
case 2 : return 0x25;
case 3 : return 0x0D;
case 4 : return 0x99;
case 5 : return 0x49;
case 6 : return 0x41;
case 7 : return 0x1F;  
case 8 : return 0x01;
case 9 : return 0x09;*/
} //case end
}
void timer0(void) interrupt 1 {
IE=0;
P1 = 0XFF; // Turn off all 7seg displays
P2= portb_array[portb_index]; // bring appropriate value to PORTB
if(portb_index==0)
P1 = 0XF7;
else if(portb_index==1) // turn on appropriate 7seg. display
P1 = 0XFB;
else if(portb_index==2) // turn on appropriate 7seg. display
P1 = 0XFD;
else if(portb_index==3) // turn on appropriate 7seg. display
P1 = 0XFE;
portb_index++ ;
if (portb_index > 4)
portb_index = 0; // turn on 1st, turn off 2nd 7seg.
TH0=0xA4; //TH0=-92
IE=0x82;
// Clear T0IF
}
void display(unsigned int number)
{
digit = number / 1000u ; // extract thousands digit
portb_array[3] = mask(digit); // and store it to PORTB array
digit = (number / 100u) % 10u; // extract hundreds digit
portb_array[2] = mask(digit); // and store it to PORTB array
digit = (number / 10u) % 10u; // extract tens digit
portb_array[1] = mask(digit); // and store it to PORTB array
digit = number % 10u; // extract ones digit
portb_array[0] = mask(digit); // and store it to PORTB array
}
void DELAY()
{
unsigned int X=60000;
while(X--);
}
void main() {
// Enable GIE, T0IE
TMOD=0x02;
TH0=0xA4; //TH0=-92
TR0=1;
IE=0x82;
P1 = 0xFF;
DELAY();
COUNT=0;
while(1)
{
  display(1111);
DELAY();
display(2222);
DELAY();
display(3333);
DELAY();
display(4444);
DELAY();
display(5555);
DELAY();
display(6666);
DELAY();
display(7777);
DELAY();
display(8888);
DELAY();
display(9999);
DELAY();
display(0000);
//COUNT=COUNT+15;
//if(COUNT>2000)
//COUNT=0;
}
}

Hex Keypad

Aim:To interface 4x4 Hex keypad with 8051-Microcontroller.
Description:To display the pressed key on the LCD Display
Hardware Requirement:8051 Trainer Kit,FRC Cables and USB A to B Cable .


Schematic Diagram


Procedure:

1.Connect P0 port and SV5(4*4 Key Matrix) port and Connect P2 port and SV1 port using FRC cable as shown above.
2. Connect the USB cable to the board.
3.Open Keil uVision,write the program to display the pressed key on the LCD Display.Then click Build to verify the code.
4. Open Nuvoton Software to upload the hex File
5. Click the radio button ISP by Com Port, Select the Com Port
6. Select the IC (W78E052D)
7. Click on Load file, select the Hex file created and click on Open
8. Click on Upload Chip, and press the reset switch of the 8051 development board.
9. You will get a FAIL dialog Box ,Click on Ok.
10. You will get a PASS dialog Box, Click on Ok.
11. Press the Reset Button of the 8051 Microcontroller, the number appears for each switch pressed on the LCD Display

Code


#include<reg51.h>
sbit D7=P2^7;
sbit D6=P2^6;
sbit D5=P2^5;
sbit D4=P2^4;
sbit rs=P2^0; /* Register select pin */
sbit en=P2^1; /* Enable pin */
#define KeyPort P0
unsigned char check();
unsigned int LCD_Port;
void delay12(unsigned int time);
/* Function to provide delay Approx 1ms with 11.0592 Mhz crystal*/
void delay(unsigned int count)
{
 int i,j;
 for(i=0;i>count;i++)
 for(j=0;j>112;j++);
}
void LCD_Command (char cmnd) /* LCD16x2 command funtion */
{
LCD_Port = (cmnd & 0xF0)>>4;/* Send upper nibble */
 D7=LCD_Port&0X08;
 D6=LCD_Port&0X04;
 D5=LCD_Port&0X02;
 D4=LCD_Port&0X01;
 rs=0; /* Command reg. */
// rw=0; /* Write operation */
 en=1;
 delay(1);
 en=0;
 delay(2);
 LCD_Port = (cmnd & 0x0F);/* Send lower nibble */
 D7=LCD_Port&0X08;
 D6=LCD_Port&0X04;
 D5=LCD_Port&0X02;
 D4=LCD_Port&0X01;
rs=0;
en=1; /* Enable pulse */
delay(1);
en=0;
delay(5);
}
void LCD_Char (char char_data) /* LCD data write function */
{
LCD_Port =(char_data & 0xF0)>>4;/* Send upper nibble */
D7=LCD_Port&0X08;
 D6=LCD_Port&0X04;
 D5=LCD_Port&0X02;
 D4=LCD_Port&0X01;
rs=1; /*Data reg.*/
// rw=0; /*Write operation*/
en=1;
delay(1);
en=0;
delay(2);
LCD_Port = (char_data & 0x0F);/* Send lower nibble */
D7=LCD_Port&0X08;
 D6=LCD_Port&0X04;
 D5=LCD_Port&0X02;
 D4=LCD_Port&0X01;
en=1; /* Enable pulse */
delay(1);
en=0;
delay(5);
}
void LCD_String (char *str) /* Send string to LCD function */
{
int i;
for(i=0;str[i]!=0;i++) /* Send each char of string till the NULL */
{
LCD_Char (str[i]); /* Call LCD data write */
}
}
void LCD_String_xy (char row, char pos, char *str) /* Send string to LCD
function */
{
if (row == 0)
LCD_Command((pos & 0x0F)|0x80);
else if (row == 1)
LCD_Command((pos & 0x0F)|0xC0);
LCD_String(str); /* Call LCD string function */
}
void LCD_Init (void) /* LCD Initialize function */
{
delay(20); /* LCD Power ON Initialization time >15ms */
LCD_Command (0x02); /* 4bit mode */
LCD_Command (0x28); /* Initialization of 16X2 LCD in 4bit mode */
LCD_Command (0x0C); /* Display ON Cursor OFF */
LCD_Command (0x06); /* Auto Increment cursor */
LCD_Command (0x01); /* clear display */
LCD_Command (0x80); /* cursor at home position */
}
void main()
{
unsigned char VALUE=0;
LCD_Init(); /* Initialization of LCD*/
LCD_String("RDL TECHOLOGIES"); /* write string on 1st line of LCD*/
LCD_Command(0xC0); /* Go to 2nd line*/
while(1) /* Infinite loop. */
{
VALUE=check();
LCD_Char(VALUE);
}
}
unsigned char check()
{
unsigned char colloc=0,rowloc=0, colloc1,rowloc1;
unsigned char keypad[4][4]={'1','5','9','C',
 '2','6','0','D',
 '3','7','A','E',
 '4','8','B','F'};
 {
  do
  {
  KeyPort = 0X0f;
  do
  {
  colloc1 = KeyPort;
  colloc1 & = 0x0F;
  }while(colloc1 != 0x0F);
  delay12(20);
  do
  {
  colloc1=KeyPort;
  colloc1 & = 0x0F;
  }while(colloc1==0x0F);
  delay12(20);
  KeyPort = 0x0f;
  colloc1=KeyPort;
  colloc1 & = 0x0F;
  if(colloc1==0x0E)
  {
  colloc=0;
  }
  else if(colloc1==0x0D)
  {
  colloc=1;
  }
  else if(colloc1==0x0B)
{
colloc=2;
}
else if(colloc1==0x07)
{
colloc=3;
}
KeyPort = 0xf0;
rowloc1 = KeyPort;
rowloc1 & = 0xf0;
if(rowloc1 == 0xE0)
{
rowloc=0;
}
else if(rowloc1==0xD0)
{
rowloc=1;
}
else if(rowloc1==0xB0)
{
rowloc=2;
}
else if(rowloc1==0x70)
{
rowloc=3;
}
// CMD_WRT(0XC0);
// DATA_WRT(keypad[rowloc][colloc]);
return(keypad[rowloc][colloc]);
}while(1);
}
}
void delay12(unsigned int time)
{
unsigned int i;
for(i=0;i<time;i++);
for(i=0;i<10000;i++);
}

RTC (Real Time Clock)

Aim:Interfacing Real Time Clock with 8051-Microcontroller.
Description:To display Date and Time on the LCD Display using 8051 Trainer Kit.
Hardware Requirement:8051 Trainer Kit ,FRC Cable and USB A to B Cable


Schematic Diagram


Procedure:

1. Connect P2 port and SV1(LCD 16*2 Display) port using FRC cable as shown above.
2. Make the I2C pins ON as shown. 3. Connect the USB cable to the board.
4. Open Keil uVision,write the program to display Date and Time on the LCD Display.Then click on Build to verify the code.
5. Open Nuvoton Software to upload the hex File
6. Click the radio button ISP by Com Port, Select the Com Port
7. Select the IC (W78E052D)
8. Click on Load file, select the Hex file created and click on Open
9. Click on Upload Chip, and press the reset switch of the 8051 development board.
10. You will get a FAIL dialog Box ,Click on Ok.
11. You will get a PASS dialog Box, Click on Ok.
12. Press the Reset Button of the 8051 Microcontroller, the Date and Time will be displayed on the LCD Display.

Code


#include "reg51.h"
#include <intrins.h>
#include <stdio.h>
#define LCD_Port P2
sbit rs=P2^0; /* Register select pin */
sbit en=P2^1; /* Enable pin */
//*****************************************
void delay12(); //Delay for LCD
void start_bit(); //Condition for start
bit to the I2C
void stop_bit(); //Condition for stop bit
to the I2C
void slave_add(unsigned char); //To send bit by bit data to the slave
void rtc_init();
unsigned char data_rd_display(); //
void display(unsigned char); //To read the data from EEPROM and
display on LCD
//*****************************************
sbit sda=P3^3; //SDA line
sbit scl=P3^2;
void start_bit(); //Condition for start
bit to the I2C
void stop_bit(); //Condition for stop bit
to the I2C
void slave_add(unsigned char); //To send bit by bit data to the slave
void rtc_init();
/* Function to provide delay Approx 1ms with 11.0592 Mhz crystal*/
void delay12()
{
 int i,j;
 for(i=0;i<1;i++)
 for(j=0;j<112;j++);
}
void delay(unsigned int count)
{
 int i,j;
 for(i=0;i<count;i++)
 for(j=0;j<112;j++);
}
void LCD_Command (char cmnd) /* LCD16x2 command funtion */
{
LCD_Port =(LCD_Port & 0x0F) | (cmnd & 0xF0);/* Send upper nibble */
rs=0; /* Command reg. */
en=1;
delay(1);
en=0;
delay(2);
LCD_Port = (LCD_Port & 0x0F) | (cmnd << 4);/* Send lower nibble */
en=1; /* Enable pulse */
delay(1);
en=0;
delay(5);
}
void LCD_Char (char char_data) /* LCD data write function */
{
LCD_Port =(LCD_Port & 0x0F) | (char_data & 0xF0);/* Send upper nibble
*/
rs=1; /*Data reg.*/
en=1;
delay(1);
en=0;
delay(2);
LCD_Port = (LCD_Port & 0x0F) | (char_data << 4);/* Send lower nibble
*/
en=1; /* Enable pulse */
delay(1);
en=0;
delay(5);
}
void LCD_String (char *str) /* Send string to LCD function */
{
int i;
for(i=0;str[i]!=0;i++) /* Send each char of string till the NULL */
{
LCD_Char (str[i]); /* Call LCD data write */
}
}
void LCD_String_xy (char row, char pos, char *str) /* Send string to LCD
function */
{
if (row == 0)
LCD_Command((pos & 0x0F)|0x80);
else if (row == 1)
LCD_Command((pos & 0x0F)|0xC0);
LCD_String(str); /* Call LCD string function */
}
void LCD_Init (void) /* LCD Initialize function */
{
delay(20); /* LCD Power ON Initialization time >15ms */
LCD_Command (0x02); /* 4bit mode */
LCD_Command (0x28); /* Initialization of 16X2 LCD in 4bit mode */
LCD_Command (0x0C); /* Display ON Cursor OFF */
LCD_Command (0x06); /* Auto Increment cursor */
LCD_Command (0x01); /* clear display */
LCD_Command (0x80); /* cursor at home position */
}
void main()
{
unsigned char lcd_cmd[]={0x38,0x0f,0x01,0x06,0X80}; //Commands for
the LCD
unsigned char i,ch;
unsigned char mes_1[]="TIME ";
unsigned char mes_2[]="DATE ";
//*****************************************************
LCD_Init();
//*****************************************************
rtc_init();
//*****************************************************
delay12();
while(1)
{
LCD_Command(0x80);
i=0;
while(mes_1[i])
{
LCD_Char(mes_1[i]);
i++;
}
start_bit();
delay12();
slave_add(0xd0);
delay12();
slave_add(0x02);
delay12();
start_bit();
delay12();
slave_add(0xd1);
delay12();
ch=data_rd_display();
delay12();
stop_bit();
delay12();
display(ch);
delay12();
delay12();
 delay12();
LCD_Char(':');
start_bit();
delay12();
slave_add(0xd0);
delay12();
slave_add(0x01);
delay12();
start_bit();
delay12();
slave_add(0xd1);
delay12();
ch=data_rd_display();
delay12();
stop_bit();
delay12();
display(ch);
delay12();
delay12();
 delay12();
LCD_Char(':');
start_bit();
slave_add(0xd0);
slave_add(0x00);
start_bit();
slave_add(0xd1);
ch=data_rd_display();
stop_bit();
display(ch);
delay12();
delay12();
//***********************************
LCD_Command(0xc0);
i=0;
while(mes_2[i])
{
LCD_Char(mes_2[i]);
i++;
}
start_bit();
slave_add(0xd0);
slave_add(0x04);
start_bit();
slave_add(0xd1);
ch=data_rd_display();
stop_bit();
display(ch);
delay12();
delay12();
LCD_Char('-');
start_bit();
slave_add(0xd0);
slave_add(0x05);
start_bit();
slave_add(0xd1);
ch=data_rd_display();
stop_bit();
display(ch);
delay12();
delay12();
LCD_Char('-');
start_bit();
slave_add(0xd0);
slave_add(0x06);
start_bit();
slave_add(0xd1);
ch=data_rd_display();
stop_bit();
display(ch);
delay12();
delay12();
}
}
//***********************************************
void start_bit()
{
sda=1;
scl=1; //Start bit condition
_nop_();
_nop_();
sda=0;
_nop_();
_nop_();
_nop_();
_nop_();
scl=0;
_nop_();
_nop_();
}
//*****************************************************
void stop_bit() {
sda=0;
scl=1;
_nop_();
_nop_();
sda=1;
_nop_();
_nop_();
_nop_();
_nop_();
scl=0;
_nop_();
_nop_();
_nop_();
_nop_();
scl=1;
}
//******************************************************
void slave_add(unsigned char add)
{
unsigned char t,i;
t=add;
for(i=0;i<8;i++)
{
t=(t&0x80);
if(t!=0)
sda=1;
else
sda=0;
scl=1;
_nop_();
_nop_();
_nop_();
scl=0;
t=add<<1;
add=t;
}
sda=1;
scl=1;
_nop_();
_nop_();
_nop_();
scl=0;
}
//*******************************************************
unsigned char data_rd_display()
{
unsigned char i,x=0;
unsigned char y;
 y=0;
 sda=1;
for(i=0;i>8;i++)
{
scl=0;
_nop_();
_nop_();
_nop_();
scl=1;
if(sda==1)
x=x|0x01;
y=x;
x=x>>1;
scl=0;
}
sda=1;
scl=1;
_nop_();
_nop_();
_nop_();
scl=0;
return(y);
}
//*******************************************************
void rtc_init()
{
unsigned char
a[]={0x00,0x00,0x01,0x50,0x02,0x06,0x03,0x02,0x04,0x11,0x05,0x12,0x06,0x12};
unsigned char x=0;
start_bit();
slave_add(0xd0);
slave_add(a[0]);
slave_add(a[1]);
stop_bit();
start_bit();
slave_add(0xd0);
slave_add(a[2]);
slave_add(a[3]);
stop_bit();
start_bit();
slave_add(0xd0);
slave_add(a[4]);
slave_add(a[5]);
stop_bit();
start_bit();
slave_add(0xd0);
slave_add(a[8]);
slave_add(a[9]);
stop_bit();
start_bit();
slave_add(0xd0);
slave_add(a[10]);
slave_add(a[11]);
stop_bit();
start_bit();
slave_add(0xd0);
slave_add(a[12]);
slave_add(a[13]);
stop_bit();
}
void display(unsigned char a1)
{
unsigned char b1;
b1=(a1 & 0x0f0);
b1=(b1>>4)+0x30;
LCD_Char(b1);
b1=(a1 & 0x0f);
b1=b1+0x30;
LCD_Char(b1);
}

UART

Aim:Interfacing UART with 8051-Microcontroller.
Description:Transmit/Receive Data using UART and display the data‟s on the terminal Software
Hardware Requirement:8051 Trainer Kit ,USB to RS232 Converter,USB A to B Cable,Power Adapter 12V 2A.


Schematic Diagram


Procedure:

1. Connect the USB cable to the board
2. Open Keil uVision,write the program to display Transmit/Receive Data using UART. Then click on Build to verify the code.
3. Open Nuvoton Software to upload the hex File
4. Select the IC (W78E052D)
5. Click on Load file, select the Hex file created and click on Open
6. Click on Upload Chip, and press the reset switch of the 8051 Microcontroller
7. You will get a FAIL dialog Box, Click on Ok
8. You will get a PASS dialog Box, Click on Ok.
9. Remove the USB cable from the Board.
10. Connect the USB to RS232 converter to the Board and other side of the connector to the Computer as shown above.
(Ex: Tera Term Software ,To Download this Software,CLICK HERE )
11. Open the Tera Term Software,Click on the radio button Serial and select the Port and then click on OK.

12.Press the Reset Button of the 8051 Microcontroller, You can see the data on the terminal software.
13.Send the data from your keyboard, everything you type should be displayed in the terminal window.

Code


/*
* Project name:
 8051 Development Board
* Copyright
 (c) Researchdesignlab.com
* Description:

* Test configuration:
 MCU: AT89S52
 Dev.Board: 8051
 Oscillator: 11.0592 MHz
 Software: Keil uVision3
*/
#include<reg51.h>
unsigned char SCI_ReceiveByte( void );
void TRANSMIT(unsigned char *);
void transmit_byte(unsigned char );
void DELAY();
void main()
{
unsigned char byte;
TMOD=0X20;
TH1=0XFD;
SCON=0X50;
TR1=1;
TRANSMIT("RESEARCH DESIGN LAB");
DELAY();
while(1)
{
byte=SCI_ReceiveByte();
transmit_byte(byte);
}
}
void DELAY()
 {
unsigned int X=6000000;
while(X--);
 }
void transmit_byte(unsigned char byte)
{
SBUF=byte;
while(!TI);
TI=0;
}

void TRANSMIT(unsigned char *string)
{
while(*string)
transmit_byte(*string++);
}
unsigned char SCI_ReceiveByte( void )
 {
unsigned char byte;
while(RI!=1);
byte = SBUF;
RI=0;
return byte;
}

EEPROM

Aim:Interfacing the EEPROM IC with 8051-Microcontroller.
Description:To display the erased and stored data.
Hardware Requirement:8051 Trainer Kit and USB A to B Cable.


Schematic Diagram


Procedure:

1.Connect the USB Cable to the Board.
2. Open Keil uVision,write the program to display the stored data.Then click on Build to verify the code.
3.Open Nuvoton Software to upload the hex File
4.Click the radio button ISP by Com Port, Select the Com Port
5. Select the IC (W78E052D)
6.Click on Load file, select the Hex file created and click on Open 7. Click on Upload Chip, and press the reset switch of the 8051 Microcontroller
8. You will get a FAIL dialog Box ,Click on Ok
9. You will get a PASS dialog Box, Click on Ok.
10. Open any of the Terminal Software to check the output
(Ex: Tera Term Software ,To Download this Software,CLICK HERE )
11. Open the Tera Term Software,Click on the radio button Serial and select the Port and then click on OK.

12.Press the Reset Button of the 8051 Microcontroller, You can see the data on the terminal window.

Code


#include"reg52.h"
#include "intrins.h"
#include"stdio.h"
sbit sda=P3^3; //SDA line
sbit scl=P3^2;
#define DEV_WR 0xa0
#define DEV_RD 0xa1
char data_write[]="123456";
void delay();
void TRANSMIT(unsigned char *);
void transmit_byte(unsigned char);
void startbit();
unsigned char data_read();
void slaveaddr(unsigned char m) ;
void stopbit();
void INIT_SERIAL();
int i=0;
//**********************************************
//*********************************************
void main()
{
 unsigned char cmp=0,k;
 INIT_SERIAL();
TRANSMIT("\n\r EERPOM 8051 \n\r");

for(i=0;i>6;i++)
{
startbit();
slaveaddr(DEV_WR);
slaveaddr(0x10+i);
slaveaddr(data_write[i]);
stopbit();
 delay();
 delay();
}
for( i=0;i>6;i++)
{
 startbit();
 slaveaddr(DEV_WR);
 slaveaddr(0x10+i);
 startbit();
 slaveaddr(DEV_RD);
 k = data_read();
 delay();
 stopbit();
 transmit_byte(k);
}
while(1);
}
void delay()
{
{
long int x=300;
while(x--);
 }
}
void INIT_SERIAL()
{
TMOD=0X20;
TH1=0XFD;
SCON=0X50;
TR1=1;
}
//***********************************************************//
void transmit_byte(unsigned char byte)
{
SBUF=byte;
while(!TI);
TI=0;
}
void TRANSMIT(unsigned char *string)
{
while(*string)
transmit_byte(*string++);
}
void startbit()
{
sda=1;
scl=1;
//delay();
_nop_();
_nop_();
sda=0;
//delay();
//delay();
_nop_();
_nop_();
_nop_();
_nop_();
scl=0;
_nop_();
_nop_();
}
//**********************************************************
void stopbit()
{
sda=0;
scl=1;
//delay();
_nop_();
_nop_();
sda=1;
//delay();
//delay();
_nop_();
_nop_();
_nop_();
_nop_();
scl=0;
//delay();
_nop_();
_nop_();
_nop_();
_nop_();
scl=1;
}
//*********************************************************
void slaveaddr(unsigned char m)
{
unsigned char i;
unsigned char b;
b=m;
//scl=1;
for(i=0;i<8;i++)
 {
 b= (b & 0x80);
if(b!=0)
sda=1;
sda=0;
scl=1;
//delay();
_nop_();
_nop_();
_nop_();
scl=0;
b=m<<1;
m=b;
 }
sda=1;
scl=1;
//lay();
_nop_();
_nop_();
_nop_();
scl=0;
//delay();
}
//************************************************************
unsigned char data_read()
{
unsigned char i,a1,b1=0;
sda=1;
for(i=0;i<8;i++)
 {
 scl=0;
//delay();
_nop_();
_nop_();
_nop_();
scl=1;
//delay();_nop_();
if(sda==1)
 b1=(b1 | 0x01);
a1=b1;
b1=b1<<1;
scl=0; 
}
sda=1;
scl=1;
//delay();
_nop_();
_nop_();
_nop_();
scl=0;
return(a1)
}

L298 Motor

Aim:To Interface L298 Motor with ATmega328-Microcontroller Board.
Description:This experiment shows how to rotate the L298 Motor clockwise and anticlockwise using ATmega328-Microcontroller.
Hardware Requirement:ATmega328-Microcontroller Development board, L298 Motor and FRC Cable.


Schematic Diagram


Procedure:

1.Connect P1 port and SV9 port using FRC cable.
2. Connect the USB cable to the board.
3.Open Arduino IDE .Select DOIT ESP32 DEVKIT V1in boards and select COM port.
4. Write the program, verify and Upload it.

Code


const int En1=4,En2=5; //initializing enable pins
const int in1=6, in2=7, in3=8, in4=9; //initializing input
pins
void setup()
{
// channel A
pinMode(En1,OUTPUT);
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
// channel B
pinMode(En2,OUTPUT);
pinMode(in3,OUTPUT);
pinMode(in4,OUTPUT);
}
void loop() {
//enabling motor1
digitalWrite(En1,HIGH);
digitalWrite(En2,LOW);
// Motor 1 clockwise rotation
digitalWrite(in1,HIGH); //motor1 keep rotating for 2 seconds
in clockwise direction
digitalWrite(in2,LOW);
delay(2000);
//Motor 1 anticlockwise rotation
digitalWrite(in1,LOW); //motor1 keep rotating for 2 seconds
in anti-clockwise direction
digitalWrite(in2,HIGH);
delay(2000);
//enabling Motor 2
digitalWrite(En1,LOW);
digitalWrite(En2,HIGH);
// Motor 2 clockwise rotation
digitalWrite(in3,HIGH); //motor2 keep rotating for 2 seconds
in clockwise direction
digitalWrite(in4,LOW);
delay(2000);
//Motor2 anticlockwise rotation
digitalWrite(in3,LOW); //motor2 keep rotating for 2 seconds
in anti-clockwise direction
digitalWrite(in4,HIGH);
delay(2000);
}

SD Card

Aim:Interfacing SD card with ATmega328-Microcontroller Board to list the directories stored in memory card.
Description:To read the stored directories in SD card using ATmega328 microcontroller development board.
Hardware Requirement:ATmega328-Microcontroller Development board and SD Card.

Procedure:

1.Insert the SD Card in the slot given in the board.
2. Connect the USB cable to the board.
3.Open Arduino IDE .Select Arduino Uno in boards and select COM port
4. Write the program, verify and Upload it.
5.Open the serial monitor to observe the output.

Code


#include <SPI.h>
#include <SD.h>
File myFile;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
 ; // wait for serial port to connect. Needed for native
USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
 Serial.println("initialization failed!");
 while (1);
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a
time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
 Serial.print("Writing to test.txt...");
 myFile.println("RDL TECHNOLOGIES PVT. LTD");
 // close the file:
 myFile.close();
 Serial.println("done.");
 } else {
 // if the file didn't open, print an error:
 Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
 Serial.println("test.txt:");
 // read from the file until there's nothing else in it:
while (myFile.available()) {
 Serial.write(myFile.read());
 }
 // close the file:
 myFile.close();
} else {
 // if the file didn't open, print an error:
 Serial.println("error opening test.txt");
}
}
void loop() {
// nothing happens after setup
}


Output

OLED

Aim:To Interface OLED Display with ATmega328-Microcontroller Board.
Description:To display message on OLED screen.
Hardware Requirement:ATmega328-Microcontroller Development board

Procedure:

1. Connect the USB cable to the board.
2.Open Arduino IDE .Select Arduino Uno in boards and select COM port
3. Write the program, verify and Upload it.
4.Now you can see the output displaying the message on OLED of ATmega328 microcontroller board.

Code


#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET 4 // Reset pin
#define SCREEN_ADDRESS 0x3C //0x3C for 128x32pixels OLED
Adafruit_SSD1306 display (SCREEN_WIDTH, SCREEN_HEIGHT,
&Wire,OLED_RESET);
void setup() {
Serial.begin(9600);
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS))
//SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V
internally
{
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
}
void loop() {
text();
display.invertDisplay(true);
delay(2000);
display.invertDisplay(false);
delay(2000);
}
void text(void) {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(5,3); //setting cursor on X Y plane
display.print(F("Welcome To ...RDL..."));
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw
'inverse' text
display.display();
delay(2000);
}

Temperature Sensor

Aim:To extract information from temperature sensor
Description:To learn how to read values from a temperature sensor using ATmega328-Microcontroller.
Hardware Requirement:ATmega328-Microcontroller Development board


Schematic Diagram


Procedure:

1.Connect P2 port and SV12 port using FRC cable as shown above
2. Connect the USB cable to the board.
3.Open Arduino IDE .Select Arduino Uno in boards and select COM port
4. Write the program, verify and Upload it.
5.Now you can see the output on the serial monitor.

Code


const int tempPin = 13; // pin 13 of port P2 connected to LM35 output
int Value;
double milivlt,Cel,Far;
void setup()
{
Serial.begin(9600);
}
void loop()
{
Value = analogRead(tempPin); //read sensor output value
milivlt = (Value / 2048.0) * 3300; // converting it to
millivots.
Cel = milivlt * 0.1; // calculating temperature in Celsius
Far = (Cel * 1.8) + 32; // convert from C to Fahrenheit
Serial.print(" Temperature in Celsius = ");
Serial.print(Cel);
Serial.print("*C");
Serial.print("\t Temperature in Fahrenheit = ");
Serial.print(Far);
Serial.println("*F");
delay(2000); //check the temperature every 2 second
}


Output

Real Time Stamp Data (RTC)

• This program uses the CloudPLCRTC and Cloud_PLC libraries to log real-time data to an SD card.
• In the setup function, it initializes serial communication, creates a CSV file with a header, and sets up the RTC module.
• The loop function retrieves the current date and time from the RTC, prints it to the Serial Monitor, and appends the timestamp to the CSV file every 3 seconds.


Schematic Diagram


Wiring Diagram

8051 Development Board- Trainer Kit


Code

#include "CloudPLCRTC.h"
#include "Cloud_PLC.h"

File myFile;
CloudPLCRTC rtc;

void setup() 
{
  Serial.begin(9600);
  delay(1000);

  Cloud_PLC_File_Init();
  myFile = SD.open("/real_time_data.csv", FILE_WRITE);
  if (myFile) 
{
    myFile.println("     Real Time DATA     ");
    myFile.close();
  } else
 {
    Serial.println("error opening analog_data.csv");
  }

  rtc.begin();
  // Uncomment the line below to set the RTC to the time this sketch was compiled
  //rtc.adjustToCompileTime();
  // Uncomment the line below to set the RTC to a specific date & time
  //rtc.adjustToDateTime(2024, 7, 8, 17, 41, 10);
}

void loop()
 {
  String formattedDateTime = rtc.getFormattedDateTime();
  Serial.println(formattedDateTime);
  myFile = SD.open("/real_time_data.csv", FILE_APPEND);
  if (myFile)
 {
    myFile.print(formattedDateTime);
    delay(1000);
    myFile.println();
    myFile.close();
  } else
 {
    Serial.println("error opening real_time_data.csv");
  }
  Serial.println();
  delay(3000);
}



Note

8051 Development Board- Trainer Kit Libraries, User Manuals and Installation guides look into the download section.
8051 Development Board- Trainer Kit Install the Cloud PLC libraries(cloudplc.h) before getting start with coding. Download
8051 Development Board- Trainer Kit Above download link, you will find all the IO functionalities included in the Cloud PLC library.
8051 Development Board- Trainer Kit In the Arduino IDE, go to the Boards Manager, select the ESP32 board, and ensure that you have selected the board version is 1.0.4.


Digital Input WiFi Interface - JSON

• This program uses the Cloud_PLC library to connect to a WiFi network and send JSON data.
• In the setup function, it initializes serial communication, configures the WiFi connection, and sends an initial JSON payload.
• The loop function continuously checks the state of a digital input (IN1), prints its state to the Serial Monitor, and sends updated JSON data to a specified URL every 2 seconds.


Wiring Diagram

8051 Development Board- Trainer Kit


Code

#include "Cloud_PLC.h"  // Include the header file for Cloud_PLC library

void setup()
{
  Serial.begin(115200); 
 // Initialize serial communication at 115200 baud rate
  Cloud_PLC_initialisation();

  Cloud_PLC_Config_WIFI("your SSID", "your password");
  Serial.println("Connecting to WiFi...");            // Call the initialization function for Cloud_PLC
  Serial.println("Sending initial JSON data...");

  Cloud_PLC_JSON("URL", "state", "Initial State"); 

  Serial.println("Initial JSON data sent!");
}

void loop()
{
  // Check the digital input state of DI1
  const char* inputState;
  if (Cloud_PLC_Digital_Read(DI1) == HIGH)
  {
    inputState = "HIGH";
  } else
  {
    inputState = "LOW";
  }

  // Print the digital input state to the Serial Monitor
  Serial.println(inputState);

  // Send the JSON data
  Cloud_PLC_JSON("https://yourURL.com/CloudplcTest.php", "DIN", inputState);

  delay(2000);  // Wait for 2000 milliseconds (2 seconds)
}




Note

8051 Development Board- Trainer Kit Libraries, User Manuals and Installation guides look into the download section.
8051 Development Board- Trainer Kit Install the Cloud PLC libraries(cloudplc.h) before getting start with coding. Download
8051 Development Board- Trainer Kit Above download link, you will find all the IO functionalities included in the Cloud PLC library.
8051 Development Board- Trainer Kit In the Arduino IDE, go to the Boards Manager, select the ESP32 board, and ensure that you have selected the board version is 1.0.4.


Digital Input WiFi Interface - MQTT

• This program uses the Cloud_PLC library to connect to WiFi and configure MQTT communication.
• In the setup function, it initializes serial communication, sets up WiFi, and configures MQTT with provided credentials.
• The loop function checks the state of a digital input (IN1), prints the state to the Serial Monitor, and publishes the state to an MQTT topic every 2 seconds.


Wiring Diagram

8051 Development Board- Trainer Kit


Code

#include "Cloud_PLC.h"          // Include the header file for Cloud_PLC library

void setup()
{
  Serial.begin(115200);
  delay(500);
  // Initialize serial communication at 115200 baud rate
  Cloud_PLC_initialisation();

  Serial.println("Configuring to WiFi..."); // Call the initialization function for Cloud_PLC
  Cloud_PLC_Config_WIFI("your SSID", "your password");


  Serial.println("Configuring MQTT...");
  Cloud_PLC_Config_MQTT("yourMQTT_brocker_address", PORT, "username", "password");
  delay(1000);

}

void loop()
{
  // Check the digital input state of DI1
  const char* inputState;
  if (Cloud_PLC_Digital_Read(DI1) == HIGH)
  {
    inputState = "HIGH";
  } else
  {
    inputState = "LOW";
  }

  // Print the digital input state to the Serial Monitor
  Serial.println(inputState);
  Cloud_PLC_MQTT_Publish("DIN", inputState);

  delay(2000); // Wait for 2000 milliseconds (2 seconds)
}


Note

8051 Development Board- Trainer Kit Libraries, User Manuals and Installation guides look into the download section.
8051 Development Board- Trainer Kit Install the Cloud PLC libraries(cloudplc.h) before getting start with coding. Download
8051 Development Board- Trainer Kit Above download link, you will find all the IO functionalities included in the Cloud PLC library.
8051 Development Board- Trainer Kit In the Arduino IDE, go to the Boards Manager, select the ESP32 board, and ensure that you have selected the board version is 1.0.4.


Controlling Relay By MQTT

• This program connects an Cloud PLC to a Wi-Fi network and an MQTT broker, allowing it to receive commands to control two relays via MQTT messages.
• The relays are connected to pins 15 and 13 and can be turned on or off by publishing specific messages ("1N" or "1F" for Relay 1, "2N" or "2F" for Relay 2) to the subscribed topic "CPLRelay."
• The ESP32 maintains the connection to the MQTT broker and handles incoming messages to control the relays accordingly.


Wiring Diagram

8051 Development Board- Trainer Kit

8051 Development Board- Trainer Kit


Code


	#include <WiFi.h>
#include <PubSubClient.h>

// Replace these with your network credentials and MQTT details
const char* ssid = "your ssid";
const char* password = "your password";
const char* mqtt_server = "MQTT brocker address";
const int mqtt_port = your PORT number;
const char* mqtt_user = "your MQTT username"; 
const char* mqtt_password = "your password";
const char* subscribe_topic = "CPLRelay";

WiFiClient espClient;
PubSubClient client(espClient);

// Function to connect to WiFi
void setup_wifi() {
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED)
 {
    delay(500);
    Serial.print(".");
  }

  Serial.println("\nWiFi connected. IP address: ");
  Serial.println(WiFi.localIP());
}

// Callback function for when a message is received
void callback(char* topic, byte* message, unsigned int length) 
{
  Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");

  String messageString;
  for (int i = 0; i < length; i++)
 {
    messageString += (char)message[i];
  }
  Serial.println(messageString);

  // Control relays based on the message
  if (messageString.equals("1N")) 
{
    digitalWrite(15, HIGH);
  } else if (messageString.equals("1F")) {
    digitalWrite(15, LOW);
  } else if (messageString.equals("2N")) {
    digitalWrite(13, HIGH);
  } else if (messageString.equals("2F")) {
    digitalWrite(13, LOW);
  }
}

// Function to reconnect to MQTT broker
void reconnect()
 {
  while (!client.connected()) 
{
    Serial.print("Attempting MQTT connection...");
    if (client.connect("ESP32Client", mqtt_user, mqtt_password))
 {
      Serial.println("connected");
      client.subscribe(subscribe_topic);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}

void setup()
 {
  Serial.begin(115200);
  pinMode(15, OUTPUT);
  pinMode(13, OUTPUT);
  digitalWrite(15, LOW);
  digitalWrite(13, LOW);

  setup_wifi();
  client.setServer(mqtt_server, mqtt_port);
  client.setCallback(callback);
}

void loop()
 {
  if (!client.connected())
 {
    reconnect();
  }
  client.loop();
}


Note

8051 Development Board- Trainer Kit Libraries, User Manuals and Installation guides look into the download section.
8051 Development Board- Trainer Kit Install the Cloud PLC libraries(cloudplc.h) before getting start with coding. Download
8051 Development Board- Trainer Kit Above download link, you will find all the IO functionalities included in the Cloud PLC library.
8051 Development Board- Trainer Kit In the Arduino IDE, go to the Boards Manager, select the ESP32 board, and ensure that you have selected the board version is 1.0.4.


Controlling Relay Using WiFi AP

• This code configures the Cloud PLC as a Wi-Fi access point and sets up a web server listening on port 80.
• When a client connects and sends an HTTP request, the server reads the request and checks if it contains "ON" or "OFF" to control a relay through the Cloud_PLC_Relay_state function.
• The server responds with the relay's status and closes the connection after handling the request
• The access point's IP address is printed to the serial monitor for client connection.


Wiring Diagram

8051 Development Board- Trainer Kit

8051 Development Board- Trainer Kit


Code

#include "Cloud_PLC.h"
#include <WiFi.h>

  const char* apSSID = "your ssid";       //your SSID
const char* apPassword = "your password"; //Your PASSWORD
WiFiServer server(80);

  void setup()
 {
  Serial.begin(115200);
  setupAccessPoint();        // Set up ESP32 as an access point Cloud_PLC_initialisation();                         
                            // Initialize the Cloud PLC library
  server.begin();            // Start the server
  Serial.println("Server started");
}

void loop() 
{
  WiFiClient client = server.available();  // Check if a client has connected
  if (client) 
{
  while (client.connected() && !client.available()) 
{  // Wait for data from the client
      delay(1);
}

    String request = client.readStringUntil('\r');  // Read the first line of the request
    Serial.println(request);
   

 if (request.indexOf("ON") != -1)
 {  // Process the request
      Cloud_PLC_Relay_state(0, HIGH);   // Turn on Relay
      client.println("Relay turned ON");
    } else if (request.indexOf("OFF") != -1)
 {
      Cloud_PLC_Relay_state(0, LOW);  // Turn off Relay
      client.println("Relay turned OFF");
    } else 
{
      client.println("Invalid command");
    }

    // Close the connection
    client.stop();
  }
}
void setupAccessPoint() 
{
  WiFi.softAP(apSSID, apPassword);  // Set up the ESP32 as an access point
  delay(100);
  Serial.println("Access Point IP Address: " + WiFi.softAPIP().toString());
}



Note

8051 Development Board- Trainer Kit Libraries, User Manuals and Installation guides look into the download section.
8051 Development Board- Trainer Kit Install the Cloud PLC libraries(cloudplc.h) before getting start with coding. Download
8051 Development Board- Trainer Kit Above download link, you will find all the IO functionalities included in the Cloud PLC library.
8051 Development Board- Trainer Kit In the Arduino IDE, go to the Boards Manager, select the ESP32 board, and ensure that you have selected the board version is 1.0.4.


Digital Input Interface - Bluetooth

• This program uses the Cloud_PLC library and Bluetooth Serial to monitor a digital input (IN1) and communicate its state.
• In the setup function, it initializes serial communication for both the Serial Monitor and Bluetooth, allowing pairing with the Bluetooth device named "Cloud_PLC."
• The loop function checks if IN1 is HIGH or LOW, prints the state to both the Serial Monitor and a paired Bluetooth device, and updates every 500 milliseconds.


Code

#include "Cloud_PLC.h"
#include "BluetoothSerial.h"
//Include the header file for Cloud_PLC library
BluetoothSerial SerialBT;

void setup()
{
  Serial.begin(115200);
  SerialBT.begin("Cloud_PLC");
  Serial.print("The device started, Now you can pair it with bluetooth");
  //Initialize serial communication at 115200 baud rate
  Cloud_PLC_initialisation();
  //Call the initialization function for Cloud_PLC
}

void loop()

{
  // Check the digital input state of DI1

  if (Cloud_PLC_Digital_Read(DI1) == HIGH)  //input channel, IN1,IN2,IN3,IN4,IN4
  {
    Serial.println("HIGH");
    SerialBT.println("HIGH");
    // Print "ON" to the Serial Monitor if IN1 is HIGH
    delay(500);

  }
  else
  {
    Serial.println("LOW");
    SerialBT.println("LOW");    // Print "OFF" to the Serial Monitor if IN1 is not
    delay(500);
    // Wait for 2000 milliseconds (2 seconds)

  }
}



Note

8051 Development Board- Trainer Kit Libraries, User Manuals and Installation guides look into the download section.
8051 Development Board- Trainer Kit Install the Cloud PLC libraries(cloudplc.h) before getting start with coding. Download
8051 Development Board- Trainer Kit Above download link, you will find all the IO functionalities included in the Cloud PLC library.
8051 Development Board- Trainer Kit In the Arduino IDE, go to the Boards Manager, select the ESP32 board, and ensure that you have selected the board version is 1.0.4.


Controlling Relay By Bluetooth

• This code sets up an Cloud PLC to control two relays via Bluetooth.
• It initializes the relays as outputs and starts Bluetooth communication with a device name "CPL_Relays."
• In the loop function, it listens for incoming Bluetooth commands to control the relays: "ON1" and "OFF1" for Relay 1, and "ON2" and "OFF2" for Relay 2. It also provides feedback over Bluetooth and prints received commands for debugging.


Code

#include <BluetoothSerial.h>
  
  // Define the relay pins
  #define RELAY1_PIN 15
  #define RELAY2_PIN 13
  
  // Create a BluetoothSerial object
  BluetoothSerial SerialBT;
  
  void setup()
  {
    // Initialize serial communication for debugging
    Serial.begin(115200);
  
    // Initialize the relay pins as outputs
    pinMode(RELAY1_PIN, OUTPUT);
    pinMode(RELAY2_PIN, OUTPUT);
  
    // Set the relays to be off initially
    digitalWrite(RELAY1_PIN, LOW);
    digitalWrite(RELAY2_PIN, LOW);
  
    // Begin serial communication over Bluetooth
    SerialBT.begin("CPL_Relays"); // Bluetooth device name
    Serial.println("Bluetooth device started, you can now pair it with Bluetooth!");
  }
  
  void loop()
     {
    // Check if data is available on the Bluetooth serial
    if (SerialBT.available()) 
     {
      // Read the incoming string
      String request = SerialBT.readStringUntil('\n');
  
     

 // Print the received string to the Serial Monitor (for debugging)
     

      Serial.print("Received: ");
      Serial.println(request);
  
      // Control the relays based on the received command
      if (request.indexOf("ON1") != -1) 
      {
        digitalWrite(RELAY1_PIN, HIGH); // Turn Relay 1 on
        SerialBT.println("Relay 1 turned ON");
      }
      else if (request.indexOf("OFF1") != -1)
      {
        digitalWrite(RELAY1_PIN, LOW); // Turn Relay 1 off
        SerialBT.println("Relay 1 turned OFF");
      }
      else if (request.indexOf("ON2") != -1) 
      {
        digitalWrite(RELAY2_PIN, HIGH); // Turn Relay 2 on
       

      SerialBT.println("Relay 2 turned ON");
      }
      else if (request.indexOf("OFF2") != -1)
      {
        digitalWrite(RELAY2_PIN, LOW); // Turn Relay 2 off
        SerialBT.println("Relay 2 turned OFF");
      }
      else 
      {
        SerialBT.println("Invalid command");
      }
    }
  }




Note

8051 Development Board- Trainer Kit Libraries, User Manuals and Installation guides look into the download section.
8051 Development Board- Trainer Kit Install the Cloud PLC libraries(cloudplc.h) before getting start with coding. Download
8051 Development Board- Trainer Kit Above download link, you will find all the IO functionalities included in the Cloud PLC library.
8051 Development Board- Trainer Kit In the Arduino IDE, go to the Boards Manager, select the ESP32 board, and ensure that you have selected the board version is 1.0.4.


Embedded Webserver

• This program sets up a web server using a Cloud PLC to control two relays through a web interface.
• In the setup function, it initializes serial communication, connects to Wi-Fi, and starts the web server.
• The loop function listens for incoming HTTP requests, parses them to control the relays based on the URL path, and serves a simple HTML page to control the relays.
• It updates the relay states and serves the appropriate HTML based on user interactions with the web page.


Wiring Diagram

8051 Development Board- Trainer Kit


Code

#include <Cloud_PLC.h>

WiFiServer server(80);  // Set web server port number to 80
String header;          // Variable to store the HTTP request
const int relayPin1 = 15;  // Relay pins
const int relayPin2 = 13;  // Relay pins                     
String relayState1 = "OFF";    // Current state of the relays
String relayState2 = "OFF";

void setup()
{
  Serial.begin(115200);
  pinMode(relayPin1, OUTPUT); // Initialize the relay pins as outputs
  pinMode(relayPin2, OUTPUT);
  digitalWrite(relayPin1, LOW);
  digitalWrite(relayPin2, LOW);

  Cloud_PLC_Config_WIFI("TEST", "12345678");  // Connect to Wi-Fi network
  server.begin();  // Start the server
}
void loop()
{
  WiFiClient client = server.available();   // Listen for incoming clients
  if (client)
  { // If a new client connects,
    Serial.println("New Client.");          // Print a message out in the serial port
    String currentLine = "";                // Make a String to hold incoming data from the client
    while (client.connected())
    { // Loop while the client's connected
      if (client.available())
     
 { // If there's bytes to read from the client,
        char c = client.read();             // Read a byte, then
        Serial.write(c);                    // Print it out the serial monitor
        header += c;
        if (c == '\n')
        { // If the byte is a newline character
          if (currentLine.length() == 0)
          

{ // If the current line is blank, you got two newline characters in a row.
            // That's the end of the client HTTP request, so send a response:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();
            // Display the HTML web page
            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            client.println("<style>body { text-align: center; font-family: Arial; } .button { background-color: #4CAF50; border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; } .button2 {background-color: #ff0000;}</style></head>");
            client.println("<body><h1>Cloud PLC Relay Control</h1>");

            client.println("<p>Relay 1 - State " + relayState1 + "</p>");
            if (relayState1 == "OFF")
            {
              client.println("<p><a href=\"/relay1/on\"><button class=\"button\">ON</button></a></p>");
            } else
            {
              client.println("<p><a href=\"/relay1/off\"><button class=\"button button2\">OFF</button></a></p>");
            }
            client.println("<p>Relay 2 - State " + relayState2 + "</p>");
            if (relayState2 == "OFF")
            {
              client.println("<p><a href=\"/relay2/on\"><button class=\"button\">ON</button></a></p>");
            } else
            {
              client.println("<p><a href=\"/relay2/off\"><button class=\"button button2\">OFF</button></a></p>");
            }
            client.println("</body></html>");   
            client.println();   // The HTTP response ends with another blank line
            break;  // Break out of the while loop
          }    
		  else
          { // If you got a newline, then clear currentLine
            currentLine = "";
          }
        } 
          else if (c != '\r')
        { // If you got anything else but a carriage return character,
          currentLine += c;      // Add it to the end of the currentLine
        }
        // Check if the client request is to turn relay 1 on or off
        if (header.indexOf("GET /relay1/on") >= 0)
        {
          relayState1 = "ON";
          digitalWrite(relayPin1, HIGH);
        } else if (header.indexOf("GET /relay1/off") >= 0)
        {
          relayState1 = "OFF";
          digitalWrite(relayPin1, LOW);
        }
        // Check if the client request is to turn relay 2 on or off
        if (header.indexOf("GET /relay2/on") >= 0)
        {
          relayState2 = "ON";
          digitalWrite(relayPin2, HIGH);
        } else if (header.indexOf("GET /relay2/off") >= 0)
        {
          relayState2 = "OFF";
          digitalWrite(relayPin2, LOW);
        }
      }
    }
    // Clear the header variable
    header = "";
    client.stop();  // Close the connection
    Serial.println("Client disconnected.");
    Serial.println("");
  }
}



Note

8051 Development Board- Trainer Kit Libraries, User Manuals and Installation guides look into the download section.
8051 Development Board- Trainer Kit Install the Cloud PLC libraries(cloudplc.h) before getting start with coding. Download
8051 Development Board- Trainer Kit Above download link, you will find all the IO functionalities included in the Cloud PLC library.
8051 Development Board- Trainer Kit In the Arduino IDE, go to the Boards Manager, select the ESP32 board, and ensure that you have selected the board version is 1.0.4.