Arduino wants to meet new friends

Leave a comment

Now that we know how to to make Arduino talk to us through serial communication channel TX/RX, our next step is to find a way to say something back to Arduino and build some intelligence so that Arduino can understand us. When I look back to the good old days of BBC MicroCommodore 64 and ZX Spectrum class of home computers, I have very fond memories of writing simple programs in Basic, but, I had a lot of fun doing so. It was an era when people wanted to be friends with their beloved computer. I figured why not do the same with Arduino. After all it is much more powerful compared to my ZX Spectrum.

So here is what we want to do. Arduino will talk to us and we will reply and that in turn will be understood by Arduino and we have a conversation. Think that you are just 8 to 10 years old and your only objective is to learn and interact with your new found friend Arduino.

Do not get taken aback by the long code posting. When we get to the bottom of this, you will understand the critical parts and the silly imaginations of the 10 year old. But, you will have one happy Arduino with a friendly personality. So let is first dive into the code.

// Arduino interacts with the user

// when we type something back to Arduino the text we type
// should be stored in a temporary buffer before we get a
// chance to use it
String userName = "";
// even thought Arduino cannot think by itself, it is very fast
// when we type the response back, we are relatively slow
// after all we humans cannot communicate in 1s and 0s
// this variable is here to tell Arduino when we have finished typing
boolean userEntryComplete = false;

// we want to initialize serial communications
void setup()
  // the buffer through which we communicate back to Arduino
  // needs to be allocated

  // we will setup Arduino to communicate with the PC
  // at 9600 baud (9600 bits per second)

  // we will let Arduino introduce itself first
  Serial.println("Greetings! My name is Arduino.");
  // let Ardunino ask us our name next
  Serial.println("May I know with whom I am talking to?");
  // asking for the name more than once will be weird
  // so we are asking as part of the setup block and not inside the loop

void loop()
  // we do not want Arduino to process anything until we finished typing
  // so we will let Arduino to check to see whether we have done before
  // it starts to process
    if(userName != "")
      // user entered something
      // if the entry is yes or no, then Arduino needs to behave intelligently
      // think that these are commands that Arduino needs to process
      if (userName.equalsIgnoreCase("no"))
        // Arduino needs to just wait to meet someone new
        // you guessed it, Arduino is sad
        Serial.println(":( ok. Bye for now.");
      else if (userName.equalsIgnoreCase("yes"))
        // this is great news for our Arduino, it gets to meet with someone new
        // Arduino is happy and eager to meet with a new friend
        Serial.println(":) Please introduce me to your friend. What is your friend's name?");
        // time for Arduino to say Hello and find out whether there is a a posibility
        // of meeting another new friend
        Serial.println("Hello! " + userName + ". Welcome to the world of Arduino!");
        Serial.println("Nice to know you " + userName + ". Do you have a friend with you?");
      // May be our user does not understand English or perhaps out right mean
      // But, Arduino will be polite and will remind the user that it is expecting
      // the user to enter his/her name
      Serial.println("Hello! I am Arduino. Please enter your name.");
    // we need to initialize these parameters so that Arduino will wait for
    // further user interaction
    userEntryComplete = false;
    userName = "";

  // we are done, but, Ardunio does not know that
  // poor Arduino will loop through this block forever

// function is automatically called when serial input data is available
// but this availability of serial data is checked only after
// each processing of our loop code
void serialEvent()
  // we will try to read the serial input only when one or more
  // bytes of data is available for us to read
  // Serial.available() method returns the number of bytes available
  while (Serial.available() > 0)
    // we can use the read() method to read the value
    // but, read() only returns the value of a byte (0 to 255)
    // we already know that user is typing through the keyboard
    // and the data will be in ASCII characters and so we will
    // convert the value we receive to a character
    char oneCharacter = (char);
    // unless we receive the special New Line character from serial monitor
    // as a signal for us to stop, we will add/concatenate the characters together
    // to form a name; most people have more than one alphabet unless we meet with
    // Q from Star Trek: Enterprise
    if (oneCharacter == '\n')
      userEntryComplete = true;
      userName += oneCharacter;

That was long, but, if you exclude the comments and silliness there is not much to it. I have been liberal with the comments so that it helps the reader to understand why the next code block is written and what it is expected to do.

Let us fire up the above program and see how Arduino behaves. I suggest that you follow this write up to understand how it works and perhaps experiment with your own variant of the above code (by typing it and not copy and paste the code).

After you compile and upload the code to your Arduino, open up Serial Monitor and you should see the following.


Pay attention to the baud rate and Newline section. It is important that these selections are made correctly, as our code above assumes these settings.

That’s good. Arduino is waiting for us to type in our name. You can type in your name and press the send button.


Cool! It works. I have another friend next to me and so I will type yes and press the send button.



I have Pooni next to me and so I will introduce her to Arduino.


Unfortunately we have run out of company and so I will type no and press send.


Bye Arduino!

Will Arduino slowly learn to become Mr.Data?

Why not?


Arduino Serial Communication

Leave a comment

