February 22, 2011

Arduino Control over Serial Port

So let's build something more difficult. We will see how you can control arduino over a serial port. We turn the onboard LED on and off through the serial port.
Let's see the video of the program working and then we will explain the code.

 * serial_control.pde
 * -----------------
 * Turns the LED 13 ON and OFF depending on what is received
 * through the serial port.
 * http://spacetinkerer.blogspot.com

#define LED 13

int input = 0;       // variable to keep the data from the serial port

void setup() {
  pinMode(LED,OUTPUT);    // declare the LED's pin as output
  Serial.begin(9600);        // connect to the serial port

void loop () {
  input = Serial.read();      // read the serial port

  // if the input is '1' turn the LED ON, if '0' turn it OFF
  if (input == '1' ) {
    Serial.println("LED13 is ON");
  if (input == '0'){
      digitalWrite(LED, LOW);
      Serial.println("LED13 is OFF");

The arduino program makes it quite easy to connect through a serial port. All you have to do is declare the "speed" at which you want to talk using this command:


We put his command in the setup() function. Then we use

input = Serial.read();

This command returns the data that are read through the serial port, and stores it to the input variable. So easily we can get the data and the do anything we want. We chose a basic example here. If the data read is '1' then we turn the LED 13 ON. If the data read is '0' then we turn it OFF.
We also used these two commands:

Serial.println("LED13 is ON");
Serial.println("LED13 is OFF");

to send data back through the serial port.

So after you upload your program to arduino, open the Serial Monitor  (Tools>Serial Monitor in the arduino program)  and start sending data! :)

You will notice that arduino will reply with the data that we told it to send "LED13 is ON" & "LED13 is OFF".

You can download the code here.

If you like my posts then subscribe via rss, or via e-mail to stay updated.

Happy Tinkering!

If you liked this article then take a second to bookmark it with...


  1. this is a great code and i am using a modified version for what i need the only problem is that the inputs are only one character. How can i make tham multiple characters?

  2. Thanks,
    It was really helpful
    I have now built a 5 led version of this with your help so
    thanks again