Arduino control over serial

I hope that you are familiar with Arduino basics. This post is just to show you how to control Arduino over serial data sent through keyboard.

Open your Arduino IDE.

Start by allocating a variable name to our LED and setting it as output.

const int ledPin = 13;
void setup()
{
  pinMode(ledPin, OUTPUT);
}
void loop()
{
}

To communicate with our processing sketch you need to initialize the serial communication; this can be done by calling the ‘begin()’ method on the ‘Serial’ object. The syntax used for initializing the Serial port is:

Serial.begin(baud_rate);

Baud is a variable unit of data transmission speed, or simply 1 baud = 1 symbol per second. A baud rate of 9600 will be fine for us.

Serial.begin(9600);

Our serial port is initialized now need to check for any available data. Therefore, in our loop() function we use a simple if statement to check if there is any available data.

if(Serial.available())
{
}

If Serial.available() evaluates to true, then ‘if’ block will be executed.

We have opened up the serial port & checked for data availability over serial, now our next job is to actually use the data to command Arduino to do desired. Declare a variable of type char called inputValue and assign it to the data available in the serial buffer.

char inputValue = Serial.read();

The read() method takes the data available in the Serial buffer and stores it to inputValue variable. This statement is typed inside the ‘if’ block.

Now, if the data we receive is ‘H’ we will turn on the LED if it is ‘L’ we will turn off the LED, if it is neither ‘H’ or ‘L’ do nothing.

if(inputValue == ‘H’)
{
  digitalWrite(ledPin, HIGH);
}
else if(inputValue == ‘L’)
{
  digitalWrite(ledPin, HIGH);
}
else
{
  //  do nothing if anything other than a ‘H’ or a ‘L’
}

At the end of the loop() function add a small delay() of 10 milliseconds to avoid overloading the Arduino with data.

Source Code

const int ledPin = 13;
void setup()
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}
void loop()
{
  if (Serial.available())
  {
    char inputValue = Serial.read();
    if(inputValue == 'H')
    {
      digitalWrite(ledPin, HIGH);
    }
    else if(inputValue == 'L')
    {
      digitalWrite(ledPin, LOW);
    }
    else
    {
      // we will do nothing if the data is anything other than a ‘H’ or a ‘L’
    }
  }
  delay(10);
}

Once upload program it to your Arduino, and open the Serial Monitor. Try sending the values to Arduino like ‘H’, ‘L’ or anything else. The LED should light up every time you send an ‘H’ and should turn off when you send a ‘L’.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s