Learning the C Language with Arduino

Wiring the Breadboard

In this example we’re using a single LED on the breadboard wired to the Arduino. As shown in the example, attach a ground wire (black) to the Gnd location on the bottom of the Arduino. Connect the wire to a ground rail on the breadboard. Using additional black wires jump to the bottom ground rail on the breadboard to complete a circuit adjacent to the LED position on the breadboard.

Place the LED in the breadboard and connect the black ground wire between the ground rail and the cathode lead (-) on the LED. Place a 150 to 220 ohm resistor in the breadboard. Attach another wire from one end of the resistor to the anode lead (+) of the LED. Finally, attach a wire between the remaining end of the resistor and Digital Pin 3 on the Arduino to compelete the circuit. This will allow us to write a program that controls flashing the LED.


Breadboard Wiring LED Circuit

Breadboard Wiring LED Circuit

Programming the Arduino

Create a new program named: LEDBlink

Type in the following code, typing the comments (shown in green) are optional. You could copy and paste into the program editor but typing your own code helps you learn the programming language too.

LEDBlink Arduino Program

/*--- Blink an LED  ---*/
//Associate LEDs with an Arduino Digital pin.
int ledPin = 3;  //We're using Digital Pin 3 on the Arduino.</code>

void setup()
{
   pinMode(ledPin, OUTPUT);   //Set up Arduino pin for output only.
}

void loop()
{
   //The HIGH and LOW values set voltage to 5 volts when HIGH and 0 volts LOW.
   digitalWrite(ledPin, HIGH); //Setting a digital pin HIGH turns on the LED.
   delay(1000);  //Get the microcontroller to wait for one second.
   digitalWrite(ledPin, LOW);  //Setting the pin to LOW turns the LED off.
   delay(1000);  //Wait another second with the LED turned off.
}

C Language Programming

As described in the related article “Introduction to Programming the Arduino“, the programming language used for the Arduino is based primarily on the C language.

Language Reference

The Arduino’s language features used in this program example are described in the Language Reference section of the Arduino web site.

Keywords

In the following program statement we are doing three things as we declare a user defined variable. First we are identifying the data type as being an integer number int”. Second, the variable name ledPin was made up for this program. Third we are assigned a value to this variable. In this case we assigned the value 3 to the variable ledPin.

int ledPin = 13; //We’re using Digital Pin 3 on the Arduino.

Syntax, Punctuation Usage

; semicolons

{} curly braces

Semicolons

One of the differences between programming languages like C and Visual Basic is the use of semicolons to end a statement.

When not to use semicolons.

  • They are ignored if used in comments.
  • They are not used in a function definition like setup()
  • They are not used on the curly braces { }

When to use semicolons.

Semicolons are used to end program statements. In the LEDBlink program they are used to end the variable declaration, ledPin, and to end program statements like pinMode, digitalWrite, and delay functions.

Curly Braces

In the LEDBlink program, the braces are used to completely surround the collection of statements used to make up the setup() function. They are also used separately to surround the loop() function’s program statements.

Standard Arduino Functions

This program only uses three different function calls.

  • pinMode()
  • digitalWrite()
  • delay()

pinMode()

pinMode(ledPin, OUTPUT); //Set up Arduino pin for output only.

In our example project, we’re using one of the Arduino’s digital pins to make an LED turn on and off. These pins are able to operate in an input mode as if to read a switch to see if it is on or off. The pins can operate in an output mode for our LED. As shown in the line of code above we are setting the pin to operate in the output mode.

digitalWrite()

digitalWrite(ledPin, HIGH); //Setting a digital pin HIGH turns on the LED.

The digitalWrite function does the actual work for us by turning the LED on or off. By specifying a High value it causes the output voltage to go high, in this case we’re using 5 volts to light the LED. Causing digitalWrite to go HIGH turns the pin on with 5 volts. When we want to turn the LED off we use digitalWrite and LOW to cause the pin to go to 0 volts.

delay()

delay(1000); //Get the microcontroller to wait for one second.

The article “Microcontrollers as Time Keepers” provides some additional information about the time intervals used within the Arduino’s capabilities. We will be using millseconds (one thousandth of a second) with this program. The delay() function causes the program to wait the specified number of milliseconds, in the following code statement it waits 1000 milliseconds for a total of one second.

Getting it Right the First Time

Hopefully you typed everything correctly getting your punctuation right with the right spelling, upper case and lower case letters as required. Selecting the Sketch -> Verify/Compile will soon let you know if something is wrong. If so, what do you do to correct the problems? Next, we’ll take a look at some possible error conditions and the error messages you might see when writing your own programs.

Check out the article “Correcting Arduino Compiler Errors“.

(c) 2009 – Vince Thompson

About these ads

Tags: , , , ,

4 Responses to “Learning the C Language with Arduino”

  1. Correcting Arduino Compiler Errors « DIY Robotics Lab Says:

    [...] DIY Robotics Lab Bringing Robotics Home « Learning the C Language with Arduino [...]

  2. Blinking Binary Bits and Bytes… « DIY Robotics Lab Says:

    [...] flash is a great way to begin programming your Arduino, so take what you’ve learned with the LEDBlink program and add more LEDs. How many should we add, 3, 4, 7, 15 or more? Is there a special number of LEDs [...]

  3. cordless screwdrivers Says:

    Hi, I cant understand how to add your site in my rss reader. Can you Help me, please :)

    • someoneknows Says:

      Scroll to the bottom and the last line should say Entries (RSS) and Comments (RSS). If you click on the Entries (RSS) you should be able to add this site to your reader.

      Thanks for checking the site.

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


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: