SOLVED: Serial communication between Arduino and 8051.

Hello everyone,

Can anyone suggest me a way to perform serial communication between an Arduino Uno and a 8051 micro-controller?For my project the Uno is the master and 8051 is the slave device. What should be the hardware configuration? I’m simply connecting the Tx of Uno with Rx of 8051 and vice versa. But I’m not getting the desired output. Communication is done at 9600 Baud rate. Both the software serial and hardware serial is used for the Arduino. The basic algorithm of the project is: Read data via blue tooth using S/W Serial. Then send some character according to the read data(from blue tooth) to the 8051 using hardware serial. The 8051 reads these characters and performs the necessary actions. The I’m using Keil IDE for 8051 programming.

Thanks :slight_smile:

Motor Control.c (1.79 KB)

Main_Prog_10_03_16.ino (2.96 KB)

life can be very difficult starting out trying to get 2 mcu to talk. have each working independently with friendly known good pc first (hyperterminal, putty, etc). then you will find the final configuration will work like majicks. its best to have both feet on the ground before learning to walk.

I'm simply connecting the Tx of Uno with Rx of 8051 and vice versa. But I'm not getting the desired output.

That should be sufficient, depending on exactly how pins are being labeled (at the board level, sometimes "RX" means "this is the RX pin of this UART" and sometimes it means "This connects to the RX pin of the other device.")

You'll want to test and debug the serial communications with a program that is simpler than your final sketch, and doesn't involve complications like bluetooth.

Standard 8051 system with 11.0592MHz clock?

How about using JSON? There is a really good Arduino JSON library out there. Basically you send a text string thats picked up by the other microcontroller. The string is then parsed and its content can be stored into variables. Have a look at the examples for for information on how it's done. I've used this library for transferring data from an ESP8266 to an Arduino UNO :slight_smile:

The first question is are they both running TTL level serial signals, is it RS-232 (+/- 12v inverted from TTL levels) or just how are they connected and are you sure you have a good ground between them? What distances are we talking about here between the processors? Are they next to each other or in different rooms/areas or?

john1993:
life can be very difficult starting out trying to get 2 mcu to talk. have each working independently with friendly known good pc first (hyperterminal, putty, etc). then you will find the final configuration will work like majicks. its best to have both feet on the ground before learning to walk.

@ john1993

Yes, I've tried that. Sent a few characters to the Arduino and 8051 both from PC using a virtual Serial Port Emulator,Realterm Serial Terminal and simulated it in the Proteus Simulator. Seems to work fine at the 8051 end. However, when I tried sending some characters via the S/W Serial Ports of Arduino, I didn't get any output at the hardware serial port of the Arduino in the simulation. I'm unable to understand what seems to be the problem!

westfw:
You’ll want to test and debug the serial communications with a program that is simpler than your final sketch, and doesn’t involve complications like bluetooth.

Standard 8051 system with 11.0592MHz clock?

@westfw
I’ve tried that, used a simpler code and used Virtual Serial Terminals to Simulate it , but there seems to be a problem at the Arduino end, I suppose. Can’t understand why I’m unable to get the desired output.
Yes, I’ve used the standard 8051 system with 11.0592 MHz Clock.

hansibull:
How about using JSON? There is a really good Arduino JSON library out there. Basically you send a text string thats picked up by the other microcontroller. The string is then parsed and its content can be stored into variables. Have a look at the examples for for information on how it's done. I've used this library for transferring data from an ESP8266 to an Arduino UNO :slight_smile:

@hansibull
Well, what I'm trying to do here is get the Uno to send a few characters to the 8051 according to some data sent via bluetooth to the Arduino Uno. Not the other way round! :slight_smile:

gpsmikey:
The first question is are they both running TTL level serial signals, is it RS-232 (+/- 12v inverted from TTL levels) or just how are they connected and are you sure you have a good ground between them? What distances are we talking about here between the processors? Are they next to each other or in different rooms/areas or?

@gpsmikey
Yes, both are running at TTL level. I'm not connecting them to the PC or something. They are connected via the Tx-Rx Serial pins of both the Uno and the 8051. They have common GND between them. I'm testing a Breadboard circuit next to each other so distance isn't a concern for the moment.

rajarshiroy:
Sent a few characters to the Arduino and 8051 both from PC using a virtual Serial Port Emulator,Realterm Serial Terminal and simulated it in the Proteus Simulator.

Did you say "simulator"?

If you are going to use a simulator, please do not post questions on this forum

We are only interested in microcontrollers - not simulators. Find another forum. One about simulators.

:grinning: :grinning: :grinning:

Paul__B:
Did you say “simulator”?

If you are going to use a simulator, please do not post questions on this forum

We are only interested in microcontrollers - not simulators. Find another forum. One about simulators.

:grinning: :grinning: :grinning:

@Paul__B
The Simulator is used to test the code as when I tested on the actual hardware circuit, it didn’t work. Also, I was unsure on how to ‘see’ the serial data at the 8051 end, sent from the the Uno to the 8051 serially. I would like you to suggest me a way to do that! Also, burning the code on both the controllers for testing a single line/couple of lines change of code and making the breadboard ckt again and again seems pretty inconvenient( for me at-least )!! I’m pretty sure most will agree to it! :slight_smile:

Most of us who are prepared to look at code here are not prepared to download it from an "ino" file and even less so "C", you need to use the "code" tags to post each in a meaningful form within the text - according to the instructions.

Clearly in order to figure out what the problem is with your code, we do need to understand the code and examine it in detail. That is quite an "ask", but if the code is presented in a convenient form, we will generally do so. So far, you have apparently not made it sufficiently inviting for any of us to actually do that and therefore the answers have been entirely general and relating only to the hardware.

Of course, a further problem is that you have code for an 8051 system which few of us are in a position to test, so we will be reduced to examining it for problems.

The connections between the two systems should be straightforward and you would realise the requirement for a common ground. The ATmegas use 5 V logic (the term "TTL" is a misnomer as it is not TTL logic levels at all) and presumably so does the 8051. As you have - apparently - connected each of these in reality to a PC and exchanged data correctly in each direction, you clearly should know all about the necessary connections.

So if it is not working, it is because the code is not working. A simulator is not going to tell you why it is not working and is even more likely to have some subtle additional restriction to make matters even more problematic than the Real Thing.

Paul__B:
Most of us who are prepared to look at code here are not prepared to download it from an “ino” file and even less so “C”, you need to use the “code” tags to post each in a meaningful form within the text - according to the instructions.

The connections between the two systems should be straightforward and you would realise the requirement for a common ground. The ATmegas use 5 V logic (the term “TTL” is a misnomer as it is not TTL logic levels at all) and presumably so does the 8051. As you have - apparently - connected each of these in reality to a PC and exchanged data correctly in each direction, you clearly should know all about the necessary connections.

So if it is not working, it is because the code is not working. A simulator is not going to tell you why it is not working and is even more likely to have some subtle additional restriction to make matters even more problematic than the Real Thing.

@Paul__B: Okay! Sorry for any inconvenience caused due to uploading the .ino and .C files. I had no clue there are ‘upload rules’!! Anyway, here’s my Arduino code. I’ll post the C code for 8051 in the next reply.

P.S: I have made the common GND connection and connected the Arduino with PC and the 8051 with Arduino. But I am unable to read any Data received from the 8051 to understand if I’ve received the correct data or not, for my .C code to actually work.

#include <SoftwareSerial.h>

#include <Servo.h>

Servo myservo1, myservo2, myservo3, myservo4;

int blueToothTx = 7;
int blueToothRx = 8;
char h;
unsigned int realservo;
String dataread;
SoftwareSerial bluetooth = SoftwareSerial(blueToothTx, blueToothRx);


void setup() 
{

  /*
  pinMode(2, OUTPUT);   // Input 1 L293D
  pinMode(3, OUTPUT);   // Input 2 L293D
  pinMode(11, OUTPUT);  // Input 3 L293D
  pinMode(12, OUTPUT);  // Input 4 L293D
  */
  myservo1.attach(5);
  myservo2.attach(6);
  myservo3.attach(9);
  myservo4.attach(10);
  
  // Set up Serial Communication to PC
  Serial.begin(9600);
  
  // Set up Bluetooth serial Communication to Android
   bluetooth.begin(9600);
}

