Wednesday, 3 February 2016

Creating Additional Serial Port on Aduino Uno #4

As discussed in #3 , Arduino Uno has only one in built Serial port. But when we have more than one device to communicate with the Arduino board, can we proceed with Uno ? If yes, how can we proceed further as Uno has only one serial port?  The answer is we can create additional Serial ports by simple programming. This is shown below:

#include<SoftwareSerial.h>
SoftwareSerial Serial2(3,4);  //here 3 is RX & 4 is TX and Serial2 is the name of the
                                              //created port  & by default name of inbuilt Serial port(0,1)
                                              // is Serial.

Once we include the above two lines of code, we can use 3,4 pins as RX & TX  with the name Serial2 respectively.
to begin we use the command     Serial2.begin(9600);
                                     to read     Serial2.read();
                                    to write     Serial2.write();


** Note that if you want to communicate with the computer you should use Serial port only not any  other created ports. 

Serial Communication In Arduino #3

                     By the name Serial means "one after the other".   Arduino communicates with computer or any other external device by Serial Communication. This implies that Arduino sends data in terms of bits one after the other. This is called Serial Communication. For this purpose we need a minimum of one Serial Port (One port means 2 pins). One pin to receive data and the other to transmit(send). In Arduino Uno we have 1 Serial Port(0-RX, 1-TX). Digital pin 0 is receiver pin(RX) & 1 is transmitter pin(TX) ( marked red in the below figure).




For instance, consider Arduino has to send a character 'U' to the device attached. How does it send ? ?
We know that it can't understand 'U', it understands only binary language(0's and 1's). So 'U' should be converted to some combination of 0's and 1's. For this purpose, we have ASCI codes. The ASCI code of 'U' is 85 & so in order to transmit 'U', Arduino transmits 01010101 (i.e, binary equivalent of 85) through pin-01(TX). But at what speed? ?
This speed of transfer is mentioned in Serial.begin(x). Here x is the speed in bits/sec. Mostly we use x=9600 i.e, Serial.begin(9600). This means 9600 bits are transmitted per second. So, to transfer 1 bit, it takes (1/9600)=104 micro seconds. Also to transfer each byte, it uses two additional bits to indicate start and stop of that particular byte. As Arduino uses TTL Serial Communication, we have start bit as "0" & stop bit as "1". So to transmit 'U', Arduino sends a wave as shown in the below figure.




                                         **Note that here LSB is transmitted first.

So, for every byte of data transmitted, there are actually 10 bits being sent( 1 bit-start, 8 bits-data & 1 bit-stop).So at a baud rate of 9600 bps, it is actually transmitting (9600/10)=960 bytes of data.


Arduino is transmitting data but where is it going to be printed ?
There is a Serial monitor for this purpose. Once your code is successfully uploaded on your board, if you want to use serial communication, then open serial monitor window
1) Tools-->Serial Monitor  or  2)directly by clicking on Serial Monitor.




Once you click on the Serial Monitor, a window will be popped out as show below. This is Serial Monitor window.







You may notice that baud rate in the serial monitor and in the sketch must match to get the correct output. If they do not match you will get something like this





As said above, if Arduino Uno has only one Serial Port, then can we use more than one device to communicate with the Arduino??


                                             **Click here for the list of ASCI values


Monday, 1 February 2016

Arduino concepts & misconcepts #2

Do you  know what is going to happen whenever you click on "upload"?
First the software compiles the code and displays the errors(if present) and if compilation is successful & if your Arduino board is connected to your PC through USB, then entire code will be uploaded to your Arduino Flash Memory(present in Atmega chip,the big chip that is present on your board).

 In Arduino Uno, flash memory is of 32Kb size so the program size must be within 32Kb . If it exceeds 32Kb, then you should resize the code by Optimisation(reducing the unwanted code) or you can use Mega board which has Flash Memory of 256Kb.

Once one code(sketch) was uploaded successfully, the same code persists till you upload other code to the board. This is the main Advantage of Arduino. There is a general misconception that Arduino has to be connected to your PC and run it (without removing USB connection). But this is WRONG. Once code is successfully uploaded to your board, Arduino just takes 5V from the USB port and nothing else(unless you are using Serial Communication). So this(supplying 5V to Arduino board) can also be achieved by using batteries. Hence it is clear that you need not connect your Arduino board to your PC once code was successfully uploaded to your board.


*** What is Serial Communication ?.
*** To which pins should we connect Battery ? and How many others methods to power an Arduino?





ARDUINO #1

What is Arduino ?

                     It is an open source platform(readily available), where you can instruct the Arduino board(through programming in Arduino software & uploading it to Arduino board) how to work.

ARDUINO UNO BOARD


ARDUINO UNO CONNECTED TO LAPTOP




Step-1) Installing the Arduino software.

Step-2) Get an Arduino board.

Step-3) Connect you Arduino board to you PC through USB cable (you will get it along with the Arduino board) and make necessary changes in Arduino settings.

Step-4) Write a code(sketch) on your Arduino software in your PC & upload it to the board. As soon as it is successfully uploaded, it starts to run according to the code.