Programming A Driverless Car Chapter 13: Serial Communication

Serial & Parallel Communication-

Serial: Data bits from device 1 to device 2 one by one it is called serial communication

Parallel: Data bits are sent from device 1 to device 2 parallely. They have sent it via 8 channels. So it is called parallel communication.

Serial communication is economical.

Differences between serial and parallel:

Synchronous Serial Interface:

A synchronous serial interface always pairs its data line with a clock signal, so all the devices on a synchronous serial bus share a common clock. This makes a more straightforward, often faster serial transfer but it also requires at least one extra wire between communicating devices. Examples are SPI, I2C.

Asynchronous Serial Interface:

Asynchronous means that data is transferred without support from an external clock signal. This transmission method is perfect for minimizing the required wires and I/O pins, but it does not mean we need to put extra effort into reliably transferring and receiving data.

 

A serial bus consists of just two wires- one for sending data and another for receiving. As such, serial devices should have two serial pins: the receiver, RX, and the transmitter TX.

Serial Interface:

A serial interface where both devices may send and receive data is either full-duplex or half-duplex.

Full Duplex- both devices can send and receive simultaneously

Half Duplex Communication- Serial devices must take turns sending and receiving.

Serial Communication with arduino:

Baud Rate- In case of asynchronous communication, there is an extra effort needed for reliable transmission of data. The rate at which the data is being transferred, i.e. the rate at which the sender sends the data and the receiver receives the data. If both the rates are same the communication will be reliable. This rate also helps us to know that there might be errors.

Information passes between computer and Arduino through the USB cable. Information is transmitted as zeros ‘0’ and ones ‘1’ also known as ‘bits’.

In Arduino we pass data using the cable. In Arduino we have serial communication:

-Compiling turns your program into binary data (ones and zeros)

-Uploading sends the bits through USB cable to the Arduino.

-The TWO LEDs near the USb connector blink when data is transmitted

 –RX blinks when the arduino is receiving data

 –TX blinks when the Arduino is transmitting data

Arduino Serial Communication Codes–

To declare the baudrate: Serial.begin();

To send serial data: Serial.print();

To read serial data: Serial.read();

Lab: Controlling Arduino using Python and Serial Communication

We are using pin number 13- with LED

int led=13;

void setup()

{

pinmode(led,output); ---- because LED is output

Serial.begin(9600);

}

 

void loop()

{

while(Serial.available())

{

char data=Serial.read();

if(data==’a’)

{

digitalWrite(led, HIGH);

}

else if(data== ‘b’)

{

digitalWrite(led, LOW);

}

}

Output: When we click a LED goes on and when we click b it switches off.

Check here.

 

Now, we will do the same program using python. We need to first install pyserial.

>>>import serial

>>>s=serial.Serial(‘COM20’,9600) -----Check in arduino tools, which COM port to use

>>>s.write(b‘a’)

1L means data is send

>>>s.write(b‘b’)

1L

>>>s.write(b‘a’)

1L

>>>s.close() ---- Close the serial port

PC Controlled Robot

The robot has to be controlled by the programming done by us.

int m11=6;

int m12=9;

int m21=10;

int m22=11;

void setup()

{

pinMode(m11,OUTPUT);

pinMode(m12,OUTPUT);

pinMode(m21,OUTPUT);

pinMode(m22,OUTPUT);

Serial.begin(9600);

}

void loop()

{

 

#we will program the robot. If we send b it should move backwards, if we click on L it should move left and so on

while(Serial.available())

{

char data=Serial.read();

if(data==’s’){

digitalWrite(m11,LOW);

digitalWrite(m12,LOW);

digitalWrite(m21,LOW);

digitalWrite(m22,LOW);

}

else if (data== ‘f’)

{

digitalWrite(m11,LOW);

digitalWrite(m12,HIGH);

digitalWrite(m21,LOW);

digitalWrite(m22,HIGH);

}

else if (data== ‘r’)

{

digitalWrite(m11,LOW);

digitalWrite(m12,HIGH);

digitalWrite(m21,LOW);

digitalWrite(m22,LOW);

}

else if (data== ‘l’)

{

digitalWrite(m11,LOW);

digitalWrite(m12,LOW);

digitalWrite(m21,LOW);

digitalWrite(m22,HIGH);

}

Else if (data== ‘b’)

{

digitalWrite(m11,HIGH);

digitalWrite(m12,LOW);

digitalWrite(m21,HIGH);

digitalWrite(m22,LOW);

}

}

}

Now, upload the program and go to serial monitor. Check by sending f,b,r,l,s.

 

Recommended Reading: Chapter 12: Programmable Systems and Microcontrollers

Next: 

To start reading from a topic of your choice, you can go back to the Table of Contents here

This course evolves with your active feedback. Do let us know your feedback in the comments section below.

Looking for jobs in Artificial Intelligence? Check here

Written By
More from Level Ten

Programming A Driverless Car Chapter 15: HC05 Interfacing

Bluetooth Technology Bluetooth is a wireless technology standard for exchanging data over...
Read More