void loop()
{

  if(bluetooth.available())
  {
    delay(10);
    h = bluetooth.read();
    if( h == '~')
      dataread = bluetooth.readStringUntil(',');  //Read data Serially
    else 
    {
      unsigned int servopos  = h;//Serial.read(); 
      unsigned int servopos1 = bluetooth.read();//Serial.read(); 
      realservo = (servopos1*256) + servopos;
      //Serial.println(realservo); 
    }
    
  }
    //dataread = inData    //make String 'forward','back', 'left', 'right'
  
  
  if (dataread.length()>0)
  {
    //Serial.println(dataread);
    if(dataread == "UP")
    {
      delay(100);
      Serial.write('W');
      //Serial.println(" Fwd ");
      
    }
    
    else if(dataread == "BACK")
    {
      delay(100);
      Serial.write('R');
      //Serial.println(" Rev ");
      
    }

    else if(dataread == "LEFT")
    {
      delay(100);
      Serial.write('A');
      //Serial.println(" Left ");
      
    }

    else if(dataread == "RIGHT")
    {
      delay(100);
      Serial.write('R');
      //Serial.println(" Right ");
      
    }

    else if(dataread == "STOP")
    {
      delay(100);
      Serial.write('S');
      //Serial.println(" Stop ");
      
    }
    else if(dataread == "OPEN")
    {
      delay(100);
      Serial.write('O');
      //Serial.println(" Open ");
      
    }
    else if(dataread == "CLOSE")
    {
      delay(100);
      Serial.write('C');
      //Serial.println(" Close ");
      
    }
    dataread = ""; //Reset the Variable
    
 }
  
  //read from Bluetooth and write to serial port
    
  if( realservo >= 1000 && realservo <1180)
  {
    int servo1 = realservo;
    servo1 = map( servo1, 1000,1180,0,180);
    myservo1.write(servo1);
    Serial.println(servo1);
    delay(10);
  }

  if( realservo >= 2000 && realservo <2180)
  {
    int servo2 = realservo;
    servo2 = map( servo2, 2000,2180,0,180);
    myservo2.write(servo2);
    Serial.println(servo2);
    delay(10);
  }

  if( realservo >= 3000 && realservo <3180)
  {
    int servo3 = realservo;
    servo3 = map( servo3, 3000,3180,0,180);
    myservo3.write(servo3);
   Serial.println(servo3);
    delay(10);
  }

  if( realservo >= 4000 && realservo <4180)
  {
    int servo4 = realservo;
    servo4 = map( servo4, 4000,4180,0,180);
    myservo4.write(servo4);
    Serial.println(servo4);
    delay(10);
  }
    
}

@Paul__B:

Here’s My .C code. I’ve used Keil IDE for the C code.

#include <reg51.h>
#include <stdio.h>

sbit WL_A = P1^7 ; //L293D Left Wheel 
sbit WL_B = P1^6 ; //L293D Left Wheel 
sbit Gr_A = P1^5 ; //L293D Gripper
sbit Gr_B = P1^4 ; //L293D Gripper
sbit WR_A = P1^3 ; //L293D Right Wheel 
sbit WR_B = P1^2 ; //L293D Right Wheel

unsigned char Ch;

void serial_init()
{
	TMOD = 0x20;	//Timer1 Mode2, 8 bit auto reload
	SCON = 0x50;	//8 Data bit, 1 start bit, 1 stop bit
	TH1  = 0xFD;	//9600 Baud
	TL1  = 0x00;
	TR1  = 1;
}	

/*void delay_ms_10()
{
	TMOD = 0x01; // Timer 0 Mode 1, 16 bit Timer
	TH0	 = 0xFD;
	TL0	 = 0x00;
	TR0 = 1;     // Timer 0 Start
	while (TF0 == 0);  // check overflow condition
		TR0 = 0;         // Stop Timer
	TF0 = 0;           // Clear flag

}	
void delay_ms_100()
{
	int i;
	for(i=0;i<10;i++)
	{
		delay_ms_10();
	}
}

void serial_send(unsigned char dat)
{
	while(!TI);
	TI = 0;
	SBUF = dat;
}*/

void main()
{
	
	serial_init();
	P1 =0x00;
	
	while(1)
	{
		unsigned char Ch;
		Ch = _getkey();
		if(Ch =='W') //Forward
		{
			WL_A = 1;
			WL_B = 0;
			WR_A = 1;
			WR_B = 0;
			//delay_ms_100();
		}
		else if(Ch =='A') //Left
		{
			WL_A = 1;
			WL_B = 0;
			WR_A = 0;
			WR_B = 1;
			//delay_ms_100();
		}
		else if(Ch =='D') //Right
		{
			WL_A = 0;
			WL_B = 1;
			WR_A = 1;
			WR_B = 0;
			//delay_ms_100();
		}
		else if(Ch =='R') //Reverse
		{
			WL_A = 0;
			WL_B = 1;
			WR_A = 0;
			WR_B = 1;
			//delay_ms_100();
		}
		else if(Ch =='S') //Stop
		{
			WL_A = 0;
			WL_B = 0;
			WR_A = 0;
			WR_B = 0;
			//delay_ms_100();
		}
		else if(Ch =='O') //Gripper Open
		{
			Gr_A = 1;
			Gr_B = 0;
			//delay_ms_100();
		}
		else if(Ch =='C') //Gripper Close
		{
			Gr_A = 0;
			Gr_B = 1;
			//delay_ms_100();
		}
		else
		{
			P1 = 0x00;
		}
		
	}
}

On an uno, you can not quite use the hardware serial to communicate with anything but a PC via usb. You can transmit to the 8051 but the usb receive will interfere with receiving of the data from the 8051.

sterretje:
On an uno, you can not quite use the hardware serial to communicate with anything but a PC via usb. You can transmit to the 8051 but the usb receive will interfere with receiving of the data from the 8051.

@sterretje:
So, what you're suggesting is, I either need to use another S/W Serial on some other port or use an external power supply for the Uno? That way, it's not connected via the USB. Well, I haven't thought it that way. Lemme give it a try :slight_smile: I always thought the Tx and Rx Transmit and Receive simultaneously with the USB and pins 0 and 1 of the Uno when we give Serial.write() command and only to the USB when we give Serial.print() command .

there should be no problem using that same port when talking to a pc to verify soft serial method. then lift those same pins and talk to the 8051. it will be convenient to debug software. then try same code using other pins for final 8051 communication. or maybe attempt that latter first. at least that would be my approach.

rajarshiroy:
Seems to work fine at the 8051 end. However, when I tried sending some characters via the S/W Serial Ports of Arduino, I didn’t get any output at the hardware serial port of the Arduino in the simulation.

well that narrows the problem down so you are half way there. until you get arduino at least talking to a pc not much point wasting time on other aspects. imo best concentrate on arduino soft serial with a simpler program.

rajarshiroy:
@Paul__B: Okay! Sorry for any inconvenience caused due to uploading the .ino and .C files. I had no clue there are ‘upload rules’!! Anyway, here’s my Arduino code. I’ll post the C code for 8051 in the next reply.

paul is normally very helpful but may have got up on wrong side of bed today. lol. its not a rule but he is right that inline code is easier to read than opening files so will facilitate help. i am not a fan of simulators either but must admit they can be helpful for the “hardware challenged”. personally i am more real-world/hands-on but ymmv.

i suggest you get arduino serial working with realterm then verify the 8051 end again too. finally see if they talk to each other using simpler code (ie led indication). not much point attempting final applications before that. might work but probably take 10x-100x longer to get there.

rajarshiroy:
@sterretje:
So, what you're suggesting is, I either need to use another S/W Serial on some other port or use an external power supply for the Uno? That way, it's not connected via the USB. Well, I haven't thought it that way. Lemme give it a try :slight_smile: I always thought the Tx and Rx Transmit and Receive simultaneously with the USB and pins 0 and 1 of the Uno when we give Serial.write() command and only to the USB when we give Serial.print() command .

The 'usb' micro on the uno is always connected to the 328 micro. So using an external usb power supply is not going to help.

I'm not sure if it's really possible to use two software serial ports; I think that it will not work if there is a chance that bluetooth and 8051 can talk to the uno at the same time.

Another type of Arduino that has a serial port available or a I2C uart or SPI uart would be my choice.

john1993:
i suggest you get arduino serial working with realterm then verify the 8051 end again too. finally see if they talk to each other using simpler code (ie led indication). not much point attempting final applications before that. might work but probably take 10x-100x longer to get there.

@john1993:

I've tried the simple code first, in both the ends( i.e the Uno and the 8051) and it seemed to work fine. After that, I started with the complex codes! :slight_smile:

Anyway! Finally, both the codes are working! :smiley: :smiley: The motors are running via bluetooth! :smiley: YIPPIE! :smiley: :grin:

sterretje:
The 'usb' micro on the uno is always connected to the 328 micro. So using an external usb power supply is not going to help.

I'm not sure if it's really possible to use two software serial ports; I think that it will not work if there is a chance that bluetooth and 8051 can talk to the uno at the same time.

Another type of Arduino that has a serial port available or a I2C uart or SPI uart would be my choice.

@sterretje
No, I was not talking about the USB power. We can use an external power supply such as the 9V Battery for the Uno to work.
Multiple software serial just works fine. See this: https://www.arduino.cc/en/Tutorial/TwoPortReceive

The bluetooh and 8051 doesn't talk with the Arduino at the same time.The Algorithm is: First the bluetooh receives via S/w serial--> Sends data via H/W serial to the 8051--> 8051 receives this data and Drives the motors! :slight_smile:

Anyway, finally both the codes are working! :smiley: :smiley: The motors are running via bluetooth! :smiley: YIPPIE! :smiley: :grin: