When you upload any sketch to the Arduino board, stare at the board as the codes get uploaded bit by bit, you will see two SMD LEDs on the board flicker, those two LEDs flickering shows that serial communication involving Transmit TX and Receive RX is going between the Arduino and your computer. The process is called serial communication.
First of all, we have to understand that using arduino is all about talking and sharing signals. Through its various GIOP, arduino takes in inputs and brings out outputs.
To communicate with some sensors, actuators and the computer itself, arduino requires a communication protocol. Communicating with the computer, arduino uses a serial communication protocol called USART or UART via USB-to-TTL serial conversion chips like the FTDI integrated circuit EEPROM named FT232 used by Sparkfun Red Board, or the Atmega16U2 chip that is found in new arduino boards.
Apart from the UART port of the arduino through which a serial communication occurs, an arduino board has extra two pins through which a serial communication can be carried out. Below is a table showing the pins on the various arduino boards that can be used for serial communication. For arduino UNO they are pins 0 and 1. 0(RX), 1(TX).
Let’s have a bit digital technological overview of the whole thing. Normally, computers use to have a standard serial communication protocol called RS232 which is almost faced off. RS stands for “Recommended Standard”. However, this protocol operates at +/-12V logic, while the arduino microcontroller works with the TTL logic levels (5V or 3.3V depending on the board). Hence, connecting the arduino serial port to the computer RS232 port will be voltage incompatible as this will damage the microcontroller on the board.
At the other hand, the arduino microcontroller cannot communicate directly with the computer via the USB; so, since the sketch is to be sent to the arduino board via the computer’s USB port, a form of interface needs to be used to facilitate the communication between the computer USB port and the arduino board, and that is the work of the FTDI chip or the Atmega16U chip. These chips are USB-to-TTL communication interface chips; they facilitate the arduino UART port which is the port through which the data coming from the USB port is converted to the logic-level of the arduino Microcontroller Unit (MCU).
Moreover, the very first arduino boards were built to interface to Computer RS-232 ports. That is how the PC arduino IDE software uploads new programs onto the Arduino board via the bootloader program also stored on the board’s processor chip.
The hand you see holding the board is that of Nicholas Zambetti. For a brief time, Nicholas Zambetti had been considered a member of the “Arduino Team” (the team that invented the arduino board in 2004). The shiny silver coloured tab on the board (circled in yellow) which his thumb rests on, is the RS232 DB9 serial port. Below is the image of the first “Aduino Prototype” which was dubbed “Wiring Lite.”
But, when more computers stopped having hardware RS-232 communications ports installed onto them. The USB-to-serial converter chips come to the rescue, they enable computer operating systems to allow application software like the Arduino IDE to talk to a device like arduino boards, but using the USB bus to send the data.
Nevertheless, we have SR232 Shied, which you can plug onto the arduino board to program it via the DB9 RS232 protocol. See image below:
Click here to buy this shield from amazon
Understanding Serial Communication
Figure 9: serial communication between arduino and a PC
Mathematically, A bit is an abstraction used to denote either a logical high or logical low in digital electronics. In reality, the “high” here means anything between 3.3 to 5.0 volts, while logical low refers to 0 volts.
- 1 bit = 0 or 1
- 4 bits = 1 Nibble
- 8 bits = 1 Byte
- 1024 Bytes (8192 Bits) = 1 Kilobyte (KB)
- 1024 Kilobytes (1048576 Bytes) = 1 Megabyte (MB)
- 1024 Megabytes = Gigabyte (GB)
Serial vs. Parallel Communication
When information is digitized, it can be transmitted over a bus, either via serial or parallel communication network. The figures below show the nature of serial and parallel communications.
In the serial communication channel shown in the figure above, 8-bit information is transmitted through one channel, one bit at a time. At the other hand, the parallel channel makes use of 8 channels to transfer the 8-bit information all at the same time. Both serial and parallel communication protocols have their cons and pros, however, serial communication protocol takes the lead as it appears today. One of the advantages of serial over parallel communication is the number of lines used. A serial communication uses only one communication channel, hence one data line. This does not mean that it’s only one wire that is used in serial communication; it means that data transfer requires only one wire. You must need wires for power and ground, as well as clock signal if needed. We will discuss more on serial and parallel data transfer in the future. Meanwhile let’s not forget that Arduino uses serial communication protocol and not parallel communication protocol.
Arduino Communication With a PC and Python Programming Language
Arduino can communicate with a PC as we already know from our first Arduino tutorial, when we uploaded our first Arduino sketch to the Arduino board. In this tutorial however, we are going to have a real-time communication over serial between Arduino and the PC, also, we are going to have a real-time communication between Arduino board and python integrated development environment (IDLE).
Arduino Communication With a PC:
Arduino communication with a PC is a half-duplex communication. This means that both devices can transmit information to each other but not simultaneously. When arduino is transmitting, the PC will be receiving, and when the PC is transmitting, arduino will be receiving, but not both at the same time.
We are going to setup two circuits where:
- Arduino will be sending information to the computer to be printed on the computer serial communication window.
- Another one where arduino will receive some real-time data from the PC serial communication console and prompt the arduino board to perform an action, here, the action will be to turn on an LED.
Arduino Sending Data to the Computer:
To setup the circuit you need the following:
- Your PC with arduino IDE installed
- Arduino UNO board……Buy component
The circuit connection needed for this setup is simply plugging the Arduino into the PC via a USB cable. With the Arduino IDE already installed, all you need to do is open the Arduino IDE and write the sketch. Download the sketch below and run it on your Arduino IDE.
Upload the sketch to the Arduino board by clicking the upload button or Ctrl+U. When the sketch is done uploading, click on the tools button, then click on serial monitor, you will see data printing on the serial window every one second. See image below.
This way, the Arduino is communicating with the PC as the transmitter, while the PC is the receiver. To confirm this, as data is being sent to the PC from the Arduino via the USB cable every second, you will see that the yellow TX LED blinks every second; that is to show that the Arduino is transmitting. TX means transmit while RX means receive.
Arduino Receiving Data from the Computer:
In this exercise, we are going to talk to the Arduino using the computer’s keyboard, display the information on the serial monitor and use the typed data to turn ON and OFF the Led.
Using the same circuit we used for the previous exercise, all you need to do download the code below in your Arduino IDE and upload.
When you paste the sketch on the IDE code editor, the codes will look like the image below:
We are going to explain the parts of the code that may look new.
We have explained Line 5 code in the previous sketch, however, let’s throw more light. Line 5 is a function used to setup baud rate communication between the Arduino board and the computer. The common baud rate is 9600, but you can choose something other than that, (make sure that the baud rate you choose in the sketch concurs with baud rate you chose on the serial monitor.
Line `10 code checks if anything was sent from the keyboard. >=means that if something was sent, then that char (character) must be equal to or greater than zero,if nothing was sent, then the Arduino will have to receive anything less than or not equal to 0, which is same as saying nothing was sent.
Line 12 code checks the value stored in the variable num. num will now become the value sent from the keyboard and was read by the arduino. The value that was read is a ‘char’ type of data.
We want to send numbers from the keyboard to the Arduino, which will be sent as char type of data.
From the ASCII table shown below, we can see that the numbers that can be represented as char are numbers from 0 to 9.
Note that, the character equivalence of the decimal number 48 is zero (0).
Also from the ASCII table, you can see that the decimal equivalence of character number 1 is 49.
So, if I pressed one(1) on the keyboard, that one(1) is equal to 49 as a char (since we are receiving the data as char)
in the sketch, if we write 1 (which is 49 in char) minus 48 (a decimal), we are having 49(char) – 48(decimal) = 1
Hence, the one that we pressed will be displayed as one, instead of 49.
Line 14 contains an “if statement”, we shall talk about the “if statement” in the future tutorials. However, what the code does is to check “if” the number that was pressed on the keyboard is equal to 1. Note that ‘=’ is not the same thing as ‘==’. The earlier is an assignment operator, while the latter is an equality sign in computer programming. So, if the condition is true, the code in the curly bracket in lines 16 to 19 will be executed.
Line 21 is another conditional statement function, “else if”, this statement checks if the number pressed on the keyboard is equal to 2. instead of being equal to 1. If it is equal to 2, the code in lines 23 to 26 will be executed.
When you upload the code and open the serial monitor console, if you press 1 on your keyboard followed by the enter key, the LED connected to pin 13 of the Arduino will come ON and a text will be displayed on the serial console window telling you that you pressed 1 and pin 13 is ON. If you press 2 on the keyboard followed by the enter key, the LED connected to pin 13 will go OFF, and a text will be displayed on the serial console window also telling you that you pressed 2 and pin 13 is OFF.
Arduino Communication with Python Programming Language:
Python is a very powerful programming language. You can do a lot with python, especially when you start programming Raspberry pi and in Robotics. To have your Arduino board communicate with python programming language requires that you install python integrated development environment (IDLE) on your computer; I will assume you have that already, else download Python and install. Comment below if you need assistance or a tutorial on installing Python.
We require the following python programming tools for this project;
Pyserial is a Python application programming interface (API) module used to access the serial port. Pyserial is like the middleman between the Arduino and the python programming language.
VPython is the Python programming language plus a 3D graphics module called Visual. VPython allows users to create objects such as spheres and cones in 3D space and displays these objects in a window. This makes it easy to create simple visualizations. We will treat a lot on 3D visualisation and displays in the future.
Have all these tools downloaded and installed on your computer and move over to the programming part.
The Arduino code for the project can be downloaded below The code is a simple code, which we have somehow discussed previously.
download the python code above run it on the Vpython development environment.
Make sure not to open the Arduino serial monitor when using the Vpython Visual window. They two cannot work at the same time.
We can have virtual display of the physical world events using Vpython, sensors and arduino. We shall delve deeper into python programming in the future. for now, get your tools and start practicing.