In the last post we covered the basics of establishing serial communications between your PC and Arduino. We used only two specific methods to accomplish our simple Hello World! program. Arduino’s official website has a lot more information about the Serial class. However, what I do not like is just the way things are organized. Alphabetically listing all available methods does not really help anyone to understand and implement specific instances. I believe that is the case with any index or dictionary or help documentation, they have all necessary information but no one can venture to read it cover to cover. Ok, enough of complaining. I believe we can crack this Serial class soon enough.

So why so much emphasis on the Serial communications you ask. It is one of the ways the microcontroller can communicate with other chips, sensors and external systems. Also, it is very useful in assisting us with debugging our application. Arduino in its basic form such as the Uno class has one serial port marked as TX/RX on the board. The MEGA series has 3 additional serial ports compared to the Uno class. I am using Arduino MEGA and so the serial port are marked as TX0/RX0, TX1/RX1, TX2/RX2 and TX3/RX3 (numbering of ports starts with 0 instead of 1). The TX0/RX0 would correspond to TX/RX in case of Uno class boards.

The first port TX0/RX0 or TX/RX as the case might be, also share the digital pins 0 and 1 of Arduino. So effectively these pin functions are multiplexed. This means that these pins can be switched by configuration or by calling specialized functions that in-turn do the necessary configuration changes to behave differently. In effect the digital pins 0 and 1 can be used as digital inputs or outputs or as serial communication channels among other things (such as interrupts for example). Your Arduino already uses this channel to communicate with the IDE so as to load your sketch. The Serial Monitor screen we have seen being used as part of the previous post also uses this channel for communication.

There are couple of on board LEDs marked as TX/RX that light up when communication is attempted through this channel. During programming you should have seen these LEDs blinking. Also when you tried the Hello World! program as described in the previous post, you should have seen that the LED marked TX should blink in your board. We introduced a delay of 500 micro seconds as part of our code and so the TX LED would blink at the rate of twice a second. So whats up with the RX LED on board? It never lights up. That is because we programmed Arduino to keep sending the message “Hello Word!” but never attempted to listen to what the computer or Serial Monitor has to say. Also we never tried to send anything from Serial Monitor to our Arduino.

Try this now, open the Serial Monitor and if you have already loaded the Hello World! sketch, then, you should see the appropriate text displayed on screen and TX LED blinking. Type some text (could be any thing, but, be polite, after all Arduino just said hello to you) in the Serial Monitor (in the text box area to the left of the SEND button) and then click on the SEND button. Your RX button should light up momentarily. You need to type some text each time before your click on SEND to see the RX LED light up. So you can also communicate with Arduino, but, we have not instructed Arduino to expect your commands and interpret the results. Rest assured that it is something we are going to attempt soon.

Only Arduino

Leave a comment

In most cases for a beginner’s project, the hardware connections to the Arduino board is pretty basic. The microcontroller being the brains of the overall project could take readings from a sensor and perform some calculations and conversions and output the result to a display or drive something such as a motor.

In essence bulk of the time and effort is spent on coding. So it is important that one gets comfortable with the range of variables (microcontroller brings in its own limitations), the wealth of libraries and most importantly bit wise operations.

So here is what we will do. We will just use Arduino for learning. I mean there is no external components to wire up and so no hardware circuitry involved. Your computer’s keyboard will serve as the input and the output will be displayed on screen. Basically we will establish serial communication between computer and Arduino to try out some examples.

Let’s first start with the conventional Blink program for Arduino.

// Arduino's usual LED Blink program
// Arduino's internal LED is connected to digital pin 13
int led = 13;

// we want to use the digital pin 13 as output
void setup()
 pinMode(led, OUTPUT);

void loop()
 // turn on the LED for 250 milli seconds
 digitalWrite(led, HIGH);

 // turn off the LED for the next 250 mill seconds
 digitalWrite(led, LOW);

 // loop through this behavior so that the LED
 // turns on and off to create the blink effect

In most cases the above program comes pre-loaded as part of your Arduino and so you can see that the on board LED blinking once you power your board. Since Arduino did not come with a built-in display, there is no way to show the text familiar to C programmers “Hello World!”.

Our objective here is to display the welcome greeting “Hello Word!” on the computer screen. We will make use of Arduino’s serial communication library to do so. And, guess what, this program is even simpler.

// Arduino Hello World! program

// we want to initialize serial communications
void setup()
 // we will setup Arduino to communicate with the PC
 // at 9600 baud (9600 bits per second)

void loop()
 // let's print the welcome text Hello World!
 Serial.println("Hello World!");

 // we will pause for 500 milli seconds

 // loop through this behavior so that the text
 // repeats forever

That’s it!

Now compile and run the program in Arduino IDE and be sure to open the Serial Monitor under Tools menu to see the output. You may also press Ctrl+Shift+M to get to the Serial Monitor display screen. Hopefully, you will see the welcome message.

Serial Monitor Output

Effectively we wrote only 2 lines of code.
(1) To setup serial communications and
(2) To write out the text we want to display

This is a good starting point. Please make sure that you can type this code in your IDE and not copy and paste. The serial class starts with capital S, but, the function println starts with the lower case p. Once you can get the initial program to work, try experimenting by changing the text to say your name and see whether your program works as expected.

We will continue this exploration in the next few posts. Perhaps we can get Arduino to interact with us.

Older Entries