May 25, 2016

Interact with Arduino through serial

In this post I will show how to use the USB serial interface to issue commands and retrieve output from an Arduino board. The main goal of this project is to build a simple interface to use Arduino as a 'sensors hub' and integrate it with other more complex devices like Raspberry PI or and PC.

Now plug your Arduino board to the PC and upload the following sketch.


int PIN_LED = 13;

void setup()
{
  pinMode(PIN_LED, OUTPUT);
  
  Serial.begin(9600);

  Serial.println("Welcome to ArduinoSerialControl");
  Serial.println("Enter 't' to retrieve current millis");
  Serial.println("Enter 'l' to toggle LED on pin 13");
}

void loop()
{
  int bytes=Serial.available();
  if (bytes > 0)
  {
    char c = Serial.read();
    switch (c)
    {
    case 't':
      // return the uptime in milliseconds
      Serial.print("millis=");
      Serial.println(millis());
      break;
    case 'l':
      // toggle on-board led connected to pin 13
      digitalWrite(PIN_LED, !digitalRead(PIN_LED));
      Serial.print("LED status is ");
      Serial.println(digitalRead(PIN_LED));
      break;
    }
  }
}


Open the serial monitor and set the baud rate to 9600. Try sending commands using the serial console.



This is just an experiment. If you want to build a solid communication using Firmata protocol.

Complete Control Of An Arduino Via Serial

No comments:

Post a Comment