Tag Archives: microcontroller

Programming a line follower robot based on atmega microcontroller

As always for beginner in robotic (even tough not really beginner in electronic), Programming a line follower robot based on atmega microcontroller again. I use USB AVR ISP for uploading my program.

20140609_212720

Advertisements

Quick and Dirty Arduino Serial Port Communication

Written by : Antonius (sw0rdm4n)

www.ringlayer.net

Partners www.indonesianbacktrack.or.id | www.jasaplus.com | www.cr0security.com

Serial port on any microcontroller board such as arduino is used for serial communication where data will be transfered (in and out to or from another devices). In this tutorial, I used an arduino compatible board -> sduino.

Image

On above picture, my sduino board is connected to notebook using 9600 baud rate serial port. We’ll be using arduino ide 1.0.5 on linux machine. In this machine the serial port detected as /dev/ttyACM0 with 9600 baud rate.

Image

In order to code using arduino ide we need 2 void function : void setup() { } and void loop() { } . void setup is initialization function, this will be running once on each time arduino board started. void loop wil be used to run any instruction untill the device is poweroff. Initialization and Sending Data Over Serial Port On initialization function we will determine that serial port uses 9600 baud rates speed : Serial.begin(9600); To send something on serial we can use some functions like : Serial.print() , Serial.println() , Serial.write, etc. In this example we’ll be using Serial.println().

Here’s a sample code :

/* simple serial communication tutorial for ringlayer.net */

void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(“Debug : Using serial println to print this message !”);
delay(2000);
}

On delay we set 2000 microseconds for 2 seconds delay. Upload the sketch to board, and run Serial Monitor to check :

Image

We can also send data with format hex / oct / bin / etc. Example below we use : Serial.println()

Image  

Reading Data From Serial Port

Reading data from serial port can be done using Serial.read() function

/* simple serial port communication by sw0rdm4n */
byte data = 0;

void setup() {
Serial.begin(9600);
}

void loop() {
Serial.println(“Sending data …”);
Serial.println(22, HEX);
data = Serial.read();
Serial.println(“Reading data …”);
delay(1000);
Serial.print(“\nGot : “);
Serial.write(data);
delay(1000);
}

Upload the sketch and monitor via Serial Monitor on the right of sketch window :

serial4   thanks : Zico, Gunslinger,  Ega, Whitehat, Edward, Eki