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);
 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

Learning to program using micro-controllers

Leave a comment

I have dabbled with electronics in the past, mostly analog. I have also worked through some tutorials available online and ventured into micro-controllers. My interests wavered over time on the choice of micro-controller. I used PIC for a while, then moved to AVR and then MSP430 and so on. I was able to easily locate what I need through various sources on the internet. I then adapted the examples and then modified it to suit my needs. But, I was never able to master the skill and develop what I needed from scratch. It dawned on me one fine day, why not just stick with one controller and then perhaps get a book/coursework and go through that cover to cover.

I decided to pick a micro-controller for my study based on the availability of information and examples. I did my research but, I think everyone knows the answer to that question. There is no beating Arduino when in comes to examples and tutorials. So Arduino it was. But, I did not want to get Uno or Leonardo type of boards. I feared that the examples will be more of a straight forward cut and paste and I might get sloppy with the true purpose of this journey. I wanted to get something that would help me grow beyond the basics, and some room to grow. I considered Due but then decided not to venture into ARM and 32-bit world without mastering the 8-bit domain. So, I decided that it would be Arduino Mega 2560.

I then started to search for a board and found one at an affordable price. I ordered that and in few days it was at my door steps. I powered in on through an USB cable and the familiar LED Blink program was pre-loaded and it worked. My PC did not detect the appropriate drivers automatically, but, a brief search on the web pointed me in the right direction and I was able to get that resolved quickly. So all set for the hardware.

I was searching for a good book in paper form. You cannot copy and paste the code from a printed book and that takes complacency out of the equation. When you type the code yourself, you will make mistakes. Those mistakes are the first steps for successful learning.

I found Arduino Cookbook by Michael Margolis and it had decent reviews. I got the book too, it is an easy read. I think it was a good choice for my purposes. Get the Second Edition as it refers to the more recent revisions of Arduino Uno.

The book had a couple of examples before the more structured approach and I was able to try out the examples. I have a decent inventory of tools and components and so at this stage I just salvage the parts from some of my old projects. Hopefully I can go through the book from cover to cover this year and record my findings.