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);
 delay(250);

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

 // 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)
 Serial.begin(9600);
}

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

 // we will pause for 500 milli seconds
 delay(500);

 // 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.

Advertisements